2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "dpif-linux.h"
26 #include <linux/types.h>
27 #include <linux/pkt_sched.h>
31 #include <sys/epoll.h>
36 #include "dpif-provider.h"
37 #include "dynamic-string.h"
39 #include "fat-rwlock.h"
41 #include "netdev-linux.h"
42 #include "netdev-vport.h"
43 #include "netlink-notifier.h"
44 #include "netlink-socket.h"
49 #include "poll-loop.h"
54 #include "unaligned.h"
58 VLOG_DEFINE_THIS_MODULE(dpif_linux);
59 enum { MAX_PORTS = USHRT_MAX };
61 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
62 * missing if we have old headers. */
63 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
65 struct dpif_linux_dp {
66 /* Generic Netlink header. */
69 /* struct ovs_header. */
73 const char *name; /* OVS_DP_ATTR_NAME. */
74 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
75 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
76 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
77 struct ovs_dp_megaflow_stats megaflow_stats;
78 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
81 static void dpif_linux_dp_init(struct dpif_linux_dp *);
82 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
83 const struct ofpbuf *);
84 static void dpif_linux_dp_dump_start(struct nl_dump *);
85 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
86 struct dpif_linux_dp *reply,
87 struct ofpbuf **bufp);
88 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
89 struct ofpbuf **bufp);
91 struct dpif_linux_flow {
92 /* Generic Netlink header. */
95 /* struct ovs_header. */
96 unsigned int nlmsg_flags;
101 * The 'stats' member points to 64-bit data that might only be aligned on
102 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
105 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
106 * the Netlink version of the command, even if actions_len is zero. */
107 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
109 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
111 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
113 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
114 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
115 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
116 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
119 static void dpif_linux_flow_init(struct dpif_linux_flow *);
120 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
121 const struct ofpbuf *);
122 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
124 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
125 struct dpif_linux_flow *reply,
126 struct ofpbuf **bufp);
127 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
128 struct dpif_flow_stats *);
130 /* One of the dpif channels between the kernel and userspace. */
131 struct dpif_channel {
132 struct nl_sock *sock; /* Netlink socket. */
133 long long int last_poll; /* Last time this channel was polled. */
136 struct dpif_handler {
137 struct dpif_channel *channels;/* Array of channels for each handler. */
138 struct epoll_event *epoll_events;
139 int epoll_fd; /* epoll fd that includes channel socks. */
140 int n_events; /* Num events returned by epoll_wait(). */
141 int event_offset; /* Offset into 'epoll_events'. */
144 /* Datapath interface for the openvswitch Linux kernel module. */
149 /* Upcall messages. */
150 struct fat_rwlock upcall_lock;
151 struct dpif_handler *handlers;
152 uint32_t n_handlers; /* Num of upcall handlers. */
153 int uc_array_size; /* Size of 'handler->channels' and */
154 /* 'handler->epoll_events'. */
156 /* Change notification. */
157 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
158 bool refresh_channels;
161 static void report_loss(struct dpif_linux *, struct dpif_channel *,
162 uint32_t ch_idx, uint32_t handler_id);
164 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
166 /* Generic Netlink family numbers for OVS.
168 * Initialized by dpif_linux_init(). */
169 static int ovs_datapath_family;
170 static int ovs_vport_family;
171 static int ovs_flow_family;
172 static int ovs_packet_family;
174 /* Generic Netlink multicast groups for OVS.
176 * Initialized by dpif_linux_init(). */
177 static unsigned int ovs_vport_mcgroup;
179 static int dpif_linux_init(void);
180 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
181 static uint32_t dpif_linux_port_get_pid(const struct dpif *,
182 odp_port_t port_no, uint32_t hash);
183 static int dpif_linux_refresh_channels(struct dpif_linux *,
184 uint32_t n_handlers);
185 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
187 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
188 const struct ofpbuf *);
190 static struct dpif_linux *
191 dpif_linux_cast(const struct dpif *dpif)
193 dpif_assert_class(dpif, &dpif_linux_class);
194 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
198 dpif_linux_enumerate(struct sset *all_dps,
199 const struct dpif_class *dpif_class OVS_UNUSED)
202 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
203 struct ofpbuf msg, buf;
206 error = dpif_linux_init();
211 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
212 dpif_linux_dp_dump_start(&dump);
213 while (nl_dump_next(&dump, &msg, &buf)) {
214 struct dpif_linux_dp dp;
216 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
217 sset_add(all_dps, dp.name);
221 return nl_dump_done(&dump);
225 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
226 bool create, struct dpif **dpifp)
228 struct dpif_linux_dp dp_request, dp;
233 error = dpif_linux_init();
238 /* Create or look up datapath. */
239 dpif_linux_dp_init(&dp_request);
241 dp_request.cmd = OVS_DP_CMD_NEW;
243 dp_request.upcall_pid = &upcall_pid;
245 /* Use OVS_DP_CMD_SET to report user features */
246 dp_request.cmd = OVS_DP_CMD_SET;
248 dp_request.name = name;
249 dp_request.user_features |= OVS_DP_F_UNALIGNED;
250 dp_request.user_features |= OVS_DP_F_VPORT_PIDS;
251 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
256 error = open_dpif(&dp, dpifp);
262 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
264 struct dpif_linux *dpif;
266 dpif = xzalloc(sizeof *dpif);
267 dpif->port_notifier = NULL;
268 fat_rwlock_init(&dpif->upcall_lock);
270 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
271 dp->dp_ifindex, dp->dp_ifindex);
273 dpif->dp_ifindex = dp->dp_ifindex;
274 *dpifp = &dpif->dpif;
279 /* Destroys the netlink sockets pointed by the elements in 'socksp'
280 * and frees the 'socksp'. */
282 vport_del_socksp(struct nl_sock **socksp, uint32_t n_socks)
286 for (i = 0; i < n_socks; i++) {
287 nl_sock_destroy(socksp[i]);
293 /* Creates an array of netlink sockets. Returns an array of the
294 * corresponding pointers. Records the error in 'error'. */
295 static struct nl_sock **
296 vport_create_socksp(uint32_t n_socks, int *error)
298 struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
301 for (i = 0; i < n_socks; i++) {
302 *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
311 vport_del_socksp(socksp, n_socks);
316 /* Given the array of pointers to netlink sockets 'socksp', returns
317 * the array of corresponding pids. If the 'socksp' is NULL, returns
318 * a single-element array of value 0. */
320 vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
325 pids = xzalloc(sizeof *pids);
329 pids = xzalloc(n_socks * sizeof *pids);
330 for (i = 0; i < n_socks; i++) {
331 pids[i] = nl_sock_pid(socksp[i]);
338 /* Given the port number 'port_idx', extracts the pids of netlink sockets
339 * associated to the port and assigns it to 'upcall_pids'. */
341 vport_get_pids(struct dpif_linux *dpif, uint32_t port_idx,
342 uint32_t **upcall_pids)
347 /* Since the nl_sock can only be assigned in either all
348 * or none "dpif->handlers" channels, the following check
350 if (!dpif->handlers[0].channels[port_idx].sock) {
354 pids = xzalloc(dpif->n_handlers * sizeof *pids);
356 for (i = 0; i < dpif->n_handlers; i++) {
357 pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
366 vport_add_channels(struct dpif_linux *dpif, odp_port_t port_no,
367 struct nl_sock **socksp)
369 struct epoll_event event;
370 uint32_t port_idx = odp_to_u32(port_no);
374 if (dpif->handlers == NULL) {
378 /* We assume that the datapath densely chooses port numbers, which can
379 * therefore be used as an index into 'channels' and 'epoll_events' of
380 * 'dpif->handler'. */
381 if (port_idx >= dpif->uc_array_size) {
382 uint32_t new_size = port_idx + 1;
384 if (new_size > MAX_PORTS) {
385 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
386 dpif_name(&dpif->dpif), port_no);
390 for (i = 0; i < dpif->n_handlers; i++) {
391 struct dpif_handler *handler = &dpif->handlers[i];
393 handler->channels = xrealloc(handler->channels,
394 new_size * sizeof *handler->channels);
396 for (j = dpif->uc_array_size; j < new_size; j++) {
397 handler->channels[j].sock = NULL;
400 handler->epoll_events = xrealloc(handler->epoll_events,
401 new_size * sizeof *handler->epoll_events);
404 dpif->uc_array_size = new_size;
407 memset(&event, 0, sizeof event);
408 event.events = EPOLLIN;
409 event.data.u32 = port_idx;
411 for (i = 0; i < dpif->n_handlers; i++) {
412 struct dpif_handler *handler = &dpif->handlers[i];
414 if (epoll_ctl(handler->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
419 dpif->handlers[i].channels[port_idx].sock = socksp[i];
420 dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
426 for (j = 0; j < i; j++) {
427 epoll_ctl(dpif->handlers[j].epoll_fd, EPOLL_CTL_DEL,
428 nl_sock_fd(socksp[j]), NULL);
429 dpif->handlers[j].channels[port_idx].sock = NULL;
436 vport_del_channels(struct dpif_linux *dpif, odp_port_t port_no)
438 uint32_t port_idx = odp_to_u32(port_no);
441 if (!dpif->handlers || port_idx >= dpif->uc_array_size) {
445 /* Since the sock can only be assigned in either all or none
446 * of "dpif->handlers" channels, the following check would
448 if (!dpif->handlers[0].channels[port_idx].sock) {
452 for (i = 0; i < dpif->n_handlers; i++) {
453 struct dpif_handler *handler = &dpif->handlers[i];
455 epoll_ctl(handler->epoll_fd, EPOLL_CTL_DEL,
456 nl_sock_fd(handler->channels[port_idx].sock), NULL);
457 nl_sock_destroy(handler->channels[port_idx].sock);
458 handler->channels[port_idx].sock = NULL;
459 handler->event_offset = handler->n_events = 0;
464 destroy_all_channels(struct dpif_linux *dpif) OVS_REQ_WRLOCK(dpif->upcall_lock)
468 if (!dpif->handlers) {
472 for (i = 0; i < dpif->uc_array_size; i++ ) {
473 struct dpif_linux_vport vport_request;
474 uint32_t upcall_pids = 0;
476 /* Since the sock can only be assigned in either all or none
477 * of "dpif->handlers" channels, the following check would
479 if (!dpif->handlers[0].channels[i].sock) {
483 /* Turn off upcalls. */
484 dpif_linux_vport_init(&vport_request);
485 vport_request.cmd = OVS_VPORT_CMD_SET;
486 vport_request.dp_ifindex = dpif->dp_ifindex;
487 vport_request.port_no = u32_to_odp(i);
488 vport_request.upcall_pids = &upcall_pids;
489 dpif_linux_vport_transact(&vport_request, NULL, NULL);
491 vport_del_channels(dpif, u32_to_odp(i));
494 for (i = 0; i < dpif->n_handlers; i++) {
495 struct dpif_handler *handler = &dpif->handlers[i];
497 close(handler->epoll_fd);
498 free(handler->epoll_events);
499 free(handler->channels);
502 free(dpif->handlers);
503 dpif->handlers = NULL;
504 dpif->n_handlers = 0;
505 dpif->uc_array_size = 0;
509 dpif_linux_close(struct dpif *dpif_)
511 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
513 nl_sock_destroy(dpif->port_notifier);
515 fat_rwlock_wrlock(&dpif->upcall_lock);
516 destroy_all_channels(dpif);
517 fat_rwlock_unlock(&dpif->upcall_lock);
519 fat_rwlock_destroy(&dpif->upcall_lock);
524 dpif_linux_destroy(struct dpif *dpif_)
526 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
527 struct dpif_linux_dp dp;
529 dpif_linux_dp_init(&dp);
530 dp.cmd = OVS_DP_CMD_DEL;
531 dp.dp_ifindex = dpif->dp_ifindex;
532 return dpif_linux_dp_transact(&dp, NULL, NULL);
536 dpif_linux_run(struct dpif *dpif_)
538 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
540 if (dpif->refresh_channels) {
541 dpif->refresh_channels = false;
542 fat_rwlock_wrlock(&dpif->upcall_lock);
543 dpif_linux_refresh_channels(dpif, dpif->n_handlers);
544 fat_rwlock_unlock(&dpif->upcall_lock);
549 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
551 struct dpif_linux_dp dp;
555 error = dpif_linux_dp_get(dpif_, &dp, &buf);
557 stats->n_hit = dp.stats.n_hit;
558 stats->n_missed = dp.stats.n_missed;
559 stats->n_lost = dp.stats.n_lost;
560 stats->n_flows = dp.stats.n_flows;
561 stats->n_masks = dp.megaflow_stats.n_masks;
562 stats->n_mask_hit = dp.megaflow_stats.n_mask_hit;
569 get_vport_type(const struct dpif_linux_vport *vport)
571 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
573 switch (vport->type) {
574 case OVS_VPORT_TYPE_NETDEV: {
575 const char *type = netdev_get_type_from_name(vport->name);
577 return type ? type : "system";
580 case OVS_VPORT_TYPE_INTERNAL:
583 case OVS_VPORT_TYPE_GENEVE:
586 case OVS_VPORT_TYPE_GRE:
589 case OVS_VPORT_TYPE_GRE64:
592 case OVS_VPORT_TYPE_VXLAN:
595 case OVS_VPORT_TYPE_LISP:
598 case OVS_VPORT_TYPE_UNSPEC:
599 case __OVS_VPORT_TYPE_MAX:
603 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
604 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
608 static enum ovs_vport_type
609 netdev_to_ovs_vport_type(const struct netdev *netdev)
611 const char *type = netdev_get_type(netdev);
613 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
614 return OVS_VPORT_TYPE_NETDEV;
615 } else if (!strcmp(type, "internal")) {
616 return OVS_VPORT_TYPE_INTERNAL;
617 } else if (!strcmp(type, "geneve")) {
618 return OVS_VPORT_TYPE_GENEVE;
619 } else if (strstr(type, "gre64")) {
620 return OVS_VPORT_TYPE_GRE64;
621 } else if (strstr(type, "gre")) {
622 return OVS_VPORT_TYPE_GRE;
623 } else if (!strcmp(type, "vxlan")) {
624 return OVS_VPORT_TYPE_VXLAN;
625 } else if (!strcmp(type, "lisp")) {
626 return OVS_VPORT_TYPE_LISP;
628 return OVS_VPORT_TYPE_UNSPEC;
633 dpif_linux_port_add__(struct dpif_linux *dpif, struct netdev *netdev,
634 odp_port_t *port_nop)
635 OVS_REQ_WRLOCK(dpif->upcall_lock)
637 const struct netdev_tunnel_config *tnl_cfg;
638 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
639 const char *name = netdev_vport_get_dpif_port(netdev,
640 namebuf, sizeof namebuf);
641 const char *type = netdev_get_type(netdev);
642 struct dpif_linux_vport request, reply;
644 uint64_t options_stub[64 / 8];
645 struct ofpbuf options;
646 struct nl_sock **socksp = NULL;
647 uint32_t *upcall_pids;
650 if (dpif->handlers) {
651 socksp = vport_create_socksp(dpif->n_handlers, &error);
657 dpif_linux_vport_init(&request);
658 request.cmd = OVS_VPORT_CMD_NEW;
659 request.dp_ifindex = dpif->dp_ifindex;
660 request.type = netdev_to_ovs_vport_type(netdev);
661 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
662 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
663 "unsupported type `%s'",
664 dpif_name(&dpif->dpif), name, type);
665 vport_del_socksp(socksp, dpif->n_handlers);
670 if (request.type == OVS_VPORT_TYPE_NETDEV) {
671 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
674 tnl_cfg = netdev_get_tunnel_config(netdev);
675 if (tnl_cfg && tnl_cfg->dst_port != 0) {
676 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
677 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
678 ntohs(tnl_cfg->dst_port));
679 request.options = ofpbuf_data(&options);
680 request.options_len = ofpbuf_size(&options);
683 request.port_no = *port_nop;
684 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
685 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
686 request.upcall_pids = upcall_pids;
688 error = dpif_linux_vport_transact(&request, &reply, &buf);
690 *port_nop = reply.port_no;
692 if (error == EBUSY && *port_nop != ODPP_NONE) {
693 VLOG_INFO("%s: requested port %"PRIu32" is in use",
694 dpif_name(&dpif->dpif), *port_nop);
697 vport_del_socksp(socksp, dpif->n_handlers);
702 error = vport_add_channels(dpif, *port_nop, socksp);
704 VLOG_INFO("%s: could not add channel for port %s",
705 dpif_name(&dpif->dpif), name);
707 /* Delete the port. */
708 dpif_linux_vport_init(&request);
709 request.cmd = OVS_VPORT_CMD_DEL;
710 request.dp_ifindex = dpif->dp_ifindex;
711 request.port_no = *port_nop;
712 dpif_linux_vport_transact(&request, NULL, NULL);
713 vport_del_socksp(socksp, dpif->n_handlers);
727 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
728 odp_port_t *port_nop)
730 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
733 fat_rwlock_wrlock(&dpif->upcall_lock);
734 error = dpif_linux_port_add__(dpif, netdev, port_nop);
735 fat_rwlock_unlock(&dpif->upcall_lock);
741 dpif_linux_port_del__(struct dpif_linux *dpif, odp_port_t port_no)
742 OVS_REQ_WRLOCK(dpif->upcall_lock)
744 struct dpif_linux_vport vport;
747 dpif_linux_vport_init(&vport);
748 vport.cmd = OVS_VPORT_CMD_DEL;
749 vport.dp_ifindex = dpif->dp_ifindex;
750 vport.port_no = port_no;
751 error = dpif_linux_vport_transact(&vport, NULL, NULL);
753 vport_del_channels(dpif, port_no);
759 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
761 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
764 fat_rwlock_wrlock(&dpif->upcall_lock);
765 error = dpif_linux_port_del__(dpif, port_no);
766 fat_rwlock_unlock(&dpif->upcall_lock);
772 dpif_linux_port_query__(const struct dpif_linux *dpif, odp_port_t port_no,
773 const char *port_name, struct dpif_port *dpif_port)
775 struct dpif_linux_vport request;
776 struct dpif_linux_vport reply;
780 dpif_linux_vport_init(&request);
781 request.cmd = OVS_VPORT_CMD_GET;
782 request.dp_ifindex = dpif->dp_ifindex;
783 request.port_no = port_no;
784 request.name = port_name;
786 error = dpif_linux_vport_transact(&request, &reply, &buf);
788 if (reply.dp_ifindex != request.dp_ifindex) {
789 /* A query by name reported that 'port_name' is in some datapath
790 * other than 'dpif', but the caller wants to know about 'dpif'. */
792 } else if (dpif_port) {
793 dpif_port->name = xstrdup(reply.name);
794 dpif_port->type = xstrdup(get_vport_type(&reply));
795 dpif_port->port_no = reply.port_no;
803 dpif_linux_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
804 struct dpif_port *dpif_port)
806 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
808 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
812 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
813 struct dpif_port *dpif_port)
815 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
817 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
821 dpif_linux_port_get_pid__(const struct dpif_linux *dpif, odp_port_t port_no,
823 OVS_REQ_RDLOCK(dpif->upcall_lock)
825 uint32_t port_idx = odp_to_u32(port_no);
828 if (dpif->handlers) {
829 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
830 * channel, since it is not heavily loaded. */
831 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
832 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
834 pid = nl_sock_pid(h->channels[idx].sock);
841 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
844 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
847 fat_rwlock_rdlock(&dpif->upcall_lock);
848 ret = dpif_linux_port_get_pid__(dpif, port_no, hash);
849 fat_rwlock_unlock(&dpif->upcall_lock);
855 dpif_linux_flow_flush(struct dpif *dpif_)
857 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
858 struct dpif_linux_flow flow;
860 dpif_linux_flow_init(&flow);
861 flow.cmd = OVS_FLOW_CMD_DEL;
862 flow.dp_ifindex = dpif->dp_ifindex;
863 return dpif_linux_flow_transact(&flow, NULL, NULL);
866 struct dpif_linux_port_state {
872 dpif_linux_port_dump_start__(const struct dpif_linux *dpif,
873 struct nl_dump *dump)
875 struct dpif_linux_vport request;
878 dpif_linux_vport_init(&request);
879 request.cmd = OVS_VPORT_CMD_GET;
880 request.dp_ifindex = dpif->dp_ifindex;
882 buf = ofpbuf_new(1024);
883 dpif_linux_vport_to_ofpbuf(&request, buf);
884 nl_dump_start(dump, NETLINK_GENERIC, buf);
889 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
891 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
892 struct dpif_linux_port_state *state;
894 *statep = state = xmalloc(sizeof *state);
895 dpif_linux_port_dump_start__(dpif, &state->dump);
897 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
902 dpif_linux_port_dump_next__(const struct dpif_linux *dpif, struct nl_dump *dump,
903 struct dpif_linux_vport *vport,
904 struct ofpbuf *buffer)
909 if (!nl_dump_next(dump, &buf, buffer)) {
913 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
915 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
916 dpif_name(&dpif->dpif), ovs_strerror(error));
922 dpif_linux_port_dump_next(const struct dpif *dpif_, void *state_,
923 struct dpif_port *dpif_port)
925 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
926 struct dpif_linux_port_state *state = state_;
927 struct dpif_linux_vport vport;
930 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport,
935 dpif_port->name = CONST_CAST(char *, vport.name);
936 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
937 dpif_port->port_no = vport.port_no;
942 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
944 struct dpif_linux_port_state *state = state_;
945 int error = nl_dump_done(&state->dump);
947 ofpbuf_uninit(&state->buf);
953 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
955 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
957 /* Lazily create the Netlink socket to listen for notifications. */
958 if (!dpif->port_notifier) {
959 struct nl_sock *sock;
962 error = nl_sock_create(NETLINK_GENERIC, &sock);
967 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
969 nl_sock_destroy(sock);
972 dpif->port_notifier = sock;
974 /* We have no idea of the current state so report that everything
980 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
981 uint64_t buf_stub[4096 / 8];
985 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
986 error = nl_sock_recv(dpif->port_notifier, &buf, false);
988 struct dpif_linux_vport vport;
990 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
992 if (vport.dp_ifindex == dpif->dp_ifindex
993 && (vport.cmd == OVS_VPORT_CMD_NEW
994 || vport.cmd == OVS_VPORT_CMD_DEL
995 || vport.cmd == OVS_VPORT_CMD_SET)) {
996 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
997 dpif->dpif.full_name, vport.name, vport.cmd);
998 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
999 dpif->refresh_channels = true;
1001 *devnamep = xstrdup(vport.name);
1002 ofpbuf_uninit(&buf);
1006 } else if (error != EAGAIN) {
1007 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1008 ovs_strerror(error));
1009 nl_sock_drain(dpif->port_notifier);
1013 ofpbuf_uninit(&buf);
1021 dpif_linux_port_poll_wait(const struct dpif *dpif_)
1023 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1025 if (dpif->port_notifier) {
1026 nl_sock_wait(dpif->port_notifier, POLLIN);
1028 poll_immediate_wake();
1033 dpif_linux_flow_get__(const struct dpif_linux *dpif,
1034 const struct nlattr *key, size_t key_len,
1035 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1037 struct dpif_linux_flow request;
1039 dpif_linux_flow_init(&request);
1040 request.cmd = OVS_FLOW_CMD_GET;
1041 request.dp_ifindex = dpif->dp_ifindex;
1043 request.key_len = key_len;
1044 return dpif_linux_flow_transact(&request, reply, bufp);
1048 dpif_linux_flow_get(const struct dpif *dpif_,
1049 const struct nlattr *key, size_t key_len,
1050 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1052 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1053 struct dpif_linux_flow reply;
1057 error = dpif_linux_flow_get__(dpif, key, key_len, &reply, &buf);
1060 dpif_linux_flow_get_stats(&reply, stats);
1063 ofpbuf_set_data(buf, CONST_CAST(struct nlattr *, reply.actions));
1064 ofpbuf_set_size(buf, reply.actions_len);
1074 dpif_linux_init_flow_put(struct dpif_linux *dpif, const struct dpif_flow_put *put,
1075 struct dpif_linux_flow *request)
1077 static const struct nlattr dummy_action;
1079 dpif_linux_flow_init(request);
1080 request->cmd = (put->flags & DPIF_FP_CREATE
1081 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1082 request->dp_ifindex = dpif->dp_ifindex;
1083 request->key = put->key;
1084 request->key_len = put->key_len;
1085 request->mask = put->mask;
1086 request->mask_len = put->mask_len;
1087 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1088 request->actions = (put->actions
1090 : CONST_CAST(struct nlattr *, &dummy_action));
1091 request->actions_len = put->actions_len;
1092 if (put->flags & DPIF_FP_ZERO_STATS) {
1093 request->clear = true;
1095 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1099 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
1101 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1102 struct dpif_linux_flow request, reply;
1106 dpif_linux_init_flow_put(dpif, put, &request);
1107 error = dpif_linux_flow_transact(&request,
1108 put->stats ? &reply : NULL,
1109 put->stats ? &buf : NULL);
1110 if (!error && put->stats) {
1111 dpif_linux_flow_get_stats(&reply, put->stats);
1118 dpif_linux_init_flow_del(struct dpif_linux *dpif, const struct dpif_flow_del *del,
1119 struct dpif_linux_flow *request)
1121 dpif_linux_flow_init(request);
1122 request->cmd = OVS_FLOW_CMD_DEL;
1123 request->dp_ifindex = dpif->dp_ifindex;
1124 request->key = del->key;
1125 request->key_len = del->key_len;
1129 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
1131 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1132 struct dpif_linux_flow request, reply;
1136 dpif_linux_init_flow_del(dpif, del, &request);
1137 error = dpif_linux_flow_transact(&request,
1138 del->stats ? &reply : NULL,
1139 del->stats ? &buf : NULL);
1140 if (!error && del->stats) {
1141 dpif_linux_flow_get_stats(&reply, del->stats);
1147 struct dpif_linux_flow_dump {
1148 struct dpif_flow_dump up;
1149 struct nl_dump nl_dump;
1153 static struct dpif_linux_flow_dump *
1154 dpif_linux_flow_dump_cast(struct dpif_flow_dump *dump)
1156 return CONTAINER_OF(dump, struct dpif_linux_flow_dump, up);
1159 static struct dpif_flow_dump *
1160 dpif_linux_flow_dump_create(const struct dpif *dpif_)
1162 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1163 struct dpif_linux_flow_dump *dump;
1164 struct dpif_linux_flow request;
1167 dump = xmalloc(sizeof *dump);
1168 dpif_flow_dump_init(&dump->up, dpif_);
1170 dpif_linux_flow_init(&request);
1171 request.cmd = OVS_FLOW_CMD_GET;
1172 request.dp_ifindex = dpif->dp_ifindex;
1174 buf = ofpbuf_new(1024);
1175 dpif_linux_flow_to_ofpbuf(&request, buf);
1176 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1178 atomic_init(&dump->status, 0);
1184 dpif_linux_flow_dump_destroy(struct dpif_flow_dump *dump_)
1186 struct dpif_linux_flow_dump *dump = dpif_linux_flow_dump_cast(dump_);
1187 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1190 atomic_read(&dump->status, &dump_status);
1192 return dump_status ? dump_status : nl_status;
1195 struct dpif_linux_flow_dump_thread {
1196 struct dpif_flow_dump_thread up;
1197 struct dpif_linux_flow_dump *dump;
1198 struct dpif_linux_flow flow;
1199 struct dpif_flow_stats stats;
1200 struct ofpbuf nl_flows; /* Always used to store flows. */
1201 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1204 static struct dpif_linux_flow_dump_thread *
1205 dpif_linux_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1207 return CONTAINER_OF(thread, struct dpif_linux_flow_dump_thread, up);
1210 static struct dpif_flow_dump_thread *
1211 dpif_linux_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1213 struct dpif_linux_flow_dump *dump = dpif_linux_flow_dump_cast(dump_);
1214 struct dpif_linux_flow_dump_thread *thread;
1216 thread = xmalloc(sizeof *thread);
1217 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1218 thread->dump = dump;
1219 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1220 thread->nl_actions = NULL;
1226 dpif_linux_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1228 struct dpif_linux_flow_dump_thread *thread
1229 = dpif_linux_flow_dump_thread_cast(thread_);
1231 ofpbuf_uninit(&thread->nl_flows);
1232 ofpbuf_delete(thread->nl_actions);
1237 dpif_linux_flow_to_dpif_flow(struct dpif_flow *dpif_flow,
1238 struct dpif_linux_flow *linux_flow)
1240 dpif_flow->key = linux_flow->key;
1241 dpif_flow->key_len = linux_flow->key_len;
1242 dpif_flow->mask = linux_flow->mask;
1243 dpif_flow->mask_len = linux_flow->mask_len;
1244 dpif_flow->actions = linux_flow->actions;
1245 dpif_flow->actions_len = linux_flow->actions_len;
1246 dpif_linux_flow_get_stats(linux_flow, &dpif_flow->stats);
1250 dpif_linux_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1251 struct dpif_flow *flows, int max_flows)
1253 struct dpif_linux_flow_dump_thread *thread
1254 = dpif_linux_flow_dump_thread_cast(thread_);
1255 struct dpif_linux_flow_dump *dump = thread->dump;
1256 struct dpif_linux *dpif = dpif_linux_cast(thread->up.dpif);
1259 ofpbuf_delete(thread->nl_actions);
1260 thread->nl_actions = NULL;
1264 || (n_flows < max_flows && ofpbuf_size(&thread->nl_flows))) {
1265 struct dpif_linux_flow linux_flow;
1266 struct ofpbuf nl_flow;
1269 /* Try to grab another flow. */
1270 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1274 /* Convert the flow to our output format. */
1275 error = dpif_linux_flow_from_ofpbuf(&linux_flow, &nl_flow);
1277 atomic_store(&dump->status, error);
1281 if (linux_flow.actions) {
1282 /* Common case: the flow includes actions. */
1283 dpif_linux_flow_to_dpif_flow(&flows[n_flows++], &linux_flow);
1285 /* Rare case: the flow does not include actions. Retrieve this
1286 * individual flow again to get the actions. */
1287 error = dpif_linux_flow_get__(dpif, linux_flow.key,
1288 linux_flow.key_len, &linux_flow,
1289 &thread->nl_actions);
1290 if (error == ENOENT) {
1291 VLOG_DBG("dumped flow disappeared on get");
1294 VLOG_WARN("error fetching dumped flow: %s",
1295 ovs_strerror(error));
1296 atomic_store(&dump->status, error);
1300 /* Save this flow. Then exit, because we only have one buffer to
1301 * handle this case. */
1302 dpif_linux_flow_to_dpif_flow(&flows[n_flows++], &linux_flow);
1310 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1313 struct ovs_header *k_exec;
1316 ofpbuf_prealloc_tailroom(buf, (64
1317 + ofpbuf_size(d_exec->packet)
1318 + ODP_KEY_METADATA_SIZE
1319 + d_exec->actions_len));
1321 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1322 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1324 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1325 k_exec->dp_ifindex = dp_ifindex;
1327 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1328 ofpbuf_data(d_exec->packet),
1329 ofpbuf_size(d_exec->packet));
1331 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1332 odp_key_from_pkt_metadata(buf, &d_exec->md);
1333 nl_msg_end_nested(buf, key_ofs);
1335 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1336 d_exec->actions, d_exec->actions_len);
1340 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1342 uint64_t request_stub[1024 / 8];
1343 struct ofpbuf request;
1346 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1347 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1348 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1349 ofpbuf_uninit(&request);
1355 dpif_linux_execute(struct dpif *dpif_, struct dpif_execute *execute)
1357 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1359 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1365 dpif_linux_operate__(struct dpif_linux *dpif,
1366 struct dpif_op **ops, size_t n_ops)
1369 struct nl_transaction txn;
1371 struct ofpbuf request;
1372 uint64_t request_stub[1024 / 8];
1374 struct ofpbuf reply;
1375 uint64_t reply_stub[1024 / 8];
1378 struct nl_transaction *txnsp[MAX_OPS];
1381 ovs_assert(n_ops <= MAX_OPS);
1382 for (i = 0; i < n_ops; i++) {
1383 struct op_auxdata *aux = &auxes[i];
1384 struct dpif_op *op = ops[i];
1385 struct dpif_flow_put *put;
1386 struct dpif_flow_del *del;
1387 struct dpif_execute *execute;
1388 struct dpif_linux_flow flow;
1390 ofpbuf_use_stub(&aux->request,
1391 aux->request_stub, sizeof aux->request_stub);
1392 aux->txn.request = &aux->request;
1394 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1395 aux->txn.reply = NULL;
1398 case DPIF_OP_FLOW_PUT:
1399 put = &op->u.flow_put;
1400 dpif_linux_init_flow_put(dpif, put, &flow);
1402 flow.nlmsg_flags |= NLM_F_ECHO;
1403 aux->txn.reply = &aux->reply;
1405 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1408 case DPIF_OP_FLOW_DEL:
1409 del = &op->u.flow_del;
1410 dpif_linux_init_flow_del(dpif, del, &flow);
1412 flow.nlmsg_flags |= NLM_F_ECHO;
1413 aux->txn.reply = &aux->reply;
1415 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1418 case DPIF_OP_EXECUTE:
1419 execute = &op->u.execute;
1420 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1429 for (i = 0; i < n_ops; i++) {
1430 txnsp[i] = &auxes[i].txn;
1432 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1434 for (i = 0; i < n_ops; i++) {
1435 struct op_auxdata *aux = &auxes[i];
1436 struct nl_transaction *txn = &auxes[i].txn;
1437 struct dpif_op *op = ops[i];
1438 struct dpif_flow_put *put;
1439 struct dpif_flow_del *del;
1441 op->error = txn->error;
1444 case DPIF_OP_FLOW_PUT:
1445 put = &op->u.flow_put;
1448 struct dpif_linux_flow reply;
1450 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1453 dpif_linux_flow_get_stats(&reply, put->stats);
1458 memset(put->stats, 0, sizeof *put->stats);
1463 case DPIF_OP_FLOW_DEL:
1464 del = &op->u.flow_del;
1467 struct dpif_linux_flow reply;
1469 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1472 dpif_linux_flow_get_stats(&reply, del->stats);
1477 memset(del->stats, 0, sizeof *del->stats);
1482 case DPIF_OP_EXECUTE:
1489 ofpbuf_uninit(&aux->request);
1490 ofpbuf_uninit(&aux->reply);
1495 dpif_linux_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1497 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1500 size_t chunk = MIN(n_ops, MAX_OPS);
1501 dpif_linux_operate__(dpif, ops, chunk);
1507 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1508 * currently in 'dpif' in the kernel, by adding a new set of channels for
1509 * any kernel vport that lacks one and deleting any channels that have no
1510 * backing kernel vports. */
1512 dpif_linux_refresh_channels(struct dpif_linux *dpif, uint32_t n_handlers)
1513 OVS_REQ_WRLOCK(dpif->upcall_lock)
1515 unsigned long int *keep_channels;
1516 struct dpif_linux_vport vport;
1517 size_t keep_channels_nbits;
1518 struct nl_dump dump;
1519 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1524 if (dpif->n_handlers != n_handlers) {
1525 destroy_all_channels(dpif);
1526 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1527 for (i = 0; i < n_handlers; i++) {
1528 struct dpif_handler *handler = &dpif->handlers[i];
1530 handler->epoll_fd = epoll_create(10);
1531 if (handler->epoll_fd < 0) {
1534 for (j = 0; j < i; j++) {
1535 close(dpif->handlers[j].epoll_fd);
1537 free(dpif->handlers);
1538 dpif->handlers = NULL;
1543 dpif->n_handlers = n_handlers;
1546 for (i = 0; i < n_handlers; i++) {
1547 struct dpif_handler *handler = &dpif->handlers[i];
1549 handler->event_offset = handler->n_events = 0;
1552 keep_channels_nbits = dpif->uc_array_size;
1553 keep_channels = bitmap_allocate(keep_channels_nbits);
1555 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1556 dpif_linux_port_dump_start__(dpif, &dump);
1557 while (!dpif_linux_port_dump_next__(dpif, &dump, &vport, &buf)) {
1558 uint32_t port_no = odp_to_u32(vport.port_no);
1559 uint32_t *upcall_pids = NULL;
1562 if (port_no >= dpif->uc_array_size
1563 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1564 struct nl_sock **socksp = vport_create_socksp(dpif->n_handlers,
1571 error = vport_add_channels(dpif, vport.port_no, socksp);
1573 VLOG_INFO("%s: could not add channels for port %s",
1574 dpif_name(&dpif->dpif), vport.name);
1575 vport_del_socksp(socksp, dpif->n_handlers);
1579 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1583 /* Configure the vport to deliver misses to 'sock'. */
1584 if (vport.upcall_pids[0] == 0
1585 || vport.n_upcall_pids != dpif->n_handlers
1586 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1588 struct dpif_linux_vport vport_request;
1590 dpif_linux_vport_init(&vport_request);
1591 vport_request.cmd = OVS_VPORT_CMD_SET;
1592 vport_request.dp_ifindex = dpif->dp_ifindex;
1593 vport_request.port_no = vport.port_no;
1594 vport_request.n_upcall_pids = dpif->n_handlers;
1595 vport_request.upcall_pids = upcall_pids;
1596 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1598 VLOG_WARN_RL(&error_rl,
1599 "%s: failed to set upcall pid on port: %s",
1600 dpif_name(&dpif->dpif), ovs_strerror(error));
1602 if (error != ENODEV && error != ENOENT) {
1605 /* The vport isn't really there, even though the dump says
1606 * it is. Probably we just hit a race after a port
1613 if (port_no < keep_channels_nbits) {
1614 bitmap_set1(keep_channels, port_no);
1621 vport_del_channels(dpif, vport.port_no);
1623 nl_dump_done(&dump);
1624 ofpbuf_uninit(&buf);
1626 /* Discard any saved channels that we didn't reuse. */
1627 for (i = 0; i < keep_channels_nbits; i++) {
1628 if (!bitmap_is_set(keep_channels, i)) {
1629 vport_del_channels(dpif, u32_to_odp(i));
1632 free(keep_channels);
1638 dpif_linux_recv_set__(struct dpif_linux *dpif, bool enable)
1639 OVS_REQ_WRLOCK(dpif->upcall_lock)
1641 if ((dpif->handlers != NULL) == enable) {
1643 } else if (!enable) {
1644 destroy_all_channels(dpif);
1647 return dpif_linux_refresh_channels(dpif, 1);
1652 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1654 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1657 fat_rwlock_wrlock(&dpif->upcall_lock);
1658 error = dpif_linux_recv_set__(dpif, enable);
1659 fat_rwlock_unlock(&dpif->upcall_lock);
1665 dpif_linux_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1667 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1670 fat_rwlock_wrlock(&dpif->upcall_lock);
1671 if (dpif->handlers) {
1672 error = dpif_linux_refresh_channels(dpif, n_handlers);
1674 fat_rwlock_unlock(&dpif->upcall_lock);
1680 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1681 uint32_t queue_id, uint32_t *priority)
1683 if (queue_id < 0xf000) {
1684 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1692 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1695 static const struct nl_policy ovs_packet_policy[] = {
1696 /* Always present. */
1697 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1698 .min_len = ETH_HEADER_LEN },
1699 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1701 /* OVS_PACKET_CMD_ACTION only. */
1702 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1705 struct ovs_header *ovs_header;
1706 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1707 struct nlmsghdr *nlmsg;
1708 struct genlmsghdr *genl;
1712 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1714 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1715 genl = ofpbuf_try_pull(&b, sizeof *genl);
1716 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1717 if (!nlmsg || !genl || !ovs_header
1718 || nlmsg->nlmsg_type != ovs_packet_family
1719 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1720 ARRAY_SIZE(ovs_packet_policy))) {
1724 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1725 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1731 /* (Re)set ALL fields of '*upcall' on successful return. */
1732 upcall->type = type;
1733 upcall->key = CONST_CAST(struct nlattr *,
1734 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1735 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1736 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1738 /* Allow overwriting the netlink attribute header without reallocating. */
1739 ofpbuf_use_stub(&upcall->packet,
1740 CONST_CAST(struct nlattr *,
1741 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1742 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1743 sizeof(struct nlattr));
1744 ofpbuf_set_data(&upcall->packet,
1745 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
1746 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
1748 *dp_ifindex = ovs_header->dp_ifindex;
1754 dpif_linux_recv__(struct dpif_linux *dpif, uint32_t handler_id,
1755 struct dpif_upcall *upcall, struct ofpbuf *buf)
1756 OVS_REQ_RDLOCK(dpif->upcall_lock)
1758 struct dpif_handler *handler;
1761 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
1765 handler = &dpif->handlers[handler_id];
1766 if (handler->event_offset >= handler->n_events) {
1769 handler->event_offset = handler->n_events = 0;
1772 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
1773 dpif->uc_array_size, 0);
1774 } while (retval < 0 && errno == EINTR);
1776 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1777 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1778 } else if (retval > 0) {
1779 handler->n_events = retval;
1783 while (handler->event_offset < handler->n_events) {
1784 int idx = handler->epoll_events[handler->event_offset].data.u32;
1785 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
1787 handler->event_offset++;
1793 if (++read_tries > 50) {
1797 error = nl_sock_recv(ch->sock, buf, false);
1798 if (error == ENOBUFS) {
1799 /* ENOBUFS typically means that we've received so many
1800 * packets that the buffer overflowed. Try again
1801 * immediately because there's almost certainly a packet
1802 * waiting for us. */
1803 report_loss(dpif, ch, idx, handler_id);
1807 ch->last_poll = time_msec();
1809 if (error == EAGAIN) {
1815 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1816 if (!error && dp_ifindex == dpif->dp_ifindex) {
1828 dpif_linux_recv(struct dpif *dpif_, uint32_t handler_id,
1829 struct dpif_upcall *upcall, struct ofpbuf *buf)
1831 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1834 fat_rwlock_rdlock(&dpif->upcall_lock);
1835 error = dpif_linux_recv__(dpif, handler_id, upcall, buf);
1836 fat_rwlock_unlock(&dpif->upcall_lock);
1842 dpif_linux_recv_wait__(struct dpif_linux *dpif, uint32_t handler_id)
1843 OVS_REQ_RDLOCK(dpif->upcall_lock)
1845 if (dpif->handlers && handler_id < dpif->n_handlers) {
1846 struct dpif_handler *handler = &dpif->handlers[handler_id];
1848 poll_fd_wait(handler->epoll_fd, POLLIN);
1853 dpif_linux_recv_wait(struct dpif *dpif_, uint32_t handler_id)
1855 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1857 fat_rwlock_rdlock(&dpif->upcall_lock);
1858 dpif_linux_recv_wait__(dpif, handler_id);
1859 fat_rwlock_unlock(&dpif->upcall_lock);
1863 dpif_linux_recv_purge__(struct dpif_linux *dpif)
1864 OVS_REQ_WRLOCK(dpif->upcall_lock)
1866 if (dpif->handlers) {
1869 for (i = 0; i < dpif->uc_array_size; i++ ) {
1870 if (!dpif->handlers[0].channels[i].sock) {
1874 for (j = 0; j < dpif->n_handlers; j++) {
1875 nl_sock_drain(dpif->handlers[j].channels[i].sock);
1882 dpif_linux_recv_purge(struct dpif *dpif_)
1884 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1886 fat_rwlock_wrlock(&dpif->upcall_lock);
1887 dpif_linux_recv_purge__(dpif);
1888 fat_rwlock_unlock(&dpif->upcall_lock);
1891 const struct dpif_class dpif_linux_class = {
1893 dpif_linux_enumerate,
1900 dpif_linux_get_stats,
1901 dpif_linux_port_add,
1902 dpif_linux_port_del,
1903 dpif_linux_port_query_by_number,
1904 dpif_linux_port_query_by_name,
1905 dpif_linux_port_get_pid,
1906 dpif_linux_port_dump_start,
1907 dpif_linux_port_dump_next,
1908 dpif_linux_port_dump_done,
1909 dpif_linux_port_poll,
1910 dpif_linux_port_poll_wait,
1911 dpif_linux_flow_get,
1912 dpif_linux_flow_put,
1913 dpif_linux_flow_del,
1914 dpif_linux_flow_flush,
1915 dpif_linux_flow_dump_create,
1916 dpif_linux_flow_dump_destroy,
1917 dpif_linux_flow_dump_thread_create,
1918 dpif_linux_flow_dump_thread_destroy,
1919 dpif_linux_flow_dump_next,
1922 dpif_linux_recv_set,
1923 dpif_linux_handlers_set,
1924 dpif_linux_queue_to_priority,
1926 dpif_linux_recv_wait,
1927 dpif_linux_recv_purge,
1931 dpif_linux_init(void)
1933 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1936 if (ovsthread_once_start(&once)) {
1937 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1938 &ovs_datapath_family);
1940 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1941 "The Open vSwitch kernel module is probably not loaded.",
1942 OVS_DATAPATH_FAMILY);
1945 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1948 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1951 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1952 &ovs_packet_family);
1955 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1956 &ovs_vport_mcgroup);
1959 ovsthread_once_done(&once);
1966 dpif_linux_is_internal_device(const char *name)
1968 struct dpif_linux_vport reply;
1972 error = dpif_linux_vport_get(name, &reply, &buf);
1975 } else if (error != ENODEV && error != ENOENT) {
1976 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1977 name, ovs_strerror(error));
1980 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1983 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1984 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1985 * positive errno value.
1987 * 'vport' will contain pointers into 'buf', so the caller should not free
1988 * 'buf' while 'vport' is still in use. */
1990 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1991 const struct ofpbuf *buf)
1993 static const struct nl_policy ovs_vport_policy[] = {
1994 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1995 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1996 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1997 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
1998 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
2000 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
2003 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
2004 struct ovs_header *ovs_header;
2005 struct nlmsghdr *nlmsg;
2006 struct genlmsghdr *genl;
2009 dpif_linux_vport_init(vport);
2011 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2012 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2013 genl = ofpbuf_try_pull(&b, sizeof *genl);
2014 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2015 if (!nlmsg || !genl || !ovs_header
2016 || nlmsg->nlmsg_type != ovs_vport_family
2017 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2018 ARRAY_SIZE(ovs_vport_policy))) {
2022 vport->cmd = genl->cmd;
2023 vport->dp_ifindex = ovs_header->dp_ifindex;
2024 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2025 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2026 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2027 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2028 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2029 / (sizeof *vport->upcall_pids);
2030 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2033 if (a[OVS_VPORT_ATTR_STATS]) {
2034 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2036 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2037 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2038 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2043 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2044 * followed by Netlink attributes corresponding to 'vport'. */
2046 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
2049 struct ovs_header *ovs_header;
2051 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2052 vport->cmd, OVS_VPORT_VERSION);
2054 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2055 ovs_header->dp_ifindex = vport->dp_ifindex;
2057 if (vport->port_no != ODPP_NONE) {
2058 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2061 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2062 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2066 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2069 if (vport->upcall_pids) {
2070 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2072 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2076 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2077 vport->stats, sizeof *vport->stats);
2080 if (vport->options) {
2081 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2082 vport->options, vport->options_len);
2086 /* Clears 'vport' to "empty" values. */
2088 dpif_linux_vport_init(struct dpif_linux_vport *vport)
2090 memset(vport, 0, sizeof *vport);
2091 vport->port_no = ODPP_NONE;
2094 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2095 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2096 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2097 * result of the command is expected to be an ovs_vport also, which is decoded
2098 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2099 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2101 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
2102 struct dpif_linux_vport *reply,
2103 struct ofpbuf **bufp)
2105 struct ofpbuf *request_buf;
2108 ovs_assert((reply != NULL) == (bufp != NULL));
2110 error = dpif_linux_init();
2114 dpif_linux_vport_init(reply);
2119 request_buf = ofpbuf_new(1024);
2120 dpif_linux_vport_to_ofpbuf(request, request_buf);
2121 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2122 ofpbuf_delete(request_buf);
2126 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
2129 dpif_linux_vport_init(reply);
2130 ofpbuf_delete(*bufp);
2137 /* Obtains information about the kernel vport named 'name' and stores it into
2138 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2139 * longer needed ('reply' will contain pointers into '*bufp'). */
2141 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
2142 struct ofpbuf **bufp)
2144 struct dpif_linux_vport request;
2146 dpif_linux_vport_init(&request);
2147 request.cmd = OVS_VPORT_CMD_GET;
2148 request.name = name;
2150 return dpif_linux_vport_transact(&request, reply, bufp);
2153 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2154 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2155 * positive errno value.
2157 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2158 * while 'dp' is still in use. */
2160 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
2162 static const struct nl_policy ovs_datapath_policy[] = {
2163 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2164 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2166 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2167 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2171 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2172 struct ovs_header *ovs_header;
2173 struct nlmsghdr *nlmsg;
2174 struct genlmsghdr *genl;
2177 dpif_linux_dp_init(dp);
2179 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2180 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2181 genl = ofpbuf_try_pull(&b, sizeof *genl);
2182 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2183 if (!nlmsg || !genl || !ovs_header
2184 || nlmsg->nlmsg_type != ovs_datapath_family
2185 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2186 ARRAY_SIZE(ovs_datapath_policy))) {
2190 dp->cmd = genl->cmd;
2191 dp->dp_ifindex = ovs_header->dp_ifindex;
2192 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2193 if (a[OVS_DP_ATTR_STATS]) {
2194 /* Can't use structure assignment because Netlink doesn't ensure
2195 * sufficient alignment for 64-bit members. */
2196 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
2200 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2201 /* Can't use structure assignment because Netlink doesn't ensure
2202 * sufficient alignment for 64-bit members. */
2203 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
2204 sizeof dp->megaflow_stats);
2210 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2212 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
2214 struct ovs_header *ovs_header;
2216 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2217 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2218 OVS_DATAPATH_VERSION);
2220 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2221 ovs_header->dp_ifindex = dp->dp_ifindex;
2224 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2227 if (dp->upcall_pid) {
2228 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2231 if (dp->user_features) {
2232 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2235 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2238 /* Clears 'dp' to "empty" values. */
2240 dpif_linux_dp_init(struct dpif_linux_dp *dp)
2242 memset(dp, 0, sizeof *dp);
2243 dp->megaflow_stats.n_masks = UINT32_MAX;
2244 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
2248 dpif_linux_dp_dump_start(struct nl_dump *dump)
2250 struct dpif_linux_dp request;
2253 dpif_linux_dp_init(&request);
2254 request.cmd = OVS_DP_CMD_GET;
2256 buf = ofpbuf_new(1024);
2257 dpif_linux_dp_to_ofpbuf(&request, buf);
2258 nl_dump_start(dump, NETLINK_GENERIC, buf);
2262 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2263 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2264 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2265 * result of the command is expected to be of the same form, which is decoded
2266 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2267 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2269 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
2270 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
2272 struct ofpbuf *request_buf;
2275 ovs_assert((reply != NULL) == (bufp != NULL));
2277 request_buf = ofpbuf_new(1024);
2278 dpif_linux_dp_to_ofpbuf(request, request_buf);
2279 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2280 ofpbuf_delete(request_buf);
2283 dpif_linux_dp_init(reply);
2285 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
2288 ofpbuf_delete(*bufp);
2295 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2296 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2297 * will contain pointers into '*bufp'). */
2299 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2300 struct ofpbuf **bufp)
2302 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2303 struct dpif_linux_dp request;
2305 dpif_linux_dp_init(&request);
2306 request.cmd = OVS_DP_CMD_GET;
2307 request.dp_ifindex = dpif->dp_ifindex;
2309 return dpif_linux_dp_transact(&request, reply, bufp);
2312 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2313 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2314 * positive errno value.
2316 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2317 * while 'flow' is still in use. */
2319 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2320 const struct ofpbuf *buf)
2322 static const struct nl_policy ovs_flow_policy[] = {
2323 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2324 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2325 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2326 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2328 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2329 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2330 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2333 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2334 struct ovs_header *ovs_header;
2335 struct nlmsghdr *nlmsg;
2336 struct genlmsghdr *genl;
2339 dpif_linux_flow_init(flow);
2341 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2342 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2343 genl = ofpbuf_try_pull(&b, sizeof *genl);
2344 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2345 if (!nlmsg || !genl || !ovs_header
2346 || nlmsg->nlmsg_type != ovs_flow_family
2347 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2348 ARRAY_SIZE(ovs_flow_policy))) {
2352 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2353 flow->dp_ifindex = ovs_header->dp_ifindex;
2354 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2355 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2357 if (a[OVS_FLOW_ATTR_MASK]) {
2358 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2359 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2361 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2362 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2363 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2365 if (a[OVS_FLOW_ATTR_STATS]) {
2366 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2368 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2369 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2371 if (a[OVS_FLOW_ATTR_USED]) {
2372 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2377 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2378 * followed by Netlink attributes corresponding to 'flow'. */
2380 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2383 struct ovs_header *ovs_header;
2385 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2386 NLM_F_REQUEST | flow->nlmsg_flags,
2387 flow->cmd, OVS_FLOW_VERSION);
2389 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2390 ovs_header->dp_ifindex = flow->dp_ifindex;
2392 if (flow->key_len) {
2393 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2396 if (flow->mask_len) {
2397 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2400 if (flow->actions || flow->actions_len) {
2401 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2402 flow->actions, flow->actions_len);
2405 /* We never need to send these to the kernel. */
2406 ovs_assert(!flow->stats);
2407 ovs_assert(!flow->tcp_flags);
2408 ovs_assert(!flow->used);
2411 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2415 /* Clears 'flow' to "empty" values. */
2417 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2419 memset(flow, 0, sizeof *flow);
2422 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2423 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2424 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2425 * result of the command is expected to be a flow also, which is decoded and
2426 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2427 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2429 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2430 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2432 struct ofpbuf *request_buf;
2435 ovs_assert((reply != NULL) == (bufp != NULL));
2438 request->nlmsg_flags |= NLM_F_ECHO;
2441 request_buf = ofpbuf_new(1024);
2442 dpif_linux_flow_to_ofpbuf(request, request_buf);
2443 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2444 ofpbuf_delete(request_buf);
2448 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2451 dpif_linux_flow_init(reply);
2452 ofpbuf_delete(*bufp);
2460 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2461 struct dpif_flow_stats *stats)
2464 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2465 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2467 stats->n_packets = 0;
2470 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2471 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2474 /* Logs information about a packet that was recently lost in 'ch' (in
2477 report_loss(struct dpif_linux *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2478 uint32_t handler_id)
2480 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2483 if (VLOG_DROP_WARN(&rl)) {
2488 if (ch->last_poll != LLONG_MIN) {
2489 ds_put_format(&s, " (last polled %lld ms ago)",
2490 time_msec() - ch->last_poll);
2493 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2494 dpif_name(&dpif->dpif), ch_idx, handler_id);