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 && dpif->uc_array_size > 0) {
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 /* Needs to check in case the socket pointer is changed in between
835 * the holding of upcall_lock. A known case happens when the main
836 * thread deletes the vport while the handler thread is handling
837 * the upcall from that port. */
838 if (h->channels[idx].sock) {
839 pid = nl_sock_pid(h->channels[idx].sock);
847 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
850 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
853 fat_rwlock_rdlock(&dpif->upcall_lock);
854 ret = dpif_linux_port_get_pid__(dpif, port_no, hash);
855 fat_rwlock_unlock(&dpif->upcall_lock);
861 dpif_linux_flow_flush(struct dpif *dpif_)
863 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
864 struct dpif_linux_flow flow;
866 dpif_linux_flow_init(&flow);
867 flow.cmd = OVS_FLOW_CMD_DEL;
868 flow.dp_ifindex = dpif->dp_ifindex;
869 return dpif_linux_flow_transact(&flow, NULL, NULL);
872 struct dpif_linux_port_state {
878 dpif_linux_port_dump_start__(const struct dpif_linux *dpif,
879 struct nl_dump *dump)
881 struct dpif_linux_vport request;
884 dpif_linux_vport_init(&request);
885 request.cmd = OVS_VPORT_CMD_GET;
886 request.dp_ifindex = dpif->dp_ifindex;
888 buf = ofpbuf_new(1024);
889 dpif_linux_vport_to_ofpbuf(&request, buf);
890 nl_dump_start(dump, NETLINK_GENERIC, buf);
895 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
897 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
898 struct dpif_linux_port_state *state;
900 *statep = state = xmalloc(sizeof *state);
901 dpif_linux_port_dump_start__(dpif, &state->dump);
903 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
908 dpif_linux_port_dump_next__(const struct dpif_linux *dpif, struct nl_dump *dump,
909 struct dpif_linux_vport *vport,
910 struct ofpbuf *buffer)
915 if (!nl_dump_next(dump, &buf, buffer)) {
919 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
921 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
922 dpif_name(&dpif->dpif), ovs_strerror(error));
928 dpif_linux_port_dump_next(const struct dpif *dpif_, void *state_,
929 struct dpif_port *dpif_port)
931 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
932 struct dpif_linux_port_state *state = state_;
933 struct dpif_linux_vport vport;
936 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport,
941 dpif_port->name = CONST_CAST(char *, vport.name);
942 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
943 dpif_port->port_no = vport.port_no;
948 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
950 struct dpif_linux_port_state *state = state_;
951 int error = nl_dump_done(&state->dump);
953 ofpbuf_uninit(&state->buf);
959 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
961 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
963 /* Lazily create the Netlink socket to listen for notifications. */
964 if (!dpif->port_notifier) {
965 struct nl_sock *sock;
968 error = nl_sock_create(NETLINK_GENERIC, &sock);
973 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
975 nl_sock_destroy(sock);
978 dpif->port_notifier = sock;
980 /* We have no idea of the current state so report that everything
986 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
987 uint64_t buf_stub[4096 / 8];
991 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
992 error = nl_sock_recv(dpif->port_notifier, &buf, false);
994 struct dpif_linux_vport vport;
996 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
998 if (vport.dp_ifindex == dpif->dp_ifindex
999 && (vport.cmd == OVS_VPORT_CMD_NEW
1000 || vport.cmd == OVS_VPORT_CMD_DEL
1001 || vport.cmd == OVS_VPORT_CMD_SET)) {
1002 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1003 dpif->dpif.full_name, vport.name, vport.cmd);
1004 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1005 dpif->refresh_channels = true;
1007 *devnamep = xstrdup(vport.name);
1008 ofpbuf_uninit(&buf);
1012 } else if (error != EAGAIN) {
1013 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1014 ovs_strerror(error));
1015 nl_sock_drain(dpif->port_notifier);
1019 ofpbuf_uninit(&buf);
1027 dpif_linux_port_poll_wait(const struct dpif *dpif_)
1029 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1031 if (dpif->port_notifier) {
1032 nl_sock_wait(dpif->port_notifier, POLLIN);
1034 poll_immediate_wake();
1039 dpif_linux_flow_get__(const struct dpif_linux *dpif,
1040 const struct nlattr *key, size_t key_len,
1041 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1043 struct dpif_linux_flow request;
1045 dpif_linux_flow_init(&request);
1046 request.cmd = OVS_FLOW_CMD_GET;
1047 request.dp_ifindex = dpif->dp_ifindex;
1049 request.key_len = key_len;
1050 return dpif_linux_flow_transact(&request, reply, bufp);
1054 dpif_linux_flow_get(const struct dpif *dpif_,
1055 const struct nlattr *key, size_t key_len,
1056 struct ofpbuf **bufp,
1057 struct nlattr **maskp, size_t *mask_len,
1058 struct nlattr **actionsp, size_t *actions_len,
1059 struct dpif_flow_stats *stats)
1061 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1062 struct dpif_linux_flow reply;
1065 error = dpif_linux_flow_get__(dpif, key, key_len, &reply, bufp);
1068 *maskp = CONST_CAST(struct nlattr *, reply.mask);
1069 *mask_len = reply.mask_len;
1072 *actionsp = CONST_CAST(struct nlattr *, reply.actions);
1073 *actions_len = reply.actions_len;
1076 dpif_linux_flow_get_stats(&reply, stats);
1083 dpif_linux_init_flow_put(struct dpif_linux *dpif, const struct dpif_flow_put *put,
1084 struct dpif_linux_flow *request)
1086 static const struct nlattr dummy_action;
1088 dpif_linux_flow_init(request);
1089 request->cmd = (put->flags & DPIF_FP_CREATE
1090 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1091 request->dp_ifindex = dpif->dp_ifindex;
1092 request->key = put->key;
1093 request->key_len = put->key_len;
1094 request->mask = put->mask;
1095 request->mask_len = put->mask_len;
1096 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1097 request->actions = (put->actions
1099 : CONST_CAST(struct nlattr *, &dummy_action));
1100 request->actions_len = put->actions_len;
1101 if (put->flags & DPIF_FP_ZERO_STATS) {
1102 request->clear = true;
1104 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1108 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
1110 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1111 struct dpif_linux_flow request, reply;
1115 dpif_linux_init_flow_put(dpif, put, &request);
1116 error = dpif_linux_flow_transact(&request,
1117 put->stats ? &reply : NULL,
1118 put->stats ? &buf : NULL);
1119 if (!error && put->stats) {
1120 dpif_linux_flow_get_stats(&reply, put->stats);
1127 dpif_linux_init_flow_del(struct dpif_linux *dpif, const struct dpif_flow_del *del,
1128 struct dpif_linux_flow *request)
1130 dpif_linux_flow_init(request);
1131 request->cmd = OVS_FLOW_CMD_DEL;
1132 request->dp_ifindex = dpif->dp_ifindex;
1133 request->key = del->key;
1134 request->key_len = del->key_len;
1138 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
1140 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1141 struct dpif_linux_flow request, reply;
1145 dpif_linux_init_flow_del(dpif, del, &request);
1146 error = dpif_linux_flow_transact(&request,
1147 del->stats ? &reply : NULL,
1148 del->stats ? &buf : NULL);
1149 if (!error && del->stats) {
1150 dpif_linux_flow_get_stats(&reply, del->stats);
1156 struct dpif_linux_flow_dump {
1157 struct dpif_flow_dump up;
1158 struct nl_dump nl_dump;
1162 static struct dpif_linux_flow_dump *
1163 dpif_linux_flow_dump_cast(struct dpif_flow_dump *dump)
1165 return CONTAINER_OF(dump, struct dpif_linux_flow_dump, up);
1168 static struct dpif_flow_dump *
1169 dpif_linux_flow_dump_create(const struct dpif *dpif_)
1171 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1172 struct dpif_linux_flow_dump *dump;
1173 struct dpif_linux_flow request;
1176 dump = xmalloc(sizeof *dump);
1177 dpif_flow_dump_init(&dump->up, dpif_);
1179 dpif_linux_flow_init(&request);
1180 request.cmd = OVS_FLOW_CMD_GET;
1181 request.dp_ifindex = dpif->dp_ifindex;
1183 buf = ofpbuf_new(1024);
1184 dpif_linux_flow_to_ofpbuf(&request, buf);
1185 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1187 atomic_init(&dump->status, 0);
1193 dpif_linux_flow_dump_destroy(struct dpif_flow_dump *dump_)
1195 struct dpif_linux_flow_dump *dump = dpif_linux_flow_dump_cast(dump_);
1196 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1199 atomic_read(&dump->status, &dump_status);
1201 return dump_status ? dump_status : nl_status;
1204 struct dpif_linux_flow_dump_thread {
1205 struct dpif_flow_dump_thread up;
1206 struct dpif_linux_flow_dump *dump;
1207 struct dpif_linux_flow flow;
1208 struct dpif_flow_stats stats;
1209 struct ofpbuf nl_flows; /* Always used to store flows. */
1210 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1213 static struct dpif_linux_flow_dump_thread *
1214 dpif_linux_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1216 return CONTAINER_OF(thread, struct dpif_linux_flow_dump_thread, up);
1219 static struct dpif_flow_dump_thread *
1220 dpif_linux_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1222 struct dpif_linux_flow_dump *dump = dpif_linux_flow_dump_cast(dump_);
1223 struct dpif_linux_flow_dump_thread *thread;
1225 thread = xmalloc(sizeof *thread);
1226 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1227 thread->dump = dump;
1228 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1229 thread->nl_actions = NULL;
1235 dpif_linux_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1237 struct dpif_linux_flow_dump_thread *thread
1238 = dpif_linux_flow_dump_thread_cast(thread_);
1240 ofpbuf_uninit(&thread->nl_flows);
1241 ofpbuf_delete(thread->nl_actions);
1246 dpif_linux_flow_to_dpif_flow(struct dpif_flow *dpif_flow,
1247 struct dpif_linux_flow *linux_flow)
1249 dpif_flow->key = linux_flow->key;
1250 dpif_flow->key_len = linux_flow->key_len;
1251 dpif_flow->mask = linux_flow->mask;
1252 dpif_flow->mask_len = linux_flow->mask_len;
1253 dpif_flow->actions = linux_flow->actions;
1254 dpif_flow->actions_len = linux_flow->actions_len;
1255 dpif_linux_flow_get_stats(linux_flow, &dpif_flow->stats);
1259 dpif_linux_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1260 struct dpif_flow *flows, int max_flows)
1262 struct dpif_linux_flow_dump_thread *thread
1263 = dpif_linux_flow_dump_thread_cast(thread_);
1264 struct dpif_linux_flow_dump *dump = thread->dump;
1265 struct dpif_linux *dpif = dpif_linux_cast(thread->up.dpif);
1268 ofpbuf_delete(thread->nl_actions);
1269 thread->nl_actions = NULL;
1273 || (n_flows < max_flows && ofpbuf_size(&thread->nl_flows))) {
1274 struct dpif_linux_flow linux_flow;
1275 struct ofpbuf nl_flow;
1278 /* Try to grab another flow. */
1279 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1283 /* Convert the flow to our output format. */
1284 error = dpif_linux_flow_from_ofpbuf(&linux_flow, &nl_flow);
1286 atomic_store(&dump->status, error);
1290 if (linux_flow.actions) {
1291 /* Common case: the flow includes actions. */
1292 dpif_linux_flow_to_dpif_flow(&flows[n_flows++], &linux_flow);
1294 /* Rare case: the flow does not include actions. Retrieve this
1295 * individual flow again to get the actions. */
1296 error = dpif_linux_flow_get__(dpif, linux_flow.key,
1297 linux_flow.key_len, &linux_flow,
1298 &thread->nl_actions);
1299 if (error == ENOENT) {
1300 VLOG_DBG("dumped flow disappeared on get");
1303 VLOG_WARN("error fetching dumped flow: %s",
1304 ovs_strerror(error));
1305 atomic_store(&dump->status, error);
1309 /* Save this flow. Then exit, because we only have one buffer to
1310 * handle this case. */
1311 dpif_linux_flow_to_dpif_flow(&flows[n_flows++], &linux_flow);
1319 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1322 struct ovs_header *k_exec;
1325 ofpbuf_prealloc_tailroom(buf, (64
1326 + ofpbuf_size(d_exec->packet)
1327 + ODP_KEY_METADATA_SIZE
1328 + d_exec->actions_len));
1330 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1331 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1333 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1334 k_exec->dp_ifindex = dp_ifindex;
1336 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1337 ofpbuf_data(d_exec->packet),
1338 ofpbuf_size(d_exec->packet));
1340 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1341 odp_key_from_pkt_metadata(buf, &d_exec->md);
1342 nl_msg_end_nested(buf, key_ofs);
1344 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1345 d_exec->actions, d_exec->actions_len);
1349 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1351 uint64_t request_stub[1024 / 8];
1352 struct ofpbuf request;
1355 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1356 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1357 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1358 ofpbuf_uninit(&request);
1364 dpif_linux_execute(struct dpif *dpif_, struct dpif_execute *execute)
1366 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1368 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1374 dpif_linux_operate__(struct dpif_linux *dpif,
1375 struct dpif_op **ops, size_t n_ops)
1378 struct nl_transaction txn;
1380 struct ofpbuf request;
1381 uint64_t request_stub[1024 / 8];
1383 struct ofpbuf reply;
1384 uint64_t reply_stub[1024 / 8];
1387 struct nl_transaction *txnsp[MAX_OPS];
1390 ovs_assert(n_ops <= MAX_OPS);
1391 for (i = 0; i < n_ops; i++) {
1392 struct op_auxdata *aux = &auxes[i];
1393 struct dpif_op *op = ops[i];
1394 struct dpif_flow_put *put;
1395 struct dpif_flow_del *del;
1396 struct dpif_execute *execute;
1397 struct dpif_linux_flow flow;
1399 ofpbuf_use_stub(&aux->request,
1400 aux->request_stub, sizeof aux->request_stub);
1401 aux->txn.request = &aux->request;
1403 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1404 aux->txn.reply = NULL;
1407 case DPIF_OP_FLOW_PUT:
1408 put = &op->u.flow_put;
1409 dpif_linux_init_flow_put(dpif, put, &flow);
1411 flow.nlmsg_flags |= NLM_F_ECHO;
1412 aux->txn.reply = &aux->reply;
1414 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1417 case DPIF_OP_FLOW_DEL:
1418 del = &op->u.flow_del;
1419 dpif_linux_init_flow_del(dpif, del, &flow);
1421 flow.nlmsg_flags |= NLM_F_ECHO;
1422 aux->txn.reply = &aux->reply;
1424 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1427 case DPIF_OP_EXECUTE:
1428 execute = &op->u.execute;
1429 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1438 for (i = 0; i < n_ops; i++) {
1439 txnsp[i] = &auxes[i].txn;
1441 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1443 for (i = 0; i < n_ops; i++) {
1444 struct op_auxdata *aux = &auxes[i];
1445 struct nl_transaction *txn = &auxes[i].txn;
1446 struct dpif_op *op = ops[i];
1447 struct dpif_flow_put *put;
1448 struct dpif_flow_del *del;
1450 op->error = txn->error;
1453 case DPIF_OP_FLOW_PUT:
1454 put = &op->u.flow_put;
1457 struct dpif_linux_flow reply;
1459 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1462 dpif_linux_flow_get_stats(&reply, put->stats);
1467 memset(put->stats, 0, sizeof *put->stats);
1472 case DPIF_OP_FLOW_DEL:
1473 del = &op->u.flow_del;
1476 struct dpif_linux_flow reply;
1478 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1481 dpif_linux_flow_get_stats(&reply, del->stats);
1486 memset(del->stats, 0, sizeof *del->stats);
1491 case DPIF_OP_EXECUTE:
1498 ofpbuf_uninit(&aux->request);
1499 ofpbuf_uninit(&aux->reply);
1504 dpif_linux_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1506 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1509 size_t chunk = MIN(n_ops, MAX_OPS);
1510 dpif_linux_operate__(dpif, ops, chunk);
1516 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1517 * currently in 'dpif' in the kernel, by adding a new set of channels for
1518 * any kernel vport that lacks one and deleting any channels that have no
1519 * backing kernel vports. */
1521 dpif_linux_refresh_channels(struct dpif_linux *dpif, uint32_t n_handlers)
1522 OVS_REQ_WRLOCK(dpif->upcall_lock)
1524 unsigned long int *keep_channels;
1525 struct dpif_linux_vport vport;
1526 size_t keep_channels_nbits;
1527 struct nl_dump dump;
1528 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1533 if (dpif->n_handlers != n_handlers) {
1534 destroy_all_channels(dpif);
1535 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1536 for (i = 0; i < n_handlers; i++) {
1537 struct dpif_handler *handler = &dpif->handlers[i];
1539 handler->epoll_fd = epoll_create(10);
1540 if (handler->epoll_fd < 0) {
1543 for (j = 0; j < i; j++) {
1544 close(dpif->handlers[j].epoll_fd);
1546 free(dpif->handlers);
1547 dpif->handlers = NULL;
1552 dpif->n_handlers = n_handlers;
1555 for (i = 0; i < n_handlers; i++) {
1556 struct dpif_handler *handler = &dpif->handlers[i];
1558 handler->event_offset = handler->n_events = 0;
1561 keep_channels_nbits = dpif->uc_array_size;
1562 keep_channels = bitmap_allocate(keep_channels_nbits);
1564 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1565 dpif_linux_port_dump_start__(dpif, &dump);
1566 while (!dpif_linux_port_dump_next__(dpif, &dump, &vport, &buf)) {
1567 uint32_t port_no = odp_to_u32(vport.port_no);
1568 uint32_t *upcall_pids = NULL;
1571 if (port_no >= dpif->uc_array_size
1572 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1573 struct nl_sock **socksp = vport_create_socksp(dpif->n_handlers,
1580 error = vport_add_channels(dpif, vport.port_no, socksp);
1582 VLOG_INFO("%s: could not add channels for port %s",
1583 dpif_name(&dpif->dpif), vport.name);
1584 vport_del_socksp(socksp, dpif->n_handlers);
1588 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1592 /* Configure the vport to deliver misses to 'sock'. */
1593 if (vport.upcall_pids[0] == 0
1594 || vport.n_upcall_pids != dpif->n_handlers
1595 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1597 struct dpif_linux_vport vport_request;
1599 dpif_linux_vport_init(&vport_request);
1600 vport_request.cmd = OVS_VPORT_CMD_SET;
1601 vport_request.dp_ifindex = dpif->dp_ifindex;
1602 vport_request.port_no = vport.port_no;
1603 vport_request.n_upcall_pids = dpif->n_handlers;
1604 vport_request.upcall_pids = upcall_pids;
1605 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1607 VLOG_WARN_RL(&error_rl,
1608 "%s: failed to set upcall pid on port: %s",
1609 dpif_name(&dpif->dpif), ovs_strerror(error));
1611 if (error != ENODEV && error != ENOENT) {
1614 /* The vport isn't really there, even though the dump says
1615 * it is. Probably we just hit a race after a port
1622 if (port_no < keep_channels_nbits) {
1623 bitmap_set1(keep_channels, port_no);
1630 vport_del_channels(dpif, vport.port_no);
1632 nl_dump_done(&dump);
1633 ofpbuf_uninit(&buf);
1635 /* Discard any saved channels that we didn't reuse. */
1636 for (i = 0; i < keep_channels_nbits; i++) {
1637 if (!bitmap_is_set(keep_channels, i)) {
1638 vport_del_channels(dpif, u32_to_odp(i));
1641 free(keep_channels);
1647 dpif_linux_recv_set__(struct dpif_linux *dpif, bool enable)
1648 OVS_REQ_WRLOCK(dpif->upcall_lock)
1650 if ((dpif->handlers != NULL) == enable) {
1652 } else if (!enable) {
1653 destroy_all_channels(dpif);
1656 return dpif_linux_refresh_channels(dpif, 1);
1661 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1663 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1666 fat_rwlock_wrlock(&dpif->upcall_lock);
1667 error = dpif_linux_recv_set__(dpif, enable);
1668 fat_rwlock_unlock(&dpif->upcall_lock);
1674 dpif_linux_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1676 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1679 fat_rwlock_wrlock(&dpif->upcall_lock);
1680 if (dpif->handlers) {
1681 error = dpif_linux_refresh_channels(dpif, n_handlers);
1683 fat_rwlock_unlock(&dpif->upcall_lock);
1689 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1690 uint32_t queue_id, uint32_t *priority)
1692 if (queue_id < 0xf000) {
1693 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1701 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1704 static const struct nl_policy ovs_packet_policy[] = {
1705 /* Always present. */
1706 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1707 .min_len = ETH_HEADER_LEN },
1708 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1710 /* OVS_PACKET_CMD_ACTION only. */
1711 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1714 struct ovs_header *ovs_header;
1715 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1716 struct nlmsghdr *nlmsg;
1717 struct genlmsghdr *genl;
1721 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1723 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1724 genl = ofpbuf_try_pull(&b, sizeof *genl);
1725 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1726 if (!nlmsg || !genl || !ovs_header
1727 || nlmsg->nlmsg_type != ovs_packet_family
1728 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1729 ARRAY_SIZE(ovs_packet_policy))) {
1733 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1734 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1740 /* (Re)set ALL fields of '*upcall' on successful return. */
1741 upcall->type = type;
1742 upcall->key = CONST_CAST(struct nlattr *,
1743 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1744 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1745 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1747 /* Allow overwriting the netlink attribute header without reallocating. */
1748 ofpbuf_use_stub(&upcall->packet,
1749 CONST_CAST(struct nlattr *,
1750 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1751 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1752 sizeof(struct nlattr));
1753 ofpbuf_set_data(&upcall->packet,
1754 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
1755 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
1757 *dp_ifindex = ovs_header->dp_ifindex;
1763 dpif_linux_recv__(struct dpif_linux *dpif, uint32_t handler_id,
1764 struct dpif_upcall *upcall, struct ofpbuf *buf)
1765 OVS_REQ_RDLOCK(dpif->upcall_lock)
1767 struct dpif_handler *handler;
1770 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
1774 handler = &dpif->handlers[handler_id];
1775 if (handler->event_offset >= handler->n_events) {
1778 handler->event_offset = handler->n_events = 0;
1781 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
1782 dpif->uc_array_size, 0);
1783 } while (retval < 0 && errno == EINTR);
1785 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1786 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1787 } else if (retval > 0) {
1788 handler->n_events = retval;
1792 while (handler->event_offset < handler->n_events) {
1793 int idx = handler->epoll_events[handler->event_offset].data.u32;
1794 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
1796 handler->event_offset++;
1802 if (++read_tries > 50) {
1806 error = nl_sock_recv(ch->sock, buf, false);
1807 if (error == ENOBUFS) {
1808 /* ENOBUFS typically means that we've received so many
1809 * packets that the buffer overflowed. Try again
1810 * immediately because there's almost certainly a packet
1811 * waiting for us. */
1812 report_loss(dpif, ch, idx, handler_id);
1816 ch->last_poll = time_msec();
1818 if (error == EAGAIN) {
1824 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1825 if (!error && dp_ifindex == dpif->dp_ifindex) {
1837 dpif_linux_recv(struct dpif *dpif_, uint32_t handler_id,
1838 struct dpif_upcall *upcall, struct ofpbuf *buf)
1840 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1843 fat_rwlock_rdlock(&dpif->upcall_lock);
1844 error = dpif_linux_recv__(dpif, handler_id, upcall, buf);
1845 fat_rwlock_unlock(&dpif->upcall_lock);
1851 dpif_linux_recv_wait__(struct dpif_linux *dpif, uint32_t handler_id)
1852 OVS_REQ_RDLOCK(dpif->upcall_lock)
1854 if (dpif->handlers && handler_id < dpif->n_handlers) {
1855 struct dpif_handler *handler = &dpif->handlers[handler_id];
1857 poll_fd_wait(handler->epoll_fd, POLLIN);
1862 dpif_linux_recv_wait(struct dpif *dpif_, uint32_t handler_id)
1864 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1866 fat_rwlock_rdlock(&dpif->upcall_lock);
1867 dpif_linux_recv_wait__(dpif, handler_id);
1868 fat_rwlock_unlock(&dpif->upcall_lock);
1872 dpif_linux_recv_purge__(struct dpif_linux *dpif)
1873 OVS_REQ_WRLOCK(dpif->upcall_lock)
1875 if (dpif->handlers) {
1878 for (i = 0; i < dpif->uc_array_size; i++ ) {
1879 if (!dpif->handlers[0].channels[i].sock) {
1883 for (j = 0; j < dpif->n_handlers; j++) {
1884 nl_sock_drain(dpif->handlers[j].channels[i].sock);
1891 dpif_linux_recv_purge(struct dpif *dpif_)
1893 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1895 fat_rwlock_wrlock(&dpif->upcall_lock);
1896 dpif_linux_recv_purge__(dpif);
1897 fat_rwlock_unlock(&dpif->upcall_lock);
1900 const struct dpif_class dpif_linux_class = {
1902 dpif_linux_enumerate,
1909 dpif_linux_get_stats,
1910 dpif_linux_port_add,
1911 dpif_linux_port_del,
1912 dpif_linux_port_query_by_number,
1913 dpif_linux_port_query_by_name,
1914 dpif_linux_port_get_pid,
1915 dpif_linux_port_dump_start,
1916 dpif_linux_port_dump_next,
1917 dpif_linux_port_dump_done,
1918 dpif_linux_port_poll,
1919 dpif_linux_port_poll_wait,
1920 dpif_linux_flow_get,
1921 dpif_linux_flow_put,
1922 dpif_linux_flow_del,
1923 dpif_linux_flow_flush,
1924 dpif_linux_flow_dump_create,
1925 dpif_linux_flow_dump_destroy,
1926 dpif_linux_flow_dump_thread_create,
1927 dpif_linux_flow_dump_thread_destroy,
1928 dpif_linux_flow_dump_next,
1931 dpif_linux_recv_set,
1932 dpif_linux_handlers_set,
1933 dpif_linux_queue_to_priority,
1935 dpif_linux_recv_wait,
1936 dpif_linux_recv_purge,
1937 NULL, /* register_upcall_cb */
1938 NULL, /* enable_upcall */
1939 NULL, /* disable_upcall */
1943 dpif_linux_init(void)
1945 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1948 if (ovsthread_once_start(&once)) {
1949 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1950 &ovs_datapath_family);
1952 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1953 "The Open vSwitch kernel module is probably not loaded.",
1954 OVS_DATAPATH_FAMILY);
1957 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1960 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1963 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1964 &ovs_packet_family);
1967 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1968 &ovs_vport_mcgroup);
1971 ovsthread_once_done(&once);
1978 dpif_linux_is_internal_device(const char *name)
1980 struct dpif_linux_vport reply;
1984 error = dpif_linux_vport_get(name, &reply, &buf);
1987 } else if (error != ENODEV && error != ENOENT) {
1988 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1989 name, ovs_strerror(error));
1992 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1995 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1996 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1997 * positive errno value.
1999 * 'vport' will contain pointers into 'buf', so the caller should not free
2000 * 'buf' while 'vport' is still in use. */
2002 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
2003 const struct ofpbuf *buf)
2005 static const struct nl_policy ovs_vport_policy[] = {
2006 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
2007 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
2008 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2009 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
2010 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
2012 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
2015 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
2016 struct ovs_header *ovs_header;
2017 struct nlmsghdr *nlmsg;
2018 struct genlmsghdr *genl;
2021 dpif_linux_vport_init(vport);
2023 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2024 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2025 genl = ofpbuf_try_pull(&b, sizeof *genl);
2026 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2027 if (!nlmsg || !genl || !ovs_header
2028 || nlmsg->nlmsg_type != ovs_vport_family
2029 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2030 ARRAY_SIZE(ovs_vport_policy))) {
2034 vport->cmd = genl->cmd;
2035 vport->dp_ifindex = ovs_header->dp_ifindex;
2036 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2037 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2038 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2039 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2040 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2041 / (sizeof *vport->upcall_pids);
2042 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2045 if (a[OVS_VPORT_ATTR_STATS]) {
2046 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2048 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2049 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2050 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2055 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2056 * followed by Netlink attributes corresponding to 'vport'. */
2058 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
2061 struct ovs_header *ovs_header;
2063 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2064 vport->cmd, OVS_VPORT_VERSION);
2066 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2067 ovs_header->dp_ifindex = vport->dp_ifindex;
2069 if (vport->port_no != ODPP_NONE) {
2070 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2073 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2074 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2078 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2081 if (vport->upcall_pids) {
2082 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2084 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2088 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2089 vport->stats, sizeof *vport->stats);
2092 if (vport->options) {
2093 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2094 vport->options, vport->options_len);
2098 /* Clears 'vport' to "empty" values. */
2100 dpif_linux_vport_init(struct dpif_linux_vport *vport)
2102 memset(vport, 0, sizeof *vport);
2103 vport->port_no = ODPP_NONE;
2106 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2107 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2108 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2109 * result of the command is expected to be an ovs_vport also, which is decoded
2110 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2111 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2113 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
2114 struct dpif_linux_vport *reply,
2115 struct ofpbuf **bufp)
2117 struct ofpbuf *request_buf;
2120 ovs_assert((reply != NULL) == (bufp != NULL));
2122 error = dpif_linux_init();
2126 dpif_linux_vport_init(reply);
2131 request_buf = ofpbuf_new(1024);
2132 dpif_linux_vport_to_ofpbuf(request, request_buf);
2133 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2134 ofpbuf_delete(request_buf);
2138 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
2141 dpif_linux_vport_init(reply);
2142 ofpbuf_delete(*bufp);
2149 /* Obtains information about the kernel vport named 'name' and stores it into
2150 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2151 * longer needed ('reply' will contain pointers into '*bufp'). */
2153 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
2154 struct ofpbuf **bufp)
2156 struct dpif_linux_vport request;
2158 dpif_linux_vport_init(&request);
2159 request.cmd = OVS_VPORT_CMD_GET;
2160 request.name = name;
2162 return dpif_linux_vport_transact(&request, reply, bufp);
2165 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2166 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2167 * positive errno value.
2169 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2170 * while 'dp' is still in use. */
2172 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
2174 static const struct nl_policy ovs_datapath_policy[] = {
2175 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2176 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2178 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2179 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2183 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2184 struct ovs_header *ovs_header;
2185 struct nlmsghdr *nlmsg;
2186 struct genlmsghdr *genl;
2189 dpif_linux_dp_init(dp);
2191 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2192 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2193 genl = ofpbuf_try_pull(&b, sizeof *genl);
2194 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2195 if (!nlmsg || !genl || !ovs_header
2196 || nlmsg->nlmsg_type != ovs_datapath_family
2197 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2198 ARRAY_SIZE(ovs_datapath_policy))) {
2202 dp->cmd = genl->cmd;
2203 dp->dp_ifindex = ovs_header->dp_ifindex;
2204 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2205 if (a[OVS_DP_ATTR_STATS]) {
2206 /* Can't use structure assignment because Netlink doesn't ensure
2207 * sufficient alignment for 64-bit members. */
2208 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
2212 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2213 /* Can't use structure assignment because Netlink doesn't ensure
2214 * sufficient alignment for 64-bit members. */
2215 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
2216 sizeof dp->megaflow_stats);
2222 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2224 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
2226 struct ovs_header *ovs_header;
2228 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2229 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2230 OVS_DATAPATH_VERSION);
2232 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2233 ovs_header->dp_ifindex = dp->dp_ifindex;
2236 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2239 if (dp->upcall_pid) {
2240 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2243 if (dp->user_features) {
2244 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2247 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2250 /* Clears 'dp' to "empty" values. */
2252 dpif_linux_dp_init(struct dpif_linux_dp *dp)
2254 memset(dp, 0, sizeof *dp);
2255 dp->megaflow_stats.n_masks = UINT32_MAX;
2256 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
2260 dpif_linux_dp_dump_start(struct nl_dump *dump)
2262 struct dpif_linux_dp request;
2265 dpif_linux_dp_init(&request);
2266 request.cmd = OVS_DP_CMD_GET;
2268 buf = ofpbuf_new(1024);
2269 dpif_linux_dp_to_ofpbuf(&request, buf);
2270 nl_dump_start(dump, NETLINK_GENERIC, buf);
2274 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2275 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2276 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2277 * result of the command is expected to be of the same form, which is decoded
2278 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2279 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2281 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
2282 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
2284 struct ofpbuf *request_buf;
2287 ovs_assert((reply != NULL) == (bufp != NULL));
2289 request_buf = ofpbuf_new(1024);
2290 dpif_linux_dp_to_ofpbuf(request, request_buf);
2291 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2292 ofpbuf_delete(request_buf);
2295 dpif_linux_dp_init(reply);
2297 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
2300 ofpbuf_delete(*bufp);
2307 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2308 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2309 * will contain pointers into '*bufp'). */
2311 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2312 struct ofpbuf **bufp)
2314 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2315 struct dpif_linux_dp request;
2317 dpif_linux_dp_init(&request);
2318 request.cmd = OVS_DP_CMD_GET;
2319 request.dp_ifindex = dpif->dp_ifindex;
2321 return dpif_linux_dp_transact(&request, reply, bufp);
2324 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2325 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2326 * positive errno value.
2328 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2329 * while 'flow' is still in use. */
2331 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2332 const struct ofpbuf *buf)
2334 static const struct nl_policy ovs_flow_policy[] = {
2335 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2336 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2337 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2338 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2340 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2341 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2342 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2345 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2346 struct ovs_header *ovs_header;
2347 struct nlmsghdr *nlmsg;
2348 struct genlmsghdr *genl;
2351 dpif_linux_flow_init(flow);
2353 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2354 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2355 genl = ofpbuf_try_pull(&b, sizeof *genl);
2356 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2357 if (!nlmsg || !genl || !ovs_header
2358 || nlmsg->nlmsg_type != ovs_flow_family
2359 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2360 ARRAY_SIZE(ovs_flow_policy))) {
2364 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2365 flow->dp_ifindex = ovs_header->dp_ifindex;
2366 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2367 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2369 if (a[OVS_FLOW_ATTR_MASK]) {
2370 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2371 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2373 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2374 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2375 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2377 if (a[OVS_FLOW_ATTR_STATS]) {
2378 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2380 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2381 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2383 if (a[OVS_FLOW_ATTR_USED]) {
2384 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2389 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2390 * followed by Netlink attributes corresponding to 'flow'. */
2392 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2395 struct ovs_header *ovs_header;
2397 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2398 NLM_F_REQUEST | flow->nlmsg_flags,
2399 flow->cmd, OVS_FLOW_VERSION);
2401 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2402 ovs_header->dp_ifindex = flow->dp_ifindex;
2404 if (flow->key_len) {
2405 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2408 if (flow->mask_len) {
2409 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2412 if (flow->actions || flow->actions_len) {
2413 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2414 flow->actions, flow->actions_len);
2417 /* We never need to send these to the kernel. */
2418 ovs_assert(!flow->stats);
2419 ovs_assert(!flow->tcp_flags);
2420 ovs_assert(!flow->used);
2423 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2427 /* Clears 'flow' to "empty" values. */
2429 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2431 memset(flow, 0, sizeof *flow);
2434 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2435 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2436 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2437 * result of the command is expected to be a flow also, which is decoded and
2438 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2439 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2441 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2442 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2444 struct ofpbuf *request_buf;
2447 ovs_assert((reply != NULL) == (bufp != NULL));
2450 request->nlmsg_flags |= NLM_F_ECHO;
2453 request_buf = ofpbuf_new(1024);
2454 dpif_linux_flow_to_ofpbuf(request, request_buf);
2455 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2456 ofpbuf_delete(request_buf);
2460 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2463 dpif_linux_flow_init(reply);
2464 ofpbuf_delete(*bufp);
2472 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2473 struct dpif_flow_stats *stats)
2476 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2477 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2479 stats->n_packets = 0;
2482 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2483 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2486 /* Logs information about a packet that was recently lost in 'ch' (in
2489 report_loss(struct dpif_linux *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2490 uint32_t handler_id)
2492 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2495 if (VLOG_DROP_WARN(&rl)) {
2500 if (ch->last_poll != LLONG_MIN) {
2501 ds_put_format(&s, " (last polled %lld ms ago)",
2502 time_msec() - ch->last_poll);
2505 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2506 dpif_name(&dpif->dpif), ch_idx, handler_id);