2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "dpif-netlink.h"
26 #include <linux/types.h>
27 #include <linux/pkt_sched.h>
31 #include <sys/epoll.h>
36 #include "dpif-provider.h"
37 #include "dynamic-string.h"
39 #include "fat-rwlock.h"
41 #include "netdev-linux.h"
42 #include "netdev-vport.h"
43 #include "netlink-notifier.h"
44 #include "netlink-socket.h"
49 #include "poll-loop.h"
54 #include "unaligned.h"
56 #include "openvswitch/vlog.h"
58 VLOG_DEFINE_THIS_MODULE(dpif_netlink);
64 enum { MAX_PORTS = USHRT_MAX };
66 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
67 * missing if we have old headers. */
68 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
70 struct dpif_netlink_dp {
71 /* Generic Netlink header. */
74 /* struct ovs_header. */
78 const char *name; /* OVS_DP_ATTR_NAME. */
79 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
80 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
81 const struct ovs_dp_stats *stats; /* OVS_DP_ATTR_STATS. */
82 const struct ovs_dp_megaflow_stats *megaflow_stats;
83 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
86 static void dpif_netlink_dp_init(struct dpif_netlink_dp *);
87 static int dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *,
88 const struct ofpbuf *);
89 static void dpif_netlink_dp_dump_start(struct nl_dump *);
90 static int dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
91 struct dpif_netlink_dp *reply,
92 struct ofpbuf **bufp);
93 static int dpif_netlink_dp_get(const struct dpif *,
94 struct dpif_netlink_dp *reply,
95 struct ofpbuf **bufp);
97 struct dpif_netlink_flow {
98 /* Generic Netlink header. */
101 /* struct ovs_header. */
102 unsigned int nlmsg_flags;
107 * The 'stats' member points to 64-bit data that might only be aligned on
108 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
111 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
112 * the Netlink version of the command, even if actions_len is zero. */
113 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
115 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
117 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
119 ovs_u128 ufid; /* OVS_FLOW_ATTR_FLOW_ID. */
120 bool ufid_present; /* Is there a UFID? */
121 bool ufid_terse; /* Skip serializing key/mask/acts? */
122 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
123 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
124 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
125 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
126 bool probe; /* OVS_FLOW_ATTR_PROBE. */
129 static void dpif_netlink_flow_init(struct dpif_netlink_flow *);
130 static int dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *,
131 const struct ofpbuf *);
132 static void dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *,
134 static int dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
135 struct dpif_netlink_flow *reply,
136 struct ofpbuf **bufp);
137 static void dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *,
138 struct dpif_flow_stats *);
139 static void dpif_netlink_flow_to_dpif_flow(struct dpif *, struct dpif_flow *,
140 const struct dpif_netlink_flow *);
142 /* One of the dpif channels between the kernel and userspace. */
143 struct dpif_channel {
144 struct nl_sock *sock; /* Netlink socket. */
145 long long int last_poll; /* Last time this channel was polled. */
149 #define VPORT_SOCK_POOL_SIZE 1
150 /* On Windows, there is no native support for epoll. There are equivalent
151 * interfaces though, that are not used currently. For simpicity, a pool of
152 * netlink sockets is used. Each socket is represented by 'struct
153 * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
154 * sharing the same socket. In the future, we can add a reference count and
156 struct dpif_windows_vport_sock {
157 struct nl_sock *nl_sock; /* netlink socket. */
161 struct dpif_handler {
162 struct dpif_channel *channels;/* Array of channels for each handler. */
163 struct epoll_event *epoll_events;
164 int epoll_fd; /* epoll fd that includes channel socks. */
165 int n_events; /* Num events returned by epoll_wait(). */
166 int event_offset; /* Offset into 'epoll_events'. */
169 /* Pool of sockets. */
170 struct dpif_windows_vport_sock *vport_sock_pool;
171 size_t last_used_pool_idx; /* Index to aid in allocating a
172 socket in the pool to a port. */
176 /* Datapath interface for the openvswitch Linux kernel module. */
177 struct dpif_netlink {
181 /* Upcall messages. */
182 struct fat_rwlock upcall_lock;
183 struct dpif_handler *handlers;
184 uint32_t n_handlers; /* Num of upcall handlers. */
185 int uc_array_size; /* Size of 'handler->channels' and */
186 /* 'handler->epoll_events'. */
188 /* Change notification. */
189 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
190 bool refresh_channels;
193 static void report_loss(struct dpif_netlink *, struct dpif_channel *,
194 uint32_t ch_idx, uint32_t handler_id);
196 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
198 /* Generic Netlink family numbers for OVS.
200 * Initialized by dpif_netlink_init(). */
201 static int ovs_datapath_family;
202 static int ovs_vport_family;
203 static int ovs_flow_family;
204 static int ovs_packet_family;
206 /* Generic Netlink multicast groups for OVS.
208 * Initialized by dpif_netlink_init(). */
209 static unsigned int ovs_vport_mcgroup;
211 static int dpif_netlink_init(void);
212 static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
213 static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
214 odp_port_t port_no, uint32_t hash);
215 static void dpif_netlink_handler_uninit(struct dpif_handler *handler);
216 static int dpif_netlink_refresh_channels(struct dpif_netlink *,
217 uint32_t n_handlers);
218 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
220 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
221 const struct ofpbuf *);
223 static struct dpif_netlink *
224 dpif_netlink_cast(const struct dpif *dpif)
226 dpif_assert_class(dpif, &dpif_netlink_class);
227 return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
231 dpif_netlink_enumerate(struct sset *all_dps,
232 const struct dpif_class *dpif_class OVS_UNUSED)
235 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
236 struct ofpbuf msg, buf;
239 error = dpif_netlink_init();
244 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
245 dpif_netlink_dp_dump_start(&dump);
246 while (nl_dump_next(&dump, &msg, &buf)) {
247 struct dpif_netlink_dp dp;
249 if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
250 sset_add(all_dps, dp.name);
254 return nl_dump_done(&dump);
258 dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
259 bool create, struct dpif **dpifp)
261 struct dpif_netlink_dp dp_request, dp;
266 error = dpif_netlink_init();
271 /* Create or look up datapath. */
272 dpif_netlink_dp_init(&dp_request);
274 dp_request.cmd = OVS_DP_CMD_NEW;
276 dp_request.upcall_pid = &upcall_pid;
278 /* Use OVS_DP_CMD_SET to report user features */
279 dp_request.cmd = OVS_DP_CMD_SET;
281 dp_request.name = name;
282 dp_request.user_features |= OVS_DP_F_UNALIGNED;
283 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
284 error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
289 error = open_dpif(&dp, dpifp);
295 open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
297 struct dpif_netlink *dpif;
299 dpif = xzalloc(sizeof *dpif);
300 dpif->port_notifier = NULL;
301 fat_rwlock_init(&dpif->upcall_lock);
303 dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
304 dp->dp_ifindex, dp->dp_ifindex);
306 dpif->dp_ifindex = dp->dp_ifindex;
307 *dpifp = &dpif->dpif;
312 /* Destroys the netlink sockets pointed by the elements in 'socksp'
313 * and frees the 'socksp'. */
315 vport_del_socksp__(struct nl_sock **socksp, uint32_t n_socks)
319 for (i = 0; i < n_socks; i++) {
320 nl_sock_destroy(socksp[i]);
326 /* Creates an array of netlink sockets. Returns an array of the
327 * corresponding pointers. Records the error in 'error'. */
328 static struct nl_sock **
329 vport_create_socksp__(uint32_t n_socks, int *error)
331 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
334 for (i = 0; i < n_socks; i++) {
335 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
344 vport_del_socksp__(socksp, n_socks);
351 vport_delete_sock_pool(struct dpif_handler *handler)
352 OVS_REQ_WRLOCK(dpif->upcall_lock)
354 if (handler->vport_sock_pool) {
356 struct dpif_windows_vport_sock *sock_pool =
357 handler->vport_sock_pool;
359 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
360 if (sock_pool[i].nl_sock) {
361 nl_sock_unsubscribe_packets(sock_pool[i].nl_sock);
362 nl_sock_destroy(sock_pool[i].nl_sock);
363 sock_pool[i].nl_sock = NULL;
367 free(handler->vport_sock_pool);
368 handler->vport_sock_pool = NULL;
373 vport_create_sock_pool(struct dpif_handler *handler)
374 OVS_REQ_WRLOCK(dpif->upcall_lock)
376 struct dpif_windows_vport_sock *sock_pool;
380 sock_pool = xzalloc(VPORT_SOCK_POOL_SIZE * sizeof *sock_pool);
381 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
382 error = nl_sock_create(NETLINK_GENERIC, &sock_pool[i].nl_sock);
387 /* Enable the netlink socket to receive packets. This is equivalent to
388 * calling nl_sock_join_mcgroup() to receive events. */
389 error = nl_sock_subscribe_packets(sock_pool[i].nl_sock);
395 handler->vport_sock_pool = sock_pool;
396 handler->last_used_pool_idx = 0;
400 vport_delete_sock_pool(handler);
404 /* Returns an array pointers to netlink sockets. The sockets are picked from a
405 * pool. Records the error in 'error'. */
406 static struct nl_sock **
407 vport_create_socksp_windows(struct dpif_netlink *dpif, int *error)
408 OVS_REQ_WRLOCK(dpif->upcall_lock)
410 uint32_t n_socks = dpif->n_handlers;
411 struct nl_sock **socksp;
414 ovs_assert(n_socks <= 1);
415 socksp = xzalloc(n_socks * sizeof *socksp);
417 /* Pick netlink sockets to use in a round-robin fashion from each
418 * handler's pool of sockets. */
419 for (i = 0; i < n_socks; i++) {
420 struct dpif_handler *handler = &dpif->handlers[i];
421 struct dpif_windows_vport_sock *sock_pool = handler->vport_sock_pool;
422 size_t index = handler->last_used_pool_idx;
424 /* A pool of sockets is allocated when the handler is initialized. */
425 if (sock_pool == NULL) {
431 ovs_assert(index < VPORT_SOCK_POOL_SIZE);
432 socksp[i] = sock_pool[index].nl_sock;
433 socksp[i] = sock_pool[index].nl_sock;
434 ovs_assert(socksp[i]);
435 index = (index == VPORT_SOCK_POOL_SIZE - 1) ? 0 : index + 1;
436 handler->last_used_pool_idx = index;
443 vport_del_socksp_windows(struct dpif_netlink *dpif, struct nl_sock **socksp)
449 static struct nl_sock **
450 vport_create_socksp(struct dpif_netlink *dpif, int *error)
453 return vport_create_socksp_windows(dpif, error);
455 return vport_create_socksp__(dpif->n_handlers, error);
460 vport_del_socksp(struct dpif_netlink *dpif, struct nl_sock **socksp)
463 vport_del_socksp_windows(dpif, socksp);
465 vport_del_socksp__(socksp, dpif->n_handlers);
469 /* Given the array of pointers to netlink sockets 'socksp', returns
470 * the array of corresponding pids. If the 'socksp' is NULL, returns
471 * a single-element array of value 0. */
473 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
478 pids = xzalloc(sizeof *pids);
482 pids = xzalloc(n_socks * sizeof *pids);
483 for (i = 0; i < n_socks; i++) {
484 pids[i] = nl_sock_pid(socksp[i]);
491 /* Given the port number 'port_idx', extracts the pids of netlink sockets
492 * associated to the port and assigns it to 'upcall_pids'. */
494 vport_get_pids(struct dpif_netlink *dpif, uint32_t port_idx,
495 uint32_t **upcall_pids)
500 /* Since the nl_sock can only be assigned in either all
501 * or none "dpif->handlers" channels, the following check
503 if (!dpif->handlers[0].channels[port_idx].sock) {
506 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
508 pids = xzalloc(dpif->n_handlers * sizeof *pids);
510 for (i = 0; i < dpif->n_handlers; i++) {
511 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
520 vport_add_channels(struct dpif_netlink *dpif, odp_port_t port_no,
521 struct nl_sock **socksp)
523 struct epoll_event event;
524 uint32_t port_idx = odp_to_u32(port_no);
528 if (dpif->handlers == NULL) {
532 /* We assume that the datapath densely chooses port numbers, which can
533 * therefore be used as an index into 'channels' and 'epoll_events' of
534 * 'dpif->handler'. */
535 if (port_idx >= dpif->uc_array_size) {
536 uint32_t new_size = port_idx + 1;
538 if (new_size > MAX_PORTS) {
539 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
540 dpif_name(&dpif->dpif), port_no);
544 for (i = 0; i < dpif->n_handlers; i++) {
545 struct dpif_handler *handler = &dpif->handlers[i];
547 handler->channels = xrealloc(handler->channels,
548 new_size * sizeof *handler->channels);
550 for (j = dpif->uc_array_size; j < new_size; j++) {
551 handler->channels[j].sock = NULL;
554 handler->epoll_events = xrealloc(handler->epoll_events,
555 new_size * sizeof *handler->epoll_events);
558 dpif->uc_array_size = new_size;
561 memset(&event, 0, sizeof event);
562 event.events = EPOLLIN;
563 event.data.u32 = port_idx;
565 for (i = 0; i < dpif->n_handlers; i++) {
566 struct dpif_handler *handler = &dpif->handlers[i];
569 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
575 dpif->handlers[i].channels[port_idx].sock = socksp[i];
576 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
582 for (j = 0; j < i; j++) {
584 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
585 nl_sock_fd(socksp[j]), NULL);
587 dpif->handlers[j].channels[port_idx].sock = NULL;
594 vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
596 uint32_t port_idx = odp_to_u32(port_no);
599 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
603 /* Since the sock can only be assigned in either all or none
604 * of "dpif->handlers" channels, the following check would
606 if (!dpif->handlers[0].channels[port_idx].sock) {
610 for (i = 0; i < dpif->n_handlers; i++) {
611 struct dpif_handler *handler = &dpif->handlers[i];
613 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
614 nl_sock_fd(handler->channels[port_idx].sock), NULL);
615 nl_sock_destroy(handler->channels[port_idx].sock);
617 handler->channels[port_idx].sock = NULL;
618 handler->event_offset = handler->n_events = 0;
623 destroy_all_channels(struct dpif_netlink *dpif)
624 OVS_REQ_WRLOCK(dpif->upcall_lock)
628 if (!dpif->handlers) {
632 for (i = 0; i < dpif->uc_array_size; i++ ) {
633 struct dpif_netlink_vport vport_request;
634 uint32_t upcall_pids = 0;
636 /* Since the sock can only be assigned in either all or none
637 * of "dpif->handlers" channels, the following check would
639 if (!dpif->handlers[0].channels[i].sock) {
643 /* Turn off upcalls. */
644 dpif_netlink_vport_init(&vport_request);
645 vport_request.cmd = OVS_VPORT_CMD_SET;
646 vport_request.dp_ifindex = dpif->dp_ifindex;
647 vport_request.port_no = u32_to_odp(i);
648 vport_request.n_upcall_pids = 1;
649 vport_request.upcall_pids = &upcall_pids;
650 dpif_netlink_vport_transact(&vport_request, NULL, NULL);
652 vport_del_channels(dpif, u32_to_odp(i));
655 for (i = 0; i < dpif->n_handlers; i++) {
656 struct dpif_handler *handler = &dpif->handlers[i];
658 dpif_netlink_handler_uninit(handler);
659 free(handler->epoll_events);
660 free(handler->channels);
663 free(dpif->handlers);
664 dpif->handlers = NULL;
665 dpif->n_handlers = 0;
666 dpif->uc_array_size = 0;
670 dpif_netlink_close(struct dpif *dpif_)
672 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
674 nl_sock_destroy(dpif->port_notifier);
676 fat_rwlock_wrlock(&dpif->upcall_lock);
677 destroy_all_channels(dpif);
678 fat_rwlock_unlock(&dpif->upcall_lock);
680 fat_rwlock_destroy(&dpif->upcall_lock);
685 dpif_netlink_destroy(struct dpif *dpif_)
687 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
688 struct dpif_netlink_dp dp;
690 dpif_netlink_dp_init(&dp);
691 dp.cmd = OVS_DP_CMD_DEL;
692 dp.dp_ifindex = dpif->dp_ifindex;
693 return dpif_netlink_dp_transact(&dp, NULL, NULL);
697 dpif_netlink_run(struct dpif *dpif_)
699 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
701 if (dpif->refresh_channels) {
702 dpif->refresh_channels = false;
703 fat_rwlock_wrlock(&dpif->upcall_lock);
704 dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
705 fat_rwlock_unlock(&dpif->upcall_lock);
711 dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
713 struct dpif_netlink_dp dp;
717 error = dpif_netlink_dp_get(dpif_, &dp, &buf);
719 memset(stats, 0, sizeof *stats);
722 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
723 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
724 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
725 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
728 if (dp.megaflow_stats) {
729 stats->n_masks = dp.megaflow_stats->n_masks;
730 stats->n_mask_hit = get_32aligned_u64(
731 &dp.megaflow_stats->n_mask_hit);
733 stats->n_masks = UINT32_MAX;
734 stats->n_mask_hit = UINT64_MAX;
742 get_vport_type(const struct dpif_netlink_vport *vport)
744 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
746 switch (vport->type) {
747 case OVS_VPORT_TYPE_NETDEV: {
748 const char *type = netdev_get_type_from_name(vport->name);
750 return type ? type : "system";
753 case OVS_VPORT_TYPE_INTERNAL:
756 case OVS_VPORT_TYPE_GENEVE:
759 case OVS_VPORT_TYPE_GRE:
762 case OVS_VPORT_TYPE_GRE64:
765 case OVS_VPORT_TYPE_VXLAN:
768 case OVS_VPORT_TYPE_LISP:
771 case OVS_VPORT_TYPE_STT:
774 case OVS_VPORT_TYPE_UNSPEC:
775 case __OVS_VPORT_TYPE_MAX:
779 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
780 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
784 static enum ovs_vport_type
785 netdev_to_ovs_vport_type(const struct netdev *netdev)
787 const char *type = netdev_get_type(netdev);
789 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
790 return OVS_VPORT_TYPE_NETDEV;
791 } else if (!strcmp(type, "internal")) {
792 return OVS_VPORT_TYPE_INTERNAL;
793 } else if (strstr(type, "stt")) {
794 return OVS_VPORT_TYPE_STT;
795 } else if (!strcmp(type, "geneve")) {
796 return OVS_VPORT_TYPE_GENEVE;
797 } else if (strstr(type, "gre64")) {
798 return OVS_VPORT_TYPE_GRE64;
799 } else if (strstr(type, "gre")) {
800 return OVS_VPORT_TYPE_GRE;
801 } else if (!strcmp(type, "vxlan")) {
802 return OVS_VPORT_TYPE_VXLAN;
803 } else if (!strcmp(type, "lisp")) {
804 return OVS_VPORT_TYPE_LISP;
806 return OVS_VPORT_TYPE_UNSPEC;
811 dpif_netlink_port_add__(struct dpif_netlink *dpif, struct netdev *netdev,
812 odp_port_t *port_nop)
813 OVS_REQ_WRLOCK(dpif->upcall_lock)
815 const struct netdev_tunnel_config *tnl_cfg;
816 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
817 const char *name = netdev_vport_get_dpif_port(netdev,
818 namebuf, sizeof namebuf);
819 const char *type = netdev_get_type(netdev);
820 struct dpif_netlink_vport request, reply;
822 uint64_t options_stub[64 / 8];
823 struct ofpbuf options;
824 struct nl_sock **socksp = NULL;
825 uint32_t *upcall_pids;
828 if (dpif->handlers) {
829 socksp = vport_create_socksp(dpif, &error);
835 dpif_netlink_vport_init(&request);
836 request.cmd = OVS_VPORT_CMD_NEW;
837 request.dp_ifindex = dpif->dp_ifindex;
838 request.type = netdev_to_ovs_vport_type(netdev);
839 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
840 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
841 "unsupported type `%s'",
842 dpif_name(&dpif->dpif), name, type);
843 vport_del_socksp(dpif, socksp);
848 if (request.type == OVS_VPORT_TYPE_NETDEV) {
850 /* XXX : Map appropiate Windows handle */
852 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
856 tnl_cfg = netdev_get_tunnel_config(netdev);
857 if (tnl_cfg && (tnl_cfg->dst_port != 0 || tnl_cfg->exts)) {
858 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
859 if (tnl_cfg->dst_port) {
860 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
861 ntohs(tnl_cfg->dst_port));
867 ext_ofs = nl_msg_start_nested(&options, OVS_TUNNEL_ATTR_EXTENSION);
868 for (i = 0; i < 32; i++) {
869 if (tnl_cfg->exts & (1 << i)) {
870 nl_msg_put_flag(&options, i);
873 nl_msg_end_nested(&options, ext_ofs);
875 request.options = options.data;
876 request.options_len = options.size;
879 request.port_no = *port_nop;
880 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
881 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
882 request.upcall_pids = upcall_pids;
884 error = dpif_netlink_vport_transact(&request, &reply, &buf);
886 *port_nop = reply.port_no;
888 if (error == EBUSY && *port_nop != ODPP_NONE) {
889 VLOG_INFO("%s: requested port %"PRIu32" is in use",
890 dpif_name(&dpif->dpif), *port_nop);
893 vport_del_socksp(dpif, socksp);
898 error = vport_add_channels(dpif, *port_nop, socksp);
900 VLOG_INFO("%s: could not add channel for port %s",
901 dpif_name(&dpif->dpif), name);
903 /* Delete the port. */
904 dpif_netlink_vport_init(&request);
905 request.cmd = OVS_VPORT_CMD_DEL;
906 request.dp_ifindex = dpif->dp_ifindex;
907 request.port_no = *port_nop;
908 dpif_netlink_vport_transact(&request, NULL, NULL);
909 vport_del_socksp(dpif, socksp);
923 dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
924 odp_port_t *port_nop)
926 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
929 fat_rwlock_wrlock(&dpif->upcall_lock);
930 error = dpif_netlink_port_add__(dpif, netdev, port_nop);
931 fat_rwlock_unlock(&dpif->upcall_lock);
937 dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no)
938 OVS_REQ_WRLOCK(dpif->upcall_lock)
940 struct dpif_netlink_vport vport;
943 dpif_netlink_vport_init(&vport);
944 vport.cmd = OVS_VPORT_CMD_DEL;
945 vport.dp_ifindex = dpif->dp_ifindex;
946 vport.port_no = port_no;
947 error = dpif_netlink_vport_transact(&vport, NULL, NULL);
949 vport_del_channels(dpif, port_no);
955 dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no)
957 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
960 fat_rwlock_wrlock(&dpif->upcall_lock);
961 error = dpif_netlink_port_del__(dpif, port_no);
962 fat_rwlock_unlock(&dpif->upcall_lock);
968 dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
969 const char *port_name, struct dpif_port *dpif_port)
971 struct dpif_netlink_vport request;
972 struct dpif_netlink_vport reply;
976 dpif_netlink_vport_init(&request);
977 request.cmd = OVS_VPORT_CMD_GET;
978 request.dp_ifindex = dpif->dp_ifindex;
979 request.port_no = port_no;
980 request.name = port_name;
982 error = dpif_netlink_vport_transact(&request, &reply, &buf);
984 if (reply.dp_ifindex != request.dp_ifindex) {
985 /* A query by name reported that 'port_name' is in some datapath
986 * other than 'dpif', but the caller wants to know about 'dpif'. */
988 } else if (dpif_port) {
989 dpif_port->name = xstrdup(reply.name);
990 dpif_port->type = xstrdup(get_vport_type(&reply));
991 dpif_port->port_no = reply.port_no;
999 dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
1000 struct dpif_port *dpif_port)
1002 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1004 return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port);
1008 dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname,
1009 struct dpif_port *dpif_port)
1011 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1013 return dpif_netlink_port_query__(dpif, 0, devname, dpif_port);
1017 dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif,
1018 odp_port_t port_no, uint32_t hash)
1019 OVS_REQ_RDLOCK(dpif->upcall_lock)
1021 uint32_t port_idx = odp_to_u32(port_no);
1024 if (dpif->handlers && dpif->uc_array_size > 0) {
1025 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
1026 * channel, since it is not heavily loaded. */
1027 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
1028 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
1030 /* Needs to check in case the socket pointer is changed in between
1031 * the holding of upcall_lock. A known case happens when the main
1032 * thread deletes the vport while the handler thread is handling
1033 * the upcall from that port. */
1034 if (h->channels[idx].sock) {
1035 pid = nl_sock_pid(h->channels[idx].sock);
1043 dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
1046 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1049 fat_rwlock_rdlock(&dpif->upcall_lock);
1050 ret = dpif_netlink_port_get_pid__(dpif, port_no, hash);
1051 fat_rwlock_unlock(&dpif->upcall_lock);
1057 dpif_netlink_flow_flush(struct dpif *dpif_)
1059 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1060 struct dpif_netlink_flow flow;
1062 dpif_netlink_flow_init(&flow);
1063 flow.cmd = OVS_FLOW_CMD_DEL;
1064 flow.dp_ifindex = dpif->dp_ifindex;
1065 return dpif_netlink_flow_transact(&flow, NULL, NULL);
1068 struct dpif_netlink_port_state {
1069 struct nl_dump dump;
1074 dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif,
1075 struct nl_dump *dump)
1077 struct dpif_netlink_vport request;
1080 dpif_netlink_vport_init(&request);
1081 request.cmd = OVS_VPORT_CMD_GET;
1082 request.dp_ifindex = dpif->dp_ifindex;
1084 buf = ofpbuf_new(1024);
1085 dpif_netlink_vport_to_ofpbuf(&request, buf);
1086 nl_dump_start(dump, NETLINK_GENERIC, buf);
1091 dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep)
1093 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1094 struct dpif_netlink_port_state *state;
1096 *statep = state = xmalloc(sizeof *state);
1097 dpif_netlink_port_dump_start__(dpif, &state->dump);
1099 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
1104 dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif,
1105 struct nl_dump *dump,
1106 struct dpif_netlink_vport *vport,
1107 struct ofpbuf *buffer)
1112 if (!nl_dump_next(dump, &buf, buffer)) {
1116 error = dpif_netlink_vport_from_ofpbuf(vport, &buf);
1118 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
1119 dpif_name(&dpif->dpif), ovs_strerror(error));
1125 dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_,
1126 struct dpif_port *dpif_port)
1128 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1129 struct dpif_netlink_port_state *state = state_;
1130 struct dpif_netlink_vport vport;
1133 error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport,
1138 dpif_port->name = CONST_CAST(char *, vport.name);
1139 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
1140 dpif_port->port_no = vport.port_no;
1145 dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
1147 struct dpif_netlink_port_state *state = state_;
1148 int error = nl_dump_done(&state->dump);
1150 ofpbuf_uninit(&state->buf);
1156 dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep)
1158 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1160 /* Lazily create the Netlink socket to listen for notifications. */
1161 if (!dpif->port_notifier) {
1162 struct nl_sock *sock;
1165 error = nl_sock_create(NETLINK_GENERIC, &sock);
1170 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
1172 nl_sock_destroy(sock);
1175 dpif->port_notifier = sock;
1177 /* We have no idea of the current state so report that everything
1183 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1184 uint64_t buf_stub[4096 / 8];
1188 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1189 error = nl_sock_recv(dpif->port_notifier, &buf, false);
1191 struct dpif_netlink_vport vport;
1193 error = dpif_netlink_vport_from_ofpbuf(&vport, &buf);
1195 if (vport.dp_ifindex == dpif->dp_ifindex
1196 && (vport.cmd == OVS_VPORT_CMD_NEW
1197 || vport.cmd == OVS_VPORT_CMD_DEL
1198 || vport.cmd == OVS_VPORT_CMD_SET)) {
1199 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1200 dpif->dpif.full_name, vport.name, vport.cmd);
1201 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1202 dpif->refresh_channels = true;
1204 *devnamep = xstrdup(vport.name);
1205 ofpbuf_uninit(&buf);
1209 } else if (error != EAGAIN) {
1210 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1211 ovs_strerror(error));
1212 nl_sock_drain(dpif->port_notifier);
1216 ofpbuf_uninit(&buf);
1224 dpif_netlink_port_poll_wait(const struct dpif *dpif_)
1226 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1228 if (dpif->port_notifier) {
1229 nl_sock_wait(dpif->port_notifier, POLLIN);
1231 poll_immediate_wake();
1236 dpif_netlink_flow_init_ufid(struct dpif_netlink_flow *request,
1237 const ovs_u128 *ufid, bool terse)
1240 request->ufid = *ufid;
1241 request->ufid_present = true;
1243 request->ufid_present = false;
1245 request->ufid_terse = terse;
1249 dpif_netlink_init_flow_get__(const struct dpif_netlink *dpif,
1250 const struct nlattr *key, size_t key_len,
1251 const ovs_u128 *ufid, bool terse,
1252 struct dpif_netlink_flow *request)
1254 dpif_netlink_flow_init(request);
1255 request->cmd = OVS_FLOW_CMD_GET;
1256 request->dp_ifindex = dpif->dp_ifindex;
1258 request->key_len = key_len;
1259 dpif_netlink_flow_init_ufid(request, ufid, terse);
1263 dpif_netlink_init_flow_get(const struct dpif_netlink *dpif,
1264 const struct dpif_flow_get *get,
1265 struct dpif_netlink_flow *request)
1267 dpif_netlink_init_flow_get__(dpif, get->key, get->key_len, get->ufid,
1272 dpif_netlink_flow_get__(const struct dpif_netlink *dpif,
1273 const struct nlattr *key, size_t key_len,
1274 const ovs_u128 *ufid, bool terse,
1275 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1277 struct dpif_netlink_flow request;
1279 dpif_netlink_init_flow_get__(dpif, key, key_len, ufid, terse, &request);
1280 return dpif_netlink_flow_transact(&request, reply, bufp);
1284 dpif_netlink_flow_get(const struct dpif_netlink *dpif,
1285 const struct dpif_netlink_flow *flow,
1286 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1288 return dpif_netlink_flow_get__(dpif, flow->key, flow->key_len,
1289 flow->ufid_present ? &flow->ufid : NULL,
1290 false, reply, bufp);
1294 dpif_netlink_init_flow_put(struct dpif_netlink *dpif,
1295 const struct dpif_flow_put *put,
1296 struct dpif_netlink_flow *request)
1298 static const struct nlattr dummy_action;
1300 dpif_netlink_flow_init(request);
1301 request->cmd = (put->flags & DPIF_FP_CREATE
1302 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1303 request->dp_ifindex = dpif->dp_ifindex;
1304 request->key = put->key;
1305 request->key_len = put->key_len;
1306 request->mask = put->mask;
1307 request->mask_len = put->mask_len;
1308 dpif_netlink_flow_init_ufid(request, put->ufid, false);
1310 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1311 request->actions = (put->actions
1313 : CONST_CAST(struct nlattr *, &dummy_action));
1314 request->actions_len = put->actions_len;
1315 if (put->flags & DPIF_FP_ZERO_STATS) {
1316 request->clear = true;
1318 if (put->flags & DPIF_FP_PROBE) {
1319 request->probe = true;
1321 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1325 dpif_netlink_init_flow_del__(struct dpif_netlink *dpif,
1326 const struct nlattr *key, size_t key_len,
1327 const ovs_u128 *ufid, bool terse,
1328 struct dpif_netlink_flow *request)
1330 dpif_netlink_flow_init(request);
1331 request->cmd = OVS_FLOW_CMD_DEL;
1332 request->dp_ifindex = dpif->dp_ifindex;
1334 request->key_len = key_len;
1335 dpif_netlink_flow_init_ufid(request, ufid, terse);
1339 dpif_netlink_init_flow_del(struct dpif_netlink *dpif,
1340 const struct dpif_flow_del *del,
1341 struct dpif_netlink_flow *request)
1343 dpif_netlink_init_flow_del__(dpif, del->key, del->key_len,
1344 del->ufid, del->terse, request);
1347 struct dpif_netlink_flow_dump {
1348 struct dpif_flow_dump up;
1349 struct nl_dump nl_dump;
1353 static struct dpif_netlink_flow_dump *
1354 dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump)
1356 return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up);
1359 static struct dpif_flow_dump *
1360 dpif_netlink_flow_dump_create(const struct dpif *dpif_, bool terse)
1362 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1363 struct dpif_netlink_flow_dump *dump;
1364 struct dpif_netlink_flow request;
1367 dump = xmalloc(sizeof *dump);
1368 dpif_flow_dump_init(&dump->up, dpif_);
1370 dpif_netlink_flow_init(&request);
1371 request.cmd = OVS_FLOW_CMD_GET;
1372 request.dp_ifindex = dpif->dp_ifindex;
1373 request.ufid_present = false;
1374 request.ufid_terse = terse;
1376 buf = ofpbuf_new(1024);
1377 dpif_netlink_flow_to_ofpbuf(&request, buf);
1378 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1380 atomic_init(&dump->status, 0);
1381 dump->up.terse = terse;
1387 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_)
1389 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1390 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1393 /* No other thread has access to 'dump' at this point. */
1394 atomic_read_relaxed(&dump->status, &dump_status);
1396 return dump_status ? dump_status : nl_status;
1399 struct dpif_netlink_flow_dump_thread {
1400 struct dpif_flow_dump_thread up;
1401 struct dpif_netlink_flow_dump *dump;
1402 struct dpif_netlink_flow flow;
1403 struct dpif_flow_stats stats;
1404 struct ofpbuf nl_flows; /* Always used to store flows. */
1405 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1408 static struct dpif_netlink_flow_dump_thread *
1409 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1411 return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up);
1414 static struct dpif_flow_dump_thread *
1415 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1417 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1418 struct dpif_netlink_flow_dump_thread *thread;
1420 thread = xmalloc(sizeof *thread);
1421 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1422 thread->dump = dump;
1423 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1424 thread->nl_actions = NULL;
1430 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1432 struct dpif_netlink_flow_dump_thread *thread
1433 = dpif_netlink_flow_dump_thread_cast(thread_);
1435 ofpbuf_uninit(&thread->nl_flows);
1436 ofpbuf_delete(thread->nl_actions);
1441 dpif_netlink_flow_to_dpif_flow(struct dpif *dpif, struct dpif_flow *dpif_flow,
1442 const struct dpif_netlink_flow *datapath_flow)
1444 dpif_flow->key = datapath_flow->key;
1445 dpif_flow->key_len = datapath_flow->key_len;
1446 dpif_flow->mask = datapath_flow->mask;
1447 dpif_flow->mask_len = datapath_flow->mask_len;
1448 dpif_flow->actions = datapath_flow->actions;
1449 dpif_flow->actions_len = datapath_flow->actions_len;
1450 dpif_flow->ufid_present = datapath_flow->ufid_present;
1451 dpif_flow->pmd_id = PMD_ID_NULL;
1452 if (datapath_flow->ufid_present) {
1453 dpif_flow->ufid = datapath_flow->ufid;
1455 ovs_assert(datapath_flow->key && datapath_flow->key_len);
1456 dpif_flow_hash(dpif, datapath_flow->key, datapath_flow->key_len,
1459 dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats);
1463 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1464 struct dpif_flow *flows, int max_flows)
1466 struct dpif_netlink_flow_dump_thread *thread
1467 = dpif_netlink_flow_dump_thread_cast(thread_);
1468 struct dpif_netlink_flow_dump *dump = thread->dump;
1469 struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif);
1472 ofpbuf_delete(thread->nl_actions);
1473 thread->nl_actions = NULL;
1477 || (n_flows < max_flows && thread->nl_flows.size)) {
1478 struct dpif_netlink_flow datapath_flow;
1479 struct ofpbuf nl_flow;
1482 /* Try to grab another flow. */
1483 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1487 /* Convert the flow to our output format. */
1488 error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow);
1490 atomic_store_relaxed(&dump->status, error);
1494 if (dump->up.terse || datapath_flow.actions) {
1495 /* Common case: we don't want actions, or the flow includes
1497 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1500 /* Rare case: the flow does not include actions. Retrieve this
1501 * individual flow again to get the actions. */
1502 error = dpif_netlink_flow_get(dpif, &datapath_flow,
1503 &datapath_flow, &thread->nl_actions);
1504 if (error == ENOENT) {
1505 VLOG_DBG("dumped flow disappeared on get");
1508 VLOG_WARN("error fetching dumped flow: %s",
1509 ovs_strerror(error));
1510 atomic_store_relaxed(&dump->status, error);
1514 /* Save this flow. Then exit, because we only have one buffer to
1515 * handle this case. */
1516 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1525 dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1528 struct ovs_header *k_exec;
1531 ofpbuf_prealloc_tailroom(buf, (64
1532 + dp_packet_size(d_exec->packet)
1533 + ODP_KEY_METADATA_SIZE
1534 + d_exec->actions_len));
1536 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1537 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1539 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1540 k_exec->dp_ifindex = dp_ifindex;
1542 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1543 dp_packet_data(d_exec->packet),
1544 dp_packet_size(d_exec->packet));
1546 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1547 odp_key_from_pkt_metadata(buf, &d_exec->packet->md);
1548 nl_msg_end_nested(buf, key_ofs);
1550 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1551 d_exec->actions, d_exec->actions_len);
1552 if (d_exec->probe) {
1553 nl_msg_put_flag(buf, OVS_PACKET_ATTR_PROBE);
1557 /* Executes, against 'dpif', up to the first 'n_ops' operations in 'ops'.
1558 * Returns the number actually executed (at least 1, if 'n_ops' is
1561 dpif_netlink_operate__(struct dpif_netlink *dpif,
1562 struct dpif_op **ops, size_t n_ops)
1564 enum { MAX_OPS = 50 };
1567 struct nl_transaction txn;
1569 struct ofpbuf request;
1570 uint64_t request_stub[1024 / 8];
1572 struct ofpbuf reply;
1573 uint64_t reply_stub[1024 / 8];
1576 struct nl_transaction *txnsp[MAX_OPS];
1579 n_ops = MIN(n_ops, MAX_OPS);
1580 for (i = 0; i < n_ops; i++) {
1581 struct op_auxdata *aux = &auxes[i];
1582 struct dpif_op *op = ops[i];
1583 struct dpif_flow_put *put;
1584 struct dpif_flow_del *del;
1585 struct dpif_flow_get *get;
1586 struct dpif_netlink_flow flow;
1588 ofpbuf_use_stub(&aux->request,
1589 aux->request_stub, sizeof aux->request_stub);
1590 aux->txn.request = &aux->request;
1592 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1593 aux->txn.reply = NULL;
1596 case DPIF_OP_FLOW_PUT:
1597 put = &op->u.flow_put;
1598 dpif_netlink_init_flow_put(dpif, put, &flow);
1600 flow.nlmsg_flags |= NLM_F_ECHO;
1601 aux->txn.reply = &aux->reply;
1603 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1606 case DPIF_OP_FLOW_DEL:
1607 del = &op->u.flow_del;
1608 dpif_netlink_init_flow_del(dpif, del, &flow);
1610 flow.nlmsg_flags |= NLM_F_ECHO;
1611 aux->txn.reply = &aux->reply;
1613 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1616 case DPIF_OP_EXECUTE:
1617 /* Can't execute a packet that won't fit in a Netlink attribute. */
1618 if (OVS_UNLIKELY(nl_attr_oversized(
1619 dp_packet_size(op->u.execute.packet)))) {
1620 /* Report an error immediately if this is the first operation.
1621 * Otherwise the easiest thing to do is to postpone to the next
1622 * call (when this will be the first operation). */
1624 VLOG_ERR_RL(&error_rl,
1625 "dropping oversized %"PRIu32"-byte packet",
1626 dp_packet_size(op->u.execute.packet));
1627 op->error = ENOBUFS;
1632 dpif_netlink_encode_execute(dpif->dp_ifindex, &op->u.execute,
1637 case DPIF_OP_FLOW_GET:
1638 get = &op->u.flow_get;
1639 dpif_netlink_init_flow_get(dpif, get, &flow);
1640 aux->txn.reply = get->buffer;
1641 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1649 for (i = 0; i < n_ops; i++) {
1650 txnsp[i] = &auxes[i].txn;
1652 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1654 for (i = 0; i < n_ops; i++) {
1655 struct op_auxdata *aux = &auxes[i];
1656 struct nl_transaction *txn = &auxes[i].txn;
1657 struct dpif_op *op = ops[i];
1658 struct dpif_flow_put *put;
1659 struct dpif_flow_del *del;
1660 struct dpif_flow_get *get;
1662 op->error = txn->error;
1665 case DPIF_OP_FLOW_PUT:
1666 put = &op->u.flow_put;
1669 struct dpif_netlink_flow reply;
1671 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1674 dpif_netlink_flow_get_stats(&reply, put->stats);
1680 case DPIF_OP_FLOW_DEL:
1681 del = &op->u.flow_del;
1684 struct dpif_netlink_flow reply;
1686 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1689 dpif_netlink_flow_get_stats(&reply, del->stats);
1695 case DPIF_OP_EXECUTE:
1698 case DPIF_OP_FLOW_GET:
1699 get = &op->u.flow_get;
1701 struct dpif_netlink_flow reply;
1703 op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply);
1705 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, get->flow,
1715 ofpbuf_uninit(&aux->request);
1716 ofpbuf_uninit(&aux->reply);
1723 dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1725 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1728 size_t chunk = dpif_netlink_operate__(dpif, ops, n_ops);
1736 dpif_netlink_handler_uninit(struct dpif_handler *handler)
1738 vport_delete_sock_pool(handler);
1742 dpif_netlink_handler_init(struct dpif_handler *handler)
1744 return vport_create_sock_pool(handler);
1749 dpif_netlink_handler_init(struct dpif_handler *handler)
1751 handler->epoll_fd = epoll_create(10);
1752 return handler->epoll_fd < 0 ? errno : 0;
1756 dpif_netlink_handler_uninit(struct dpif_handler *handler)
1758 close(handler->epoll_fd);
1762 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1763 * currently in 'dpif' in the kernel, by adding a new set of channels for
1764 * any kernel vport that lacks one and deleting any channels that have no
1765 * backing kernel vports. */
1767 dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers)
1768 OVS_REQ_WRLOCK(dpif->upcall_lock)
1770 unsigned long int *keep_channels;
1771 struct dpif_netlink_vport vport;
1772 size_t keep_channels_nbits;
1773 struct nl_dump dump;
1774 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1779 ovs_assert(!WINDOWS || n_handlers <= 1);
1780 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
1782 if (dpif->n_handlers != n_handlers) {
1783 destroy_all_channels(dpif);
1784 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1785 for (i = 0; i < n_handlers; i++) {
1787 struct dpif_handler *handler = &dpif->handlers[i];
1789 error = dpif_netlink_handler_init(handler);
1792 struct dpif_handler *tmp = &dpif->handlers[i];
1795 for (j = 0; j < i; j++) {
1796 dpif_netlink_handler_uninit(tmp);
1798 free(dpif->handlers);
1799 dpif->handlers = NULL;
1804 dpif->n_handlers = n_handlers;
1807 for (i = 0; i < n_handlers; i++) {
1808 struct dpif_handler *handler = &dpif->handlers[i];
1810 handler->event_offset = handler->n_events = 0;
1813 keep_channels_nbits = dpif->uc_array_size;
1814 keep_channels = bitmap_allocate(keep_channels_nbits);
1816 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1817 dpif_netlink_port_dump_start__(dpif, &dump);
1818 while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) {
1819 uint32_t port_no = odp_to_u32(vport.port_no);
1820 uint32_t *upcall_pids = NULL;
1823 if (port_no >= dpif->uc_array_size
1824 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1825 struct nl_sock **socksp = vport_create_socksp(dpif, &error);
1831 error = vport_add_channels(dpif, vport.port_no, socksp);
1833 VLOG_INFO("%s: could not add channels for port %s",
1834 dpif_name(&dpif->dpif), vport.name);
1835 vport_del_socksp(dpif, socksp);
1839 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1843 /* Configure the vport to deliver misses to 'sock'. */
1844 if (vport.upcall_pids[0] == 0
1845 || vport.n_upcall_pids != dpif->n_handlers
1846 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1848 struct dpif_netlink_vport vport_request;
1850 dpif_netlink_vport_init(&vport_request);
1851 vport_request.cmd = OVS_VPORT_CMD_SET;
1852 vport_request.dp_ifindex = dpif->dp_ifindex;
1853 vport_request.port_no = vport.port_no;
1854 vport_request.n_upcall_pids = dpif->n_handlers;
1855 vport_request.upcall_pids = upcall_pids;
1856 error = dpif_netlink_vport_transact(&vport_request, NULL, NULL);
1858 VLOG_WARN_RL(&error_rl,
1859 "%s: failed to set upcall pid on port: %s",
1860 dpif_name(&dpif->dpif), ovs_strerror(error));
1862 if (error != ENODEV && error != ENOENT) {
1865 /* The vport isn't really there, even though the dump says
1866 * it is. Probably we just hit a race after a port
1873 if (port_no < keep_channels_nbits) {
1874 bitmap_set1(keep_channels, port_no);
1881 vport_del_channels(dpif, vport.port_no);
1883 nl_dump_done(&dump);
1884 ofpbuf_uninit(&buf);
1886 /* Discard any saved channels that we didn't reuse. */
1887 for (i = 0; i < keep_channels_nbits; i++) {
1888 if (!bitmap_is_set(keep_channels, i)) {
1889 vport_del_channels(dpif, u32_to_odp(i));
1892 free(keep_channels);
1898 dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable)
1899 OVS_REQ_WRLOCK(dpif->upcall_lock)
1901 if ((dpif->handlers != NULL) == enable) {
1903 } else if (!enable) {
1904 destroy_all_channels(dpif);
1907 return dpif_netlink_refresh_channels(dpif, 1);
1912 dpif_netlink_recv_set(struct dpif *dpif_, bool enable)
1914 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1917 fat_rwlock_wrlock(&dpif->upcall_lock);
1918 error = dpif_netlink_recv_set__(dpif, enable);
1919 fat_rwlock_unlock(&dpif->upcall_lock);
1925 dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1927 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1931 /* Multiple upcall handlers will be supported once kernel datapath supports
1933 if (n_handlers > 1) {
1938 fat_rwlock_wrlock(&dpif->upcall_lock);
1939 if (dpif->handlers) {
1940 error = dpif_netlink_refresh_channels(dpif, n_handlers);
1942 fat_rwlock_unlock(&dpif->upcall_lock);
1948 dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1949 uint32_t queue_id, uint32_t *priority)
1951 if (queue_id < 0xf000) {
1952 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1960 parse_odp_packet(const struct dpif_netlink *dpif, struct ofpbuf *buf,
1961 struct dpif_upcall *upcall, int *dp_ifindex)
1963 static const struct nl_policy ovs_packet_policy[] = {
1964 /* Always present. */
1965 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1966 .min_len = ETH_HEADER_LEN },
1967 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1969 /* OVS_PACKET_CMD_ACTION only. */
1970 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1971 [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true },
1974 struct ovs_header *ovs_header;
1975 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1976 struct nlmsghdr *nlmsg;
1977 struct genlmsghdr *genl;
1981 ofpbuf_use_const(&b, buf->data, buf->size);
1983 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1984 genl = ofpbuf_try_pull(&b, sizeof *genl);
1985 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1986 if (!nlmsg || !genl || !ovs_header
1987 || nlmsg->nlmsg_type != ovs_packet_family
1988 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1989 ARRAY_SIZE(ovs_packet_policy))) {
1993 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1994 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
2000 /* (Re)set ALL fields of '*upcall' on successful return. */
2001 upcall->type = type;
2002 upcall->key = CONST_CAST(struct nlattr *,
2003 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
2004 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
2005 dpif_flow_hash(&dpif->dpif, upcall->key, upcall->key_len, &upcall->ufid);
2006 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
2007 upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY];
2009 /* Allow overwriting the netlink attribute header without reallocating. */
2010 dp_packet_use_stub(&upcall->packet,
2011 CONST_CAST(struct nlattr *,
2012 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
2013 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
2014 sizeof(struct nlattr));
2015 dp_packet_set_data(&upcall->packet,
2016 (char *)dp_packet_data(&upcall->packet) + sizeof(struct nlattr));
2017 dp_packet_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
2019 *dp_ifindex = ovs_header->dp_ifindex;
2025 #define PACKET_RECV_BATCH_SIZE 50
2027 dpif_netlink_recv_windows(struct dpif_netlink *dpif, uint32_t handler_id,
2028 struct dpif_upcall *upcall, struct ofpbuf *buf)
2029 OVS_REQ_RDLOCK(dpif->upcall_lock)
2031 struct dpif_handler *handler;
2033 struct dpif_windows_vport_sock *sock_pool;
2036 if (!dpif->handlers) {
2040 /* Only one handler is supported currently. */
2041 if (handler_id >= 1) {
2045 if (handler_id >= dpif->n_handlers) {
2049 handler = &dpif->handlers[handler_id];
2050 sock_pool = handler->vport_sock_pool;
2052 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2057 if (++read_tries > PACKET_RECV_BATCH_SIZE) {
2061 error = nl_sock_recv(sock_pool[i].nl_sock, buf, false);
2062 if (error == ENOBUFS) {
2063 /* ENOBUFS typically means that we've received so many
2064 * packets that the buffer overflowed. Try again
2065 * immediately because there's almost certainly a packet
2066 * waiting for us. */
2067 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2071 /* XXX: ch->last_poll = time_msec(); */
2073 if (error == EAGAIN) {
2079 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2080 if (!error && dp_ifindex == dpif->dp_ifindex) {
2092 dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id,
2093 struct dpif_upcall *upcall, struct ofpbuf *buf)
2094 OVS_REQ_RDLOCK(dpif->upcall_lock)
2096 struct dpif_handler *handler;
2099 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
2103 handler = &dpif->handlers[handler_id];
2104 if (handler->event_offset >= handler->n_events) {
2107 handler->event_offset = handler->n_events = 0;
2110 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
2111 dpif->uc_array_size, 0);
2112 } while (retval < 0 && errno == EINTR);
2115 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2116 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
2117 } else if (retval > 0) {
2118 handler->n_events = retval;
2122 while (handler->event_offset < handler->n_events) {
2123 int idx = handler->epoll_events[handler->event_offset].data.u32;
2124 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
2126 handler->event_offset++;
2132 if (++read_tries > 50) {
2136 error = nl_sock_recv(ch->sock, buf, false);
2137 if (error == ENOBUFS) {
2138 /* ENOBUFS typically means that we've received so many
2139 * packets that the buffer overflowed. Try again
2140 * immediately because there's almost certainly a packet
2141 * waiting for us. */
2142 report_loss(dpif, ch, idx, handler_id);
2146 ch->last_poll = time_msec();
2148 if (error == EAGAIN) {
2154 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2155 if (!error && dp_ifindex == dpif->dp_ifindex) {
2168 dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id,
2169 struct dpif_upcall *upcall, struct ofpbuf *buf)
2171 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2174 fat_rwlock_rdlock(&dpif->upcall_lock);
2176 error = dpif_netlink_recv_windows(dpif, handler_id, upcall, buf);
2178 error = dpif_netlink_recv__(dpif, handler_id, upcall, buf);
2180 fat_rwlock_unlock(&dpif->upcall_lock);
2186 dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id)
2187 OVS_REQ_RDLOCK(dpif->upcall_lock)
2191 struct dpif_windows_vport_sock *sock_pool =
2192 dpif->handlers[handler_id].vport_sock_pool;
2194 /* Only one handler is supported currently. */
2195 if (handler_id >= 1) {
2199 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2200 nl_sock_wait(sock_pool[i].nl_sock, POLLIN);
2203 if (dpif->handlers && handler_id < dpif->n_handlers) {
2204 struct dpif_handler *handler = &dpif->handlers[handler_id];
2206 poll_fd_wait(handler->epoll_fd, POLLIN);
2212 dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id)
2214 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2216 fat_rwlock_rdlock(&dpif->upcall_lock);
2217 dpif_netlink_recv_wait__(dpif, handler_id);
2218 fat_rwlock_unlock(&dpif->upcall_lock);
2222 dpif_netlink_recv_purge__(struct dpif_netlink *dpif)
2223 OVS_REQ_WRLOCK(dpif->upcall_lock)
2225 if (dpif->handlers) {
2228 for (i = 0; i < dpif->uc_array_size; i++ ) {
2229 if (!dpif->handlers[0].channels[i].sock) {
2233 for (j = 0; j < dpif->n_handlers; j++) {
2234 nl_sock_drain(dpif->handlers[j].channels[i].sock);
2241 dpif_netlink_recv_purge(struct dpif *dpif_)
2243 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2245 fat_rwlock_wrlock(&dpif->upcall_lock);
2246 dpif_netlink_recv_purge__(dpif);
2247 fat_rwlock_unlock(&dpif->upcall_lock);
2251 dpif_netlink_get_datapath_version(void)
2253 char *version_str = NULL;
2257 #define MAX_VERSION_STR_SIZE 80
2258 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2261 f = fopen(LINUX_DATAPATH_VERSION_FILE, "r");
2264 char version[MAX_VERSION_STR_SIZE];
2266 if (fgets(version, MAX_VERSION_STR_SIZE, f)) {
2267 newline = strchr(version, '\n');
2271 version_str = xstrdup(version);
2280 const struct dpif_class dpif_netlink_class = {
2283 dpif_netlink_enumerate,
2287 dpif_netlink_destroy,
2290 dpif_netlink_get_stats,
2291 dpif_netlink_port_add,
2292 dpif_netlink_port_del,
2293 dpif_netlink_port_query_by_number,
2294 dpif_netlink_port_query_by_name,
2295 dpif_netlink_port_get_pid,
2296 dpif_netlink_port_dump_start,
2297 dpif_netlink_port_dump_next,
2298 dpif_netlink_port_dump_done,
2299 dpif_netlink_port_poll,
2300 dpif_netlink_port_poll_wait,
2301 dpif_netlink_flow_flush,
2302 dpif_netlink_flow_dump_create,
2303 dpif_netlink_flow_dump_destroy,
2304 dpif_netlink_flow_dump_thread_create,
2305 dpif_netlink_flow_dump_thread_destroy,
2306 dpif_netlink_flow_dump_next,
2307 dpif_netlink_operate,
2308 dpif_netlink_recv_set,
2309 dpif_netlink_handlers_set,
2310 NULL, /* poll_thread_set */
2311 dpif_netlink_queue_to_priority,
2313 dpif_netlink_recv_wait,
2314 dpif_netlink_recv_purge,
2315 NULL, /* register_upcall_cb */
2316 NULL, /* enable_upcall */
2317 NULL, /* disable_upcall */
2318 dpif_netlink_get_datapath_version, /* get_datapath_version */
2322 dpif_netlink_init(void)
2324 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2327 if (ovsthread_once_start(&once)) {
2328 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
2329 &ovs_datapath_family);
2331 VLOG_ERR("Generic Netlink family '%s' does not exist. "
2332 "The Open vSwitch kernel module is probably not loaded.",
2333 OVS_DATAPATH_FAMILY);
2336 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
2339 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
2342 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
2343 &ovs_packet_family);
2346 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
2347 &ovs_vport_mcgroup);
2350 ovsthread_once_done(&once);
2357 dpif_netlink_is_internal_device(const char *name)
2359 struct dpif_netlink_vport reply;
2363 error = dpif_netlink_vport_get(name, &reply, &buf);
2366 } else if (error != ENODEV && error != ENOENT) {
2367 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
2368 name, ovs_strerror(error));
2371 return reply.type == OVS_VPORT_TYPE_INTERNAL;
2374 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2375 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
2376 * positive errno value.
2378 * 'vport' will contain pointers into 'buf', so the caller should not free
2379 * 'buf' while 'vport' is still in use. */
2381 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport,
2382 const struct ofpbuf *buf)
2384 static const struct nl_policy ovs_vport_policy[] = {
2385 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
2386 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
2387 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2388 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
2389 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
2391 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
2394 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
2395 struct ovs_header *ovs_header;
2396 struct nlmsghdr *nlmsg;
2397 struct genlmsghdr *genl;
2400 dpif_netlink_vport_init(vport);
2402 ofpbuf_use_const(&b, buf->data, buf->size);
2403 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2404 genl = ofpbuf_try_pull(&b, sizeof *genl);
2405 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2406 if (!nlmsg || !genl || !ovs_header
2407 || nlmsg->nlmsg_type != ovs_vport_family
2408 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2409 ARRAY_SIZE(ovs_vport_policy))) {
2413 vport->cmd = genl->cmd;
2414 vport->dp_ifindex = ovs_header->dp_ifindex;
2415 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2416 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2417 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2418 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2419 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2420 / (sizeof *vport->upcall_pids);
2421 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2424 if (a[OVS_VPORT_ATTR_STATS]) {
2425 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2427 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2428 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2429 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2434 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2435 * followed by Netlink attributes corresponding to 'vport'. */
2437 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport,
2440 struct ovs_header *ovs_header;
2442 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2443 vport->cmd, OVS_VPORT_VERSION);
2445 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2446 ovs_header->dp_ifindex = vport->dp_ifindex;
2448 if (vport->port_no != ODPP_NONE) {
2449 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2452 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2453 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2457 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2460 if (vport->upcall_pids) {
2461 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2463 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2467 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2468 vport->stats, sizeof *vport->stats);
2471 if (vport->options) {
2472 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2473 vport->options, vport->options_len);
2477 /* Clears 'vport' to "empty" values. */
2479 dpif_netlink_vport_init(struct dpif_netlink_vport *vport)
2481 memset(vport, 0, sizeof *vport);
2482 vport->port_no = ODPP_NONE;
2485 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2486 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2487 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2488 * result of the command is expected to be an ovs_vport also, which is decoded
2489 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2490 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2492 dpif_netlink_vport_transact(const struct dpif_netlink_vport *request,
2493 struct dpif_netlink_vport *reply,
2494 struct ofpbuf **bufp)
2496 struct ofpbuf *request_buf;
2499 ovs_assert((reply != NULL) == (bufp != NULL));
2501 error = dpif_netlink_init();
2505 dpif_netlink_vport_init(reply);
2510 request_buf = ofpbuf_new(1024);
2511 dpif_netlink_vport_to_ofpbuf(request, request_buf);
2512 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2513 ofpbuf_delete(request_buf);
2517 error = dpif_netlink_vport_from_ofpbuf(reply, *bufp);
2520 dpif_netlink_vport_init(reply);
2521 ofpbuf_delete(*bufp);
2528 /* Obtains information about the kernel vport named 'name' and stores it into
2529 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2530 * longer needed ('reply' will contain pointers into '*bufp'). */
2532 dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply,
2533 struct ofpbuf **bufp)
2535 struct dpif_netlink_vport request;
2537 dpif_netlink_vport_init(&request);
2538 request.cmd = OVS_VPORT_CMD_GET;
2539 request.name = name;
2541 return dpif_netlink_vport_transact(&request, reply, bufp);
2544 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2545 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2546 * positive errno value.
2548 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2549 * while 'dp' is still in use. */
2551 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf)
2553 static const struct nl_policy ovs_datapath_policy[] = {
2554 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2555 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2557 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2558 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2562 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2563 struct ovs_header *ovs_header;
2564 struct nlmsghdr *nlmsg;
2565 struct genlmsghdr *genl;
2568 dpif_netlink_dp_init(dp);
2570 ofpbuf_use_const(&b, buf->data, buf->size);
2571 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2572 genl = ofpbuf_try_pull(&b, sizeof *genl);
2573 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2574 if (!nlmsg || !genl || !ovs_header
2575 || nlmsg->nlmsg_type != ovs_datapath_family
2576 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2577 ARRAY_SIZE(ovs_datapath_policy))) {
2581 dp->cmd = genl->cmd;
2582 dp->dp_ifindex = ovs_header->dp_ifindex;
2583 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2584 if (a[OVS_DP_ATTR_STATS]) {
2585 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
2588 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2589 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
2595 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2597 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf)
2599 struct ovs_header *ovs_header;
2601 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2602 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2603 OVS_DATAPATH_VERSION);
2605 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2606 ovs_header->dp_ifindex = dp->dp_ifindex;
2609 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2612 if (dp->upcall_pid) {
2613 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2616 if (dp->user_features) {
2617 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2620 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2623 /* Clears 'dp' to "empty" values. */
2625 dpif_netlink_dp_init(struct dpif_netlink_dp *dp)
2627 memset(dp, 0, sizeof *dp);
2631 dpif_netlink_dp_dump_start(struct nl_dump *dump)
2633 struct dpif_netlink_dp request;
2636 dpif_netlink_dp_init(&request);
2637 request.cmd = OVS_DP_CMD_GET;
2639 buf = ofpbuf_new(1024);
2640 dpif_netlink_dp_to_ofpbuf(&request, buf);
2641 nl_dump_start(dump, NETLINK_GENERIC, buf);
2645 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2646 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2647 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2648 * result of the command is expected to be of the same form, which is decoded
2649 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2650 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2652 dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
2653 struct dpif_netlink_dp *reply, struct ofpbuf **bufp)
2655 struct ofpbuf *request_buf;
2658 ovs_assert((reply != NULL) == (bufp != NULL));
2660 request_buf = ofpbuf_new(1024);
2661 dpif_netlink_dp_to_ofpbuf(request, request_buf);
2662 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2663 ofpbuf_delete(request_buf);
2666 dpif_netlink_dp_init(reply);
2668 error = dpif_netlink_dp_from_ofpbuf(reply, *bufp);
2671 ofpbuf_delete(*bufp);
2678 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2679 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2680 * will contain pointers into '*bufp'). */
2682 dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply,
2683 struct ofpbuf **bufp)
2685 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2686 struct dpif_netlink_dp request;
2688 dpif_netlink_dp_init(&request);
2689 request.cmd = OVS_DP_CMD_GET;
2690 request.dp_ifindex = dpif->dp_ifindex;
2692 return dpif_netlink_dp_transact(&request, reply, bufp);
2695 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2696 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2697 * positive errno value.
2699 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2700 * while 'flow' is still in use. */
2702 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow,
2703 const struct ofpbuf *buf)
2705 static const struct nl_policy ovs_flow_policy[__OVS_FLOW_ATTR_MAX] = {
2706 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED, .optional = true },
2707 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2708 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2709 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2711 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2712 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2713 [OVS_FLOW_ATTR_UFID] = { .type = NL_A_UNSPEC, .optional = true,
2714 .min_len = sizeof(ovs_u128) },
2715 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2716 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
2717 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
2720 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2721 struct ovs_header *ovs_header;
2722 struct nlmsghdr *nlmsg;
2723 struct genlmsghdr *genl;
2726 dpif_netlink_flow_init(flow);
2728 ofpbuf_use_const(&b, buf->data, buf->size);
2729 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2730 genl = ofpbuf_try_pull(&b, sizeof *genl);
2731 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2732 if (!nlmsg || !genl || !ovs_header
2733 || nlmsg->nlmsg_type != ovs_flow_family
2734 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2735 ARRAY_SIZE(ovs_flow_policy))) {
2738 if (!a[OVS_FLOW_ATTR_KEY] && !a[OVS_FLOW_ATTR_UFID]) {
2742 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2743 flow->dp_ifindex = ovs_header->dp_ifindex;
2744 if (a[OVS_FLOW_ATTR_KEY]) {
2745 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2746 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2749 if (a[OVS_FLOW_ATTR_UFID]) {
2750 const ovs_u128 *ufid;
2752 ufid = nl_attr_get_unspec(a[OVS_FLOW_ATTR_UFID],
2753 nl_attr_get_size(a[OVS_FLOW_ATTR_UFID]));
2755 flow->ufid_present = true;
2757 if (a[OVS_FLOW_ATTR_MASK]) {
2758 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2759 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2761 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2762 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2763 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2765 if (a[OVS_FLOW_ATTR_STATS]) {
2766 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2768 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2769 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2771 if (a[OVS_FLOW_ATTR_USED]) {
2772 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2777 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2778 * followed by Netlink attributes corresponding to 'flow'. */
2780 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow,
2783 struct ovs_header *ovs_header;
2785 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2786 NLM_F_REQUEST | flow->nlmsg_flags,
2787 flow->cmd, OVS_FLOW_VERSION);
2789 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2790 ovs_header->dp_ifindex = flow->dp_ifindex;
2792 if (flow->ufid_present) {
2793 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_UFID, &flow->ufid,
2796 if (flow->ufid_terse) {
2797 nl_msg_put_u32(buf, OVS_FLOW_ATTR_UFID_FLAGS,
2798 OVS_UFID_F_OMIT_KEY | OVS_UFID_F_OMIT_MASK
2799 | OVS_UFID_F_OMIT_ACTIONS);
2801 if (!flow->ufid_terse || !flow->ufid_present) {
2802 if (flow->key_len) {
2803 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY,
2804 flow->key, flow->key_len);
2807 if (flow->mask_len) {
2808 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK,
2809 flow->mask, flow->mask_len);
2811 if (flow->actions || flow->actions_len) {
2812 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2813 flow->actions, flow->actions_len);
2817 /* We never need to send these to the kernel. */
2818 ovs_assert(!flow->stats);
2819 ovs_assert(!flow->tcp_flags);
2820 ovs_assert(!flow->used);
2823 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2826 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
2830 /* Clears 'flow' to "empty" values. */
2832 dpif_netlink_flow_init(struct dpif_netlink_flow *flow)
2834 memset(flow, 0, sizeof *flow);
2837 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2838 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2839 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2840 * result of the command is expected to be a flow also, which is decoded and
2841 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2842 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2844 dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
2845 struct dpif_netlink_flow *reply,
2846 struct ofpbuf **bufp)
2848 struct ofpbuf *request_buf;
2851 ovs_assert((reply != NULL) == (bufp != NULL));
2854 request->nlmsg_flags |= NLM_F_ECHO;
2857 request_buf = ofpbuf_new(1024);
2858 dpif_netlink_flow_to_ofpbuf(request, request_buf);
2859 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2860 ofpbuf_delete(request_buf);
2864 error = dpif_netlink_flow_from_ofpbuf(reply, *bufp);
2867 dpif_netlink_flow_init(reply);
2868 ofpbuf_delete(*bufp);
2876 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow,
2877 struct dpif_flow_stats *stats)
2880 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
2881 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
2883 stats->n_packets = 0;
2886 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2887 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2890 /* Logs information about a packet that was recently lost in 'ch' (in
2893 report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2894 uint32_t handler_id)
2896 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2899 if (VLOG_DROP_WARN(&rl)) {
2904 if (ch->last_poll != LLONG_MIN) {
2905 ds_put_format(&s, " (last polled %lld ms ago)",
2906 time_msec() - ch->last_poll);
2909 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2910 dpif_name(&dpif->dpif), ch_idx, handler_id);