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 *, struct dpif_channel *, uint32_t ch_idx,
162 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 *, uint32_t n_handlers);
184 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
186 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
187 const struct ofpbuf *);
189 static struct dpif_linux *
190 dpif_linux_cast(const struct dpif *dpif)
192 dpif_assert_class(dpif, &dpif_linux_class);
193 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
197 dpif_linux_enumerate(struct sset *all_dps)
200 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
201 struct ofpbuf msg, buf;
204 error = dpif_linux_init();
209 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
210 dpif_linux_dp_dump_start(&dump);
211 while (nl_dump_next(&dump, &msg, &buf)) {
212 struct dpif_linux_dp dp;
214 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
215 sset_add(all_dps, dp.name);
219 return nl_dump_done(&dump);
223 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
224 bool create, struct dpif **dpifp)
226 struct dpif_linux_dp dp_request, dp;
231 error = dpif_linux_init();
236 /* Create or look up datapath. */
237 dpif_linux_dp_init(&dp_request);
239 dp_request.cmd = OVS_DP_CMD_NEW;
241 dp_request.upcall_pid = &upcall_pid;
243 /* Use OVS_DP_CMD_SET to report user features */
244 dp_request.cmd = OVS_DP_CMD_SET;
246 dp_request.name = name;
247 dp_request.user_features |= OVS_DP_F_UNALIGNED;
248 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
249 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
254 error = open_dpif(&dp, dpifp);
260 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
262 struct dpif_linux *dpif;
264 dpif = xzalloc(sizeof *dpif);
265 dpif->port_notifier = NULL;
266 fat_rwlock_init(&dpif->upcall_lock);
268 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
269 dp->dp_ifindex, dp->dp_ifindex);
271 dpif->dp_ifindex = dp->dp_ifindex;
272 *dpifp = &dpif->dpif;
277 /* Destroys the netlink sockets pointed by the elements in 'socksp'
278 * and frees the 'socksp'. */
280 vport_del_socksp(struct nl_sock **socksp, uint32_t n_socks)
284 for (i = 0; i < n_socks; i++) {
285 nl_sock_destroy(socksp[i]);
291 /* Creates an array of netlink sockets. Returns an array of the
292 * corresponding pointers. Records the error in 'error'. */
293 static struct nl_sock **
294 vport_create_socksp(uint32_t n_socks, int *error)
296 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
299 for (i = 0; i < n_socks; i++) {
300 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
309 vport_del_socksp(socksp, n_socks);
314 /* Given the array of pointers to netlink sockets 'socksp', returns
315 * the array of corresponding pids. If the 'socksp' is NULL, returns
316 * a single-element array of value 0. */
318 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
323 pids = xzalloc(sizeof *pids);
327 pids = xzalloc(n_socks * sizeof *pids);
328 for (i = 0; i < n_socks; i++) {
329 pids[i] = nl_sock_pid(socksp[i]);
336 /* Given the port number 'port_idx', extracts the pids of netlink sockets
337 * associated to the port and assigns it to 'upcall_pids'. */
339 vport_get_pids(struct dpif_linux *dpif, uint32_t port_idx,
340 uint32_t **upcall_pids)
345 /* Since the nl_sock can only be assigned in either all
346 * or none "dpif->handlers" channels, the following check
348 if (!dpif->handlers[0].channels[port_idx].sock) {
352 pids = xzalloc(dpif->n_handlers * sizeof *pids);
354 for (i = 0; i < dpif->n_handlers; i++) {
355 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
364 vport_add_channels(struct dpif_linux *dpif, odp_port_t port_no,
365 struct nl_sock **socksp)
367 struct epoll_event event;
368 uint32_t port_idx = odp_to_u32(port_no);
372 if (dpif->handlers == NULL) {
376 /* We assume that the datapath densely chooses port numbers, which can
377 * therefore be used as an index into 'channels' and 'epoll_events' of
378 * 'dpif->handler'. */
379 if (port_idx >= dpif->uc_array_size) {
380 uint32_t new_size = port_idx + 1;
382 if (new_size > MAX_PORTS) {
383 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
384 dpif_name(&dpif->dpif), port_no);
388 for (i = 0; i < dpif->n_handlers; i++) {
389 struct dpif_handler *handler = &dpif->handlers[i];
391 handler->channels = xrealloc(handler->channels,
392 new_size * sizeof *handler->channels);
394 for (j = dpif->uc_array_size; j < new_size; j++) {
395 handler->channels[j].sock = NULL;
398 handler->epoll_events = xrealloc(handler->epoll_events,
399 new_size * sizeof *handler->epoll_events);
402 dpif->uc_array_size = new_size;
405 memset(&event, 0, sizeof event);
406 event.events = EPOLLIN;
407 event.data.u32 = port_idx;
409 for (i = 0; i < dpif->n_handlers; i++) {
410 struct dpif_handler *handler = &dpif->handlers[i];
412 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
417 dpif->handlers[i].channels[port_idx].sock = socksp[i];
418 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
424 for (j = 0; j < i; j++) {
425 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
426 nl_sock_fd(socksp[j]), NULL);
427 dpif->handlers[j].channels[port_idx].sock = NULL;
434 vport_del_channels(struct dpif_linux *dpif, odp_port_t port_no)
436 uint32_t port_idx = odp_to_u32(port_no);
439 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
443 /* Since the sock can only be assigned in either all or none
444 * of "dpif->handlers" channels, the following check would
446 if (!dpif->handlers[0].channels[port_idx].sock) {
450 for (i = 0; i < dpif->n_handlers; i++) {
451 struct dpif_handler *handler = &dpif->handlers[i];
453 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
454 nl_sock_fd(handler->channels[port_idx].sock), NULL);
455 nl_sock_destroy(handler->channels[port_idx].sock);
456 handler->channels[port_idx].sock = NULL;
457 handler->event_offset = handler->n_events = 0;
462 destroy_all_channels(struct dpif_linux *dpif)
466 if (!dpif->handlers) {
470 for (i = 0; i < dpif->uc_array_size; i++ ) {
471 struct dpif_linux_vport vport_request;
472 uint32_t upcall_pids = 0;
474 /* Since the sock can only be assigned in either all or none
475 * of "dpif->handlers" channels, the following check would
477 if (!dpif->handlers[0].channels[i].sock) {
481 /* Turn off upcalls. */
482 dpif_linux_vport_init(&vport_request);
483 vport_request.cmd = OVS_VPORT_CMD_SET;
484 vport_request.dp_ifindex = dpif->dp_ifindex;
485 vport_request.port_no = u32_to_odp(i);
486 vport_request.upcall_pids = &upcall_pids;
487 dpif_linux_vport_transact(&vport_request, NULL, NULL);
489 vport_del_channels(dpif, u32_to_odp(i));
492 for (i = 0; i < dpif->n_handlers; i++) {
493 struct dpif_handler *handler = &dpif->handlers[i];
495 close(handler->epoll_fd);
496 free(handler->epoll_events);
497 free(handler->channels);
500 free(dpif->handlers);
501 dpif->handlers = NULL;
502 dpif->n_handlers = 0;
503 dpif->uc_array_size = 0;
507 dpif_linux_close(struct dpif *dpif_)
509 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
511 nl_sock_destroy(dpif->port_notifier);
513 fat_rwlock_wrlock(&dpif->upcall_lock);
514 destroy_all_channels(dpif);
515 fat_rwlock_unlock(&dpif->upcall_lock);
517 fat_rwlock_destroy(&dpif->upcall_lock);
522 dpif_linux_destroy(struct dpif *dpif_)
524 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
525 struct dpif_linux_dp dp;
527 dpif_linux_dp_init(&dp);
528 dp.cmd = OVS_DP_CMD_DEL;
529 dp.dp_ifindex = dpif->dp_ifindex;
530 return dpif_linux_dp_transact(&dp, NULL, NULL);
534 dpif_linux_run(struct dpif *dpif_)
536 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
538 if (dpif->refresh_channels) {
539 dpif->refresh_channels = false;
540 fat_rwlock_wrlock(&dpif->upcall_lock);
541 dpif_linux_refresh_channels(dpif_, dpif->n_handlers);
542 fat_rwlock_unlock(&dpif->upcall_lock);
547 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
549 struct dpif_linux_dp dp;
553 error = dpif_linux_dp_get(dpif_, &dp, &buf);
555 stats->n_hit = dp.stats.n_hit;
556 stats->n_missed = dp.stats.n_missed;
557 stats->n_lost = dp.stats.n_lost;
558 stats->n_flows = dp.stats.n_flows;
559 stats->n_masks = dp.megaflow_stats.n_masks;
560 stats->n_mask_hit = dp.megaflow_stats.n_mask_hit;
567 get_vport_type(const struct dpif_linux_vport *vport)
569 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
571 switch (vport->type) {
572 case OVS_VPORT_TYPE_NETDEV: {
573 const char *type = netdev_get_type_from_name(vport->name);
575 return type ? type : "system";
578 case OVS_VPORT_TYPE_INTERNAL:
581 case OVS_VPORT_TYPE_GRE:
584 case OVS_VPORT_TYPE_GRE64:
587 case OVS_VPORT_TYPE_VXLAN:
590 case OVS_VPORT_TYPE_LISP:
593 case OVS_VPORT_TYPE_UNSPEC:
594 case __OVS_VPORT_TYPE_MAX:
598 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
599 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
603 static enum ovs_vport_type
604 netdev_to_ovs_vport_type(const struct netdev *netdev)
606 const char *type = netdev_get_type(netdev);
608 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
609 return OVS_VPORT_TYPE_NETDEV;
610 } else if (!strcmp(type, "internal")) {
611 return OVS_VPORT_TYPE_INTERNAL;
612 } else if (strstr(type, "gre64")) {
613 return OVS_VPORT_TYPE_GRE64;
614 } else if (strstr(type, "gre")) {
615 return OVS_VPORT_TYPE_GRE;
616 } else if (!strcmp(type, "vxlan")) {
617 return OVS_VPORT_TYPE_VXLAN;
618 } else if (!strcmp(type, "lisp")) {
619 return OVS_VPORT_TYPE_LISP;
621 return OVS_VPORT_TYPE_UNSPEC;
626 dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
627 odp_port_t *port_nop)
629 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
630 const struct netdev_tunnel_config *tnl_cfg;
631 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
632 const char *name = netdev_vport_get_dpif_port(netdev,
633 namebuf, sizeof namebuf);
634 const char *type = netdev_get_type(netdev);
635 struct dpif_linux_vport request, reply;
637 uint64_t options_stub[64 / 8];
638 struct ofpbuf options;
639 struct nl_sock **socksp = NULL;
640 uint32_t *upcall_pids;
643 if (dpif->handlers) {
644 socksp = vport_create_socksp(dpif->n_handlers, &error);
650 dpif_linux_vport_init(&request);
651 request.cmd = OVS_VPORT_CMD_NEW;
652 request.dp_ifindex = dpif->dp_ifindex;
653 request.type = netdev_to_ovs_vport_type(netdev);
654 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
655 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
656 "unsupported type `%s'",
657 dpif_name(dpif_), name, type);
658 vport_del_socksp(socksp, dpif->n_handlers);
663 if (request.type == OVS_VPORT_TYPE_NETDEV) {
664 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
667 tnl_cfg = netdev_get_tunnel_config(netdev);
668 if (tnl_cfg && tnl_cfg->dst_port != 0) {
669 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
670 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
671 ntohs(tnl_cfg->dst_port));
672 request.options = ofpbuf_data(&options);
673 request.options_len = ofpbuf_size(&options);
676 request.port_no = *port_nop;
677 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
678 request.n_upcall_pids = dpif->n_handlers;
679 request.upcall_pids = upcall_pids;
681 error = dpif_linux_vport_transact(&request, &reply, &buf);
683 *port_nop = reply.port_no;
685 if (error == EBUSY && *port_nop != ODPP_NONE) {
686 VLOG_INFO("%s: requested port %"PRIu32" is in use",
687 dpif_name(dpif_), *port_nop);
690 vport_del_socksp(socksp, dpif->n_handlers);
695 error = vport_add_channels(dpif, *port_nop, socksp);
697 VLOG_INFO("%s: could not add channel for port %s",
698 dpif_name(dpif_), name);
700 /* Delete the port. */
701 dpif_linux_vport_init(&request);
702 request.cmd = OVS_VPORT_CMD_DEL;
703 request.dp_ifindex = dpif->dp_ifindex;
704 request.port_no = *port_nop;
705 dpif_linux_vport_transact(&request, NULL, NULL);
706 vport_del_socksp(socksp, dpif->n_handlers);
720 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
721 odp_port_t *port_nop)
723 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
726 fat_rwlock_wrlock(&dpif->upcall_lock);
727 error = dpif_linux_port_add__(dpif_, netdev, port_nop);
728 fat_rwlock_unlock(&dpif->upcall_lock);
734 dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
736 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
737 struct dpif_linux_vport vport;
740 dpif_linux_vport_init(&vport);
741 vport.cmd = OVS_VPORT_CMD_DEL;
742 vport.dp_ifindex = dpif->dp_ifindex;
743 vport.port_no = port_no;
744 error = dpif_linux_vport_transact(&vport, NULL, NULL);
746 vport_del_channels(dpif, port_no);
752 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
754 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
757 fat_rwlock_wrlock(&dpif->upcall_lock);
758 error = dpif_linux_port_del__(dpif_, port_no);
759 fat_rwlock_unlock(&dpif->upcall_lock);
765 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
766 const char *port_name, struct dpif_port *dpif_port)
768 struct dpif_linux_vport request;
769 struct dpif_linux_vport reply;
773 dpif_linux_vport_init(&request);
774 request.cmd = OVS_VPORT_CMD_GET;
775 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
776 request.port_no = port_no;
777 request.name = port_name;
779 error = dpif_linux_vport_transact(&request, &reply, &buf);
781 if (reply.dp_ifindex != request.dp_ifindex) {
782 /* A query by name reported that 'port_name' is in some datapath
783 * other than 'dpif', but the caller wants to know about 'dpif'. */
785 } else if (dpif_port) {
786 dpif_port->name = xstrdup(reply.name);
787 dpif_port->type = xstrdup(get_vport_type(&reply));
788 dpif_port->port_no = reply.port_no;
796 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
797 struct dpif_port *dpif_port)
799 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
803 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
804 struct dpif_port *dpif_port)
806 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
810 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
813 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
814 uint32_t port_idx = odp_to_u32(port_no);
817 fat_rwlock_rdlock(&dpif->upcall_lock);
818 if (dpif->handlers) {
819 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
820 * channel, since it is not heavily loaded. */
821 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
822 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
824 pid = nl_sock_pid(h->channels[idx].sock);
826 fat_rwlock_unlock(&dpif->upcall_lock);
832 dpif_linux_flow_flush(struct dpif *dpif_)
834 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
835 struct dpif_linux_flow flow;
837 dpif_linux_flow_init(&flow);
838 flow.cmd = OVS_FLOW_CMD_DEL;
839 flow.dp_ifindex = dpif->dp_ifindex;
840 return dpif_linux_flow_transact(&flow, NULL, NULL);
843 struct dpif_linux_port_state {
849 dpif_linux_port_dump_start__(const struct dpif *dpif_, struct nl_dump *dump)
851 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
852 struct dpif_linux_vport request;
855 dpif_linux_vport_init(&request);
856 request.cmd = OVS_VPORT_CMD_GET;
857 request.dp_ifindex = dpif->dp_ifindex;
859 buf = ofpbuf_new(1024);
860 dpif_linux_vport_to_ofpbuf(&request, buf);
861 nl_dump_start(dump, NETLINK_GENERIC, buf);
866 dpif_linux_port_dump_start(const struct dpif *dpif, void **statep)
868 struct dpif_linux_port_state *state;
870 *statep = state = xmalloc(sizeof *state);
871 dpif_linux_port_dump_start__(dpif, &state->dump);
873 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
878 dpif_linux_port_dump_next__(const struct dpif *dpif_, struct nl_dump *dump,
879 struct dpif_linux_vport *vport,
880 struct ofpbuf *buffer)
882 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
886 if (!nl_dump_next(dump, &buf, buffer)) {
890 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
892 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
893 dpif_name(&dpif->dpif), ovs_strerror(error));
899 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
900 struct dpif_port *dpif_port)
902 struct dpif_linux_port_state *state = state_;
903 struct dpif_linux_vport vport;
906 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport,
911 dpif_port->name = CONST_CAST(char *, vport.name);
912 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
913 dpif_port->port_no = vport.port_no;
918 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
920 struct dpif_linux_port_state *state = state_;
921 int error = nl_dump_done(&state->dump);
923 ofpbuf_uninit(&state->buf);
929 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
931 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
933 /* Lazily create the Netlink socket to listen for notifications. */
934 if (!dpif->port_notifier) {
935 struct nl_sock *sock;
938 error = nl_sock_create(NETLINK_GENERIC, &sock);
943 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
945 nl_sock_destroy(sock);
948 dpif->port_notifier = sock;
950 /* We have no idea of the current state so report that everything
956 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
957 uint64_t buf_stub[4096 / 8];
961 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
962 error = nl_sock_recv(dpif->port_notifier, &buf, false);
964 struct dpif_linux_vport vport;
966 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
968 if (vport.dp_ifindex == dpif->dp_ifindex
969 && (vport.cmd == OVS_VPORT_CMD_NEW
970 || vport.cmd == OVS_VPORT_CMD_DEL
971 || vport.cmd == OVS_VPORT_CMD_SET)) {
972 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
973 dpif->dpif.full_name, vport.name, vport.cmd);
974 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
975 dpif->refresh_channels = true;
977 *devnamep = xstrdup(vport.name);
982 } else if (error != EAGAIN) {
983 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
984 ovs_strerror(error));
985 nl_sock_drain(dpif->port_notifier);
997 dpif_linux_port_poll_wait(const struct dpif *dpif_)
999 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1001 if (dpif->port_notifier) {
1002 nl_sock_wait(dpif->port_notifier, POLLIN);
1004 poll_immediate_wake();
1009 dpif_linux_flow_get__(const struct dpif *dpif_,
1010 const struct nlattr *key, size_t key_len,
1011 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1013 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1014 struct dpif_linux_flow request;
1016 dpif_linux_flow_init(&request);
1017 request.cmd = OVS_FLOW_CMD_GET;
1018 request.dp_ifindex = dpif->dp_ifindex;
1020 request.key_len = key_len;
1021 return dpif_linux_flow_transact(&request, reply, bufp);
1025 dpif_linux_flow_get(const struct dpif *dpif_,
1026 const struct nlattr *key, size_t key_len,
1027 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1029 struct dpif_linux_flow reply;
1033 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
1036 dpif_linux_flow_get_stats(&reply, stats);
1039 ofpbuf_set_data(buf, CONST_CAST(struct nlattr *, reply.actions));
1040 ofpbuf_set_size(buf, reply.actions_len);
1050 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
1051 struct dpif_linux_flow *request)
1053 static const struct nlattr dummy_action;
1055 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1057 dpif_linux_flow_init(request);
1058 request->cmd = (put->flags & DPIF_FP_CREATE
1059 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1060 request->dp_ifindex = dpif->dp_ifindex;
1061 request->key = put->key;
1062 request->key_len = put->key_len;
1063 request->mask = put->mask;
1064 request->mask_len = put->mask_len;
1065 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1066 request->actions = (put->actions
1068 : CONST_CAST(struct nlattr *, &dummy_action));
1069 request->actions_len = put->actions_len;
1070 if (put->flags & DPIF_FP_ZERO_STATS) {
1071 request->clear = true;
1073 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1077 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
1079 struct dpif_linux_flow request, reply;
1083 dpif_linux_init_flow_put(dpif_, put, &request);
1084 error = dpif_linux_flow_transact(&request,
1085 put->stats ? &reply : NULL,
1086 put->stats ? &buf : NULL);
1087 if (!error && put->stats) {
1088 dpif_linux_flow_get_stats(&reply, put->stats);
1095 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
1096 struct dpif_linux_flow *request)
1098 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1100 dpif_linux_flow_init(request);
1101 request->cmd = OVS_FLOW_CMD_DEL;
1102 request->dp_ifindex = dpif->dp_ifindex;
1103 request->key = del->key;
1104 request->key_len = del->key_len;
1108 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
1110 struct dpif_linux_flow request, reply;
1114 dpif_linux_init_flow_del(dpif_, del, &request);
1115 error = dpif_linux_flow_transact(&request,
1116 del->stats ? &reply : NULL,
1117 del->stats ? &buf : NULL);
1118 if (!error && del->stats) {
1119 dpif_linux_flow_get_stats(&reply, del->stats);
1125 struct dpif_linux_flow_state {
1126 struct dpif_linux_flow flow;
1127 struct dpif_flow_stats stats;
1128 struct ofpbuf buffer; /* Always used to store flows. */
1129 struct ofpbuf *tmp; /* Used if kernel does not supply actions. */
1132 struct dpif_linux_flow_iter {
1133 struct nl_dump dump;
1138 dpif_linux_flow_dump_state_init(void **statep)
1140 struct dpif_linux_flow_state *state;
1142 *statep = state = xmalloc(sizeof *state);
1143 ofpbuf_init(&state->buffer, NL_DUMP_BUFSIZE);
1148 dpif_linux_flow_dump_state_uninit(void *state_)
1150 struct dpif_linux_flow_state *state = state_;
1152 ofpbuf_uninit(&state->buffer);
1153 ofpbuf_delete(state->tmp);
1158 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **iterp)
1160 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1161 struct dpif_linux_flow_iter *iter;
1162 struct dpif_linux_flow request;
1165 *iterp = iter = xmalloc(sizeof *iter);
1167 dpif_linux_flow_init(&request);
1168 request.cmd = OVS_FLOW_CMD_GET;
1169 request.dp_ifindex = dpif->dp_ifindex;
1171 buf = ofpbuf_new(1024);
1172 dpif_linux_flow_to_ofpbuf(&request, buf);
1173 nl_dump_start(&iter->dump, NETLINK_GENERIC, buf);
1175 atomic_init(&iter->status, 0);
1181 dpif_linux_flow_dump_next(const struct dpif *dpif_, void *iter_, void *state_,
1182 const struct nlattr **key, size_t *key_len,
1183 const struct nlattr **mask, size_t *mask_len,
1184 const struct nlattr **actions, size_t *actions_len,
1185 const struct dpif_flow_stats **stats)
1187 struct dpif_linux_flow_iter *iter = iter_;
1188 struct dpif_linux_flow_state *state = state_;
1193 ofpbuf_delete(state->tmp);
1196 if (!nl_dump_next(&iter->dump, &buf, &state->buffer)) {
1200 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1202 atomic_store(&iter->status, error);
1206 if (actions && !state->flow.actions) {
1207 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1208 state->flow.key_len,
1209 &state->flow, &state->tmp);
1210 if (error == ENOENT) {
1211 VLOG_DBG("dumped flow disappeared on get");
1213 VLOG_WARN("error fetching dumped flow: %s",
1214 ovs_strerror(error));
1220 *actions = state->flow.actions;
1221 *actions_len = state->flow.actions_len;
1224 *key = state->flow.key;
1225 *key_len = state->flow.key_len;
1228 *mask = state->flow.mask;
1229 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1232 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1233 *stats = &state->stats;
1239 dpif_linux_flow_dump_next_may_destroy_keys(void *state_)
1241 struct dpif_linux_flow_state *state = state_;
1243 return ofpbuf_size(&state->buffer) ? false : true;
1247 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1249 struct dpif_linux_flow_iter *iter = iter_;
1251 unsigned int nl_status = nl_dump_done(&iter->dump);
1253 atomic_read(&iter->status, &dump_status);
1255 return dump_status ? dump_status : nl_status;
1259 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1262 struct ovs_header *k_exec;
1265 ofpbuf_prealloc_tailroom(buf, (64
1266 + ofpbuf_size(d_exec->packet)
1267 + ODP_KEY_METADATA_SIZE
1268 + d_exec->actions_len));
1270 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1271 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1273 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1274 k_exec->dp_ifindex = dp_ifindex;
1276 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1277 ofpbuf_data(d_exec->packet),
1278 ofpbuf_size(d_exec->packet));
1280 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1281 odp_key_from_pkt_metadata(buf, &d_exec->md);
1282 nl_msg_end_nested(buf, key_ofs);
1284 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1285 d_exec->actions, d_exec->actions_len);
1289 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1291 uint64_t request_stub[1024 / 8];
1292 struct ofpbuf request;
1295 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1296 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1297 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1298 ofpbuf_uninit(&request);
1304 dpif_linux_execute(struct dpif *dpif_, struct dpif_execute *execute)
1306 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1308 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1314 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1316 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1319 struct nl_transaction txn;
1321 struct ofpbuf request;
1322 uint64_t request_stub[1024 / 8];
1324 struct ofpbuf reply;
1325 uint64_t reply_stub[1024 / 8];
1328 struct nl_transaction *txnsp[MAX_OPS];
1331 ovs_assert(n_ops <= MAX_OPS);
1332 for (i = 0; i < n_ops; i++) {
1333 struct op_auxdata *aux = &auxes[i];
1334 struct dpif_op *op = ops[i];
1335 struct dpif_flow_put *put;
1336 struct dpif_flow_del *del;
1337 struct dpif_execute *execute;
1338 struct dpif_linux_flow flow;
1340 ofpbuf_use_stub(&aux->request,
1341 aux->request_stub, sizeof aux->request_stub);
1342 aux->txn.request = &aux->request;
1344 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1345 aux->txn.reply = NULL;
1348 case DPIF_OP_FLOW_PUT:
1349 put = &op->u.flow_put;
1350 dpif_linux_init_flow_put(dpif_, put, &flow);
1352 flow.nlmsg_flags |= NLM_F_ECHO;
1353 aux->txn.reply = &aux->reply;
1355 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1358 case DPIF_OP_FLOW_DEL:
1359 del = &op->u.flow_del;
1360 dpif_linux_init_flow_del(dpif_, del, &flow);
1362 flow.nlmsg_flags |= NLM_F_ECHO;
1363 aux->txn.reply = &aux->reply;
1365 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1368 case DPIF_OP_EXECUTE:
1369 execute = &op->u.execute;
1370 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1379 for (i = 0; i < n_ops; i++) {
1380 txnsp[i] = &auxes[i].txn;
1382 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1384 for (i = 0; i < n_ops; i++) {
1385 struct op_auxdata *aux = &auxes[i];
1386 struct nl_transaction *txn = &auxes[i].txn;
1387 struct dpif_op *op = ops[i];
1388 struct dpif_flow_put *put;
1389 struct dpif_flow_del *del;
1391 op->error = txn->error;
1394 case DPIF_OP_FLOW_PUT:
1395 put = &op->u.flow_put;
1398 struct dpif_linux_flow reply;
1400 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1403 dpif_linux_flow_get_stats(&reply, put->stats);
1408 memset(put->stats, 0, sizeof *put->stats);
1413 case DPIF_OP_FLOW_DEL:
1414 del = &op->u.flow_del;
1417 struct dpif_linux_flow reply;
1419 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1422 dpif_linux_flow_get_stats(&reply, del->stats);
1427 memset(del->stats, 0, sizeof *del->stats);
1432 case DPIF_OP_EXECUTE:
1439 ofpbuf_uninit(&aux->request);
1440 ofpbuf_uninit(&aux->reply);
1445 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1448 size_t chunk = MIN(n_ops, MAX_OPS);
1449 dpif_linux_operate__(dpif, ops, chunk);
1455 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1456 * currently in 'dpif' in the kernel, by adding a new set of channels for
1457 * any kernel vport that lacks one and deleting any channels that have no
1458 * backing kernel vports. */
1460 dpif_linux_refresh_channels(struct dpif *dpif_, uint32_t n_handlers)
1462 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1463 unsigned long int *keep_channels;
1464 struct dpif_linux_vport vport;
1465 size_t keep_channels_nbits;
1466 struct nl_dump dump;
1467 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1472 if (dpif->n_handlers != n_handlers) {
1473 destroy_all_channels(dpif);
1474 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1475 for (i = 0; i < n_handlers; i++) {
1476 struct dpif_handler *handler = &dpif->handlers[i];
1478 handler->epoll_fd = epoll_create(10);
1479 if (handler->epoll_fd < 0) {
1482 for (j = 0; j < i; j++) {
1483 close(dpif->handlers[j].epoll_fd);
1485 free(dpif->handlers);
1486 dpif->handlers = NULL;
1491 dpif->n_handlers = n_handlers;
1494 for (i = 0; i < n_handlers; i++) {
1495 struct dpif_handler *handler = &dpif->handlers[i];
1497 handler->event_offset = handler->n_events = 0;
1500 keep_channels_nbits = dpif->uc_array_size;
1501 keep_channels = bitmap_allocate(keep_channels_nbits);
1503 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1504 dpif_linux_port_dump_start__(dpif_, &dump);
1505 while (!dpif_linux_port_dump_next__(dpif_, &dump, &vport, &buf)) {
1506 uint32_t port_no = odp_to_u32(vport.port_no);
1507 uint32_t *upcall_pids = NULL;
1510 if (port_no >= dpif->uc_array_size
1511 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1512 struct nl_sock **socksp = vport_create_socksp(dpif->n_handlers,
1519 error = vport_add_channels(dpif, vport.port_no, socksp);
1521 VLOG_INFO("%s: could not add channels for port %s",
1522 dpif_name(dpif_), vport.name);
1523 vport_del_socksp(socksp, dpif->n_handlers);
1527 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1531 /* Configure the vport to deliver misses to 'sock'. */
1532 if (vport.upcall_pids[0] == 0
1533 || vport.n_upcall_pids != dpif->n_handlers
1534 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1536 struct dpif_linux_vport vport_request;
1538 dpif_linux_vport_init(&vport_request);
1539 vport_request.cmd = OVS_VPORT_CMD_SET;
1540 vport_request.dp_ifindex = dpif->dp_ifindex;
1541 vport_request.port_no = vport.port_no;
1542 vport_request.n_upcall_pids = dpif->n_handlers;
1543 vport_request.upcall_pids = upcall_pids;
1544 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1546 VLOG_WARN_RL(&error_rl,
1547 "%s: failed to set upcall pid on port: %s",
1548 dpif_name(&dpif->dpif), ovs_strerror(error));
1550 if (error != ENODEV && error != ENOENT) {
1553 /* The vport isn't really there, even though the dump says
1554 * it is. Probably we just hit a race after a port
1561 if (port_no < keep_channels_nbits) {
1562 bitmap_set1(keep_channels, port_no);
1569 vport_del_channels(dpif, vport.port_no);
1571 nl_dump_done(&dump);
1572 ofpbuf_uninit(&buf);
1574 /* Discard any saved channels that we didn't reuse. */
1575 for (i = 0; i < keep_channels_nbits; i++) {
1576 if (!bitmap_is_set(keep_channels, i)) {
1577 vport_del_channels(dpif, u32_to_odp(i));
1580 free(keep_channels);
1586 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1588 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1590 if ((dpif->handlers != NULL) == enable) {
1592 } else if (!enable) {
1593 destroy_all_channels(dpif);
1596 return dpif_linux_refresh_channels(dpif_, 1);
1601 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1603 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1606 fat_rwlock_wrlock(&dpif->upcall_lock);
1607 error = dpif_linux_recv_set__(dpif_, enable);
1608 fat_rwlock_unlock(&dpif->upcall_lock);
1614 dpif_linux_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1616 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1619 fat_rwlock_wrlock(&dpif->upcall_lock);
1620 if (dpif->handlers) {
1621 error = dpif_linux_refresh_channels(dpif_, n_handlers);
1623 fat_rwlock_unlock(&dpif->upcall_lock);
1629 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1630 uint32_t queue_id, uint32_t *priority)
1632 if (queue_id < 0xf000) {
1633 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1641 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1644 static const struct nl_policy ovs_packet_policy[] = {
1645 /* Always present. */
1646 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1647 .min_len = ETH_HEADER_LEN },
1648 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1650 /* OVS_PACKET_CMD_ACTION only. */
1651 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1654 struct ovs_header *ovs_header;
1655 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1656 struct nlmsghdr *nlmsg;
1657 struct genlmsghdr *genl;
1661 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1663 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1664 genl = ofpbuf_try_pull(&b, sizeof *genl);
1665 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1666 if (!nlmsg || !genl || !ovs_header
1667 || nlmsg->nlmsg_type != ovs_packet_family
1668 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1669 ARRAY_SIZE(ovs_packet_policy))) {
1673 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1674 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1680 /* (Re)set ALL fields of '*upcall' on successful return. */
1681 upcall->type = type;
1682 upcall->key = CONST_CAST(struct nlattr *,
1683 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1684 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1685 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1687 /* Allow overwriting the netlink attribute header without reallocating. */
1688 ofpbuf_use_stub(&upcall->packet,
1689 CONST_CAST(struct nlattr *,
1690 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1691 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1692 sizeof(struct nlattr));
1693 ofpbuf_set_data(&upcall->packet,
1694 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
1695 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
1697 *dp_ifindex = ovs_header->dp_ifindex;
1703 dpif_linux_recv__(struct dpif *dpif_, uint32_t handler_id,
1704 struct dpif_upcall *upcall, struct ofpbuf *buf)
1706 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1707 struct dpif_handler *handler;
1710 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
1714 handler = &dpif->handlers[handler_id];
1715 if (handler->event_offset >= handler->n_events) {
1718 handler->event_offset = handler->n_events = 0;
1721 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
1722 dpif->uc_array_size, 0);
1723 } while (retval < 0 && errno == EINTR);
1725 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1726 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1727 } else if (retval > 0) {
1728 handler->n_events = retval;
1732 while (handler->event_offset < handler->n_events) {
1733 int idx = handler->epoll_events[handler->event_offset].data.u32;
1734 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
1736 handler->event_offset++;
1742 if (++read_tries > 50) {
1746 error = nl_sock_recv(ch->sock, buf, false);
1747 if (error == ENOBUFS) {
1748 /* ENOBUFS typically means that we've received so many
1749 * packets that the buffer overflowed. Try again
1750 * immediately because there's almost certainly a packet
1751 * waiting for us. */
1752 report_loss(dpif_, ch, idx, handler_id);
1756 ch->last_poll = time_msec();
1758 if (error == EAGAIN) {
1764 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1765 if (!error && dp_ifindex == dpif->dp_ifindex) {
1777 dpif_linux_recv(struct dpif *dpif_, uint32_t handler_id,
1778 struct dpif_upcall *upcall, struct ofpbuf *buf)
1780 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1783 fat_rwlock_rdlock(&dpif->upcall_lock);
1784 error = dpif_linux_recv__(dpif_, handler_id, upcall, buf);
1785 fat_rwlock_unlock(&dpif->upcall_lock);
1791 dpif_linux_recv_wait(struct dpif *dpif_, uint32_t handler_id)
1793 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1795 fat_rwlock_rdlock(&dpif->upcall_lock);
1796 if (dpif->handlers && handler_id < dpif->n_handlers) {
1797 struct dpif_handler *handler = &dpif->handlers[handler_id];
1799 poll_fd_wait(handler->epoll_fd, POLLIN);
1801 fat_rwlock_unlock(&dpif->upcall_lock);
1805 dpif_linux_recv_purge(struct dpif *dpif_)
1807 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1809 fat_rwlock_wrlock(&dpif->upcall_lock);
1810 if (dpif->handlers) {
1813 for (i = 0; i < dpif->uc_array_size; i++ ) {
1814 if (!dpif->handlers[0].channels[i].sock) {
1818 for (j = 0; j < dpif->n_handlers; j++) {
1819 nl_sock_drain(dpif->handlers[j].channels[i].sock);
1823 fat_rwlock_unlock(&dpif->upcall_lock);
1826 const struct dpif_class dpif_linux_class = {
1828 dpif_linux_enumerate,
1835 dpif_linux_get_stats,
1836 dpif_linux_port_add,
1837 dpif_linux_port_del,
1838 dpif_linux_port_query_by_number,
1839 dpif_linux_port_query_by_name,
1840 dpif_linux_port_get_pid,
1841 dpif_linux_port_dump_start,
1842 dpif_linux_port_dump_next,
1843 dpif_linux_port_dump_done,
1844 dpif_linux_port_poll,
1845 dpif_linux_port_poll_wait,
1846 dpif_linux_flow_get,
1847 dpif_linux_flow_put,
1848 dpif_linux_flow_del,
1849 dpif_linux_flow_flush,
1850 dpif_linux_flow_dump_state_init,
1851 dpif_linux_flow_dump_start,
1852 dpif_linux_flow_dump_next,
1853 dpif_linux_flow_dump_next_may_destroy_keys,
1854 dpif_linux_flow_dump_done,
1855 dpif_linux_flow_dump_state_uninit,
1858 dpif_linux_recv_set,
1859 dpif_linux_handlers_set,
1860 dpif_linux_queue_to_priority,
1862 dpif_linux_recv_wait,
1863 dpif_linux_recv_purge,
1867 dpif_linux_init(void)
1869 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1872 if (ovsthread_once_start(&once)) {
1873 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1874 &ovs_datapath_family);
1876 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1877 "The Open vSwitch kernel module is probably not loaded.",
1878 OVS_DATAPATH_FAMILY);
1881 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1884 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1887 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1888 &ovs_packet_family);
1891 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1892 &ovs_vport_mcgroup);
1895 ovsthread_once_done(&once);
1902 dpif_linux_is_internal_device(const char *name)
1904 struct dpif_linux_vport reply;
1908 error = dpif_linux_vport_get(name, &reply, &buf);
1911 } else if (error != ENODEV && error != ENOENT) {
1912 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1913 name, ovs_strerror(error));
1916 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1919 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1920 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1921 * positive errno value.
1923 * 'vport' will contain pointers into 'buf', so the caller should not free
1924 * 'buf' while 'vport' is still in use. */
1926 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1927 const struct ofpbuf *buf)
1929 static const struct nl_policy ovs_vport_policy[] = {
1930 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1931 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1932 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1933 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
1934 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1936 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1939 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1940 struct ovs_header *ovs_header;
1941 struct nlmsghdr *nlmsg;
1942 struct genlmsghdr *genl;
1945 dpif_linux_vport_init(vport);
1947 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1948 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1949 genl = ofpbuf_try_pull(&b, sizeof *genl);
1950 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1951 if (!nlmsg || !genl || !ovs_header
1952 || nlmsg->nlmsg_type != ovs_vport_family
1953 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1954 ARRAY_SIZE(ovs_vport_policy))) {
1958 vport->cmd = genl->cmd;
1959 vport->dp_ifindex = ovs_header->dp_ifindex;
1960 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1961 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1962 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1963 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1964 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
1965 / (sizeof *vport->upcall_pids);
1966 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1969 if (a[OVS_VPORT_ATTR_STATS]) {
1970 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1972 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1973 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1974 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1979 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1980 * followed by Netlink attributes corresponding to 'vport'. */
1982 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1985 struct ovs_header *ovs_header;
1987 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1988 vport->cmd, OVS_VPORT_VERSION);
1990 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1991 ovs_header->dp_ifindex = vport->dp_ifindex;
1993 if (vport->port_no != ODPP_NONE) {
1994 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1997 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1998 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2002 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2005 if (vport->upcall_pids) {
2006 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2008 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2012 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2013 vport->stats, sizeof *vport->stats);
2016 if (vport->options) {
2017 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2018 vport->options, vport->options_len);
2022 /* Clears 'vport' to "empty" values. */
2024 dpif_linux_vport_init(struct dpif_linux_vport *vport)
2026 memset(vport, 0, sizeof *vport);
2027 vport->port_no = ODPP_NONE;
2030 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2031 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2032 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2033 * result of the command is expected to be an ovs_vport also, which is decoded
2034 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2035 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2037 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
2038 struct dpif_linux_vport *reply,
2039 struct ofpbuf **bufp)
2041 struct ofpbuf *request_buf;
2044 ovs_assert((reply != NULL) == (bufp != NULL));
2046 error = dpif_linux_init();
2050 dpif_linux_vport_init(reply);
2055 request_buf = ofpbuf_new(1024);
2056 dpif_linux_vport_to_ofpbuf(request, request_buf);
2057 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2058 ofpbuf_delete(request_buf);
2062 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
2065 dpif_linux_vport_init(reply);
2066 ofpbuf_delete(*bufp);
2073 /* Obtains information about the kernel vport named 'name' and stores it into
2074 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2075 * longer needed ('reply' will contain pointers into '*bufp'). */
2077 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
2078 struct ofpbuf **bufp)
2080 struct dpif_linux_vport request;
2082 dpif_linux_vport_init(&request);
2083 request.cmd = OVS_VPORT_CMD_GET;
2084 request.name = name;
2086 return dpif_linux_vport_transact(&request, reply, bufp);
2089 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2090 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2091 * positive errno value.
2093 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2094 * while 'dp' is still in use. */
2096 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
2098 static const struct nl_policy ovs_datapath_policy[] = {
2099 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2100 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2102 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2103 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2107 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2108 struct ovs_header *ovs_header;
2109 struct nlmsghdr *nlmsg;
2110 struct genlmsghdr *genl;
2113 dpif_linux_dp_init(dp);
2115 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2116 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2117 genl = ofpbuf_try_pull(&b, sizeof *genl);
2118 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2119 if (!nlmsg || !genl || !ovs_header
2120 || nlmsg->nlmsg_type != ovs_datapath_family
2121 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2122 ARRAY_SIZE(ovs_datapath_policy))) {
2126 dp->cmd = genl->cmd;
2127 dp->dp_ifindex = ovs_header->dp_ifindex;
2128 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2129 if (a[OVS_DP_ATTR_STATS]) {
2130 /* Can't use structure assignment because Netlink doesn't ensure
2131 * sufficient alignment for 64-bit members. */
2132 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
2136 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2137 /* Can't use structure assignment because Netlink doesn't ensure
2138 * sufficient alignment for 64-bit members. */
2139 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
2140 sizeof dp->megaflow_stats);
2146 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2148 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
2150 struct ovs_header *ovs_header;
2152 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2153 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2154 OVS_DATAPATH_VERSION);
2156 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2157 ovs_header->dp_ifindex = dp->dp_ifindex;
2160 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2163 if (dp->upcall_pid) {
2164 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2167 if (dp->user_features) {
2168 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2171 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2174 /* Clears 'dp' to "empty" values. */
2176 dpif_linux_dp_init(struct dpif_linux_dp *dp)
2178 memset(dp, 0, sizeof *dp);
2179 dp->megaflow_stats.n_masks = UINT32_MAX;
2180 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
2184 dpif_linux_dp_dump_start(struct nl_dump *dump)
2186 struct dpif_linux_dp request;
2189 dpif_linux_dp_init(&request);
2190 request.cmd = OVS_DP_CMD_GET;
2192 buf = ofpbuf_new(1024);
2193 dpif_linux_dp_to_ofpbuf(&request, buf);
2194 nl_dump_start(dump, NETLINK_GENERIC, buf);
2198 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2199 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2200 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2201 * result of the command is expected to be of the same form, which is decoded
2202 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2203 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2205 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
2206 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
2208 struct ofpbuf *request_buf;
2211 ovs_assert((reply != NULL) == (bufp != NULL));
2213 request_buf = ofpbuf_new(1024);
2214 dpif_linux_dp_to_ofpbuf(request, request_buf);
2215 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2216 ofpbuf_delete(request_buf);
2219 dpif_linux_dp_init(reply);
2221 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
2224 ofpbuf_delete(*bufp);
2231 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2232 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2233 * will contain pointers into '*bufp'). */
2235 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2236 struct ofpbuf **bufp)
2238 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2239 struct dpif_linux_dp request;
2241 dpif_linux_dp_init(&request);
2242 request.cmd = OVS_DP_CMD_GET;
2243 request.dp_ifindex = dpif->dp_ifindex;
2245 return dpif_linux_dp_transact(&request, reply, bufp);
2248 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2249 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2250 * positive errno value.
2252 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2253 * while 'flow' is still in use. */
2255 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2256 const struct ofpbuf *buf)
2258 static const struct nl_policy ovs_flow_policy[] = {
2259 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2260 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2261 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2262 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2264 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2265 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2266 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2269 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2270 struct ovs_header *ovs_header;
2271 struct nlmsghdr *nlmsg;
2272 struct genlmsghdr *genl;
2275 dpif_linux_flow_init(flow);
2277 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2278 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2279 genl = ofpbuf_try_pull(&b, sizeof *genl);
2280 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2281 if (!nlmsg || !genl || !ovs_header
2282 || nlmsg->nlmsg_type != ovs_flow_family
2283 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2284 ARRAY_SIZE(ovs_flow_policy))) {
2288 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2289 flow->dp_ifindex = ovs_header->dp_ifindex;
2290 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2291 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2293 if (a[OVS_FLOW_ATTR_MASK]) {
2294 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2295 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2297 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2298 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2299 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2301 if (a[OVS_FLOW_ATTR_STATS]) {
2302 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2304 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2305 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2307 if (a[OVS_FLOW_ATTR_USED]) {
2308 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2313 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2314 * followed by Netlink attributes corresponding to 'flow'. */
2316 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2319 struct ovs_header *ovs_header;
2321 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2322 NLM_F_REQUEST | flow->nlmsg_flags,
2323 flow->cmd, OVS_FLOW_VERSION);
2325 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2326 ovs_header->dp_ifindex = flow->dp_ifindex;
2328 if (flow->key_len) {
2329 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2332 if (flow->mask_len) {
2333 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2336 if (flow->actions || flow->actions_len) {
2337 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2338 flow->actions, flow->actions_len);
2341 /* We never need to send these to the kernel. */
2342 ovs_assert(!flow->stats);
2343 ovs_assert(!flow->tcp_flags);
2344 ovs_assert(!flow->used);
2347 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2351 /* Clears 'flow' to "empty" values. */
2353 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2355 memset(flow, 0, sizeof *flow);
2358 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2359 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2360 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2361 * result of the command is expected to be a flow also, which is decoded and
2362 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2363 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2365 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2366 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2368 struct ofpbuf *request_buf;
2371 ovs_assert((reply != NULL) == (bufp != NULL));
2374 request->nlmsg_flags |= NLM_F_ECHO;
2377 request_buf = ofpbuf_new(1024);
2378 dpif_linux_flow_to_ofpbuf(request, request_buf);
2379 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2380 ofpbuf_delete(request_buf);
2384 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2387 dpif_linux_flow_init(reply);
2388 ofpbuf_delete(*bufp);
2396 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2397 struct dpif_flow_stats *stats)
2400 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2401 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2403 stats->n_packets = 0;
2406 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2407 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2410 /* Logs information about a packet that was recently lost in 'ch' (in
2413 report_loss(struct dpif *dpif_, struct dpif_channel *ch, uint32_t ch_idx,
2414 uint32_t handler_id)
2416 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2419 if (VLOG_DROP_WARN(&rl)) {
2424 if (ch->last_poll != LLONG_MIN) {
2425 ds_put_format(&s, " (last polled %lld ms ago)",
2426 time_msec() - ch->last_poll);
2429 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2430 dpif_name(dpif_), ch_idx, handler_id);