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-linux.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"
58 VLOG_DEFINE_THIS_MODULE(dpif_linux);
59 enum { MAX_PORTS = USHRT_MAX };
61 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
62 * missing if we have old headers. */
63 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
65 struct dpif_linux_dp {
66 /* Generic Netlink header. */
69 /* struct ovs_header. */
73 const char *name; /* OVS_DP_ATTR_NAME. */
74 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
75 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
76 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
77 struct ovs_dp_megaflow_stats megaflow_stats;
78 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
81 static void dpif_linux_dp_init(struct dpif_linux_dp *);
82 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
83 const struct ofpbuf *);
84 static void dpif_linux_dp_dump_start(struct nl_dump *);
85 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
86 struct dpif_linux_dp *reply,
87 struct ofpbuf **bufp);
88 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
89 struct ofpbuf **bufp);
91 struct dpif_linux_flow {
92 /* Generic Netlink header. */
95 /* struct ovs_header. */
96 unsigned int nlmsg_flags;
101 * The 'stats' member points to 64-bit data that might only be aligned on
102 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
105 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
106 * the Netlink version of the command, even if actions_len is zero. */
107 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
109 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
111 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
113 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
114 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
115 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
116 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
119 static void dpif_linux_flow_init(struct dpif_linux_flow *);
120 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
121 const struct ofpbuf *);
122 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
124 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
125 struct dpif_linux_flow *reply,
126 struct ofpbuf **bufp);
127 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
128 struct dpif_flow_stats *);
130 /* One of the dpif channels between the kernel and userspace. */
131 struct dpif_channel {
132 struct nl_sock *sock; /* Netlink socket. */
133 long long int last_poll; /* Last time this channel was polled. */
136 struct dpif_handler {
137 struct dpif_channel *channels;/* Array of channels for each handler. */
138 struct epoll_event *epoll_events;
139 int epoll_fd; /* epoll fd that includes channel socks. */
140 int n_events; /* Num events returned by epoll_wait(). */
141 int event_offset; /* Offset into 'epoll_events'. */
144 /* Datapath interface for the openvswitch Linux kernel module. */
149 /* Upcall messages. */
150 struct fat_rwlock upcall_lock;
151 struct dpif_handler *handlers;
152 uint32_t n_handlers; /* Num of upcall handlers. */
153 int uc_array_size; /* Size of 'handler->channels' and */
154 /* 'handler->epoll_events'. */
156 /* Change notification. */
157 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
158 bool refresh_channels;
161 static void report_loss(struct dpif_linux *, struct dpif_channel *,
162 uint32_t ch_idx, uint32_t handler_id);
164 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
166 /* Generic Netlink family numbers for OVS.
168 * Initialized by dpif_linux_init(). */
169 static int ovs_datapath_family;
170 static int ovs_vport_family;
171 static int ovs_flow_family;
172 static int ovs_packet_family;
174 /* Generic Netlink multicast groups for OVS.
176 * Initialized by dpif_linux_init(). */
177 static unsigned int ovs_vport_mcgroup;
179 static int dpif_linux_init(void);
180 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
181 static uint32_t dpif_linux_port_get_pid(const struct dpif *,
182 odp_port_t port_no, uint32_t hash);
183 static int dpif_linux_refresh_channels(struct dpif_linux *,
184 uint32_t n_handlers);
185 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
187 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
188 const struct ofpbuf *);
190 static struct dpif_linux *
191 dpif_linux_cast(const struct dpif *dpif)
193 dpif_assert_class(dpif, &dpif_linux_class);
194 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
198 dpif_linux_enumerate(struct sset *all_dps)
201 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
202 struct ofpbuf msg, buf;
205 error = dpif_linux_init();
210 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
211 dpif_linux_dp_dump_start(&dump);
212 while (nl_dump_next(&dump, &msg, &buf)) {
213 struct dpif_linux_dp dp;
215 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
216 sset_add(all_dps, dp.name);
220 return nl_dump_done(&dump);
224 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
225 bool create, struct dpif **dpifp)
227 struct dpif_linux_dp dp_request, dp;
232 error = dpif_linux_init();
237 /* Create or look up datapath. */
238 dpif_linux_dp_init(&dp_request);
240 dp_request.cmd = OVS_DP_CMD_NEW;
242 dp_request.upcall_pid = &upcall_pid;
244 /* Use OVS_DP_CMD_SET to report user features */
245 dp_request.cmd = OVS_DP_CMD_SET;
247 dp_request.name = name;
248 dp_request.user_features |= OVS_DP_F_UNALIGNED;
249 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
250 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
255 error = open_dpif(&dp, dpifp);
261 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
263 struct dpif_linux *dpif;
265 dpif = xzalloc(sizeof *dpif);
266 dpif->port_notifier = NULL;
267 fat_rwlock_init(&dpif->upcall_lock);
269 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
270 dp->dp_ifindex, dp->dp_ifindex);
272 dpif->dp_ifindex = dp->dp_ifindex;
273 *dpifp = &dpif->dpif;
278 /* Destroys the netlink sockets pointed by the elements in 'socksp'
279 * and frees the 'socksp'. */
281 vport_del_socksp(struct nl_sock **socksp, uint32_t n_socks)
285 for (i = 0; i < n_socks; i++) {
286 nl_sock_destroy(socksp[i]);
292 /* Creates an array of netlink sockets. Returns an array of the
293 * corresponding pointers. Records the error in 'error'. */
294 static struct nl_sock **
295 vport_create_socksp(uint32_t n_socks, int *error)
297 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
300 for (i = 0; i < n_socks; i++) {
301 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
310 vport_del_socksp(socksp, n_socks);
315 /* Given the array of pointers to netlink sockets 'socksp', returns
316 * the array of corresponding pids. If the 'socksp' is NULL, returns
317 * a single-element array of value 0. */
319 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
324 pids = xzalloc(sizeof *pids);
328 pids = xzalloc(n_socks * sizeof *pids);
329 for (i = 0; i < n_socks; i++) {
330 pids[i] = nl_sock_pid(socksp[i]);
337 /* Given the port number 'port_idx', extracts the pids of netlink sockets
338 * associated to the port and assigns it to 'upcall_pids'. */
340 vport_get_pids(struct dpif_linux *dpif, uint32_t port_idx,
341 uint32_t **upcall_pids)
346 /* Since the nl_sock can only be assigned in either all
347 * or none "dpif->handlers" channels, the following check
349 if (!dpif->handlers[0].channels[port_idx].sock) {
353 pids = xzalloc(dpif->n_handlers * sizeof *pids);
355 for (i = 0; i < dpif->n_handlers; i++) {
356 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
365 vport_add_channels(struct dpif_linux *dpif, odp_port_t port_no,
366 struct nl_sock **socksp)
368 struct epoll_event event;
369 uint32_t port_idx = odp_to_u32(port_no);
373 if (dpif->handlers == NULL) {
377 /* We assume that the datapath densely chooses port numbers, which can
378 * therefore be used as an index into 'channels' and 'epoll_events' of
379 * 'dpif->handler'. */
380 if (port_idx >= dpif->uc_array_size) {
381 uint32_t new_size = port_idx + 1;
383 if (new_size > MAX_PORTS) {
384 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
385 dpif_name(&dpif->dpif), port_no);
389 for (i = 0; i < dpif->n_handlers; i++) {
390 struct dpif_handler *handler = &dpif->handlers[i];
392 handler->channels = xrealloc(handler->channels,
393 new_size * sizeof *handler->channels);
395 for (j = dpif->uc_array_size; j < new_size; j++) {
396 handler->channels[j].sock = NULL;
399 handler->epoll_events = xrealloc(handler->epoll_events,
400 new_size * sizeof *handler->epoll_events);
403 dpif->uc_array_size = new_size;
406 memset(&event, 0, sizeof event);
407 event.events = EPOLLIN;
408 event.data.u32 = port_idx;
410 for (i = 0; i < dpif->n_handlers; i++) {
411 struct dpif_handler *handler = &dpif->handlers[i];
413 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
418 dpif->handlers[i].channels[port_idx].sock = socksp[i];
419 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
425 for (j = 0; j < i; j++) {
426 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
427 nl_sock_fd(socksp[j]), NULL);
428 dpif->handlers[j].channels[port_idx].sock = NULL;
435 vport_del_channels(struct dpif_linux *dpif, odp_port_t port_no)
437 uint32_t port_idx = odp_to_u32(port_no);
440 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
444 /* Since the sock can only be assigned in either all or none
445 * of "dpif->handlers" channels, the following check would
447 if (!dpif->handlers[0].channels[port_idx].sock) {
451 for (i = 0; i < dpif->n_handlers; i++) {
452 struct dpif_handler *handler = &dpif->handlers[i];
454 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
455 nl_sock_fd(handler->channels[port_idx].sock), NULL);
456 nl_sock_destroy(handler->channels[port_idx].sock);
457 handler->channels[port_idx].sock = NULL;
458 handler->event_offset = handler->n_events = 0;
463 destroy_all_channels(struct dpif_linux *dpif) OVS_REQ_WRLOCK(dpif->upcall_lock)
467 if (!dpif->handlers) {
471 for (i = 0; i < dpif->uc_array_size; i++ ) {
472 struct dpif_linux_vport vport_request;
473 uint32_t upcall_pids = 0;
475 /* Since the sock can only be assigned in either all or none
476 * of "dpif->handlers" channels, the following check would
478 if (!dpif->handlers[0].channels[i].sock) {
482 /* Turn off upcalls. */
483 dpif_linux_vport_init(&vport_request);
484 vport_request.cmd = OVS_VPORT_CMD_SET;
485 vport_request.dp_ifindex = dpif->dp_ifindex;
486 vport_request.port_no = u32_to_odp(i);
487 vport_request.upcall_pids = &upcall_pids;
488 dpif_linux_vport_transact(&vport_request, NULL, NULL);
490 vport_del_channels(dpif, u32_to_odp(i));
493 for (i = 0; i < dpif->n_handlers; i++) {
494 struct dpif_handler *handler = &dpif->handlers[i];
496 close(handler->epoll_fd);
497 free(handler->epoll_events);
498 free(handler->channels);
501 free(dpif->handlers);
502 dpif->handlers = NULL;
503 dpif->n_handlers = 0;
504 dpif->uc_array_size = 0;
508 dpif_linux_close(struct dpif *dpif_)
510 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
512 nl_sock_destroy(dpif->port_notifier);
514 fat_rwlock_wrlock(&dpif->upcall_lock);
515 destroy_all_channels(dpif);
516 fat_rwlock_unlock(&dpif->upcall_lock);
518 fat_rwlock_destroy(&dpif->upcall_lock);
523 dpif_linux_destroy(struct dpif *dpif_)
525 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
526 struct dpif_linux_dp dp;
528 dpif_linux_dp_init(&dp);
529 dp.cmd = OVS_DP_CMD_DEL;
530 dp.dp_ifindex = dpif->dp_ifindex;
531 return dpif_linux_dp_transact(&dp, NULL, NULL);
535 dpif_linux_run(struct dpif *dpif_)
537 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
539 if (dpif->refresh_channels) {
540 dpif->refresh_channels = false;
541 fat_rwlock_wrlock(&dpif->upcall_lock);
542 dpif_linux_refresh_channels(dpif, dpif->n_handlers);
543 fat_rwlock_unlock(&dpif->upcall_lock);
548 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
550 struct dpif_linux_dp dp;
554 error = dpif_linux_dp_get(dpif_, &dp, &buf);
556 stats->n_hit = dp.stats.n_hit;
557 stats->n_missed = dp.stats.n_missed;
558 stats->n_lost = dp.stats.n_lost;
559 stats->n_flows = dp.stats.n_flows;
560 stats->n_masks = dp.megaflow_stats.n_masks;
561 stats->n_mask_hit = dp.megaflow_stats.n_mask_hit;
568 get_vport_type(const struct dpif_linux_vport *vport)
570 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
572 switch (vport->type) {
573 case OVS_VPORT_TYPE_NETDEV: {
574 const char *type = netdev_get_type_from_name(vport->name);
576 return type ? type : "system";
579 case OVS_VPORT_TYPE_INTERNAL:
582 case OVS_VPORT_TYPE_GRE:
585 case OVS_VPORT_TYPE_GRE64:
588 case OVS_VPORT_TYPE_VXLAN:
591 case OVS_VPORT_TYPE_LISP:
594 case OVS_VPORT_TYPE_UNSPEC:
595 case __OVS_VPORT_TYPE_MAX:
599 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
600 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
604 static enum ovs_vport_type
605 netdev_to_ovs_vport_type(const struct netdev *netdev)
607 const char *type = netdev_get_type(netdev);
609 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
610 return OVS_VPORT_TYPE_NETDEV;
611 } else if (!strcmp(type, "internal")) {
612 return OVS_VPORT_TYPE_INTERNAL;
613 } else if (strstr(type, "gre64")) {
614 return OVS_VPORT_TYPE_GRE64;
615 } else if (strstr(type, "gre")) {
616 return OVS_VPORT_TYPE_GRE;
617 } else if (!strcmp(type, "vxlan")) {
618 return OVS_VPORT_TYPE_VXLAN;
619 } else if (!strcmp(type, "lisp")) {
620 return OVS_VPORT_TYPE_LISP;
622 return OVS_VPORT_TYPE_UNSPEC;
627 dpif_linux_port_add__(struct dpif_linux *dpif, struct netdev *netdev,
628 odp_port_t *port_nop)
629 OVS_REQ_WRLOCK(dpif->upcall_lock)
631 const struct netdev_tunnel_config *tnl_cfg;
632 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
633 const char *name = netdev_vport_get_dpif_port(netdev,
634 namebuf, sizeof namebuf);
635 const char *type = netdev_get_type(netdev);
636 struct dpif_linux_vport request, reply;
638 uint64_t options_stub[64 / 8];
639 struct ofpbuf options;
640 struct nl_sock **socksp = NULL;
641 uint32_t *upcall_pids;
644 if (dpif->handlers) {
645 socksp = vport_create_socksp(dpif->n_handlers, &error);
651 dpif_linux_vport_init(&request);
652 request.cmd = OVS_VPORT_CMD_NEW;
653 request.dp_ifindex = dpif->dp_ifindex;
654 request.type = netdev_to_ovs_vport_type(netdev);
655 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
656 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
657 "unsupported type `%s'",
658 dpif_name(&dpif->dpif), name, type);
659 vport_del_socksp(socksp, dpif->n_handlers);
664 if (request.type == OVS_VPORT_TYPE_NETDEV) {
665 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
668 tnl_cfg = netdev_get_tunnel_config(netdev);
669 if (tnl_cfg && tnl_cfg->dst_port != 0) {
670 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
671 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
672 ntohs(tnl_cfg->dst_port));
673 request.options = ofpbuf_data(&options);
674 request.options_len = ofpbuf_size(&options);
677 request.port_no = *port_nop;
678 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
679 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
680 request.upcall_pids = upcall_pids;
682 error = dpif_linux_vport_transact(&request, &reply, &buf);
684 *port_nop = reply.port_no;
686 if (error == EBUSY && *port_nop != ODPP_NONE) {
687 VLOG_INFO("%s: requested port %"PRIu32" is in use",
688 dpif_name(&dpif->dpif), *port_nop);
691 vport_del_socksp(socksp, dpif->n_handlers);
696 error = vport_add_channels(dpif, *port_nop, socksp);
698 VLOG_INFO("%s: could not add channel for port %s",
699 dpif_name(&dpif->dpif), name);
701 /* Delete the port. */
702 dpif_linux_vport_init(&request);
703 request.cmd = OVS_VPORT_CMD_DEL;
704 request.dp_ifindex = dpif->dp_ifindex;
705 request.port_no = *port_nop;
706 dpif_linux_vport_transact(&request, NULL, NULL);
707 vport_del_socksp(socksp, dpif->n_handlers);
721 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
722 odp_port_t *port_nop)
724 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
727 fat_rwlock_wrlock(&dpif->upcall_lock);
728 error = dpif_linux_port_add__(dpif, netdev, port_nop);
729 fat_rwlock_unlock(&dpif->upcall_lock);
735 dpif_linux_port_del__(struct dpif_linux *dpif, odp_port_t port_no)
736 OVS_REQ_WRLOCK(dpif->upcall_lock)
738 struct dpif_linux_vport vport;
741 dpif_linux_vport_init(&vport);
742 vport.cmd = OVS_VPORT_CMD_DEL;
743 vport.dp_ifindex = dpif->dp_ifindex;
744 vport.port_no = port_no;
745 error = dpif_linux_vport_transact(&vport, NULL, NULL);
747 vport_del_channels(dpif, port_no);
753 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
755 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
758 fat_rwlock_wrlock(&dpif->upcall_lock);
759 error = dpif_linux_port_del__(dpif, port_no);
760 fat_rwlock_unlock(&dpif->upcall_lock);
766 dpif_linux_port_query__(const struct dpif_linux *dpif, odp_port_t port_no,
767 const char *port_name, struct dpif_port *dpif_port)
769 struct dpif_linux_vport request;
770 struct dpif_linux_vport reply;
774 dpif_linux_vport_init(&request);
775 request.cmd = OVS_VPORT_CMD_GET;
776 request.dp_ifindex = dpif->dp_ifindex;
777 request.port_no = port_no;
778 request.name = port_name;
780 error = dpif_linux_vport_transact(&request, &reply, &buf);
782 if (reply.dp_ifindex != request.dp_ifindex) {
783 /* A query by name reported that 'port_name' is in some datapath
784 * other than 'dpif', but the caller wants to know about 'dpif'. */
786 } else if (dpif_port) {
787 dpif_port->name = xstrdup(reply.name);
788 dpif_port->type = xstrdup(get_vport_type(&reply));
789 dpif_port->port_no = reply.port_no;
797 dpif_linux_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
798 struct dpif_port *dpif_port)
800 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
802 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
806 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
807 struct dpif_port *dpif_port)
809 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
811 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
815 dpif_linux_port_get_pid__(const struct dpif_linux *dpif, odp_port_t port_no,
817 OVS_REQ_RDLOCK(dpif->upcall_lock)
819 uint32_t port_idx = odp_to_u32(port_no);
822 if (dpif->handlers) {
823 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
824 * channel, since it is not heavily loaded. */
825 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
826 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
828 pid = nl_sock_pid(h->channels[idx].sock);
835 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
838 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
841 fat_rwlock_rdlock(&dpif->upcall_lock);
842 ret = dpif_linux_port_get_pid__(dpif, port_no, hash);
843 fat_rwlock_unlock(&dpif->upcall_lock);
849 dpif_linux_flow_flush(struct dpif *dpif_)
851 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
852 struct dpif_linux_flow flow;
854 dpif_linux_flow_init(&flow);
855 flow.cmd = OVS_FLOW_CMD_DEL;
856 flow.dp_ifindex = dpif->dp_ifindex;
857 return dpif_linux_flow_transact(&flow, NULL, NULL);
860 struct dpif_linux_port_state {
866 dpif_linux_port_dump_start__(const struct dpif_linux *dpif,
867 struct nl_dump *dump)
869 struct dpif_linux_vport request;
872 dpif_linux_vport_init(&request);
873 request.cmd = OVS_VPORT_CMD_GET;
874 request.dp_ifindex = dpif->dp_ifindex;
876 buf = ofpbuf_new(1024);
877 dpif_linux_vport_to_ofpbuf(&request, buf);
878 nl_dump_start(dump, NETLINK_GENERIC, buf);
883 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
885 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
886 struct dpif_linux_port_state *state;
888 *statep = state = xmalloc(sizeof *state);
889 dpif_linux_port_dump_start__(dpif, &state->dump);
891 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
896 dpif_linux_port_dump_next__(const struct dpif_linux *dpif, struct nl_dump *dump,
897 struct dpif_linux_vport *vport,
898 struct ofpbuf *buffer)
903 if (!nl_dump_next(dump, &buf, buffer)) {
907 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
909 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
910 dpif_name(&dpif->dpif), ovs_strerror(error));
916 dpif_linux_port_dump_next(const struct dpif *dpif_, void *state_,
917 struct dpif_port *dpif_port)
919 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
920 struct dpif_linux_port_state *state = state_;
921 struct dpif_linux_vport vport;
924 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport,
929 dpif_port->name = CONST_CAST(char *, vport.name);
930 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
931 dpif_port->port_no = vport.port_no;
936 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
938 struct dpif_linux_port_state *state = state_;
939 int error = nl_dump_done(&state->dump);
941 ofpbuf_uninit(&state->buf);
947 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
949 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
951 /* Lazily create the Netlink socket to listen for notifications. */
952 if (!dpif->port_notifier) {
953 struct nl_sock *sock;
956 error = nl_sock_create(NETLINK_GENERIC, &sock);
961 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
963 nl_sock_destroy(sock);
966 dpif->port_notifier = sock;
968 /* We have no idea of the current state so report that everything
974 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
975 uint64_t buf_stub[4096 / 8];
979 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
980 error = nl_sock_recv(dpif->port_notifier, &buf, false);
982 struct dpif_linux_vport vport;
984 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
986 if (vport.dp_ifindex == dpif->dp_ifindex
987 && (vport.cmd == OVS_VPORT_CMD_NEW
988 || vport.cmd == OVS_VPORT_CMD_DEL
989 || vport.cmd == OVS_VPORT_CMD_SET)) {
990 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
991 dpif->dpif.full_name, vport.name, vport.cmd);
992 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
993 dpif->refresh_channels = true;
995 *devnamep = xstrdup(vport.name);
1000 } else if (error != EAGAIN) {
1001 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1002 ovs_strerror(error));
1003 nl_sock_drain(dpif->port_notifier);
1007 ofpbuf_uninit(&buf);
1015 dpif_linux_port_poll_wait(const struct dpif *dpif_)
1017 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1019 if (dpif->port_notifier) {
1020 nl_sock_wait(dpif->port_notifier, POLLIN);
1022 poll_immediate_wake();
1027 dpif_linux_flow_get__(const struct dpif_linux *dpif,
1028 const struct nlattr *key, size_t key_len,
1029 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1031 struct dpif_linux_flow request;
1033 dpif_linux_flow_init(&request);
1034 request.cmd = OVS_FLOW_CMD_GET;
1035 request.dp_ifindex = dpif->dp_ifindex;
1037 request.key_len = key_len;
1038 return dpif_linux_flow_transact(&request, reply, bufp);
1042 dpif_linux_flow_get(const struct dpif *dpif_,
1043 const struct nlattr *key, size_t key_len,
1044 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1046 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1047 struct dpif_linux_flow reply;
1051 error = dpif_linux_flow_get__(dpif, key, key_len, &reply, &buf);
1054 dpif_linux_flow_get_stats(&reply, stats);
1057 ofpbuf_set_data(buf, CONST_CAST(struct nlattr *, reply.actions));
1058 ofpbuf_set_size(buf, reply.actions_len);
1068 dpif_linux_init_flow_put(struct dpif_linux *dpif, const struct dpif_flow_put *put,
1069 struct dpif_linux_flow *request)
1071 static const struct nlattr dummy_action;
1073 dpif_linux_flow_init(request);
1074 request->cmd = (put->flags & DPIF_FP_CREATE
1075 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1076 request->dp_ifindex = dpif->dp_ifindex;
1077 request->key = put->key;
1078 request->key_len = put->key_len;
1079 request->mask = put->mask;
1080 request->mask_len = put->mask_len;
1081 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1082 request->actions = (put->actions
1084 : CONST_CAST(struct nlattr *, &dummy_action));
1085 request->actions_len = put->actions_len;
1086 if (put->flags & DPIF_FP_ZERO_STATS) {
1087 request->clear = true;
1089 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1093 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
1095 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1096 struct dpif_linux_flow request, reply;
1100 dpif_linux_init_flow_put(dpif, put, &request);
1101 error = dpif_linux_flow_transact(&request,
1102 put->stats ? &reply : NULL,
1103 put->stats ? &buf : NULL);
1104 if (!error && put->stats) {
1105 dpif_linux_flow_get_stats(&reply, put->stats);
1112 dpif_linux_init_flow_del(struct dpif_linux *dpif, const struct dpif_flow_del *del,
1113 struct dpif_linux_flow *request)
1115 dpif_linux_flow_init(request);
1116 request->cmd = OVS_FLOW_CMD_DEL;
1117 request->dp_ifindex = dpif->dp_ifindex;
1118 request->key = del->key;
1119 request->key_len = del->key_len;
1123 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
1125 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1126 struct dpif_linux_flow request, reply;
1130 dpif_linux_init_flow_del(dpif, del, &request);
1131 error = dpif_linux_flow_transact(&request,
1132 del->stats ? &reply : NULL,
1133 del->stats ? &buf : NULL);
1134 if (!error && del->stats) {
1135 dpif_linux_flow_get_stats(&reply, del->stats);
1141 struct dpif_linux_flow_dump {
1142 struct dpif_flow_dump up;
1143 struct nl_dump nl_dump;
1147 static struct dpif_linux_flow_dump *
1148 dpif_linux_flow_dump_cast(struct dpif_flow_dump *dump)
1150 return CONTAINER_OF(dump, struct dpif_linux_flow_dump, up);
1153 static struct dpif_flow_dump *
1154 dpif_linux_flow_dump_create(const struct dpif *dpif_)
1156 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1157 struct dpif_linux_flow_dump *dump;
1158 struct dpif_linux_flow request;
1161 dump = xmalloc(sizeof *dump);
1162 dpif_flow_dump_init(&dump->up, dpif_);
1164 dpif_linux_flow_init(&request);
1165 request.cmd = OVS_FLOW_CMD_GET;
1166 request.dp_ifindex = dpif->dp_ifindex;
1168 buf = ofpbuf_new(1024);
1169 dpif_linux_flow_to_ofpbuf(&request, buf);
1170 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1172 atomic_init(&dump->status, 0);
1178 dpif_linux_flow_dump_destroy(struct dpif_flow_dump *dump_)
1180 struct dpif_linux_flow_dump *dump = dpif_linux_flow_dump_cast(dump_);
1181 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1184 atomic_read(&dump->status, &dump_status);
1186 return dump_status ? dump_status : nl_status;
1189 struct dpif_linux_flow_dump_thread {
1190 struct dpif_flow_dump_thread up;
1191 struct dpif_linux_flow_dump *dump;
1192 struct dpif_linux_flow flow;
1193 struct dpif_flow_stats stats;
1194 struct ofpbuf nl_flows; /* Always used to store flows. */
1195 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1198 static struct dpif_linux_flow_dump_thread *
1199 dpif_linux_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1201 return CONTAINER_OF(thread, struct dpif_linux_flow_dump_thread, up);
1204 static struct dpif_flow_dump_thread *
1205 dpif_linux_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1207 struct dpif_linux_flow_dump *dump = dpif_linux_flow_dump_cast(dump_);
1208 struct dpif_linux_flow_dump_thread *thread;
1210 thread = xmalloc(sizeof *thread);
1211 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1212 thread->dump = dump;
1213 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1214 thread->nl_actions = NULL;
1220 dpif_linux_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1222 struct dpif_linux_flow_dump_thread *thread
1223 = dpif_linux_flow_dump_thread_cast(thread_);
1225 ofpbuf_uninit(&thread->nl_flows);
1226 ofpbuf_delete(thread->nl_actions);
1231 dpif_linux_flow_to_dpif_flow(struct dpif_flow *dpif_flow,
1232 struct dpif_linux_flow *linux_flow)
1234 dpif_flow->key = linux_flow->key;
1235 dpif_flow->key_len = linux_flow->key_len;
1236 dpif_flow->mask = linux_flow->mask;
1237 dpif_flow->mask_len = linux_flow->mask_len;
1238 dpif_flow->actions = linux_flow->actions;
1239 dpif_flow->actions_len = linux_flow->actions_len;
1240 dpif_linux_flow_get_stats(linux_flow, &dpif_flow->stats);
1244 dpif_linux_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1245 struct dpif_flow *flows, int max_flows)
1247 struct dpif_linux_flow_dump_thread *thread
1248 = dpif_linux_flow_dump_thread_cast(thread_);
1249 struct dpif_linux_flow_dump *dump = thread->dump;
1250 struct dpif_linux *dpif = dpif_linux_cast(thread->up.dpif);
1253 ofpbuf_delete(thread->nl_actions);
1254 thread->nl_actions = NULL;
1258 || (n_flows < max_flows && ofpbuf_size(&thread->nl_flows))) {
1259 struct dpif_linux_flow linux_flow;
1260 struct ofpbuf nl_flow;
1263 /* Try to grab another flow. */
1264 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1268 /* Convert the flow to our output format. */
1269 error = dpif_linux_flow_from_ofpbuf(&linux_flow, &nl_flow);
1271 atomic_store(&dump->status, error);
1275 if (linux_flow.actions) {
1276 /* Common case: the flow includes actions. */
1277 dpif_linux_flow_to_dpif_flow(&flows[n_flows++], &linux_flow);
1279 /* Rare case: the flow does not include actions. Retrieve this
1280 * individual flow again to get the actions. */
1281 error = dpif_linux_flow_get__(dpif, linux_flow.key,
1282 linux_flow.key_len, &linux_flow,
1283 &thread->nl_actions);
1284 if (error == ENOENT) {
1285 VLOG_DBG("dumped flow disappeared on get");
1288 VLOG_WARN("error fetching dumped flow: %s",
1289 ovs_strerror(error));
1290 atomic_store(&dump->status, error);
1294 /* Save this flow. Then exit, because we only have one buffer to
1295 * handle this case. */
1296 dpif_linux_flow_to_dpif_flow(&flows[n_flows++], &linux_flow);
1304 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1307 struct ovs_header *k_exec;
1310 ofpbuf_prealloc_tailroom(buf, (64
1311 + ofpbuf_size(d_exec->packet)
1312 + ODP_KEY_METADATA_SIZE
1313 + d_exec->actions_len));
1315 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1316 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1318 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1319 k_exec->dp_ifindex = dp_ifindex;
1321 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1322 ofpbuf_data(d_exec->packet),
1323 ofpbuf_size(d_exec->packet));
1325 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1326 odp_key_from_pkt_metadata(buf, &d_exec->md);
1327 nl_msg_end_nested(buf, key_ofs);
1329 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1330 d_exec->actions, d_exec->actions_len);
1334 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1336 uint64_t request_stub[1024 / 8];
1337 struct ofpbuf request;
1340 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1341 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1342 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1343 ofpbuf_uninit(&request);
1349 dpif_linux_execute(struct dpif *dpif_, struct dpif_execute *execute)
1351 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1353 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1359 dpif_linux_operate__(struct dpif_linux *dpif, struct dpif_op **ops, size_t n_ops)
1363 struct nl_transaction txn;
1365 struct ofpbuf request;
1366 uint64_t request_stub[1024 / 8];
1368 struct ofpbuf reply;
1369 uint64_t reply_stub[1024 / 8];
1372 struct nl_transaction *txnsp[MAX_OPS];
1375 ovs_assert(n_ops <= MAX_OPS);
1376 for (i = 0; i < n_ops; i++) {
1377 struct op_auxdata *aux = &auxes[i];
1378 struct dpif_op *op = ops[i];
1379 struct dpif_flow_put *put;
1380 struct dpif_flow_del *del;
1381 struct dpif_execute *execute;
1382 struct dpif_linux_flow flow;
1384 ofpbuf_use_stub(&aux->request,
1385 aux->request_stub, sizeof aux->request_stub);
1386 aux->txn.request = &aux->request;
1388 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1389 aux->txn.reply = NULL;
1392 case DPIF_OP_FLOW_PUT:
1393 put = &op->u.flow_put;
1394 dpif_linux_init_flow_put(dpif, put, &flow);
1396 flow.nlmsg_flags |= NLM_F_ECHO;
1397 aux->txn.reply = &aux->reply;
1399 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1402 case DPIF_OP_FLOW_DEL:
1403 del = &op->u.flow_del;
1404 dpif_linux_init_flow_del(dpif, del, &flow);
1406 flow.nlmsg_flags |= NLM_F_ECHO;
1407 aux->txn.reply = &aux->reply;
1409 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1412 case DPIF_OP_EXECUTE:
1413 execute = &op->u.execute;
1414 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1423 for (i = 0; i < n_ops; i++) {
1424 txnsp[i] = &auxes[i].txn;
1426 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1428 for (i = 0; i < n_ops; i++) {
1429 struct op_auxdata *aux = &auxes[i];
1430 struct nl_transaction *txn = &auxes[i].txn;
1431 struct dpif_op *op = ops[i];
1432 struct dpif_flow_put *put;
1433 struct dpif_flow_del *del;
1435 op->error = txn->error;
1438 case DPIF_OP_FLOW_PUT:
1439 put = &op->u.flow_put;
1442 struct dpif_linux_flow reply;
1444 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1447 dpif_linux_flow_get_stats(&reply, put->stats);
1452 memset(put->stats, 0, sizeof *put->stats);
1457 case DPIF_OP_FLOW_DEL:
1458 del = &op->u.flow_del;
1461 struct dpif_linux_flow reply;
1463 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1466 dpif_linux_flow_get_stats(&reply, del->stats);
1471 memset(del->stats, 0, sizeof *del->stats);
1476 case DPIF_OP_EXECUTE:
1483 ofpbuf_uninit(&aux->request);
1484 ofpbuf_uninit(&aux->reply);
1489 dpif_linux_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1491 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1494 size_t chunk = MIN(n_ops, MAX_OPS);
1495 dpif_linux_operate__(dpif, ops, chunk);
1501 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1502 * currently in 'dpif' in the kernel, by adding a new set of channels for
1503 * any kernel vport that lacks one and deleting any channels that have no
1504 * backing kernel vports. */
1506 dpif_linux_refresh_channels(struct dpif_linux *dpif, uint32_t n_handlers)
1507 OVS_REQ_WRLOCK(dpif->upcall_lock)
1509 unsigned long int *keep_channels;
1510 struct dpif_linux_vport vport;
1511 size_t keep_channels_nbits;
1512 struct nl_dump dump;
1513 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1518 if (dpif->n_handlers != n_handlers) {
1519 destroy_all_channels(dpif);
1520 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1521 for (i = 0; i < n_handlers; i++) {
1522 struct dpif_handler *handler = &dpif->handlers[i];
1524 handler->epoll_fd = epoll_create(10);
1525 if (handler->epoll_fd < 0) {
1528 for (j = 0; j < i; j++) {
1529 close(dpif->handlers[j].epoll_fd);
1531 free(dpif->handlers);
1532 dpif->handlers = NULL;
1537 dpif->n_handlers = n_handlers;
1540 for (i = 0; i < n_handlers; i++) {
1541 struct dpif_handler *handler = &dpif->handlers[i];
1543 handler->event_offset = handler->n_events = 0;
1546 keep_channels_nbits = dpif->uc_array_size;
1547 keep_channels = bitmap_allocate(keep_channels_nbits);
1549 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1550 dpif_linux_port_dump_start__(dpif, &dump);
1551 while (!dpif_linux_port_dump_next__(dpif, &dump, &vport, &buf)) {
1552 uint32_t port_no = odp_to_u32(vport.port_no);
1553 uint32_t *upcall_pids = NULL;
1556 if (port_no >= dpif->uc_array_size
1557 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1558 struct nl_sock **socksp = vport_create_socksp(dpif->n_handlers,
1565 error = vport_add_channels(dpif, vport.port_no, socksp);
1567 VLOG_INFO("%s: could not add channels for port %s",
1568 dpif_name(&dpif->dpif), vport.name);
1569 vport_del_socksp(socksp, dpif->n_handlers);
1573 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1577 /* Configure the vport to deliver misses to 'sock'. */
1578 if (vport.upcall_pids[0] == 0
1579 || vport.n_upcall_pids != dpif->n_handlers
1580 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1582 struct dpif_linux_vport vport_request;
1584 dpif_linux_vport_init(&vport_request);
1585 vport_request.cmd = OVS_VPORT_CMD_SET;
1586 vport_request.dp_ifindex = dpif->dp_ifindex;
1587 vport_request.port_no = vport.port_no;
1588 vport_request.n_upcall_pids = dpif->n_handlers;
1589 vport_request.upcall_pids = upcall_pids;
1590 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1592 VLOG_WARN_RL(&error_rl,
1593 "%s: failed to set upcall pid on port: %s",
1594 dpif_name(&dpif->dpif), ovs_strerror(error));
1596 if (error != ENODEV && error != ENOENT) {
1599 /* The vport isn't really there, even though the dump says
1600 * it is. Probably we just hit a race after a port
1607 if (port_no < keep_channels_nbits) {
1608 bitmap_set1(keep_channels, port_no);
1615 vport_del_channels(dpif, vport.port_no);
1617 nl_dump_done(&dump);
1618 ofpbuf_uninit(&buf);
1620 /* Discard any saved channels that we didn't reuse. */
1621 for (i = 0; i < keep_channels_nbits; i++) {
1622 if (!bitmap_is_set(keep_channels, i)) {
1623 vport_del_channels(dpif, u32_to_odp(i));
1626 free(keep_channels);
1632 dpif_linux_recv_set__(struct dpif_linux *dpif, bool enable)
1633 OVS_REQ_WRLOCK(dpif->upcall_lock)
1635 if ((dpif->handlers != NULL) == enable) {
1637 } else if (!enable) {
1638 destroy_all_channels(dpif);
1641 return dpif_linux_refresh_channels(dpif, 1);
1646 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1648 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1651 fat_rwlock_wrlock(&dpif->upcall_lock);
1652 error = dpif_linux_recv_set__(dpif, enable);
1653 fat_rwlock_unlock(&dpif->upcall_lock);
1659 dpif_linux_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1661 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1664 fat_rwlock_wrlock(&dpif->upcall_lock);
1665 if (dpif->handlers) {
1666 error = dpif_linux_refresh_channels(dpif, n_handlers);
1668 fat_rwlock_unlock(&dpif->upcall_lock);
1674 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1675 uint32_t queue_id, uint32_t *priority)
1677 if (queue_id < 0xf000) {
1678 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1686 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1689 static const struct nl_policy ovs_packet_policy[] = {
1690 /* Always present. */
1691 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1692 .min_len = ETH_HEADER_LEN },
1693 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1695 /* OVS_PACKET_CMD_ACTION only. */
1696 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1699 struct ovs_header *ovs_header;
1700 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1701 struct nlmsghdr *nlmsg;
1702 struct genlmsghdr *genl;
1706 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1708 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1709 genl = ofpbuf_try_pull(&b, sizeof *genl);
1710 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1711 if (!nlmsg || !genl || !ovs_header
1712 || nlmsg->nlmsg_type != ovs_packet_family
1713 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1714 ARRAY_SIZE(ovs_packet_policy))) {
1718 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1719 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1725 /* (Re)set ALL fields of '*upcall' on successful return. */
1726 upcall->type = type;
1727 upcall->key = CONST_CAST(struct nlattr *,
1728 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1729 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1730 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1732 /* Allow overwriting the netlink attribute header without reallocating. */
1733 ofpbuf_use_stub(&upcall->packet,
1734 CONST_CAST(struct nlattr *,
1735 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1736 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1737 sizeof(struct nlattr));
1738 ofpbuf_set_data(&upcall->packet,
1739 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
1740 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
1742 *dp_ifindex = ovs_header->dp_ifindex;
1748 dpif_linux_recv__(struct dpif_linux *dpif, uint32_t handler_id,
1749 struct dpif_upcall *upcall, struct ofpbuf *buf)
1750 OVS_REQ_RDLOCK(dpif->upcall_lock)
1752 struct dpif_handler *handler;
1755 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
1759 handler = &dpif->handlers[handler_id];
1760 if (handler->event_offset >= handler->n_events) {
1763 handler->event_offset = handler->n_events = 0;
1766 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
1767 dpif->uc_array_size, 0);
1768 } while (retval < 0 && errno == EINTR);
1770 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1771 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1772 } else if (retval > 0) {
1773 handler->n_events = retval;
1777 while (handler->event_offset < handler->n_events) {
1778 int idx = handler->epoll_events[handler->event_offset].data.u32;
1779 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
1781 handler->event_offset++;
1787 if (++read_tries > 50) {
1791 error = nl_sock_recv(ch->sock, buf, false);
1792 if (error == ENOBUFS) {
1793 /* ENOBUFS typically means that we've received so many
1794 * packets that the buffer overflowed. Try again
1795 * immediately because there's almost certainly a packet
1796 * waiting for us. */
1797 report_loss(dpif, ch, idx, handler_id);
1801 ch->last_poll = time_msec();
1803 if (error == EAGAIN) {
1809 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1810 if (!error && dp_ifindex == dpif->dp_ifindex) {
1822 dpif_linux_recv(struct dpif *dpif_, uint32_t handler_id,
1823 struct dpif_upcall *upcall, struct ofpbuf *buf)
1825 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1828 fat_rwlock_rdlock(&dpif->upcall_lock);
1829 error = dpif_linux_recv__(dpif, handler_id, upcall, buf);
1830 fat_rwlock_unlock(&dpif->upcall_lock);
1836 dpif_linux_recv_wait__(struct dpif_linux *dpif, uint32_t handler_id)
1837 OVS_REQ_RDLOCK(dpif->upcall_lock)
1839 if (dpif->handlers && handler_id < dpif->n_handlers) {
1840 struct dpif_handler *handler = &dpif->handlers[handler_id];
1842 poll_fd_wait(handler->epoll_fd, POLLIN);
1847 dpif_linux_recv_wait(struct dpif *dpif_, uint32_t handler_id)
1849 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1851 fat_rwlock_rdlock(&dpif->upcall_lock);
1852 dpif_linux_recv_wait__(dpif, handler_id);
1853 fat_rwlock_unlock(&dpif->upcall_lock);
1857 dpif_linux_recv_purge__(struct dpif_linux *dpif)
1858 OVS_REQ_WRLOCK(dpif->upcall_lock)
1860 if (dpif->handlers) {
1863 for (i = 0; i < dpif->uc_array_size; i++ ) {
1864 if (!dpif->handlers[0].channels[i].sock) {
1868 for (j = 0; j < dpif->n_handlers; j++) {
1869 nl_sock_drain(dpif->handlers[j].channels[i].sock);
1876 dpif_linux_recv_purge(struct dpif *dpif_)
1878 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1880 fat_rwlock_wrlock(&dpif->upcall_lock);
1881 dpif_linux_recv_purge__(dpif);
1882 fat_rwlock_unlock(&dpif->upcall_lock);
1885 const struct dpif_class dpif_linux_class = {
1887 dpif_linux_enumerate,
1894 dpif_linux_get_stats,
1895 dpif_linux_port_add,
1896 dpif_linux_port_del,
1897 dpif_linux_port_query_by_number,
1898 dpif_linux_port_query_by_name,
1899 dpif_linux_port_get_pid,
1900 dpif_linux_port_dump_start,
1901 dpif_linux_port_dump_next,
1902 dpif_linux_port_dump_done,
1903 dpif_linux_port_poll,
1904 dpif_linux_port_poll_wait,
1905 dpif_linux_flow_get,
1906 dpif_linux_flow_put,
1907 dpif_linux_flow_del,
1908 dpif_linux_flow_flush,
1909 dpif_linux_flow_dump_create,
1910 dpif_linux_flow_dump_destroy,
1911 dpif_linux_flow_dump_thread_create,
1912 dpif_linux_flow_dump_thread_destroy,
1913 dpif_linux_flow_dump_next,
1916 dpif_linux_recv_set,
1917 dpif_linux_handlers_set,
1918 dpif_linux_queue_to_priority,
1920 dpif_linux_recv_wait,
1921 dpif_linux_recv_purge,
1925 dpif_linux_init(void)
1927 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1930 if (ovsthread_once_start(&once)) {
1931 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1932 &ovs_datapath_family);
1934 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1935 "The Open vSwitch kernel module is probably not loaded.",
1936 OVS_DATAPATH_FAMILY);
1939 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1942 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1945 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1946 &ovs_packet_family);
1949 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1950 &ovs_vport_mcgroup);
1953 ovsthread_once_done(&once);
1960 dpif_linux_is_internal_device(const char *name)
1962 struct dpif_linux_vport reply;
1966 error = dpif_linux_vport_get(name, &reply, &buf);
1969 } else if (error != ENODEV && error != ENOENT) {
1970 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1971 name, ovs_strerror(error));
1974 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1977 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1978 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1979 * positive errno value.
1981 * 'vport' will contain pointers into 'buf', so the caller should not free
1982 * 'buf' while 'vport' is still in use. */
1984 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1985 const struct ofpbuf *buf)
1987 static const struct nl_policy ovs_vport_policy[] = {
1988 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1989 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1990 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1991 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
1992 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1994 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1997 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1998 struct ovs_header *ovs_header;
1999 struct nlmsghdr *nlmsg;
2000 struct genlmsghdr *genl;
2003 dpif_linux_vport_init(vport);
2005 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2006 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2007 genl = ofpbuf_try_pull(&b, sizeof *genl);
2008 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2009 if (!nlmsg || !genl || !ovs_header
2010 || nlmsg->nlmsg_type != ovs_vport_family
2011 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2012 ARRAY_SIZE(ovs_vport_policy))) {
2016 vport->cmd = genl->cmd;
2017 vport->dp_ifindex = ovs_header->dp_ifindex;
2018 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2019 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2020 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2021 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2022 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2023 / (sizeof *vport->upcall_pids);
2024 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2027 if (a[OVS_VPORT_ATTR_STATS]) {
2028 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2030 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2031 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2032 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2037 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2038 * followed by Netlink attributes corresponding to 'vport'. */
2040 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
2043 struct ovs_header *ovs_header;
2045 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2046 vport->cmd, OVS_VPORT_VERSION);
2048 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2049 ovs_header->dp_ifindex = vport->dp_ifindex;
2051 if (vport->port_no != ODPP_NONE) {
2052 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2055 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2056 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2060 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2063 if (vport->upcall_pids) {
2064 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2066 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2070 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2071 vport->stats, sizeof *vport->stats);
2074 if (vport->options) {
2075 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2076 vport->options, vport->options_len);
2080 /* Clears 'vport' to "empty" values. */
2082 dpif_linux_vport_init(struct dpif_linux_vport *vport)
2084 memset(vport, 0, sizeof *vport);
2085 vport->port_no = ODPP_NONE;
2088 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2089 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2090 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2091 * result of the command is expected to be an ovs_vport also, which is decoded
2092 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2093 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2095 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
2096 struct dpif_linux_vport *reply,
2097 struct ofpbuf **bufp)
2099 struct ofpbuf *request_buf;
2102 ovs_assert((reply != NULL) == (bufp != NULL));
2104 error = dpif_linux_init();
2108 dpif_linux_vport_init(reply);
2113 request_buf = ofpbuf_new(1024);
2114 dpif_linux_vport_to_ofpbuf(request, request_buf);
2115 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2116 ofpbuf_delete(request_buf);
2120 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
2123 dpif_linux_vport_init(reply);
2124 ofpbuf_delete(*bufp);
2131 /* Obtains information about the kernel vport named 'name' and stores it into
2132 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2133 * longer needed ('reply' will contain pointers into '*bufp'). */
2135 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
2136 struct ofpbuf **bufp)
2138 struct dpif_linux_vport request;
2140 dpif_linux_vport_init(&request);
2141 request.cmd = OVS_VPORT_CMD_GET;
2142 request.name = name;
2144 return dpif_linux_vport_transact(&request, reply, bufp);
2147 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2148 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2149 * positive errno value.
2151 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2152 * while 'dp' is still in use. */
2154 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
2156 static const struct nl_policy ovs_datapath_policy[] = {
2157 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2158 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2160 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2161 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2165 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2166 struct ovs_header *ovs_header;
2167 struct nlmsghdr *nlmsg;
2168 struct genlmsghdr *genl;
2171 dpif_linux_dp_init(dp);
2173 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2174 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2175 genl = ofpbuf_try_pull(&b, sizeof *genl);
2176 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2177 if (!nlmsg || !genl || !ovs_header
2178 || nlmsg->nlmsg_type != ovs_datapath_family
2179 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2180 ARRAY_SIZE(ovs_datapath_policy))) {
2184 dp->cmd = genl->cmd;
2185 dp->dp_ifindex = ovs_header->dp_ifindex;
2186 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2187 if (a[OVS_DP_ATTR_STATS]) {
2188 /* Can't use structure assignment because Netlink doesn't ensure
2189 * sufficient alignment for 64-bit members. */
2190 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
2194 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2195 /* Can't use structure assignment because Netlink doesn't ensure
2196 * sufficient alignment for 64-bit members. */
2197 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
2198 sizeof dp->megaflow_stats);
2204 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2206 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
2208 struct ovs_header *ovs_header;
2210 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2211 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2212 OVS_DATAPATH_VERSION);
2214 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2215 ovs_header->dp_ifindex = dp->dp_ifindex;
2218 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2221 if (dp->upcall_pid) {
2222 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2225 if (dp->user_features) {
2226 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2229 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2232 /* Clears 'dp' to "empty" values. */
2234 dpif_linux_dp_init(struct dpif_linux_dp *dp)
2236 memset(dp, 0, sizeof *dp);
2237 dp->megaflow_stats.n_masks = UINT32_MAX;
2238 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
2242 dpif_linux_dp_dump_start(struct nl_dump *dump)
2244 struct dpif_linux_dp request;
2247 dpif_linux_dp_init(&request);
2248 request.cmd = OVS_DP_CMD_GET;
2250 buf = ofpbuf_new(1024);
2251 dpif_linux_dp_to_ofpbuf(&request, buf);
2252 nl_dump_start(dump, NETLINK_GENERIC, buf);
2256 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2257 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2258 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2259 * result of the command is expected to be of the same form, which is decoded
2260 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2261 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2263 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
2264 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
2266 struct ofpbuf *request_buf;
2269 ovs_assert((reply != NULL) == (bufp != NULL));
2271 request_buf = ofpbuf_new(1024);
2272 dpif_linux_dp_to_ofpbuf(request, request_buf);
2273 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2274 ofpbuf_delete(request_buf);
2277 dpif_linux_dp_init(reply);
2279 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
2282 ofpbuf_delete(*bufp);
2289 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2290 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2291 * will contain pointers into '*bufp'). */
2293 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2294 struct ofpbuf **bufp)
2296 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2297 struct dpif_linux_dp request;
2299 dpif_linux_dp_init(&request);
2300 request.cmd = OVS_DP_CMD_GET;
2301 request.dp_ifindex = dpif->dp_ifindex;
2303 return dpif_linux_dp_transact(&request, reply, bufp);
2306 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2307 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2308 * positive errno value.
2310 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2311 * while 'flow' is still in use. */
2313 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2314 const struct ofpbuf *buf)
2316 static const struct nl_policy ovs_flow_policy[] = {
2317 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2318 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2319 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2320 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2322 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2323 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2324 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2327 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2328 struct ovs_header *ovs_header;
2329 struct nlmsghdr *nlmsg;
2330 struct genlmsghdr *genl;
2333 dpif_linux_flow_init(flow);
2335 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2336 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2337 genl = ofpbuf_try_pull(&b, sizeof *genl);
2338 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2339 if (!nlmsg || !genl || !ovs_header
2340 || nlmsg->nlmsg_type != ovs_flow_family
2341 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2342 ARRAY_SIZE(ovs_flow_policy))) {
2346 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2347 flow->dp_ifindex = ovs_header->dp_ifindex;
2348 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2349 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2351 if (a[OVS_FLOW_ATTR_MASK]) {
2352 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2353 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2355 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2356 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2357 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2359 if (a[OVS_FLOW_ATTR_STATS]) {
2360 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2362 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2363 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2365 if (a[OVS_FLOW_ATTR_USED]) {
2366 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2371 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2372 * followed by Netlink attributes corresponding to 'flow'. */
2374 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2377 struct ovs_header *ovs_header;
2379 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2380 NLM_F_REQUEST | flow->nlmsg_flags,
2381 flow->cmd, OVS_FLOW_VERSION);
2383 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2384 ovs_header->dp_ifindex = flow->dp_ifindex;
2386 if (flow->key_len) {
2387 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2390 if (flow->mask_len) {
2391 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2394 if (flow->actions || flow->actions_len) {
2395 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2396 flow->actions, flow->actions_len);
2399 /* We never need to send these to the kernel. */
2400 ovs_assert(!flow->stats);
2401 ovs_assert(!flow->tcp_flags);
2402 ovs_assert(!flow->used);
2405 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2409 /* Clears 'flow' to "empty" values. */
2411 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2413 memset(flow, 0, sizeof *flow);
2416 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2417 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2418 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2419 * result of the command is expected to be a flow also, which is decoded and
2420 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2421 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2423 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2424 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2426 struct ofpbuf *request_buf;
2429 ovs_assert((reply != NULL) == (bufp != NULL));
2432 request->nlmsg_flags |= NLM_F_ECHO;
2435 request_buf = ofpbuf_new(1024);
2436 dpif_linux_flow_to_ofpbuf(request, request_buf);
2437 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2438 ofpbuf_delete(request_buf);
2442 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2445 dpif_linux_flow_init(reply);
2446 ofpbuf_delete(*bufp);
2454 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2455 struct dpif_flow_stats *stats)
2458 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2459 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2461 stats->n_packets = 0;
2464 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2465 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2468 /* Logs information about a packet that was recently lost in 'ch' (in
2471 report_loss(struct dpif_linux *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2472 uint32_t handler_id)
2474 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2477 if (VLOG_DROP_WARN(&rl)) {
2482 if (ch->last_poll != LLONG_MIN) {
2483 ds_put_format(&s, " (last polled %lld ms ago)",
2484 time_msec() - ch->last_poll);
2487 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2488 dpif_name(&dpif->dpif), ch_idx, handler_id);