2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 *);
141 static bool dpif_netlink_check_ufid__(struct dpif *dpif);
142 static bool dpif_netlink_check_ufid(struct dpif *dpif);
144 /* One of the dpif channels between the kernel and userspace. */
145 struct dpif_channel {
146 struct nl_sock *sock; /* Netlink socket. */
147 long long int last_poll; /* Last time this channel was polled. */
151 #define VPORT_SOCK_POOL_SIZE 1
152 /* On Windows, there is no native support for epoll. There are equivalent
153 * interfaces though, that are not used currently. For simpicity, a pool of
154 * netlink sockets is used. Each socket is represented by 'struct
155 * dpif_windows_vport_sock'. Since it is a pool, multiple OVS ports may be
156 * sharing the same socket. In the future, we can add a reference count and
158 struct dpif_windows_vport_sock {
159 struct nl_sock *nl_sock; /* netlink socket. */
163 struct dpif_handler {
164 struct dpif_channel *channels;/* Array of channels for each handler. */
165 struct epoll_event *epoll_events;
166 int epoll_fd; /* epoll fd that includes channel socks. */
167 int n_events; /* Num events returned by epoll_wait(). */
168 int event_offset; /* Offset into 'epoll_events'. */
171 /* Pool of sockets. */
172 struct dpif_windows_vport_sock *vport_sock_pool;
173 size_t last_used_pool_idx; /* Index to aid in allocating a
174 socket in the pool to a port. */
178 /* Datapath interface for the openvswitch Linux kernel module. */
179 struct dpif_netlink {
183 /* Upcall messages. */
184 struct fat_rwlock upcall_lock;
185 struct dpif_handler *handlers;
186 uint32_t n_handlers; /* Num of upcall handlers. */
187 int uc_array_size; /* Size of 'handler->channels' and */
188 /* 'handler->epoll_events'. */
190 /* Change notification. */
191 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
192 bool refresh_channels;
194 /* If the datapath supports indexing flows using unique identifiers, then
195 * we can reduce the size of netlink messages by omitting fields like the
196 * flow key during flow operations. */
200 static void report_loss(struct dpif_netlink *, struct dpif_channel *,
201 uint32_t ch_idx, uint32_t handler_id);
203 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
205 /* Generic Netlink family numbers for OVS.
207 * Initialized by dpif_netlink_init(). */
208 static int ovs_datapath_family;
209 static int ovs_vport_family;
210 static int ovs_flow_family;
211 static int ovs_packet_family;
213 /* Generic Netlink multicast groups for OVS.
215 * Initialized by dpif_netlink_init(). */
216 static unsigned int ovs_vport_mcgroup;
218 static int dpif_netlink_init(void);
219 static int open_dpif(const struct dpif_netlink_dp *, struct dpif **);
220 static uint32_t dpif_netlink_port_get_pid(const struct dpif *,
221 odp_port_t port_no, uint32_t hash);
222 static void dpif_netlink_handler_uninit(struct dpif_handler *handler);
223 static int dpif_netlink_refresh_channels(struct dpif_netlink *,
224 uint32_t n_handlers);
225 static void dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *,
227 static int dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *,
228 const struct ofpbuf *);
230 static struct dpif_netlink *
231 dpif_netlink_cast(const struct dpif *dpif)
233 dpif_assert_class(dpif, &dpif_netlink_class);
234 return CONTAINER_OF(dpif, struct dpif_netlink, dpif);
238 dpif_netlink_enumerate(struct sset *all_dps,
239 const struct dpif_class *dpif_class OVS_UNUSED)
242 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
243 struct ofpbuf msg, buf;
246 error = dpif_netlink_init();
251 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
252 dpif_netlink_dp_dump_start(&dump);
253 while (nl_dump_next(&dump, &msg, &buf)) {
254 struct dpif_netlink_dp dp;
256 if (!dpif_netlink_dp_from_ofpbuf(&dp, &msg)) {
257 sset_add(all_dps, dp.name);
261 return nl_dump_done(&dump);
265 dpif_netlink_open(const struct dpif_class *class OVS_UNUSED, const char *name,
266 bool create, struct dpif **dpifp)
268 struct dpif_netlink_dp dp_request, dp;
273 error = dpif_netlink_init();
278 /* Create or look up datapath. */
279 dpif_netlink_dp_init(&dp_request);
281 dp_request.cmd = OVS_DP_CMD_NEW;
283 dp_request.upcall_pid = &upcall_pid;
285 /* Use OVS_DP_CMD_SET to report user features */
286 dp_request.cmd = OVS_DP_CMD_SET;
288 dp_request.name = name;
289 dp_request.user_features |= OVS_DP_F_UNALIGNED;
290 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
291 error = dpif_netlink_dp_transact(&dp_request, &dp, &buf);
296 error = open_dpif(&dp, dpifp);
302 open_dpif(const struct dpif_netlink_dp *dp, struct dpif **dpifp)
304 struct dpif_netlink *dpif;
306 dpif = xzalloc(sizeof *dpif);
307 dpif->port_notifier = NULL;
308 fat_rwlock_init(&dpif->upcall_lock);
310 dpif_init(&dpif->dpif, &dpif_netlink_class, dp->name,
311 dp->dp_ifindex, dp->dp_ifindex);
313 dpif->dp_ifindex = dp->dp_ifindex;
314 dpif->ufid_supported = dpif_netlink_check_ufid__(&dpif->dpif);
315 *dpifp = &dpif->dpif;
320 /* Destroys the netlink sockets pointed by the elements in 'socksp'
321 * and frees the 'socksp'. */
323 vport_del_socksp__(struct nl_sock **socksp, uint32_t n_socks)
327 for (i = 0; i < n_socks; i++) {
328 nl_sock_destroy(socksp[i]);
334 /* Creates an array of netlink sockets. Returns an array of the
335 * corresponding pointers. Records the error in 'error'. */
336 static struct nl_sock **
337 vport_create_socksp__(uint32_t n_socks, int *error)
339 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
342 for (i = 0; i < n_socks; i++) {
343 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
352 vport_del_socksp__(socksp, n_socks);
359 vport_delete_sock_pool(struct dpif_handler *handler)
360 OVS_REQ_WRLOCK(dpif->upcall_lock)
362 if (handler->vport_sock_pool) {
364 struct dpif_windows_vport_sock *sock_pool =
365 handler->vport_sock_pool;
367 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
368 if (sock_pool[i].nl_sock) {
369 nl_sock_unsubscribe_packets(sock_pool[i].nl_sock);
370 nl_sock_destroy(sock_pool[i].nl_sock);
371 sock_pool[i].nl_sock = NULL;
375 free(handler->vport_sock_pool);
376 handler->vport_sock_pool = NULL;
381 vport_create_sock_pool(struct dpif_handler *handler)
382 OVS_REQ_WRLOCK(dpif->upcall_lock)
384 struct dpif_windows_vport_sock *sock_pool;
388 sock_pool = xzalloc(VPORT_SOCK_POOL_SIZE * sizeof *sock_pool);
389 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
390 error = nl_sock_create(NETLINK_GENERIC, &sock_pool[i].nl_sock);
395 /* Enable the netlink socket to receive packets. This is equivalent to
396 * calling nl_sock_join_mcgroup() to receive events. */
397 error = nl_sock_subscribe_packets(sock_pool[i].nl_sock);
403 handler->vport_sock_pool = sock_pool;
404 handler->last_used_pool_idx = 0;
408 vport_delete_sock_pool(handler);
412 /* Returns an array pointers to netlink sockets. The sockets are picked from a
413 * pool. Records the error in 'error'. */
414 static struct nl_sock **
415 vport_create_socksp_windows(struct dpif_netlink *dpif, int *error)
416 OVS_REQ_WRLOCK(dpif->upcall_lock)
418 uint32_t n_socks = dpif->n_handlers;
419 struct nl_sock **socksp;
422 ovs_assert(n_socks <= 1);
423 socksp = xzalloc(n_socks * sizeof *socksp);
425 /* Pick netlink sockets to use in a round-robin fashion from each
426 * handler's pool of sockets. */
427 for (i = 0; i < n_socks; i++) {
428 struct dpif_handler *handler = &dpif->handlers[i];
429 struct dpif_windows_vport_sock *sock_pool = handler->vport_sock_pool;
430 size_t index = handler->last_used_pool_idx;
432 /* A pool of sockets is allocated when the handler is initialized. */
433 if (sock_pool == NULL) {
439 ovs_assert(index < VPORT_SOCK_POOL_SIZE);
440 socksp[i] = sock_pool[index].nl_sock;
441 socksp[i] = sock_pool[index].nl_sock;
442 ovs_assert(socksp[i]);
443 index = (index == VPORT_SOCK_POOL_SIZE - 1) ? 0 : index + 1;
444 handler->last_used_pool_idx = index;
451 vport_del_socksp_windows(struct dpif_netlink *dpif, struct nl_sock **socksp)
457 static struct nl_sock **
458 vport_create_socksp(struct dpif_netlink *dpif, int *error)
461 return vport_create_socksp_windows(dpif, error);
463 return vport_create_socksp__(dpif->n_handlers, error);
468 vport_del_socksp(struct dpif_netlink *dpif, struct nl_sock **socksp)
471 vport_del_socksp_windows(dpif, socksp);
473 vport_del_socksp__(socksp, dpif->n_handlers);
477 /* Given the array of pointers to netlink sockets 'socksp', returns
478 * the array of corresponding pids. If the 'socksp' is NULL, returns
479 * a single-element array of value 0. */
481 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
486 pids = xzalloc(sizeof *pids);
490 pids = xzalloc(n_socks * sizeof *pids);
491 for (i = 0; i < n_socks; i++) {
492 pids[i] = nl_sock_pid(socksp[i]);
499 /* Given the port number 'port_idx', extracts the pids of netlink sockets
500 * associated to the port and assigns it to 'upcall_pids'. */
502 vport_get_pids(struct dpif_netlink *dpif, uint32_t port_idx,
503 uint32_t **upcall_pids)
508 /* Since the nl_sock can only be assigned in either all
509 * or none "dpif->handlers" channels, the following check
511 if (!dpif->handlers[0].channels[port_idx].sock) {
514 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
516 pids = xzalloc(dpif->n_handlers * sizeof *pids);
518 for (i = 0; i < dpif->n_handlers; i++) {
519 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
528 vport_add_channels(struct dpif_netlink *dpif, odp_port_t port_no,
529 struct nl_sock **socksp)
531 struct epoll_event event;
532 uint32_t port_idx = odp_to_u32(port_no);
536 if (dpif->handlers == NULL) {
540 /* We assume that the datapath densely chooses port numbers, which can
541 * therefore be used as an index into 'channels' and 'epoll_events' of
542 * 'dpif->handler'. */
543 if (port_idx >= dpif->uc_array_size) {
544 uint32_t new_size = port_idx + 1;
546 if (new_size > MAX_PORTS) {
547 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
548 dpif_name(&dpif->dpif), port_no);
552 for (i = 0; i < dpif->n_handlers; i++) {
553 struct dpif_handler *handler = &dpif->handlers[i];
555 handler->channels = xrealloc(handler->channels,
556 new_size * sizeof *handler->channels);
558 for (j = dpif->uc_array_size; j < new_size; j++) {
559 handler->channels[j].sock = NULL;
562 handler->epoll_events = xrealloc(handler->epoll_events,
563 new_size * sizeof *handler->epoll_events);
566 dpif->uc_array_size = new_size;
569 memset(&event, 0, sizeof event);
570 event.events = EPOLLIN;
571 event.data.u32 = port_idx;
573 for (i = 0; i < dpif->n_handlers; i++) {
574 struct dpif_handler *handler = &dpif->handlers[i];
577 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
583 dpif->handlers[i].channels[port_idx].sock = socksp[i];
584 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
590 for (j = 0; j < i; j++) {
592 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
593 nl_sock_fd(socksp[j]), NULL);
595 dpif->handlers[j].channels[port_idx].sock = NULL;
602 vport_del_channels(struct dpif_netlink *dpif, odp_port_t port_no)
604 uint32_t port_idx = odp_to_u32(port_no);
607 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
611 /* Since the sock can only be assigned in either all or none
612 * of "dpif->handlers" channels, the following check would
614 if (!dpif->handlers[0].channels[port_idx].sock) {
618 for (i = 0; i < dpif->n_handlers; i++) {
619 struct dpif_handler *handler = &dpif->handlers[i];
621 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
622 nl_sock_fd(handler->channels[port_idx].sock), NULL);
623 nl_sock_destroy(handler->channels[port_idx].sock);
625 handler->channels[port_idx].sock = NULL;
626 handler->event_offset = handler->n_events = 0;
631 destroy_all_channels(struct dpif_netlink *dpif)
632 OVS_REQ_WRLOCK(dpif->upcall_lock)
636 if (!dpif->handlers) {
640 for (i = 0; i < dpif->uc_array_size; i++ ) {
641 struct dpif_netlink_vport vport_request;
642 uint32_t upcall_pids = 0;
644 /* Since the sock can only be assigned in either all or none
645 * of "dpif->handlers" channels, the following check would
647 if (!dpif->handlers[0].channels[i].sock) {
651 /* Turn off upcalls. */
652 dpif_netlink_vport_init(&vport_request);
653 vport_request.cmd = OVS_VPORT_CMD_SET;
654 vport_request.dp_ifindex = dpif->dp_ifindex;
655 vport_request.port_no = u32_to_odp(i);
656 vport_request.upcall_pids = &upcall_pids;
657 dpif_netlink_vport_transact(&vport_request, NULL, NULL);
659 vport_del_channels(dpif, u32_to_odp(i));
662 for (i = 0; i < dpif->n_handlers; i++) {
663 struct dpif_handler *handler = &dpif->handlers[i];
665 dpif_netlink_handler_uninit(handler);
666 free(handler->epoll_events);
667 free(handler->channels);
670 free(dpif->handlers);
671 dpif->handlers = NULL;
672 dpif->n_handlers = 0;
673 dpif->uc_array_size = 0;
677 dpif_netlink_close(struct dpif *dpif_)
679 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
681 nl_sock_destroy(dpif->port_notifier);
683 fat_rwlock_wrlock(&dpif->upcall_lock);
684 destroy_all_channels(dpif);
685 fat_rwlock_unlock(&dpif->upcall_lock);
687 fat_rwlock_destroy(&dpif->upcall_lock);
692 dpif_netlink_destroy(struct dpif *dpif_)
694 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
695 struct dpif_netlink_dp dp;
697 dpif_netlink_dp_init(&dp);
698 dp.cmd = OVS_DP_CMD_DEL;
699 dp.dp_ifindex = dpif->dp_ifindex;
700 return dpif_netlink_dp_transact(&dp, NULL, NULL);
704 dpif_netlink_run(struct dpif *dpif_)
706 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
708 if (dpif->refresh_channels) {
709 dpif->refresh_channels = false;
710 fat_rwlock_wrlock(&dpif->upcall_lock);
711 dpif_netlink_refresh_channels(dpif, dpif->n_handlers);
712 fat_rwlock_unlock(&dpif->upcall_lock);
718 dpif_netlink_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
720 struct dpif_netlink_dp dp;
724 error = dpif_netlink_dp_get(dpif_, &dp, &buf);
726 memset(stats, 0, sizeof *stats);
729 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
730 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
731 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
732 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
735 if (dp.megaflow_stats) {
736 stats->n_masks = dp.megaflow_stats->n_masks;
737 stats->n_mask_hit = get_32aligned_u64(
738 &dp.megaflow_stats->n_mask_hit);
740 stats->n_masks = UINT32_MAX;
741 stats->n_mask_hit = UINT64_MAX;
749 get_vport_type(const struct dpif_netlink_vport *vport)
751 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
753 switch (vport->type) {
754 case OVS_VPORT_TYPE_NETDEV: {
755 const char *type = netdev_get_type_from_name(vport->name);
757 return type ? type : "system";
760 case OVS_VPORT_TYPE_INTERNAL:
763 case OVS_VPORT_TYPE_GENEVE:
766 case OVS_VPORT_TYPE_GRE:
769 case OVS_VPORT_TYPE_GRE64:
772 case OVS_VPORT_TYPE_VXLAN:
775 case OVS_VPORT_TYPE_LISP:
778 case OVS_VPORT_TYPE_UNSPEC:
779 case __OVS_VPORT_TYPE_MAX:
783 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
784 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
788 static enum ovs_vport_type
789 netdev_to_ovs_vport_type(const struct netdev *netdev)
791 const char *type = netdev_get_type(netdev);
793 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
794 return OVS_VPORT_TYPE_NETDEV;
795 } else if (!strcmp(type, "internal")) {
796 return OVS_VPORT_TYPE_INTERNAL;
797 } else if (!strcmp(type, "geneve")) {
798 return OVS_VPORT_TYPE_GENEVE;
799 } else if (strstr(type, "gre64")) {
800 return OVS_VPORT_TYPE_GRE64;
801 } else if (strstr(type, "gre")) {
802 return OVS_VPORT_TYPE_GRE;
803 } else if (!strcmp(type, "vxlan")) {
804 return OVS_VPORT_TYPE_VXLAN;
805 } else if (!strcmp(type, "lisp")) {
806 return OVS_VPORT_TYPE_LISP;
808 return OVS_VPORT_TYPE_UNSPEC;
813 dpif_netlink_port_add__(struct dpif_netlink *dpif, struct netdev *netdev,
814 odp_port_t *port_nop)
815 OVS_REQ_WRLOCK(dpif->upcall_lock)
817 const struct netdev_tunnel_config *tnl_cfg;
818 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
819 const char *name = netdev_vport_get_dpif_port(netdev,
820 namebuf, sizeof namebuf);
821 const char *type = netdev_get_type(netdev);
822 struct dpif_netlink_vport request, reply;
824 uint64_t options_stub[64 / 8];
825 struct ofpbuf options;
826 struct nl_sock **socksp = NULL;
827 uint32_t *upcall_pids;
830 if (dpif->handlers) {
831 socksp = vport_create_socksp(dpif, &error);
837 dpif_netlink_vport_init(&request);
838 request.cmd = OVS_VPORT_CMD_NEW;
839 request.dp_ifindex = dpif->dp_ifindex;
840 request.type = netdev_to_ovs_vport_type(netdev);
841 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
842 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
843 "unsupported type `%s'",
844 dpif_name(&dpif->dpif), name, type);
845 vport_del_socksp(dpif, socksp);
850 if (request.type == OVS_VPORT_TYPE_NETDEV) {
852 /* XXX : Map appropiate Windows handle */
854 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
858 tnl_cfg = netdev_get_tunnel_config(netdev);
859 if (tnl_cfg && tnl_cfg->dst_port != 0) {
860 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
861 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
862 ntohs(tnl_cfg->dst_port));
863 request.options = ofpbuf_data(&options);
864 request.options_len = ofpbuf_size(&options);
867 request.port_no = *port_nop;
868 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
869 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
870 request.upcall_pids = upcall_pids;
872 error = dpif_netlink_vport_transact(&request, &reply, &buf);
874 *port_nop = reply.port_no;
876 if (error == EBUSY && *port_nop != ODPP_NONE) {
877 VLOG_INFO("%s: requested port %"PRIu32" is in use",
878 dpif_name(&dpif->dpif), *port_nop);
881 vport_del_socksp(dpif, socksp);
886 error = vport_add_channels(dpif, *port_nop, socksp);
888 VLOG_INFO("%s: could not add channel for port %s",
889 dpif_name(&dpif->dpif), name);
891 /* Delete the port. */
892 dpif_netlink_vport_init(&request);
893 request.cmd = OVS_VPORT_CMD_DEL;
894 request.dp_ifindex = dpif->dp_ifindex;
895 request.port_no = *port_nop;
896 dpif_netlink_vport_transact(&request, NULL, NULL);
897 vport_del_socksp(dpif, socksp);
911 dpif_netlink_port_add(struct dpif *dpif_, struct netdev *netdev,
912 odp_port_t *port_nop)
914 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
917 fat_rwlock_wrlock(&dpif->upcall_lock);
918 error = dpif_netlink_port_add__(dpif, netdev, port_nop);
919 fat_rwlock_unlock(&dpif->upcall_lock);
925 dpif_netlink_port_del__(struct dpif_netlink *dpif, odp_port_t port_no)
926 OVS_REQ_WRLOCK(dpif->upcall_lock)
928 struct dpif_netlink_vport vport;
931 dpif_netlink_vport_init(&vport);
932 vport.cmd = OVS_VPORT_CMD_DEL;
933 vport.dp_ifindex = dpif->dp_ifindex;
934 vport.port_no = port_no;
935 error = dpif_netlink_vport_transact(&vport, NULL, NULL);
937 vport_del_channels(dpif, port_no);
943 dpif_netlink_port_del(struct dpif *dpif_, odp_port_t port_no)
945 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
948 fat_rwlock_wrlock(&dpif->upcall_lock);
949 error = dpif_netlink_port_del__(dpif, port_no);
950 fat_rwlock_unlock(&dpif->upcall_lock);
956 dpif_netlink_port_query__(const struct dpif_netlink *dpif, odp_port_t port_no,
957 const char *port_name, struct dpif_port *dpif_port)
959 struct dpif_netlink_vport request;
960 struct dpif_netlink_vport reply;
964 dpif_netlink_vport_init(&request);
965 request.cmd = OVS_VPORT_CMD_GET;
966 request.dp_ifindex = dpif->dp_ifindex;
967 request.port_no = port_no;
968 request.name = port_name;
970 error = dpif_netlink_vport_transact(&request, &reply, &buf);
972 if (reply.dp_ifindex != request.dp_ifindex) {
973 /* A query by name reported that 'port_name' is in some datapath
974 * other than 'dpif', but the caller wants to know about 'dpif'. */
976 } else if (dpif_port) {
977 dpif_port->name = xstrdup(reply.name);
978 dpif_port->type = xstrdup(get_vport_type(&reply));
979 dpif_port->port_no = reply.port_no;
987 dpif_netlink_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
988 struct dpif_port *dpif_port)
990 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
992 return dpif_netlink_port_query__(dpif, port_no, NULL, dpif_port);
996 dpif_netlink_port_query_by_name(const struct dpif *dpif_, const char *devname,
997 struct dpif_port *dpif_port)
999 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1001 return dpif_netlink_port_query__(dpif, 0, devname, dpif_port);
1005 dpif_netlink_port_get_pid__(const struct dpif_netlink *dpif,
1006 odp_port_t port_no, uint32_t hash)
1007 OVS_REQ_RDLOCK(dpif->upcall_lock)
1009 uint32_t port_idx = odp_to_u32(port_no);
1012 if (dpif->handlers && dpif->uc_array_size > 0) {
1013 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
1014 * channel, since it is not heavily loaded. */
1015 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
1016 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
1018 /* Needs to check in case the socket pointer is changed in between
1019 * the holding of upcall_lock. A known case happens when the main
1020 * thread deletes the vport while the handler thread is handling
1021 * the upcall from that port. */
1022 if (h->channels[idx].sock) {
1023 pid = nl_sock_pid(h->channels[idx].sock);
1031 dpif_netlink_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
1034 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1037 fat_rwlock_rdlock(&dpif->upcall_lock);
1038 ret = dpif_netlink_port_get_pid__(dpif, port_no, hash);
1039 fat_rwlock_unlock(&dpif->upcall_lock);
1045 dpif_netlink_flow_flush(struct dpif *dpif_)
1047 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1048 struct dpif_netlink_flow flow;
1050 dpif_netlink_flow_init(&flow);
1051 flow.cmd = OVS_FLOW_CMD_DEL;
1052 flow.dp_ifindex = dpif->dp_ifindex;
1053 return dpif_netlink_flow_transact(&flow, NULL, NULL);
1056 struct dpif_netlink_port_state {
1057 struct nl_dump dump;
1062 dpif_netlink_port_dump_start__(const struct dpif_netlink *dpif,
1063 struct nl_dump *dump)
1065 struct dpif_netlink_vport request;
1068 dpif_netlink_vport_init(&request);
1069 request.cmd = OVS_VPORT_CMD_GET;
1070 request.dp_ifindex = dpif->dp_ifindex;
1072 buf = ofpbuf_new(1024);
1073 dpif_netlink_vport_to_ofpbuf(&request, buf);
1074 nl_dump_start(dump, NETLINK_GENERIC, buf);
1079 dpif_netlink_port_dump_start(const struct dpif *dpif_, void **statep)
1081 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1082 struct dpif_netlink_port_state *state;
1084 *statep = state = xmalloc(sizeof *state);
1085 dpif_netlink_port_dump_start__(dpif, &state->dump);
1087 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
1092 dpif_netlink_port_dump_next__(const struct dpif_netlink *dpif,
1093 struct nl_dump *dump,
1094 struct dpif_netlink_vport *vport,
1095 struct ofpbuf *buffer)
1100 if (!nl_dump_next(dump, &buf, buffer)) {
1104 error = dpif_netlink_vport_from_ofpbuf(vport, &buf);
1106 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
1107 dpif_name(&dpif->dpif), ovs_strerror(error));
1113 dpif_netlink_port_dump_next(const struct dpif *dpif_, void *state_,
1114 struct dpif_port *dpif_port)
1116 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1117 struct dpif_netlink_port_state *state = state_;
1118 struct dpif_netlink_vport vport;
1121 error = dpif_netlink_port_dump_next__(dpif, &state->dump, &vport,
1126 dpif_port->name = CONST_CAST(char *, vport.name);
1127 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
1128 dpif_port->port_no = vport.port_no;
1133 dpif_netlink_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
1135 struct dpif_netlink_port_state *state = state_;
1136 int error = nl_dump_done(&state->dump);
1138 ofpbuf_uninit(&state->buf);
1144 dpif_netlink_port_poll(const struct dpif *dpif_, char **devnamep)
1146 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1148 /* Lazily create the Netlink socket to listen for notifications. */
1149 if (!dpif->port_notifier) {
1150 struct nl_sock *sock;
1153 error = nl_sock_create(NETLINK_GENERIC, &sock);
1158 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
1160 nl_sock_destroy(sock);
1163 dpif->port_notifier = sock;
1165 /* We have no idea of the current state so report that everything
1171 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1172 uint64_t buf_stub[4096 / 8];
1176 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1177 error = nl_sock_recv(dpif->port_notifier, &buf, false);
1179 struct dpif_netlink_vport vport;
1181 error = dpif_netlink_vport_from_ofpbuf(&vport, &buf);
1183 if (vport.dp_ifindex == dpif->dp_ifindex
1184 && (vport.cmd == OVS_VPORT_CMD_NEW
1185 || vport.cmd == OVS_VPORT_CMD_DEL
1186 || vport.cmd == OVS_VPORT_CMD_SET)) {
1187 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1188 dpif->dpif.full_name, vport.name, vport.cmd);
1189 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1190 dpif->refresh_channels = true;
1192 *devnamep = xstrdup(vport.name);
1193 ofpbuf_uninit(&buf);
1197 } else if (error != EAGAIN) {
1198 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1199 ovs_strerror(error));
1200 nl_sock_drain(dpif->port_notifier);
1204 ofpbuf_uninit(&buf);
1212 dpif_netlink_port_poll_wait(const struct dpif *dpif_)
1214 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1216 if (dpif->port_notifier) {
1217 nl_sock_wait(dpif->port_notifier, POLLIN);
1219 poll_immediate_wake();
1224 dpif_netlink_flow_init_ufid(struct dpif_netlink_flow *request,
1225 const ovs_u128 *ufid, bool terse)
1228 request->ufid = *ufid;
1229 request->ufid_present = true;
1231 request->ufid_present = false;
1233 request->ufid_terse = terse;
1237 dpif_netlink_init_flow_get__(const struct dpif_netlink *dpif,
1238 const struct nlattr *key, size_t key_len,
1239 const ovs_u128 *ufid, bool terse,
1240 struct dpif_netlink_flow *request)
1242 dpif_netlink_flow_init(request);
1243 request->cmd = OVS_FLOW_CMD_GET;
1244 request->dp_ifindex = dpif->dp_ifindex;
1246 request->key_len = key_len;
1247 dpif_netlink_flow_init_ufid(request, ufid, terse);
1251 dpif_netlink_init_flow_get(const struct dpif_netlink *dpif,
1252 const struct dpif_flow_get *get,
1253 struct dpif_netlink_flow *request)
1255 dpif_netlink_init_flow_get__(dpif, get->key, get->key_len, get->ufid,
1260 dpif_netlink_flow_get__(const struct dpif_netlink *dpif,
1261 const struct nlattr *key, size_t key_len,
1262 const ovs_u128 *ufid, bool terse,
1263 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1265 struct dpif_netlink_flow request;
1267 dpif_netlink_init_flow_get__(dpif, key, key_len, ufid, terse, &request);
1268 return dpif_netlink_flow_transact(&request, reply, bufp);
1272 dpif_netlink_flow_get(const struct dpif_netlink *dpif,
1273 const struct dpif_netlink_flow *flow,
1274 struct dpif_netlink_flow *reply, struct ofpbuf **bufp)
1276 return dpif_netlink_flow_get__(dpif, flow->key, flow->key_len,
1277 flow->ufid_present ? &flow->ufid : NULL,
1278 false, reply, bufp);
1282 dpif_netlink_init_flow_put(struct dpif_netlink *dpif,
1283 const struct dpif_flow_put *put,
1284 struct dpif_netlink_flow *request)
1286 static const struct nlattr dummy_action;
1288 dpif_netlink_flow_init(request);
1289 request->cmd = (put->flags & DPIF_FP_CREATE
1290 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1291 request->dp_ifindex = dpif->dp_ifindex;
1292 request->key = put->key;
1293 request->key_len = put->key_len;
1294 request->mask = put->mask;
1295 request->mask_len = put->mask_len;
1296 dpif_netlink_flow_init_ufid(request, put->ufid, false);
1298 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1299 request->actions = (put->actions
1301 : CONST_CAST(struct nlattr *, &dummy_action));
1302 request->actions_len = put->actions_len;
1303 if (put->flags & DPIF_FP_ZERO_STATS) {
1304 request->clear = true;
1306 if (put->flags & DPIF_FP_PROBE) {
1307 request->probe = true;
1309 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1313 dpif_netlink_init_flow_del__(struct dpif_netlink *dpif,
1314 const struct nlattr *key, size_t key_len,
1315 const ovs_u128 *ufid, bool terse,
1316 struct dpif_netlink_flow *request)
1318 dpif_netlink_flow_init(request);
1319 request->cmd = OVS_FLOW_CMD_DEL;
1320 request->dp_ifindex = dpif->dp_ifindex;
1322 request->key_len = key_len;
1323 dpif_netlink_flow_init_ufid(request, ufid, terse);
1327 dpif_netlink_init_flow_del(struct dpif_netlink *dpif,
1328 const struct dpif_flow_del *del,
1329 struct dpif_netlink_flow *request)
1331 return dpif_netlink_init_flow_del__(dpif, del->key, del->key_len,
1332 del->ufid, dpif->ufid_supported,
1336 struct dpif_netlink_flow_dump {
1337 struct dpif_flow_dump up;
1338 struct nl_dump nl_dump;
1342 static struct dpif_netlink_flow_dump *
1343 dpif_netlink_flow_dump_cast(struct dpif_flow_dump *dump)
1345 return CONTAINER_OF(dump, struct dpif_netlink_flow_dump, up);
1348 static struct dpif_flow_dump *
1349 dpif_netlink_flow_dump_create(const struct dpif *dpif_, bool terse)
1351 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1352 struct dpif_netlink_flow_dump *dump;
1353 struct dpif_netlink_flow request;
1356 dump = xmalloc(sizeof *dump);
1357 dpif_flow_dump_init(&dump->up, dpif_);
1359 dpif_netlink_flow_init(&request);
1360 request.cmd = OVS_FLOW_CMD_GET;
1361 request.dp_ifindex = dpif->dp_ifindex;
1362 request.ufid_present = false;
1363 request.ufid_terse = terse;
1365 buf = ofpbuf_new(1024);
1366 dpif_netlink_flow_to_ofpbuf(&request, buf);
1367 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1369 atomic_init(&dump->status, 0);
1370 dump->up.terse = terse;
1376 dpif_netlink_flow_dump_destroy(struct dpif_flow_dump *dump_)
1378 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1379 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1382 /* No other thread has access to 'dump' at this point. */
1383 atomic_read_relaxed(&dump->status, &dump_status);
1385 return dump_status ? dump_status : nl_status;
1388 struct dpif_netlink_flow_dump_thread {
1389 struct dpif_flow_dump_thread up;
1390 struct dpif_netlink_flow_dump *dump;
1391 struct dpif_netlink_flow flow;
1392 struct dpif_flow_stats stats;
1393 struct ofpbuf nl_flows; /* Always used to store flows. */
1394 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1397 static struct dpif_netlink_flow_dump_thread *
1398 dpif_netlink_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1400 return CONTAINER_OF(thread, struct dpif_netlink_flow_dump_thread, up);
1403 static struct dpif_flow_dump_thread *
1404 dpif_netlink_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1406 struct dpif_netlink_flow_dump *dump = dpif_netlink_flow_dump_cast(dump_);
1407 struct dpif_netlink_flow_dump_thread *thread;
1409 thread = xmalloc(sizeof *thread);
1410 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1411 thread->dump = dump;
1412 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1413 thread->nl_actions = NULL;
1419 dpif_netlink_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1421 struct dpif_netlink_flow_dump_thread *thread
1422 = dpif_netlink_flow_dump_thread_cast(thread_);
1424 ofpbuf_uninit(&thread->nl_flows);
1425 ofpbuf_delete(thread->nl_actions);
1430 dpif_netlink_flow_to_dpif_flow(struct dpif *dpif, struct dpif_flow *dpif_flow,
1431 const struct dpif_netlink_flow *datapath_flow)
1433 dpif_flow->key = datapath_flow->key;
1434 dpif_flow->key_len = datapath_flow->key_len;
1435 dpif_flow->mask = datapath_flow->mask;
1436 dpif_flow->mask_len = datapath_flow->mask_len;
1437 dpif_flow->actions = datapath_flow->actions;
1438 dpif_flow->actions_len = datapath_flow->actions_len;
1439 dpif_flow->ufid_present = datapath_flow->ufid_present;
1440 if (datapath_flow->ufid_present) {
1441 dpif_flow->ufid = datapath_flow->ufid;
1443 ovs_assert(datapath_flow->key && datapath_flow->key_len);
1444 dpif_flow_hash(dpif, datapath_flow->key, datapath_flow->key_len,
1447 dpif_netlink_flow_get_stats(datapath_flow, &dpif_flow->stats);
1451 dpif_netlink_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1452 struct dpif_flow *flows, int max_flows)
1454 struct dpif_netlink_flow_dump_thread *thread
1455 = dpif_netlink_flow_dump_thread_cast(thread_);
1456 struct dpif_netlink_flow_dump *dump = thread->dump;
1457 struct dpif_netlink *dpif = dpif_netlink_cast(thread->up.dpif);
1460 ofpbuf_delete(thread->nl_actions);
1461 thread->nl_actions = NULL;
1465 || (n_flows < max_flows && ofpbuf_size(&thread->nl_flows))) {
1466 struct dpif_netlink_flow datapath_flow;
1467 struct ofpbuf nl_flow;
1470 /* Try to grab another flow. */
1471 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1475 /* Convert the flow to our output format. */
1476 error = dpif_netlink_flow_from_ofpbuf(&datapath_flow, &nl_flow);
1478 atomic_store_relaxed(&dump->status, error);
1482 if (dump->up.terse || datapath_flow.actions) {
1483 /* Common case: we don't want actions, or the flow includes
1485 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1488 /* Rare case: the flow does not include actions. Retrieve this
1489 * individual flow again to get the actions. */
1490 error = dpif_netlink_flow_get(dpif, &datapath_flow,
1491 &datapath_flow, &thread->nl_actions);
1492 if (error == ENOENT) {
1493 VLOG_DBG("dumped flow disappeared on get");
1496 VLOG_WARN("error fetching dumped flow: %s",
1497 ovs_strerror(error));
1498 atomic_store_relaxed(&dump->status, error);
1502 /* Save this flow. Then exit, because we only have one buffer to
1503 * handle this case. */
1504 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, &flows[n_flows++],
1513 dpif_netlink_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1516 struct ovs_header *k_exec;
1519 ofpbuf_prealloc_tailroom(buf, (64
1520 + ofpbuf_size(d_exec->packet)
1521 + ODP_KEY_METADATA_SIZE
1522 + d_exec->actions_len));
1524 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1525 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1527 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1528 k_exec->dp_ifindex = dp_ifindex;
1530 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1531 ofpbuf_data(d_exec->packet),
1532 ofpbuf_size(d_exec->packet));
1534 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1535 odp_key_from_pkt_metadata(buf, &d_exec->md);
1536 nl_msg_end_nested(buf, key_ofs);
1538 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1539 d_exec->actions, d_exec->actions_len);
1540 if (d_exec->probe) {
1541 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
1548 dpif_netlink_operate__(struct dpif_netlink *dpif,
1549 struct dpif_op **ops, size_t n_ops)
1552 struct nl_transaction txn;
1554 struct ofpbuf request;
1555 uint64_t request_stub[1024 / 8];
1557 struct ofpbuf reply;
1558 uint64_t reply_stub[1024 / 8];
1561 struct nl_transaction *txnsp[MAX_OPS];
1564 ovs_assert(n_ops <= MAX_OPS);
1565 for (i = 0; i < n_ops; i++) {
1566 struct op_auxdata *aux = &auxes[i];
1567 struct dpif_op *op = ops[i];
1568 struct dpif_flow_put *put;
1569 struct dpif_flow_del *del;
1570 struct dpif_execute *execute;
1571 struct dpif_flow_get *get;
1572 struct dpif_netlink_flow flow;
1574 ofpbuf_use_stub(&aux->request,
1575 aux->request_stub, sizeof aux->request_stub);
1576 aux->txn.request = &aux->request;
1578 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1579 aux->txn.reply = NULL;
1582 case DPIF_OP_FLOW_PUT:
1583 put = &op->u.flow_put;
1584 dpif_netlink_init_flow_put(dpif, put, &flow);
1586 flow.nlmsg_flags |= NLM_F_ECHO;
1587 aux->txn.reply = &aux->reply;
1589 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1592 case DPIF_OP_FLOW_DEL:
1593 del = &op->u.flow_del;
1594 dpif_netlink_init_flow_del(dpif, del, &flow);
1596 flow.nlmsg_flags |= NLM_F_ECHO;
1597 aux->txn.reply = &aux->reply;
1599 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1602 case DPIF_OP_EXECUTE:
1603 execute = &op->u.execute;
1604 dpif_netlink_encode_execute(dpif->dp_ifindex, execute,
1608 case DPIF_OP_FLOW_GET:
1609 get = &op->u.flow_get;
1610 dpif_netlink_init_flow_get(dpif, get, &flow);
1611 aux->txn.reply = get->buffer;
1612 dpif_netlink_flow_to_ofpbuf(&flow, &aux->request);
1620 for (i = 0; i < n_ops; i++) {
1621 txnsp[i] = &auxes[i].txn;
1623 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1625 for (i = 0; i < n_ops; i++) {
1626 struct op_auxdata *aux = &auxes[i];
1627 struct nl_transaction *txn = &auxes[i].txn;
1628 struct dpif_op *op = ops[i];
1629 struct dpif_flow_put *put;
1630 struct dpif_flow_del *del;
1631 struct dpif_flow_get *get;
1633 op->error = txn->error;
1636 case DPIF_OP_FLOW_PUT:
1637 put = &op->u.flow_put;
1640 struct dpif_netlink_flow reply;
1642 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1645 dpif_netlink_flow_get_stats(&reply, put->stats);
1651 case DPIF_OP_FLOW_DEL:
1652 del = &op->u.flow_del;
1655 struct dpif_netlink_flow reply;
1657 op->error = dpif_netlink_flow_from_ofpbuf(&reply,
1660 dpif_netlink_flow_get_stats(&reply, del->stats);
1666 case DPIF_OP_EXECUTE:
1669 case DPIF_OP_FLOW_GET:
1670 get = &op->u.flow_get;
1672 struct dpif_netlink_flow reply;
1674 op->error = dpif_netlink_flow_from_ofpbuf(&reply, txn->reply);
1676 dpif_netlink_flow_to_dpif_flow(&dpif->dpif, get->flow,
1686 ofpbuf_uninit(&aux->request);
1687 ofpbuf_uninit(&aux->reply);
1692 dpif_netlink_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1694 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1697 size_t chunk = MIN(n_ops, MAX_OPS);
1698 dpif_netlink_operate__(dpif, ops, chunk);
1706 dpif_netlink_handler_uninit(struct dpif_handler *handler)
1708 vport_delete_sock_pool(handler);
1712 dpif_netlink_handler_init(struct dpif_handler *handler)
1714 return vport_create_sock_pool(handler);
1719 dpif_netlink_handler_init(struct dpif_handler *handler)
1721 handler->epoll_fd = epoll_create(10);
1722 return handler->epoll_fd < 0 ? errno : 0;
1726 dpif_netlink_handler_uninit(struct dpif_handler *handler)
1728 close(handler->epoll_fd);
1732 /* Checks support for unique flow identifiers. */
1734 dpif_netlink_check_ufid__(struct dpif *dpif_)
1737 struct odputil_keybuf keybuf;
1741 memset(&flow, 0, sizeof flow);
1742 flow.dl_type = htons(0x1234);
1744 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1745 odp_flow_key_from_flow(&key, &flow, NULL, 0, true);
1746 dpif_flow_hash(dpif_, ofpbuf_data(&key), ofpbuf_size(&key), &ufid);
1748 return dpif_probe_feature(dpif_, "UFID", &key, &ufid);
1752 dpif_netlink_check_ufid(struct dpif *dpif_)
1754 const struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1756 if (dpif->ufid_supported) {
1757 VLOG_INFO("%s: Datapath supports userspace flow ids",
1760 VLOG_INFO("%s: Datapath does not support userspace flow ids",
1763 return dpif->ufid_supported;
1766 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1767 * currently in 'dpif' in the kernel, by adding a new set of channels for
1768 * any kernel vport that lacks one and deleting any channels that have no
1769 * backing kernel vports. */
1771 dpif_netlink_refresh_channels(struct dpif_netlink *dpif, uint32_t n_handlers)
1772 OVS_REQ_WRLOCK(dpif->upcall_lock)
1774 unsigned long int *keep_channels;
1775 struct dpif_netlink_vport vport;
1776 size_t keep_channels_nbits;
1777 struct nl_dump dump;
1778 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1783 ovs_assert(!WINDOWS || n_handlers <= 1);
1784 ovs_assert(!WINDOWS || dpif->n_handlers <= 1);
1786 if (dpif->n_handlers != n_handlers) {
1787 destroy_all_channels(dpif);
1788 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1789 for (i = 0; i < n_handlers; i++) {
1791 struct dpif_handler *handler = &dpif->handlers[i];
1793 error = dpif_netlink_handler_init(handler);
1796 struct dpif_handler *tmp = &dpif->handlers[i];
1799 for (j = 0; j < i; j++) {
1800 dpif_netlink_handler_uninit(tmp);
1802 free(dpif->handlers);
1803 dpif->handlers = NULL;
1808 dpif->n_handlers = n_handlers;
1811 for (i = 0; i < n_handlers; i++) {
1812 struct dpif_handler *handler = &dpif->handlers[i];
1814 handler->event_offset = handler->n_events = 0;
1817 keep_channels_nbits = dpif->uc_array_size;
1818 keep_channels = bitmap_allocate(keep_channels_nbits);
1820 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1821 dpif_netlink_port_dump_start__(dpif, &dump);
1822 while (!dpif_netlink_port_dump_next__(dpif, &dump, &vport, &buf)) {
1823 uint32_t port_no = odp_to_u32(vport.port_no);
1824 uint32_t *upcall_pids = NULL;
1827 if (port_no >= dpif->uc_array_size
1828 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1829 struct nl_sock **socksp = vport_create_socksp(dpif, &error);
1835 error = vport_add_channels(dpif, vport.port_no, socksp);
1837 VLOG_INFO("%s: could not add channels for port %s",
1838 dpif_name(&dpif->dpif), vport.name);
1839 vport_del_socksp(dpif, socksp);
1843 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1847 /* Configure the vport to deliver misses to 'sock'. */
1848 if (vport.upcall_pids[0] == 0
1849 || vport.n_upcall_pids != dpif->n_handlers
1850 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1852 struct dpif_netlink_vport vport_request;
1854 dpif_netlink_vport_init(&vport_request);
1855 vport_request.cmd = OVS_VPORT_CMD_SET;
1856 vport_request.dp_ifindex = dpif->dp_ifindex;
1857 vport_request.port_no = vport.port_no;
1858 vport_request.n_upcall_pids = dpif->n_handlers;
1859 vport_request.upcall_pids = upcall_pids;
1860 error = dpif_netlink_vport_transact(&vport_request, NULL, NULL);
1862 VLOG_WARN_RL(&error_rl,
1863 "%s: failed to set upcall pid on port: %s",
1864 dpif_name(&dpif->dpif), ovs_strerror(error));
1866 if (error != ENODEV && error != ENOENT) {
1869 /* The vport isn't really there, even though the dump says
1870 * it is. Probably we just hit a race after a port
1877 if (port_no < keep_channels_nbits) {
1878 bitmap_set1(keep_channels, port_no);
1885 vport_del_channels(dpif, vport.port_no);
1887 nl_dump_done(&dump);
1888 ofpbuf_uninit(&buf);
1890 /* Discard any saved channels that we didn't reuse. */
1891 for (i = 0; i < keep_channels_nbits; i++) {
1892 if (!bitmap_is_set(keep_channels, i)) {
1893 vport_del_channels(dpif, u32_to_odp(i));
1896 free(keep_channels);
1902 dpif_netlink_recv_set__(struct dpif_netlink *dpif, bool enable)
1903 OVS_REQ_WRLOCK(dpif->upcall_lock)
1905 if ((dpif->handlers != NULL) == enable) {
1907 } else if (!enable) {
1908 destroy_all_channels(dpif);
1911 return dpif_netlink_refresh_channels(dpif, 1);
1916 dpif_netlink_recv_set(struct dpif *dpif_, bool enable)
1918 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1921 fat_rwlock_wrlock(&dpif->upcall_lock);
1922 error = dpif_netlink_recv_set__(dpif, enable);
1923 fat_rwlock_unlock(&dpif->upcall_lock);
1929 dpif_netlink_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1931 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
1935 /* Multiple upcall handlers will be supported once kernel datapath supports
1937 if (n_handlers > 1) {
1942 fat_rwlock_wrlock(&dpif->upcall_lock);
1943 if (dpif->handlers) {
1944 error = dpif_netlink_refresh_channels(dpif, n_handlers);
1946 fat_rwlock_unlock(&dpif->upcall_lock);
1952 dpif_netlink_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1953 uint32_t queue_id, uint32_t *priority)
1955 if (queue_id < 0xf000) {
1956 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1964 parse_odp_packet(const struct dpif_netlink *dpif, struct ofpbuf *buf,
1965 struct dpif_upcall *upcall, int *dp_ifindex)
1967 static const struct nl_policy ovs_packet_policy[] = {
1968 /* Always present. */
1969 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1970 .min_len = ETH_HEADER_LEN },
1971 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1973 /* OVS_PACKET_CMD_ACTION only. */
1974 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1975 [OVS_PACKET_ATTR_EGRESS_TUN_KEY] = { .type = NL_A_NESTED, .optional = true },
1978 struct ovs_header *ovs_header;
1979 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1980 struct nlmsghdr *nlmsg;
1981 struct genlmsghdr *genl;
1985 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1987 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1988 genl = ofpbuf_try_pull(&b, sizeof *genl);
1989 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1990 if (!nlmsg || !genl || !ovs_header
1991 || nlmsg->nlmsg_type != ovs_packet_family
1992 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1993 ARRAY_SIZE(ovs_packet_policy))) {
1997 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1998 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
2004 /* (Re)set ALL fields of '*upcall' on successful return. */
2005 upcall->type = type;
2006 upcall->key = CONST_CAST(struct nlattr *,
2007 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
2008 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
2009 dpif_flow_hash(&dpif->dpif, upcall->key, upcall->key_len, &upcall->ufid);
2010 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
2011 upcall->out_tun_key = a[OVS_PACKET_ATTR_EGRESS_TUN_KEY];
2013 /* Allow overwriting the netlink attribute header without reallocating. */
2014 ofpbuf_use_stub(&upcall->packet,
2015 CONST_CAST(struct nlattr *,
2016 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
2017 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
2018 sizeof(struct nlattr));
2019 ofpbuf_set_data(&upcall->packet,
2020 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
2021 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
2023 *dp_ifindex = ovs_header->dp_ifindex;
2029 #define PACKET_RECV_BATCH_SIZE 50
2031 dpif_netlink_recv_windows(struct dpif_netlink *dpif, uint32_t handler_id,
2032 struct dpif_upcall *upcall, struct ofpbuf *buf)
2033 OVS_REQ_RDLOCK(dpif->upcall_lock)
2035 struct dpif_handler *handler;
2037 struct dpif_windows_vport_sock *sock_pool;
2040 if (!dpif->handlers) {
2044 /* Only one handler is supported currently. */
2045 if (handler_id >= 1) {
2049 if (handler_id >= dpif->n_handlers) {
2053 handler = &dpif->handlers[handler_id];
2054 sock_pool = handler->vport_sock_pool;
2056 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2061 if (++read_tries > PACKET_RECV_BATCH_SIZE) {
2065 error = nl_sock_recv(sock_pool[i].nl_sock, buf, false);
2066 if (error == ENOBUFS) {
2067 /* ENOBUFS typically means that we've received so many
2068 * packets that the buffer overflowed. Try again
2069 * immediately because there's almost certainly a packet
2070 * waiting for us. */
2071 /* XXX: report_loss(dpif, ch, idx, handler_id); */
2075 /* XXX: ch->last_poll = time_msec(); */
2077 if (error == EAGAIN) {
2083 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2084 if (!error && dp_ifindex == dpif->dp_ifindex) {
2096 dpif_netlink_recv__(struct dpif_netlink *dpif, uint32_t handler_id,
2097 struct dpif_upcall *upcall, struct ofpbuf *buf)
2098 OVS_REQ_RDLOCK(dpif->upcall_lock)
2100 struct dpif_handler *handler;
2103 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
2107 handler = &dpif->handlers[handler_id];
2108 if (handler->event_offset >= handler->n_events) {
2111 handler->event_offset = handler->n_events = 0;
2114 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
2115 dpif->uc_array_size, 0);
2116 } while (retval < 0 && errno == EINTR);
2119 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2120 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
2121 } else if (retval > 0) {
2122 handler->n_events = retval;
2126 while (handler->event_offset < handler->n_events) {
2127 int idx = handler->epoll_events[handler->event_offset].data.u32;
2128 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
2130 handler->event_offset++;
2136 if (++read_tries > 50) {
2140 error = nl_sock_recv(ch->sock, buf, false);
2141 if (error == ENOBUFS) {
2142 /* ENOBUFS typically means that we've received so many
2143 * packets that the buffer overflowed. Try again
2144 * immediately because there's almost certainly a packet
2145 * waiting for us. */
2146 report_loss(dpif, ch, idx, handler_id);
2150 ch->last_poll = time_msec();
2152 if (error == EAGAIN) {
2158 error = parse_odp_packet(dpif, buf, upcall, &dp_ifindex);
2159 if (!error && dp_ifindex == dpif->dp_ifindex) {
2172 dpif_netlink_recv(struct dpif *dpif_, uint32_t handler_id,
2173 struct dpif_upcall *upcall, struct ofpbuf *buf)
2175 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2178 fat_rwlock_rdlock(&dpif->upcall_lock);
2180 error = dpif_netlink_recv_windows(dpif, handler_id, upcall, buf);
2182 error = dpif_netlink_recv__(dpif, handler_id, upcall, buf);
2184 fat_rwlock_unlock(&dpif->upcall_lock);
2190 dpif_netlink_recv_wait__(struct dpif_netlink *dpif, uint32_t handler_id)
2191 OVS_REQ_RDLOCK(dpif->upcall_lock)
2195 struct dpif_windows_vport_sock *sock_pool =
2196 dpif->handlers[handler_id].vport_sock_pool;
2198 /* Only one handler is supported currently. */
2199 if (handler_id >= 1) {
2203 for (i = 0; i < VPORT_SOCK_POOL_SIZE; i++) {
2204 nl_sock_wait(sock_pool[i].nl_sock, POLLIN);
2207 if (dpif->handlers && handler_id < dpif->n_handlers) {
2208 struct dpif_handler *handler = &dpif->handlers[handler_id];
2210 poll_fd_wait(handler->epoll_fd, POLLIN);
2216 dpif_netlink_recv_wait(struct dpif *dpif_, uint32_t handler_id)
2218 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2220 fat_rwlock_rdlock(&dpif->upcall_lock);
2221 dpif_netlink_recv_wait__(dpif, handler_id);
2222 fat_rwlock_unlock(&dpif->upcall_lock);
2226 dpif_netlink_recv_purge__(struct dpif_netlink *dpif)
2227 OVS_REQ_WRLOCK(dpif->upcall_lock)
2229 if (dpif->handlers) {
2232 for (i = 0; i < dpif->uc_array_size; i++ ) {
2233 if (!dpif->handlers[0].channels[i].sock) {
2237 for (j = 0; j < dpif->n_handlers; j++) {
2238 nl_sock_drain(dpif->handlers[j].channels[i].sock);
2245 dpif_netlink_recv_purge(struct dpif *dpif_)
2247 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2249 fat_rwlock_wrlock(&dpif->upcall_lock);
2250 dpif_netlink_recv_purge__(dpif);
2251 fat_rwlock_unlock(&dpif->upcall_lock);
2255 dpif_netlink_get_datapath_version(void)
2257 char *version_str = NULL;
2261 #define MAX_VERSION_STR_SIZE 80
2262 #define LINUX_DATAPATH_VERSION_FILE "/sys/module/openvswitch/version"
2265 f = fopen(LINUX_DATAPATH_VERSION_FILE, "r");
2268 char version[MAX_VERSION_STR_SIZE];
2270 if (fgets(version, MAX_VERSION_STR_SIZE, f)) {
2271 newline = strchr(version, '\n');
2275 version_str = xstrdup(version);
2284 const struct dpif_class dpif_netlink_class = {
2286 dpif_netlink_enumerate,
2290 dpif_netlink_destroy,
2293 dpif_netlink_get_stats,
2294 dpif_netlink_port_add,
2295 dpif_netlink_port_del,
2296 dpif_netlink_port_query_by_number,
2297 dpif_netlink_port_query_by_name,
2298 dpif_netlink_port_get_pid,
2299 dpif_netlink_port_dump_start,
2300 dpif_netlink_port_dump_next,
2301 dpif_netlink_port_dump_done,
2302 dpif_netlink_port_poll,
2303 dpif_netlink_port_poll_wait,
2304 dpif_netlink_flow_flush,
2305 dpif_netlink_flow_dump_create,
2306 dpif_netlink_flow_dump_destroy,
2307 dpif_netlink_flow_dump_thread_create,
2308 dpif_netlink_flow_dump_thread_destroy,
2309 dpif_netlink_flow_dump_next,
2310 dpif_netlink_operate,
2311 dpif_netlink_recv_set,
2312 dpif_netlink_handlers_set,
2313 NULL, /* poll_thread_set */
2314 dpif_netlink_queue_to_priority,
2316 dpif_netlink_recv_wait,
2317 dpif_netlink_recv_purge,
2318 NULL, /* register_upcall_cb */
2319 NULL, /* enable_upcall */
2320 NULL, /* disable_upcall */
2321 dpif_netlink_get_datapath_version, /* get_datapath_version */
2322 dpif_netlink_check_ufid,
2326 dpif_netlink_init(void)
2328 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
2331 if (ovsthread_once_start(&once)) {
2332 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
2333 &ovs_datapath_family);
2335 VLOG_ERR("Generic Netlink family '%s' does not exist. "
2336 "The Open vSwitch kernel module is probably not loaded.",
2337 OVS_DATAPATH_FAMILY);
2340 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
2343 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
2346 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
2347 &ovs_packet_family);
2350 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
2351 &ovs_vport_mcgroup);
2354 ovsthread_once_done(&once);
2361 dpif_netlink_is_internal_device(const char *name)
2363 struct dpif_netlink_vport reply;
2367 error = dpif_netlink_vport_get(name, &reply, &buf);
2370 } else if (error != ENODEV && error != ENOENT) {
2371 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
2372 name, ovs_strerror(error));
2375 return reply.type == OVS_VPORT_TYPE_INTERNAL;
2378 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2379 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
2380 * positive errno value.
2382 * 'vport' will contain pointers into 'buf', so the caller should not free
2383 * 'buf' while 'vport' is still in use. */
2385 dpif_netlink_vport_from_ofpbuf(struct dpif_netlink_vport *vport,
2386 const struct ofpbuf *buf)
2388 static const struct nl_policy ovs_vport_policy[] = {
2389 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
2390 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
2391 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2392 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
2393 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
2395 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
2398 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
2399 struct ovs_header *ovs_header;
2400 struct nlmsghdr *nlmsg;
2401 struct genlmsghdr *genl;
2404 dpif_netlink_vport_init(vport);
2406 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2407 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2408 genl = ofpbuf_try_pull(&b, sizeof *genl);
2409 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2410 if (!nlmsg || !genl || !ovs_header
2411 || nlmsg->nlmsg_type != ovs_vport_family
2412 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2413 ARRAY_SIZE(ovs_vport_policy))) {
2417 vport->cmd = genl->cmd;
2418 vport->dp_ifindex = ovs_header->dp_ifindex;
2419 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2420 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2421 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2422 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2423 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2424 / (sizeof *vport->upcall_pids);
2425 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2428 if (a[OVS_VPORT_ATTR_STATS]) {
2429 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2431 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2432 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2433 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2438 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2439 * followed by Netlink attributes corresponding to 'vport'. */
2441 dpif_netlink_vport_to_ofpbuf(const struct dpif_netlink_vport *vport,
2444 struct ovs_header *ovs_header;
2446 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2447 vport->cmd, OVS_VPORT_VERSION);
2449 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2450 ovs_header->dp_ifindex = vport->dp_ifindex;
2452 if (vport->port_no != ODPP_NONE) {
2453 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2456 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2457 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2461 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2464 if (vport->upcall_pids) {
2465 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2467 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2471 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2472 vport->stats, sizeof *vport->stats);
2475 if (vport->options) {
2476 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2477 vport->options, vport->options_len);
2481 /* Clears 'vport' to "empty" values. */
2483 dpif_netlink_vport_init(struct dpif_netlink_vport *vport)
2485 memset(vport, 0, sizeof *vport);
2486 vport->port_no = ODPP_NONE;
2489 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2490 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2491 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2492 * result of the command is expected to be an ovs_vport also, which is decoded
2493 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2494 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2496 dpif_netlink_vport_transact(const struct dpif_netlink_vport *request,
2497 struct dpif_netlink_vport *reply,
2498 struct ofpbuf **bufp)
2500 struct ofpbuf *request_buf;
2503 ovs_assert((reply != NULL) == (bufp != NULL));
2505 error = dpif_netlink_init();
2509 dpif_netlink_vport_init(reply);
2514 request_buf = ofpbuf_new(1024);
2515 dpif_netlink_vport_to_ofpbuf(request, request_buf);
2516 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2517 ofpbuf_delete(request_buf);
2521 error = dpif_netlink_vport_from_ofpbuf(reply, *bufp);
2524 dpif_netlink_vport_init(reply);
2525 ofpbuf_delete(*bufp);
2532 /* Obtains information about the kernel vport named 'name' and stores it into
2533 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2534 * longer needed ('reply' will contain pointers into '*bufp'). */
2536 dpif_netlink_vport_get(const char *name, struct dpif_netlink_vport *reply,
2537 struct ofpbuf **bufp)
2539 struct dpif_netlink_vport request;
2541 dpif_netlink_vport_init(&request);
2542 request.cmd = OVS_VPORT_CMD_GET;
2543 request.name = name;
2545 return dpif_netlink_vport_transact(&request, reply, bufp);
2548 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2549 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2550 * positive errno value.
2552 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2553 * while 'dp' is still in use. */
2555 dpif_netlink_dp_from_ofpbuf(struct dpif_netlink_dp *dp, const struct ofpbuf *buf)
2557 static const struct nl_policy ovs_datapath_policy[] = {
2558 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2559 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2561 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2562 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2566 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2567 struct ovs_header *ovs_header;
2568 struct nlmsghdr *nlmsg;
2569 struct genlmsghdr *genl;
2572 dpif_netlink_dp_init(dp);
2574 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2575 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2576 genl = ofpbuf_try_pull(&b, sizeof *genl);
2577 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2578 if (!nlmsg || !genl || !ovs_header
2579 || nlmsg->nlmsg_type != ovs_datapath_family
2580 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2581 ARRAY_SIZE(ovs_datapath_policy))) {
2585 dp->cmd = genl->cmd;
2586 dp->dp_ifindex = ovs_header->dp_ifindex;
2587 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2588 if (a[OVS_DP_ATTR_STATS]) {
2589 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
2592 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2593 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
2599 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2601 dpif_netlink_dp_to_ofpbuf(const struct dpif_netlink_dp *dp, struct ofpbuf *buf)
2603 struct ovs_header *ovs_header;
2605 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2606 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2607 OVS_DATAPATH_VERSION);
2609 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2610 ovs_header->dp_ifindex = dp->dp_ifindex;
2613 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2616 if (dp->upcall_pid) {
2617 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2620 if (dp->user_features) {
2621 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2624 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2627 /* Clears 'dp' to "empty" values. */
2629 dpif_netlink_dp_init(struct dpif_netlink_dp *dp)
2631 memset(dp, 0, sizeof *dp);
2635 dpif_netlink_dp_dump_start(struct nl_dump *dump)
2637 struct dpif_netlink_dp request;
2640 dpif_netlink_dp_init(&request);
2641 request.cmd = OVS_DP_CMD_GET;
2643 buf = ofpbuf_new(1024);
2644 dpif_netlink_dp_to_ofpbuf(&request, buf);
2645 nl_dump_start(dump, NETLINK_GENERIC, buf);
2649 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2650 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2651 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2652 * result of the command is expected to be of the same form, which is decoded
2653 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2654 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2656 dpif_netlink_dp_transact(const struct dpif_netlink_dp *request,
2657 struct dpif_netlink_dp *reply, struct ofpbuf **bufp)
2659 struct ofpbuf *request_buf;
2662 ovs_assert((reply != NULL) == (bufp != NULL));
2664 request_buf = ofpbuf_new(1024);
2665 dpif_netlink_dp_to_ofpbuf(request, request_buf);
2666 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2667 ofpbuf_delete(request_buf);
2670 dpif_netlink_dp_init(reply);
2672 error = dpif_netlink_dp_from_ofpbuf(reply, *bufp);
2675 ofpbuf_delete(*bufp);
2682 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2683 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2684 * will contain pointers into '*bufp'). */
2686 dpif_netlink_dp_get(const struct dpif *dpif_, struct dpif_netlink_dp *reply,
2687 struct ofpbuf **bufp)
2689 struct dpif_netlink *dpif = dpif_netlink_cast(dpif_);
2690 struct dpif_netlink_dp request;
2692 dpif_netlink_dp_init(&request);
2693 request.cmd = OVS_DP_CMD_GET;
2694 request.dp_ifindex = dpif->dp_ifindex;
2696 return dpif_netlink_dp_transact(&request, reply, bufp);
2699 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2700 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2701 * positive errno value.
2703 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2704 * while 'flow' is still in use. */
2706 dpif_netlink_flow_from_ofpbuf(struct dpif_netlink_flow *flow,
2707 const struct ofpbuf *buf)
2709 static const struct nl_policy ovs_flow_policy[__OVS_FLOW_ATTR_MAX] = {
2710 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED, .optional = true },
2711 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2712 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2713 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2715 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2716 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2717 [OVS_FLOW_ATTR_UFID] = { .type = NL_A_UNSPEC, .optional = true,
2718 .min_len = sizeof(ovs_u128) },
2719 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2720 /* The kernel never uses OVS_FLOW_ATTR_PROBE. */
2721 /* The kernel never uses OVS_FLOW_ATTR_UFID_FLAGS. */
2724 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2725 struct ovs_header *ovs_header;
2726 struct nlmsghdr *nlmsg;
2727 struct genlmsghdr *genl;
2730 dpif_netlink_flow_init(flow);
2732 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2733 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2734 genl = ofpbuf_try_pull(&b, sizeof *genl);
2735 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2736 if (!nlmsg || !genl || !ovs_header
2737 || nlmsg->nlmsg_type != ovs_flow_family
2738 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2739 ARRAY_SIZE(ovs_flow_policy))) {
2742 if (!a[OVS_FLOW_ATTR_KEY] && !a[OVS_FLOW_ATTR_UFID]) {
2746 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2747 flow->dp_ifindex = ovs_header->dp_ifindex;
2748 if (a[OVS_FLOW_ATTR_KEY]) {
2749 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2750 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2753 if (a[OVS_FLOW_ATTR_UFID]) {
2754 const ovs_u128 *ufid;
2756 ufid = nl_attr_get_unspec(a[OVS_FLOW_ATTR_UFID],
2757 nl_attr_get_size(a[OVS_FLOW_ATTR_UFID]));
2759 flow->ufid_present = true;
2761 if (a[OVS_FLOW_ATTR_MASK]) {
2762 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2763 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2765 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2766 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2767 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2769 if (a[OVS_FLOW_ATTR_STATS]) {
2770 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2772 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2773 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2775 if (a[OVS_FLOW_ATTR_USED]) {
2776 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2781 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2782 * followed by Netlink attributes corresponding to 'flow'. */
2784 dpif_netlink_flow_to_ofpbuf(const struct dpif_netlink_flow *flow,
2787 struct ovs_header *ovs_header;
2789 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2790 NLM_F_REQUEST | flow->nlmsg_flags,
2791 flow->cmd, OVS_FLOW_VERSION);
2793 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2794 ovs_header->dp_ifindex = flow->dp_ifindex;
2796 if (flow->ufid_present) {
2797 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_UFID, &flow->ufid,
2800 if (flow->ufid_terse) {
2801 nl_msg_put_u32(buf, OVS_FLOW_ATTR_UFID_FLAGS,
2802 OVS_UFID_F_OMIT_KEY | OVS_UFID_F_OMIT_MASK
2803 | OVS_UFID_F_OMIT_ACTIONS);
2805 if (!flow->ufid_terse || !flow->ufid_present) {
2806 if (flow->key_len) {
2807 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY,
2808 flow->key, flow->key_len);
2811 if (flow->mask_len) {
2812 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK,
2813 flow->mask, flow->mask_len);
2815 if (flow->actions || flow->actions_len) {
2816 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2817 flow->actions, flow->actions_len);
2821 /* We never need to send these to the kernel. */
2822 ovs_assert(!flow->stats);
2823 ovs_assert(!flow->tcp_flags);
2824 ovs_assert(!flow->used);
2827 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2830 nl_msg_put_flag(buf, OVS_FLOW_ATTR_PROBE);
2834 /* Clears 'flow' to "empty" values. */
2836 dpif_netlink_flow_init(struct dpif_netlink_flow *flow)
2838 memset(flow, 0, sizeof *flow);
2841 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2842 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2843 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2844 * result of the command is expected to be a flow also, which is decoded and
2845 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2846 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2848 dpif_netlink_flow_transact(struct dpif_netlink_flow *request,
2849 struct dpif_netlink_flow *reply,
2850 struct ofpbuf **bufp)
2852 struct ofpbuf *request_buf;
2855 ovs_assert((reply != NULL) == (bufp != NULL));
2858 request->nlmsg_flags |= NLM_F_ECHO;
2861 request_buf = ofpbuf_new(1024);
2862 dpif_netlink_flow_to_ofpbuf(request, request_buf);
2863 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2864 ofpbuf_delete(request_buf);
2868 error = dpif_netlink_flow_from_ofpbuf(reply, *bufp);
2871 dpif_netlink_flow_init(reply);
2872 ofpbuf_delete(*bufp);
2880 dpif_netlink_flow_get_stats(const struct dpif_netlink_flow *flow,
2881 struct dpif_flow_stats *stats)
2884 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
2885 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
2887 stats->n_packets = 0;
2890 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2891 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2894 /* Logs information about a packet that was recently lost in 'ch' (in
2897 report_loss(struct dpif_netlink *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2898 uint32_t handler_id)
2900 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2903 if (VLOG_DROP_WARN(&rl)) {
2908 if (ch->last_poll != LLONG_MIN) {
2909 ds_put_format(&s, " (last polled %lld ms ago)",
2910 time_msec() - ch->last_poll);
2913 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2914 dpif_name(&dpif->dpif), ch_idx, handler_id);