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 *, 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_linux *dpif, struct netdev *netdev,
627 odp_port_t *port_nop)
629 const struct netdev_tunnel_config *tnl_cfg;
630 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
631 const char *name = netdev_vport_get_dpif_port(netdev,
632 namebuf, sizeof namebuf);
633 const char *type = netdev_get_type(netdev);
634 struct dpif_linux_vport request, reply;
636 uint64_t options_stub[64 / 8];
637 struct ofpbuf options;
638 struct nl_sock **socksp = NULL;
639 uint32_t *upcall_pids;
642 if (dpif->handlers) {
643 socksp = vport_create_socksp(dpif->n_handlers, &error);
649 dpif_linux_vport_init(&request);
650 request.cmd = OVS_VPORT_CMD_NEW;
651 request.dp_ifindex = dpif->dp_ifindex;
652 request.type = netdev_to_ovs_vport_type(netdev);
653 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
654 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
655 "unsupported type `%s'",
656 dpif_name(&dpif->dpif), name, type);
657 vport_del_socksp(socksp, dpif->n_handlers);
662 if (request.type == OVS_VPORT_TYPE_NETDEV) {
663 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
666 tnl_cfg = netdev_get_tunnel_config(netdev);
667 if (tnl_cfg && tnl_cfg->dst_port != 0) {
668 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
669 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
670 ntohs(tnl_cfg->dst_port));
671 request.options = ofpbuf_data(&options);
672 request.options_len = ofpbuf_size(&options);
675 request.port_no = *port_nop;
676 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
677 request.n_upcall_pids = dpif->n_handlers;
678 request.upcall_pids = upcall_pids;
680 error = dpif_linux_vport_transact(&request, &reply, &buf);
682 *port_nop = reply.port_no;
684 if (error == EBUSY && *port_nop != ODPP_NONE) {
685 VLOG_INFO("%s: requested port %"PRIu32" is in use",
686 dpif_name(&dpif->dpif), *port_nop);
689 vport_del_socksp(socksp, dpif->n_handlers);
694 error = vport_add_channels(dpif, *port_nop, socksp);
696 VLOG_INFO("%s: could not add channel for port %s",
697 dpif_name(&dpif->dpif), name);
699 /* Delete the port. */
700 dpif_linux_vport_init(&request);
701 request.cmd = OVS_VPORT_CMD_DEL;
702 request.dp_ifindex = dpif->dp_ifindex;
703 request.port_no = *port_nop;
704 dpif_linux_vport_transact(&request, NULL, NULL);
705 vport_del_socksp(socksp, dpif->n_handlers);
719 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
720 odp_port_t *port_nop)
722 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
725 fat_rwlock_wrlock(&dpif->upcall_lock);
726 error = dpif_linux_port_add__(dpif, netdev, port_nop);
727 fat_rwlock_unlock(&dpif->upcall_lock);
733 dpif_linux_port_del__(struct dpif_linux *dpif, odp_port_t port_no)
735 struct dpif_linux_vport vport;
738 dpif_linux_vport_init(&vport);
739 vport.cmd = OVS_VPORT_CMD_DEL;
740 vport.dp_ifindex = dpif->dp_ifindex;
741 vport.port_no = port_no;
742 error = dpif_linux_vport_transact(&vport, NULL, NULL);
744 vport_del_channels(dpif, port_no);
750 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
752 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
755 fat_rwlock_wrlock(&dpif->upcall_lock);
756 error = dpif_linux_port_del__(dpif, port_no);
757 fat_rwlock_unlock(&dpif->upcall_lock);
763 dpif_linux_port_query__(const struct dpif_linux *dpif, odp_port_t port_no,
764 const char *port_name, struct dpif_port *dpif_port)
766 struct dpif_linux_vport request;
767 struct dpif_linux_vport reply;
771 dpif_linux_vport_init(&request);
772 request.cmd = OVS_VPORT_CMD_GET;
773 request.dp_ifindex = dpif->dp_ifindex;
774 request.port_no = port_no;
775 request.name = port_name;
777 error = dpif_linux_vport_transact(&request, &reply, &buf);
779 if (reply.dp_ifindex != request.dp_ifindex) {
780 /* A query by name reported that 'port_name' is in some datapath
781 * other than 'dpif', but the caller wants to know about 'dpif'. */
783 } else if (dpif_port) {
784 dpif_port->name = xstrdup(reply.name);
785 dpif_port->type = xstrdup(get_vport_type(&reply));
786 dpif_port->port_no = reply.port_no;
794 dpif_linux_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
795 struct dpif_port *dpif_port)
797 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
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 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
808 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
812 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
815 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
816 uint32_t port_idx = odp_to_u32(port_no);
819 fat_rwlock_rdlock(&dpif->upcall_lock);
820 if (dpif->handlers) {
821 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
822 * channel, since it is not heavily loaded. */
823 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
824 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
826 pid = nl_sock_pid(h->channels[idx].sock);
828 fat_rwlock_unlock(&dpif->upcall_lock);
834 dpif_linux_flow_flush(struct dpif *dpif_)
836 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
837 struct dpif_linux_flow flow;
839 dpif_linux_flow_init(&flow);
840 flow.cmd = OVS_FLOW_CMD_DEL;
841 flow.dp_ifindex = dpif->dp_ifindex;
842 return dpif_linux_flow_transact(&flow, NULL, NULL);
845 struct dpif_linux_port_state {
851 dpif_linux_port_dump_start__(const struct dpif_linux *dpif,
852 struct nl_dump *dump)
854 struct dpif_linux_vport request;
857 dpif_linux_vport_init(&request);
858 request.cmd = OVS_VPORT_CMD_GET;
859 request.dp_ifindex = dpif->dp_ifindex;
861 buf = ofpbuf_new(1024);
862 dpif_linux_vport_to_ofpbuf(&request, buf);
863 nl_dump_start(dump, NETLINK_GENERIC, buf);
868 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
870 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
871 struct dpif_linux_port_state *state;
873 *statep = state = xmalloc(sizeof *state);
874 dpif_linux_port_dump_start__(dpif, &state->dump);
876 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
881 dpif_linux_port_dump_next__(const struct dpif_linux *dpif, struct nl_dump *dump,
882 struct dpif_linux_vport *vport,
883 struct ofpbuf *buffer)
888 if (!nl_dump_next(dump, &buf, buffer)) {
892 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
894 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
895 dpif_name(&dpif->dpif), ovs_strerror(error));
901 dpif_linux_port_dump_next(const struct dpif *dpif_, void *state_,
902 struct dpif_port *dpif_port)
904 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
905 struct dpif_linux_port_state *state = state_;
906 struct dpif_linux_vport vport;
909 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport,
914 dpif_port->name = CONST_CAST(char *, vport.name);
915 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
916 dpif_port->port_no = vport.port_no;
921 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
923 struct dpif_linux_port_state *state = state_;
924 int error = nl_dump_done(&state->dump);
926 ofpbuf_uninit(&state->buf);
932 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
934 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
936 /* Lazily create the Netlink socket to listen for notifications. */
937 if (!dpif->port_notifier) {
938 struct nl_sock *sock;
941 error = nl_sock_create(NETLINK_GENERIC, &sock);
946 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
948 nl_sock_destroy(sock);
951 dpif->port_notifier = sock;
953 /* We have no idea of the current state so report that everything
959 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
960 uint64_t buf_stub[4096 / 8];
964 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
965 error = nl_sock_recv(dpif->port_notifier, &buf, false);
967 struct dpif_linux_vport vport;
969 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
971 if (vport.dp_ifindex == dpif->dp_ifindex
972 && (vport.cmd == OVS_VPORT_CMD_NEW
973 || vport.cmd == OVS_VPORT_CMD_DEL
974 || vport.cmd == OVS_VPORT_CMD_SET)) {
975 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
976 dpif->dpif.full_name, vport.name, vport.cmd);
977 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
978 dpif->refresh_channels = true;
980 *devnamep = xstrdup(vport.name);
985 } else if (error != EAGAIN) {
986 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
987 ovs_strerror(error));
988 nl_sock_drain(dpif->port_notifier);
1000 dpif_linux_port_poll_wait(const struct dpif *dpif_)
1002 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1004 if (dpif->port_notifier) {
1005 nl_sock_wait(dpif->port_notifier, POLLIN);
1007 poll_immediate_wake();
1012 dpif_linux_flow_get__(const struct dpif_linux *dpif,
1013 const struct nlattr *key, size_t key_len,
1014 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1016 struct dpif_linux_flow request;
1018 dpif_linux_flow_init(&request);
1019 request.cmd = OVS_FLOW_CMD_GET;
1020 request.dp_ifindex = dpif->dp_ifindex;
1022 request.key_len = key_len;
1023 return dpif_linux_flow_transact(&request, reply, bufp);
1027 dpif_linux_flow_get(const struct dpif *dpif_,
1028 const struct nlattr *key, size_t key_len,
1029 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1031 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1032 struct dpif_linux_flow reply;
1036 error = dpif_linux_flow_get__(dpif, key, key_len, &reply, &buf);
1039 dpif_linux_flow_get_stats(&reply, stats);
1042 ofpbuf_set_data(buf, CONST_CAST(struct nlattr *, reply.actions));
1043 ofpbuf_set_size(buf, reply.actions_len);
1053 dpif_linux_init_flow_put(struct dpif_linux *dpif, const struct dpif_flow_put *put,
1054 struct dpif_linux_flow *request)
1056 static const struct nlattr dummy_action;
1058 dpif_linux_flow_init(request);
1059 request->cmd = (put->flags & DPIF_FP_CREATE
1060 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1061 request->dp_ifindex = dpif->dp_ifindex;
1062 request->key = put->key;
1063 request->key_len = put->key_len;
1064 request->mask = put->mask;
1065 request->mask_len = put->mask_len;
1066 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1067 request->actions = (put->actions
1069 : CONST_CAST(struct nlattr *, &dummy_action));
1070 request->actions_len = put->actions_len;
1071 if (put->flags & DPIF_FP_ZERO_STATS) {
1072 request->clear = true;
1074 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1078 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
1080 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1081 struct dpif_linux_flow request, reply;
1085 dpif_linux_init_flow_put(dpif, put, &request);
1086 error = dpif_linux_flow_transact(&request,
1087 put->stats ? &reply : NULL,
1088 put->stats ? &buf : NULL);
1089 if (!error && put->stats) {
1090 dpif_linux_flow_get_stats(&reply, put->stats);
1097 dpif_linux_init_flow_del(struct dpif_linux *dpif, const struct dpif_flow_del *del,
1098 struct dpif_linux_flow *request)
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 *dpif = dpif_linux_cast(dpif_);
1111 struct dpif_linux_flow request, reply;
1115 dpif_linux_init_flow_del(dpif, del, &request);
1116 error = dpif_linux_flow_transact(&request,
1117 del->stats ? &reply : NULL,
1118 del->stats ? &buf : NULL);
1119 if (!error && del->stats) {
1120 dpif_linux_flow_get_stats(&reply, del->stats);
1126 struct dpif_linux_flow_state {
1127 struct dpif_linux_flow flow;
1128 struct dpif_flow_stats stats;
1129 struct ofpbuf buffer; /* Always used to store flows. */
1130 struct ofpbuf *tmp; /* Used if kernel does not supply actions. */
1133 struct dpif_linux_flow_iter {
1134 struct nl_dump dump;
1139 dpif_linux_flow_dump_state_init(void **statep)
1141 struct dpif_linux_flow_state *state;
1143 *statep = state = xmalloc(sizeof *state);
1144 ofpbuf_init(&state->buffer, NL_DUMP_BUFSIZE);
1149 dpif_linux_flow_dump_state_uninit(void *state_)
1151 struct dpif_linux_flow_state *state = state_;
1153 ofpbuf_uninit(&state->buffer);
1154 ofpbuf_delete(state->tmp);
1159 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **iterp)
1161 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1162 struct dpif_linux_flow_iter *iter;
1163 struct dpif_linux_flow request;
1166 *iterp = iter = xmalloc(sizeof *iter);
1168 dpif_linux_flow_init(&request);
1169 request.cmd = OVS_FLOW_CMD_GET;
1170 request.dp_ifindex = dpif->dp_ifindex;
1172 buf = ofpbuf_new(1024);
1173 dpif_linux_flow_to_ofpbuf(&request, buf);
1174 nl_dump_start(&iter->dump, NETLINK_GENERIC, buf);
1176 atomic_init(&iter->status, 0);
1182 dpif_linux_flow_dump_next(const struct dpif *dpif_, void *iter_, void *state_,
1183 const struct nlattr **key, size_t *key_len,
1184 const struct nlattr **mask, size_t *mask_len,
1185 const struct nlattr **actions, size_t *actions_len,
1186 const struct dpif_flow_stats **stats)
1188 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1189 struct dpif_linux_flow_iter *iter = iter_;
1190 struct dpif_linux_flow_state *state = state_;
1195 ofpbuf_delete(state->tmp);
1198 if (!nl_dump_next(&iter->dump, &buf, &state->buffer)) {
1202 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1204 atomic_store(&iter->status, error);
1208 if (actions && !state->flow.actions) {
1209 error = dpif_linux_flow_get__(dpif, state->flow.key,
1210 state->flow.key_len,
1211 &state->flow, &state->tmp);
1212 if (error == ENOENT) {
1213 VLOG_DBG("dumped flow disappeared on get");
1215 VLOG_WARN("error fetching dumped flow: %s",
1216 ovs_strerror(error));
1222 *actions = state->flow.actions;
1223 *actions_len = state->flow.actions_len;
1226 *key = state->flow.key;
1227 *key_len = state->flow.key_len;
1230 *mask = state->flow.mask;
1231 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1234 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1235 *stats = &state->stats;
1241 dpif_linux_flow_dump_next_may_destroy_keys(void *state_)
1243 struct dpif_linux_flow_state *state = state_;
1245 return ofpbuf_size(&state->buffer) ? false : true;
1249 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1251 struct dpif_linux_flow_iter *iter = iter_;
1253 unsigned int nl_status = nl_dump_done(&iter->dump);
1255 atomic_read(&iter->status, &dump_status);
1257 return dump_status ? dump_status : nl_status;
1261 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1264 struct ovs_header *k_exec;
1267 ofpbuf_prealloc_tailroom(buf, (64
1268 + ofpbuf_size(d_exec->packet)
1269 + ODP_KEY_METADATA_SIZE
1270 + d_exec->actions_len));
1272 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1273 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1275 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1276 k_exec->dp_ifindex = dp_ifindex;
1278 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1279 ofpbuf_data(d_exec->packet),
1280 ofpbuf_size(d_exec->packet));
1282 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1283 odp_key_from_pkt_metadata(buf, &d_exec->md);
1284 nl_msg_end_nested(buf, key_ofs);
1286 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1287 d_exec->actions, d_exec->actions_len);
1291 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1293 uint64_t request_stub[1024 / 8];
1294 struct ofpbuf request;
1297 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1298 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1299 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1300 ofpbuf_uninit(&request);
1306 dpif_linux_execute(struct dpif *dpif_, struct dpif_execute *execute)
1308 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1310 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1316 dpif_linux_operate__(struct dpif_linux *dpif, struct dpif_op **ops, size_t n_ops)
1320 struct nl_transaction txn;
1322 struct ofpbuf request;
1323 uint64_t request_stub[1024 / 8];
1325 struct ofpbuf reply;
1326 uint64_t reply_stub[1024 / 8];
1329 struct nl_transaction *txnsp[MAX_OPS];
1332 ovs_assert(n_ops <= MAX_OPS);
1333 for (i = 0; i < n_ops; i++) {
1334 struct op_auxdata *aux = &auxes[i];
1335 struct dpif_op *op = ops[i];
1336 struct dpif_flow_put *put;
1337 struct dpif_flow_del *del;
1338 struct dpif_execute *execute;
1339 struct dpif_linux_flow flow;
1341 ofpbuf_use_stub(&aux->request,
1342 aux->request_stub, sizeof aux->request_stub);
1343 aux->txn.request = &aux->request;
1345 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1346 aux->txn.reply = NULL;
1349 case DPIF_OP_FLOW_PUT:
1350 put = &op->u.flow_put;
1351 dpif_linux_init_flow_put(dpif, put, &flow);
1353 flow.nlmsg_flags |= NLM_F_ECHO;
1354 aux->txn.reply = &aux->reply;
1356 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1359 case DPIF_OP_FLOW_DEL:
1360 del = &op->u.flow_del;
1361 dpif_linux_init_flow_del(dpif, del, &flow);
1363 flow.nlmsg_flags |= NLM_F_ECHO;
1364 aux->txn.reply = &aux->reply;
1366 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1369 case DPIF_OP_EXECUTE:
1370 execute = &op->u.execute;
1371 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1380 for (i = 0; i < n_ops; i++) {
1381 txnsp[i] = &auxes[i].txn;
1383 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1385 for (i = 0; i < n_ops; i++) {
1386 struct op_auxdata *aux = &auxes[i];
1387 struct nl_transaction *txn = &auxes[i].txn;
1388 struct dpif_op *op = ops[i];
1389 struct dpif_flow_put *put;
1390 struct dpif_flow_del *del;
1392 op->error = txn->error;
1395 case DPIF_OP_FLOW_PUT:
1396 put = &op->u.flow_put;
1399 struct dpif_linux_flow reply;
1401 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1404 dpif_linux_flow_get_stats(&reply, put->stats);
1409 memset(put->stats, 0, sizeof *put->stats);
1414 case DPIF_OP_FLOW_DEL:
1415 del = &op->u.flow_del;
1418 struct dpif_linux_flow reply;
1420 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1423 dpif_linux_flow_get_stats(&reply, del->stats);
1428 memset(del->stats, 0, sizeof *del->stats);
1433 case DPIF_OP_EXECUTE:
1440 ofpbuf_uninit(&aux->request);
1441 ofpbuf_uninit(&aux->reply);
1446 dpif_linux_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1448 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1451 size_t chunk = MIN(n_ops, MAX_OPS);
1452 dpif_linux_operate__(dpif, ops, chunk);
1458 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1459 * currently in 'dpif' in the kernel, by adding a new set of channels for
1460 * any kernel vport that lacks one and deleting any channels that have no
1461 * backing kernel vports. */
1463 dpif_linux_refresh_channels(struct dpif_linux *dpif, uint32_t n_handlers)
1465 unsigned long int *keep_channels;
1466 struct dpif_linux_vport vport;
1467 size_t keep_channels_nbits;
1468 struct nl_dump dump;
1469 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1474 if (dpif->n_handlers != n_handlers) {
1475 destroy_all_channels(dpif);
1476 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1477 for (i = 0; i < n_handlers; i++) {
1478 struct dpif_handler *handler = &dpif->handlers[i];
1480 handler->epoll_fd = epoll_create(10);
1481 if (handler->epoll_fd < 0) {
1484 for (j = 0; j < i; j++) {
1485 close(dpif->handlers[j].epoll_fd);
1487 free(dpif->handlers);
1488 dpif->handlers = NULL;
1493 dpif->n_handlers = n_handlers;
1496 for (i = 0; i < n_handlers; i++) {
1497 struct dpif_handler *handler = &dpif->handlers[i];
1499 handler->event_offset = handler->n_events = 0;
1502 keep_channels_nbits = dpif->uc_array_size;
1503 keep_channels = bitmap_allocate(keep_channels_nbits);
1505 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1506 dpif_linux_port_dump_start__(dpif, &dump);
1507 while (!dpif_linux_port_dump_next__(dpif, &dump, &vport, &buf)) {
1508 uint32_t port_no = odp_to_u32(vport.port_no);
1509 uint32_t *upcall_pids = NULL;
1512 if (port_no >= dpif->uc_array_size
1513 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1514 struct nl_sock **socksp = vport_create_socksp(dpif->n_handlers,
1521 error = vport_add_channels(dpif, vport.port_no, socksp);
1523 VLOG_INFO("%s: could not add channels for port %s",
1524 dpif_name(&dpif->dpif), vport.name);
1525 vport_del_socksp(socksp, dpif->n_handlers);
1529 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1533 /* Configure the vport to deliver misses to 'sock'. */
1534 if (vport.upcall_pids[0] == 0
1535 || vport.n_upcall_pids != dpif->n_handlers
1536 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1538 struct dpif_linux_vport vport_request;
1540 dpif_linux_vport_init(&vport_request);
1541 vport_request.cmd = OVS_VPORT_CMD_SET;
1542 vport_request.dp_ifindex = dpif->dp_ifindex;
1543 vport_request.port_no = vport.port_no;
1544 vport_request.n_upcall_pids = dpif->n_handlers;
1545 vport_request.upcall_pids = upcall_pids;
1546 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1548 VLOG_WARN_RL(&error_rl,
1549 "%s: failed to set upcall pid on port: %s",
1550 dpif_name(&dpif->dpif), ovs_strerror(error));
1552 if (error != ENODEV && error != ENOENT) {
1555 /* The vport isn't really there, even though the dump says
1556 * it is. Probably we just hit a race after a port
1563 if (port_no < keep_channels_nbits) {
1564 bitmap_set1(keep_channels, port_no);
1571 vport_del_channels(dpif, vport.port_no);
1573 nl_dump_done(&dump);
1574 ofpbuf_uninit(&buf);
1576 /* Discard any saved channels that we didn't reuse. */
1577 for (i = 0; i < keep_channels_nbits; i++) {
1578 if (!bitmap_is_set(keep_channels, i)) {
1579 vport_del_channels(dpif, u32_to_odp(i));
1582 free(keep_channels);
1588 dpif_linux_recv_set__(struct dpif_linux *dpif, bool enable)
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_linux *dpif, uint32_t handler_id,
1704 struct dpif_upcall *upcall, struct ofpbuf *buf)
1706 struct dpif_handler *handler;
1709 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
1713 handler = &dpif->handlers[handler_id];
1714 if (handler->event_offset >= handler->n_events) {
1717 handler->event_offset = handler->n_events = 0;
1720 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
1721 dpif->uc_array_size, 0);
1722 } while (retval < 0 && errno == EINTR);
1724 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1725 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1726 } else if (retval > 0) {
1727 handler->n_events = retval;
1731 while (handler->event_offset < handler->n_events) {
1732 int idx = handler->epoll_events[handler->event_offset].data.u32;
1733 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
1735 handler->event_offset++;
1741 if (++read_tries > 50) {
1745 error = nl_sock_recv(ch->sock, buf, false);
1746 if (error == ENOBUFS) {
1747 /* ENOBUFS typically means that we've received so many
1748 * packets that the buffer overflowed. Try again
1749 * immediately because there's almost certainly a packet
1750 * waiting for us. */
1751 report_loss(dpif, ch, idx, handler_id);
1755 ch->last_poll = time_msec();
1757 if (error == EAGAIN) {
1763 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1764 if (!error && dp_ifindex == dpif->dp_ifindex) {
1776 dpif_linux_recv(struct dpif *dpif_, uint32_t handler_id,
1777 struct dpif_upcall *upcall, struct ofpbuf *buf)
1779 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1782 fat_rwlock_rdlock(&dpif->upcall_lock);
1783 error = dpif_linux_recv__(dpif, handler_id, upcall, buf);
1784 fat_rwlock_unlock(&dpif->upcall_lock);
1790 dpif_linux_recv_wait(struct dpif *dpif_, uint32_t handler_id)
1792 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1794 fat_rwlock_rdlock(&dpif->upcall_lock);
1795 if (dpif->handlers && handler_id < dpif->n_handlers) {
1796 struct dpif_handler *handler = &dpif->handlers[handler_id];
1798 poll_fd_wait(handler->epoll_fd, POLLIN);
1800 fat_rwlock_unlock(&dpif->upcall_lock);
1804 dpif_linux_recv_purge(struct dpif *dpif_)
1806 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1808 fat_rwlock_wrlock(&dpif->upcall_lock);
1809 if (dpif->handlers) {
1812 for (i = 0; i < dpif->uc_array_size; i++ ) {
1813 if (!dpif->handlers[0].channels[i].sock) {
1817 for (j = 0; j < dpif->n_handlers; j++) {
1818 nl_sock_drain(dpif->handlers[j].channels[i].sock);
1822 fat_rwlock_unlock(&dpif->upcall_lock);
1825 const struct dpif_class dpif_linux_class = {
1827 dpif_linux_enumerate,
1834 dpif_linux_get_stats,
1835 dpif_linux_port_add,
1836 dpif_linux_port_del,
1837 dpif_linux_port_query_by_number,
1838 dpif_linux_port_query_by_name,
1839 dpif_linux_port_get_pid,
1840 dpif_linux_port_dump_start,
1841 dpif_linux_port_dump_next,
1842 dpif_linux_port_dump_done,
1843 dpif_linux_port_poll,
1844 dpif_linux_port_poll_wait,
1845 dpif_linux_flow_get,
1846 dpif_linux_flow_put,
1847 dpif_linux_flow_del,
1848 dpif_linux_flow_flush,
1849 dpif_linux_flow_dump_state_init,
1850 dpif_linux_flow_dump_start,
1851 dpif_linux_flow_dump_next,
1852 dpif_linux_flow_dump_next_may_destroy_keys,
1853 dpif_linux_flow_dump_done,
1854 dpif_linux_flow_dump_state_uninit,
1857 dpif_linux_recv_set,
1858 dpif_linux_handlers_set,
1859 dpif_linux_queue_to_priority,
1861 dpif_linux_recv_wait,
1862 dpif_linux_recv_purge,
1866 dpif_linux_init(void)
1868 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1871 if (ovsthread_once_start(&once)) {
1872 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1873 &ovs_datapath_family);
1875 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1876 "The Open vSwitch kernel module is probably not loaded.",
1877 OVS_DATAPATH_FAMILY);
1880 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1883 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1886 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1887 &ovs_packet_family);
1890 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1891 &ovs_vport_mcgroup);
1894 ovsthread_once_done(&once);
1901 dpif_linux_is_internal_device(const char *name)
1903 struct dpif_linux_vport reply;
1907 error = dpif_linux_vport_get(name, &reply, &buf);
1910 } else if (error != ENODEV && error != ENOENT) {
1911 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1912 name, ovs_strerror(error));
1915 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1918 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1919 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1920 * positive errno value.
1922 * 'vport' will contain pointers into 'buf', so the caller should not free
1923 * 'buf' while 'vport' is still in use. */
1925 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1926 const struct ofpbuf *buf)
1928 static const struct nl_policy ovs_vport_policy[] = {
1929 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1930 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1931 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1932 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
1933 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1935 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1938 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1939 struct ovs_header *ovs_header;
1940 struct nlmsghdr *nlmsg;
1941 struct genlmsghdr *genl;
1944 dpif_linux_vport_init(vport);
1946 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1947 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1948 genl = ofpbuf_try_pull(&b, sizeof *genl);
1949 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1950 if (!nlmsg || !genl || !ovs_header
1951 || nlmsg->nlmsg_type != ovs_vport_family
1952 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1953 ARRAY_SIZE(ovs_vport_policy))) {
1957 vport->cmd = genl->cmd;
1958 vport->dp_ifindex = ovs_header->dp_ifindex;
1959 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1960 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1961 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1962 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1963 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
1964 / (sizeof *vport->upcall_pids);
1965 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1968 if (a[OVS_VPORT_ATTR_STATS]) {
1969 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1971 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1972 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1973 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1978 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1979 * followed by Netlink attributes corresponding to 'vport'. */
1981 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1984 struct ovs_header *ovs_header;
1986 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1987 vport->cmd, OVS_VPORT_VERSION);
1989 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1990 ovs_header->dp_ifindex = vport->dp_ifindex;
1992 if (vport->port_no != ODPP_NONE) {
1993 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1996 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1997 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2001 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2004 if (vport->upcall_pids) {
2005 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2007 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2011 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2012 vport->stats, sizeof *vport->stats);
2015 if (vport->options) {
2016 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2017 vport->options, vport->options_len);
2021 /* Clears 'vport' to "empty" values. */
2023 dpif_linux_vport_init(struct dpif_linux_vport *vport)
2025 memset(vport, 0, sizeof *vport);
2026 vport->port_no = ODPP_NONE;
2029 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2030 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2031 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2032 * result of the command is expected to be an ovs_vport also, which is decoded
2033 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2034 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2036 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
2037 struct dpif_linux_vport *reply,
2038 struct ofpbuf **bufp)
2040 struct ofpbuf *request_buf;
2043 ovs_assert((reply != NULL) == (bufp != NULL));
2045 error = dpif_linux_init();
2049 dpif_linux_vport_init(reply);
2054 request_buf = ofpbuf_new(1024);
2055 dpif_linux_vport_to_ofpbuf(request, request_buf);
2056 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2057 ofpbuf_delete(request_buf);
2061 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
2064 dpif_linux_vport_init(reply);
2065 ofpbuf_delete(*bufp);
2072 /* Obtains information about the kernel vport named 'name' and stores it into
2073 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2074 * longer needed ('reply' will contain pointers into '*bufp'). */
2076 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
2077 struct ofpbuf **bufp)
2079 struct dpif_linux_vport request;
2081 dpif_linux_vport_init(&request);
2082 request.cmd = OVS_VPORT_CMD_GET;
2083 request.name = name;
2085 return dpif_linux_vport_transact(&request, reply, bufp);
2088 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2089 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2090 * positive errno value.
2092 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2093 * while 'dp' is still in use. */
2095 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
2097 static const struct nl_policy ovs_datapath_policy[] = {
2098 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2099 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2101 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2102 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2106 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2107 struct ovs_header *ovs_header;
2108 struct nlmsghdr *nlmsg;
2109 struct genlmsghdr *genl;
2112 dpif_linux_dp_init(dp);
2114 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2115 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2116 genl = ofpbuf_try_pull(&b, sizeof *genl);
2117 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2118 if (!nlmsg || !genl || !ovs_header
2119 || nlmsg->nlmsg_type != ovs_datapath_family
2120 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2121 ARRAY_SIZE(ovs_datapath_policy))) {
2125 dp->cmd = genl->cmd;
2126 dp->dp_ifindex = ovs_header->dp_ifindex;
2127 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2128 if (a[OVS_DP_ATTR_STATS]) {
2129 /* Can't use structure assignment because Netlink doesn't ensure
2130 * sufficient alignment for 64-bit members. */
2131 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
2135 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2136 /* Can't use structure assignment because Netlink doesn't ensure
2137 * sufficient alignment for 64-bit members. */
2138 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
2139 sizeof dp->megaflow_stats);
2145 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2147 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
2149 struct ovs_header *ovs_header;
2151 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2152 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2153 OVS_DATAPATH_VERSION);
2155 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2156 ovs_header->dp_ifindex = dp->dp_ifindex;
2159 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2162 if (dp->upcall_pid) {
2163 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2166 if (dp->user_features) {
2167 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2170 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2173 /* Clears 'dp' to "empty" values. */
2175 dpif_linux_dp_init(struct dpif_linux_dp *dp)
2177 memset(dp, 0, sizeof *dp);
2178 dp->megaflow_stats.n_masks = UINT32_MAX;
2179 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
2183 dpif_linux_dp_dump_start(struct nl_dump *dump)
2185 struct dpif_linux_dp request;
2188 dpif_linux_dp_init(&request);
2189 request.cmd = OVS_DP_CMD_GET;
2191 buf = ofpbuf_new(1024);
2192 dpif_linux_dp_to_ofpbuf(&request, buf);
2193 nl_dump_start(dump, NETLINK_GENERIC, buf);
2197 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2198 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2199 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2200 * result of the command is expected to be of the same form, which is decoded
2201 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2202 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2204 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
2205 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
2207 struct ofpbuf *request_buf;
2210 ovs_assert((reply != NULL) == (bufp != NULL));
2212 request_buf = ofpbuf_new(1024);
2213 dpif_linux_dp_to_ofpbuf(request, request_buf);
2214 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2215 ofpbuf_delete(request_buf);
2218 dpif_linux_dp_init(reply);
2220 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
2223 ofpbuf_delete(*bufp);
2230 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2231 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2232 * will contain pointers into '*bufp'). */
2234 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2235 struct ofpbuf **bufp)
2237 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2238 struct dpif_linux_dp request;
2240 dpif_linux_dp_init(&request);
2241 request.cmd = OVS_DP_CMD_GET;
2242 request.dp_ifindex = dpif->dp_ifindex;
2244 return dpif_linux_dp_transact(&request, reply, bufp);
2247 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2248 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2249 * positive errno value.
2251 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2252 * while 'flow' is still in use. */
2254 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2255 const struct ofpbuf *buf)
2257 static const struct nl_policy ovs_flow_policy[] = {
2258 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2259 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2260 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2261 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2263 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2264 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2265 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2268 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2269 struct ovs_header *ovs_header;
2270 struct nlmsghdr *nlmsg;
2271 struct genlmsghdr *genl;
2274 dpif_linux_flow_init(flow);
2276 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2277 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2278 genl = ofpbuf_try_pull(&b, sizeof *genl);
2279 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2280 if (!nlmsg || !genl || !ovs_header
2281 || nlmsg->nlmsg_type != ovs_flow_family
2282 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2283 ARRAY_SIZE(ovs_flow_policy))) {
2287 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2288 flow->dp_ifindex = ovs_header->dp_ifindex;
2289 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2290 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2292 if (a[OVS_FLOW_ATTR_MASK]) {
2293 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2294 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2296 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2297 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2298 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2300 if (a[OVS_FLOW_ATTR_STATS]) {
2301 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2303 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2304 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2306 if (a[OVS_FLOW_ATTR_USED]) {
2307 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2312 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2313 * followed by Netlink attributes corresponding to 'flow'. */
2315 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2318 struct ovs_header *ovs_header;
2320 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2321 NLM_F_REQUEST | flow->nlmsg_flags,
2322 flow->cmd, OVS_FLOW_VERSION);
2324 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2325 ovs_header->dp_ifindex = flow->dp_ifindex;
2327 if (flow->key_len) {
2328 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2331 if (flow->mask_len) {
2332 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2335 if (flow->actions || flow->actions_len) {
2336 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2337 flow->actions, flow->actions_len);
2340 /* We never need to send these to the kernel. */
2341 ovs_assert(!flow->stats);
2342 ovs_assert(!flow->tcp_flags);
2343 ovs_assert(!flow->used);
2346 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2350 /* Clears 'flow' to "empty" values. */
2352 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2354 memset(flow, 0, sizeof *flow);
2357 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2358 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2359 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2360 * result of the command is expected to be a flow also, which is decoded and
2361 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2362 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2364 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2365 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2367 struct ofpbuf *request_buf;
2370 ovs_assert((reply != NULL) == (bufp != NULL));
2373 request->nlmsg_flags |= NLM_F_ECHO;
2376 request_buf = ofpbuf_new(1024);
2377 dpif_linux_flow_to_ofpbuf(request, request_buf);
2378 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2379 ofpbuf_delete(request_buf);
2383 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2386 dpif_linux_flow_init(reply);
2387 ofpbuf_delete(*bufp);
2395 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2396 struct dpif_flow_stats *stats)
2399 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2400 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2402 stats->n_packets = 0;
2405 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2406 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2409 /* Logs information about a packet that was recently lost in 'ch' (in
2412 report_loss(struct dpif_linux *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2413 uint32_t handler_id)
2415 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2418 if (VLOG_DROP_WARN(&rl)) {
2423 if (ch->last_poll != LLONG_MIN) {
2424 ds_put_format(&s, " (last polled %lld ms ago)",
2425 time_msec() - ch->last_poll);
2428 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2429 dpif_name(&dpif->dpif), ch_idx, handler_id);