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 const struct ovs_dp_stats *stats; /* OVS_DP_ATTR_STATS. */
77 const 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 memset(stats, 0, sizeof *stats);
560 stats->n_hit = get_32aligned_u64(&dp.stats->n_hit);
561 stats->n_missed = get_32aligned_u64(&dp.stats->n_missed);
562 stats->n_lost = get_32aligned_u64(&dp.stats->n_lost);
563 stats->n_flows = get_32aligned_u64(&dp.stats->n_flows);
566 if (dp.megaflow_stats) {
567 stats->n_masks = dp.megaflow_stats->n_masks;
568 stats->n_mask_hit = get_32aligned_u64(
569 &dp.megaflow_stats->n_mask_hit);
571 stats->n_masks = UINT32_MAX;
572 stats->n_mask_hit = UINT64_MAX;
580 get_vport_type(const struct dpif_linux_vport *vport)
582 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
584 switch (vport->type) {
585 case OVS_VPORT_TYPE_NETDEV: {
586 const char *type = netdev_get_type_from_name(vport->name);
588 return type ? type : "system";
591 case OVS_VPORT_TYPE_INTERNAL:
594 case OVS_VPORT_TYPE_GENEVE:
597 case OVS_VPORT_TYPE_GRE:
600 case OVS_VPORT_TYPE_GRE64:
603 case OVS_VPORT_TYPE_VXLAN:
606 case OVS_VPORT_TYPE_LISP:
609 case OVS_VPORT_TYPE_UNSPEC:
610 case __OVS_VPORT_TYPE_MAX:
614 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
615 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
619 static enum ovs_vport_type
620 netdev_to_ovs_vport_type(const struct netdev *netdev)
622 const char *type = netdev_get_type(netdev);
624 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
625 return OVS_VPORT_TYPE_NETDEV;
626 } else if (!strcmp(type, "internal")) {
627 return OVS_VPORT_TYPE_INTERNAL;
628 } else if (!strcmp(type, "geneve")) {
629 return OVS_VPORT_TYPE_GENEVE;
630 } else if (strstr(type, "gre64")) {
631 return OVS_VPORT_TYPE_GRE64;
632 } else if (strstr(type, "gre")) {
633 return OVS_VPORT_TYPE_GRE;
634 } else if (!strcmp(type, "vxlan")) {
635 return OVS_VPORT_TYPE_VXLAN;
636 } else if (!strcmp(type, "lisp")) {
637 return OVS_VPORT_TYPE_LISP;
639 return OVS_VPORT_TYPE_UNSPEC;
644 dpif_linux_port_add__(struct dpif_linux *dpif, struct netdev *netdev,
645 odp_port_t *port_nop)
646 OVS_REQ_WRLOCK(dpif->upcall_lock)
648 const struct netdev_tunnel_config *tnl_cfg;
649 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
650 const char *name = netdev_vport_get_dpif_port(netdev,
651 namebuf, sizeof namebuf);
652 const char *type = netdev_get_type(netdev);
653 struct dpif_linux_vport request, reply;
655 uint64_t options_stub[64 / 8];
656 struct ofpbuf options;
657 struct nl_sock **socksp = NULL;
658 uint32_t *upcall_pids;
661 if (dpif->handlers) {
662 socksp = vport_create_socksp(dpif->n_handlers, &error);
668 dpif_linux_vport_init(&request);
669 request.cmd = OVS_VPORT_CMD_NEW;
670 request.dp_ifindex = dpif->dp_ifindex;
671 request.type = netdev_to_ovs_vport_type(netdev);
672 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
673 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
674 "unsupported type `%s'",
675 dpif_name(&dpif->dpif), name, type);
676 vport_del_socksp(socksp, dpif->n_handlers);
681 if (request.type == OVS_VPORT_TYPE_NETDEV) {
682 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
685 tnl_cfg = netdev_get_tunnel_config(netdev);
686 if (tnl_cfg && tnl_cfg->dst_port != 0) {
687 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
688 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
689 ntohs(tnl_cfg->dst_port));
690 request.options = ofpbuf_data(&options);
691 request.options_len = ofpbuf_size(&options);
694 request.port_no = *port_nop;
695 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
696 request.n_upcall_pids = socksp ? dpif->n_handlers : 1;
697 request.upcall_pids = upcall_pids;
699 error = dpif_linux_vport_transact(&request, &reply, &buf);
701 *port_nop = reply.port_no;
703 if (error == EBUSY && *port_nop != ODPP_NONE) {
704 VLOG_INFO("%s: requested port %"PRIu32" is in use",
705 dpif_name(&dpif->dpif), *port_nop);
708 vport_del_socksp(socksp, dpif->n_handlers);
713 error = vport_add_channels(dpif, *port_nop, socksp);
715 VLOG_INFO("%s: could not add channel for port %s",
716 dpif_name(&dpif->dpif), name);
718 /* Delete the port. */
719 dpif_linux_vport_init(&request);
720 request.cmd = OVS_VPORT_CMD_DEL;
721 request.dp_ifindex = dpif->dp_ifindex;
722 request.port_no = *port_nop;
723 dpif_linux_vport_transact(&request, NULL, NULL);
724 vport_del_socksp(socksp, dpif->n_handlers);
738 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
739 odp_port_t *port_nop)
741 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
744 fat_rwlock_wrlock(&dpif->upcall_lock);
745 error = dpif_linux_port_add__(dpif, netdev, port_nop);
746 fat_rwlock_unlock(&dpif->upcall_lock);
752 dpif_linux_port_del__(struct dpif_linux *dpif, odp_port_t port_no)
753 OVS_REQ_WRLOCK(dpif->upcall_lock)
755 struct dpif_linux_vport vport;
758 dpif_linux_vport_init(&vport);
759 vport.cmd = OVS_VPORT_CMD_DEL;
760 vport.dp_ifindex = dpif->dp_ifindex;
761 vport.port_no = port_no;
762 error = dpif_linux_vport_transact(&vport, NULL, NULL);
764 vport_del_channels(dpif, port_no);
770 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
772 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
775 fat_rwlock_wrlock(&dpif->upcall_lock);
776 error = dpif_linux_port_del__(dpif, port_no);
777 fat_rwlock_unlock(&dpif->upcall_lock);
783 dpif_linux_port_query__(const struct dpif_linux *dpif, odp_port_t port_no,
784 const char *port_name, struct dpif_port *dpif_port)
786 struct dpif_linux_vport request;
787 struct dpif_linux_vport reply;
791 dpif_linux_vport_init(&request);
792 request.cmd = OVS_VPORT_CMD_GET;
793 request.dp_ifindex = dpif->dp_ifindex;
794 request.port_no = port_no;
795 request.name = port_name;
797 error = dpif_linux_vport_transact(&request, &reply, &buf);
799 if (reply.dp_ifindex != request.dp_ifindex) {
800 /* A query by name reported that 'port_name' is in some datapath
801 * other than 'dpif', but the caller wants to know about 'dpif'. */
803 } else if (dpif_port) {
804 dpif_port->name = xstrdup(reply.name);
805 dpif_port->type = xstrdup(get_vport_type(&reply));
806 dpif_port->port_no = reply.port_no;
814 dpif_linux_port_query_by_number(const struct dpif *dpif_, odp_port_t port_no,
815 struct dpif_port *dpif_port)
817 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
819 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
823 dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
824 struct dpif_port *dpif_port)
826 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
828 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
832 dpif_linux_port_get_pid__(const struct dpif_linux *dpif, odp_port_t port_no,
834 OVS_REQ_RDLOCK(dpif->upcall_lock)
836 uint32_t port_idx = odp_to_u32(port_no);
839 if (dpif->handlers && dpif->uc_array_size > 0) {
840 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
841 * channel, since it is not heavily loaded. */
842 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
843 struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
845 /* Needs to check in case the socket pointer is changed in between
846 * the holding of upcall_lock. A known case happens when the main
847 * thread deletes the vport while the handler thread is handling
848 * the upcall from that port. */
849 if (h->channels[idx].sock) {
850 pid = nl_sock_pid(h->channels[idx].sock);
858 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
861 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
864 fat_rwlock_rdlock(&dpif->upcall_lock);
865 ret = dpif_linux_port_get_pid__(dpif, port_no, hash);
866 fat_rwlock_unlock(&dpif->upcall_lock);
872 dpif_linux_flow_flush(struct dpif *dpif_)
874 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
875 struct dpif_linux_flow flow;
877 dpif_linux_flow_init(&flow);
878 flow.cmd = OVS_FLOW_CMD_DEL;
879 flow.dp_ifindex = dpif->dp_ifindex;
880 return dpif_linux_flow_transact(&flow, NULL, NULL);
883 struct dpif_linux_port_state {
889 dpif_linux_port_dump_start__(const struct dpif_linux *dpif,
890 struct nl_dump *dump)
892 struct dpif_linux_vport request;
895 dpif_linux_vport_init(&request);
896 request.cmd = OVS_VPORT_CMD_GET;
897 request.dp_ifindex = dpif->dp_ifindex;
899 buf = ofpbuf_new(1024);
900 dpif_linux_vport_to_ofpbuf(&request, buf);
901 nl_dump_start(dump, NETLINK_GENERIC, buf);
906 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
908 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
909 struct dpif_linux_port_state *state;
911 *statep = state = xmalloc(sizeof *state);
912 dpif_linux_port_dump_start__(dpif, &state->dump);
914 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
919 dpif_linux_port_dump_next__(const struct dpif_linux *dpif, struct nl_dump *dump,
920 struct dpif_linux_vport *vport,
921 struct ofpbuf *buffer)
926 if (!nl_dump_next(dump, &buf, buffer)) {
930 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
932 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
933 dpif_name(&dpif->dpif), ovs_strerror(error));
939 dpif_linux_port_dump_next(const struct dpif *dpif_, void *state_,
940 struct dpif_port *dpif_port)
942 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
943 struct dpif_linux_port_state *state = state_;
944 struct dpif_linux_vport vport;
947 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport,
952 dpif_port->name = CONST_CAST(char *, vport.name);
953 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
954 dpif_port->port_no = vport.port_no;
959 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
961 struct dpif_linux_port_state *state = state_;
962 int error = nl_dump_done(&state->dump);
964 ofpbuf_uninit(&state->buf);
970 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
972 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
974 /* Lazily create the Netlink socket to listen for notifications. */
975 if (!dpif->port_notifier) {
976 struct nl_sock *sock;
979 error = nl_sock_create(NETLINK_GENERIC, &sock);
984 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
986 nl_sock_destroy(sock);
989 dpif->port_notifier = sock;
991 /* We have no idea of the current state so report that everything
997 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
998 uint64_t buf_stub[4096 / 8];
1002 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
1003 error = nl_sock_recv(dpif->port_notifier, &buf, false);
1005 struct dpif_linux_vport vport;
1007 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
1009 if (vport.dp_ifindex == dpif->dp_ifindex
1010 && (vport.cmd == OVS_VPORT_CMD_NEW
1011 || vport.cmd == OVS_VPORT_CMD_DEL
1012 || vport.cmd == OVS_VPORT_CMD_SET)) {
1013 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1014 dpif->dpif.full_name, vport.name, vport.cmd);
1015 if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->handlers) {
1016 dpif->refresh_channels = true;
1018 *devnamep = xstrdup(vport.name);
1019 ofpbuf_uninit(&buf);
1023 } else if (error != EAGAIN) {
1024 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
1025 ovs_strerror(error));
1026 nl_sock_drain(dpif->port_notifier);
1030 ofpbuf_uninit(&buf);
1038 dpif_linux_port_poll_wait(const struct dpif *dpif_)
1040 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1042 if (dpif->port_notifier) {
1043 nl_sock_wait(dpif->port_notifier, POLLIN);
1045 poll_immediate_wake();
1050 dpif_linux_flow_get__(const struct dpif_linux *dpif,
1051 const struct nlattr *key, size_t key_len,
1052 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1054 struct dpif_linux_flow request;
1056 dpif_linux_flow_init(&request);
1057 request.cmd = OVS_FLOW_CMD_GET;
1058 request.dp_ifindex = dpif->dp_ifindex;
1060 request.key_len = key_len;
1061 return dpif_linux_flow_transact(&request, reply, bufp);
1065 dpif_linux_flow_get(const struct dpif *dpif_,
1066 const struct nlattr *key, size_t key_len,
1067 struct ofpbuf **bufp,
1068 struct nlattr **maskp, size_t *mask_len,
1069 struct nlattr **actionsp, size_t *actions_len,
1070 struct dpif_flow_stats *stats)
1072 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1073 struct dpif_linux_flow reply;
1076 error = dpif_linux_flow_get__(dpif, key, key_len, &reply, bufp);
1079 *maskp = CONST_CAST(struct nlattr *, reply.mask);
1080 *mask_len = reply.mask_len;
1083 *actionsp = CONST_CAST(struct nlattr *, reply.actions);
1084 *actions_len = reply.actions_len;
1087 dpif_linux_flow_get_stats(&reply, stats);
1094 dpif_linux_init_flow_put(struct dpif_linux *dpif, const struct dpif_flow_put *put,
1095 struct dpif_linux_flow *request)
1097 static const struct nlattr dummy_action;
1099 dpif_linux_flow_init(request);
1100 request->cmd = (put->flags & DPIF_FP_CREATE
1101 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
1102 request->dp_ifindex = dpif->dp_ifindex;
1103 request->key = put->key;
1104 request->key_len = put->key_len;
1105 request->mask = put->mask;
1106 request->mask_len = put->mask_len;
1107 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
1108 request->actions = (put->actions
1110 : CONST_CAST(struct nlattr *, &dummy_action));
1111 request->actions_len = put->actions_len;
1112 if (put->flags & DPIF_FP_ZERO_STATS) {
1113 request->clear = true;
1115 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
1119 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
1121 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1122 struct dpif_linux_flow request, reply;
1126 dpif_linux_init_flow_put(dpif, put, &request);
1127 error = dpif_linux_flow_transact(&request,
1128 put->stats ? &reply : NULL,
1129 put->stats ? &buf : NULL);
1130 if (!error && put->stats) {
1131 dpif_linux_flow_get_stats(&reply, put->stats);
1138 dpif_linux_init_flow_del(struct dpif_linux *dpif, const struct dpif_flow_del *del,
1139 struct dpif_linux_flow *request)
1141 dpif_linux_flow_init(request);
1142 request->cmd = OVS_FLOW_CMD_DEL;
1143 request->dp_ifindex = dpif->dp_ifindex;
1144 request->key = del->key;
1145 request->key_len = del->key_len;
1149 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
1151 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1152 struct dpif_linux_flow request, reply;
1156 dpif_linux_init_flow_del(dpif, del, &request);
1157 error = dpif_linux_flow_transact(&request,
1158 del->stats ? &reply : NULL,
1159 del->stats ? &buf : NULL);
1160 if (!error && del->stats) {
1161 dpif_linux_flow_get_stats(&reply, del->stats);
1167 struct dpif_linux_flow_dump {
1168 struct dpif_flow_dump up;
1169 struct nl_dump nl_dump;
1173 static struct dpif_linux_flow_dump *
1174 dpif_linux_flow_dump_cast(struct dpif_flow_dump *dump)
1176 return CONTAINER_OF(dump, struct dpif_linux_flow_dump, up);
1179 static struct dpif_flow_dump *
1180 dpif_linux_flow_dump_create(const struct dpif *dpif_)
1182 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1183 struct dpif_linux_flow_dump *dump;
1184 struct dpif_linux_flow request;
1187 dump = xmalloc(sizeof *dump);
1188 dpif_flow_dump_init(&dump->up, dpif_);
1190 dpif_linux_flow_init(&request);
1191 request.cmd = OVS_FLOW_CMD_GET;
1192 request.dp_ifindex = dpif->dp_ifindex;
1194 buf = ofpbuf_new(1024);
1195 dpif_linux_flow_to_ofpbuf(&request, buf);
1196 nl_dump_start(&dump->nl_dump, NETLINK_GENERIC, buf);
1198 atomic_init(&dump->status, 0);
1204 dpif_linux_flow_dump_destroy(struct dpif_flow_dump *dump_)
1206 struct dpif_linux_flow_dump *dump = dpif_linux_flow_dump_cast(dump_);
1207 unsigned int nl_status = nl_dump_done(&dump->nl_dump);
1210 atomic_read(&dump->status, &dump_status);
1212 return dump_status ? dump_status : nl_status;
1215 struct dpif_linux_flow_dump_thread {
1216 struct dpif_flow_dump_thread up;
1217 struct dpif_linux_flow_dump *dump;
1218 struct dpif_linux_flow flow;
1219 struct dpif_flow_stats stats;
1220 struct ofpbuf nl_flows; /* Always used to store flows. */
1221 struct ofpbuf *nl_actions; /* Used if kernel does not supply actions. */
1224 static struct dpif_linux_flow_dump_thread *
1225 dpif_linux_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1227 return CONTAINER_OF(thread, struct dpif_linux_flow_dump_thread, up);
1230 static struct dpif_flow_dump_thread *
1231 dpif_linux_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1233 struct dpif_linux_flow_dump *dump = dpif_linux_flow_dump_cast(dump_);
1234 struct dpif_linux_flow_dump_thread *thread;
1236 thread = xmalloc(sizeof *thread);
1237 dpif_flow_dump_thread_init(&thread->up, &dump->up);
1238 thread->dump = dump;
1239 ofpbuf_init(&thread->nl_flows, NL_DUMP_BUFSIZE);
1240 thread->nl_actions = NULL;
1246 dpif_linux_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1248 struct dpif_linux_flow_dump_thread *thread
1249 = dpif_linux_flow_dump_thread_cast(thread_);
1251 ofpbuf_uninit(&thread->nl_flows);
1252 ofpbuf_delete(thread->nl_actions);
1257 dpif_linux_flow_to_dpif_flow(struct dpif_flow *dpif_flow,
1258 struct dpif_linux_flow *linux_flow)
1260 dpif_flow->key = linux_flow->key;
1261 dpif_flow->key_len = linux_flow->key_len;
1262 dpif_flow->mask = linux_flow->mask;
1263 dpif_flow->mask_len = linux_flow->mask_len;
1264 dpif_flow->actions = linux_flow->actions;
1265 dpif_flow->actions_len = linux_flow->actions_len;
1266 dpif_linux_flow_get_stats(linux_flow, &dpif_flow->stats);
1270 dpif_linux_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1271 struct dpif_flow *flows, int max_flows)
1273 struct dpif_linux_flow_dump_thread *thread
1274 = dpif_linux_flow_dump_thread_cast(thread_);
1275 struct dpif_linux_flow_dump *dump = thread->dump;
1276 struct dpif_linux *dpif = dpif_linux_cast(thread->up.dpif);
1279 ofpbuf_delete(thread->nl_actions);
1280 thread->nl_actions = NULL;
1284 || (n_flows < max_flows && ofpbuf_size(&thread->nl_flows))) {
1285 struct dpif_linux_flow linux_flow;
1286 struct ofpbuf nl_flow;
1289 /* Try to grab another flow. */
1290 if (!nl_dump_next(&dump->nl_dump, &nl_flow, &thread->nl_flows)) {
1294 /* Convert the flow to our output format. */
1295 error = dpif_linux_flow_from_ofpbuf(&linux_flow, &nl_flow);
1297 atomic_store(&dump->status, error);
1301 if (linux_flow.actions) {
1302 /* Common case: the flow includes actions. */
1303 dpif_linux_flow_to_dpif_flow(&flows[n_flows++], &linux_flow);
1305 /* Rare case: the flow does not include actions. Retrieve this
1306 * individual flow again to get the actions. */
1307 error = dpif_linux_flow_get__(dpif, linux_flow.key,
1308 linux_flow.key_len, &linux_flow,
1309 &thread->nl_actions);
1310 if (error == ENOENT) {
1311 VLOG_DBG("dumped flow disappeared on get");
1314 VLOG_WARN("error fetching dumped flow: %s",
1315 ovs_strerror(error));
1316 atomic_store(&dump->status, error);
1320 /* Save this flow. Then exit, because we only have one buffer to
1321 * handle this case. */
1322 dpif_linux_flow_to_dpif_flow(&flows[n_flows++], &linux_flow);
1330 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1333 struct ovs_header *k_exec;
1336 ofpbuf_prealloc_tailroom(buf, (64
1337 + ofpbuf_size(d_exec->packet)
1338 + ODP_KEY_METADATA_SIZE
1339 + d_exec->actions_len));
1341 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1342 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1344 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1345 k_exec->dp_ifindex = dp_ifindex;
1347 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1348 ofpbuf_data(d_exec->packet),
1349 ofpbuf_size(d_exec->packet));
1351 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1352 odp_key_from_pkt_metadata(buf, &d_exec->md);
1353 nl_msg_end_nested(buf, key_ofs);
1355 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1356 d_exec->actions, d_exec->actions_len);
1360 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1362 uint64_t request_stub[1024 / 8];
1363 struct ofpbuf request;
1366 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1367 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1368 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1369 ofpbuf_uninit(&request);
1375 dpif_linux_execute(struct dpif *dpif_, struct dpif_execute *execute)
1377 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1379 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1385 dpif_linux_operate__(struct dpif_linux *dpif,
1386 struct dpif_op **ops, size_t n_ops)
1389 struct nl_transaction txn;
1391 struct ofpbuf request;
1392 uint64_t request_stub[1024 / 8];
1394 struct ofpbuf reply;
1395 uint64_t reply_stub[1024 / 8];
1398 struct nl_transaction *txnsp[MAX_OPS];
1401 ovs_assert(n_ops <= MAX_OPS);
1402 for (i = 0; i < n_ops; i++) {
1403 struct op_auxdata *aux = &auxes[i];
1404 struct dpif_op *op = ops[i];
1405 struct dpif_flow_put *put;
1406 struct dpif_flow_del *del;
1407 struct dpif_execute *execute;
1408 struct dpif_linux_flow flow;
1410 ofpbuf_use_stub(&aux->request,
1411 aux->request_stub, sizeof aux->request_stub);
1412 aux->txn.request = &aux->request;
1414 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1415 aux->txn.reply = NULL;
1418 case DPIF_OP_FLOW_PUT:
1419 put = &op->u.flow_put;
1420 dpif_linux_init_flow_put(dpif, put, &flow);
1422 flow.nlmsg_flags |= NLM_F_ECHO;
1423 aux->txn.reply = &aux->reply;
1425 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1428 case DPIF_OP_FLOW_DEL:
1429 del = &op->u.flow_del;
1430 dpif_linux_init_flow_del(dpif, del, &flow);
1432 flow.nlmsg_flags |= NLM_F_ECHO;
1433 aux->txn.reply = &aux->reply;
1435 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1438 case DPIF_OP_EXECUTE:
1439 execute = &op->u.execute;
1440 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1449 for (i = 0; i < n_ops; i++) {
1450 txnsp[i] = &auxes[i].txn;
1452 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1454 for (i = 0; i < n_ops; i++) {
1455 struct op_auxdata *aux = &auxes[i];
1456 struct nl_transaction *txn = &auxes[i].txn;
1457 struct dpif_op *op = ops[i];
1458 struct dpif_flow_put *put;
1459 struct dpif_flow_del *del;
1461 op->error = txn->error;
1464 case DPIF_OP_FLOW_PUT:
1465 put = &op->u.flow_put;
1468 struct dpif_linux_flow reply;
1470 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1473 dpif_linux_flow_get_stats(&reply, put->stats);
1478 memset(put->stats, 0, sizeof *put->stats);
1483 case DPIF_OP_FLOW_DEL:
1484 del = &op->u.flow_del;
1487 struct dpif_linux_flow reply;
1489 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1492 dpif_linux_flow_get_stats(&reply, del->stats);
1497 memset(del->stats, 0, sizeof *del->stats);
1502 case DPIF_OP_EXECUTE:
1509 ofpbuf_uninit(&aux->request);
1510 ofpbuf_uninit(&aux->reply);
1515 dpif_linux_operate(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1517 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1520 size_t chunk = MIN(n_ops, MAX_OPS);
1521 dpif_linux_operate__(dpif, ops, chunk);
1527 /* Synchronizes 'channels' in 'dpif->handlers' with the set of vports
1528 * currently in 'dpif' in the kernel, by adding a new set of channels for
1529 * any kernel vport that lacks one and deleting any channels that have no
1530 * backing kernel vports. */
1532 dpif_linux_refresh_channels(struct dpif_linux *dpif, uint32_t n_handlers)
1533 OVS_REQ_WRLOCK(dpif->upcall_lock)
1535 unsigned long int *keep_channels;
1536 struct dpif_linux_vport vport;
1537 size_t keep_channels_nbits;
1538 struct nl_dump dump;
1539 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1544 if (dpif->n_handlers != n_handlers) {
1545 destroy_all_channels(dpif);
1546 dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
1547 for (i = 0; i < n_handlers; i++) {
1548 struct dpif_handler *handler = &dpif->handlers[i];
1550 handler->epoll_fd = epoll_create(10);
1551 if (handler->epoll_fd < 0) {
1554 for (j = 0; j < i; j++) {
1555 close(dpif->handlers[j].epoll_fd);
1557 free(dpif->handlers);
1558 dpif->handlers = NULL;
1563 dpif->n_handlers = n_handlers;
1566 for (i = 0; i < n_handlers; i++) {
1567 struct dpif_handler *handler = &dpif->handlers[i];
1569 handler->event_offset = handler->n_events = 0;
1572 keep_channels_nbits = dpif->uc_array_size;
1573 keep_channels = bitmap_allocate(keep_channels_nbits);
1575 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1576 dpif_linux_port_dump_start__(dpif, &dump);
1577 while (!dpif_linux_port_dump_next__(dpif, &dump, &vport, &buf)) {
1578 uint32_t port_no = odp_to_u32(vport.port_no);
1579 uint32_t *upcall_pids = NULL;
1582 if (port_no >= dpif->uc_array_size
1583 || !vport_get_pids(dpif, port_no, &upcall_pids)) {
1584 struct nl_sock **socksp = vport_create_socksp(dpif->n_handlers,
1591 error = vport_add_channels(dpif, vport.port_no, socksp);
1593 VLOG_INFO("%s: could not add channels for port %s",
1594 dpif_name(&dpif->dpif), vport.name);
1595 vport_del_socksp(socksp, dpif->n_handlers);
1599 upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
1603 /* Configure the vport to deliver misses to 'sock'. */
1604 if (vport.upcall_pids[0] == 0
1605 || vport.n_upcall_pids != dpif->n_handlers
1606 || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
1608 struct dpif_linux_vport vport_request;
1610 dpif_linux_vport_init(&vport_request);
1611 vport_request.cmd = OVS_VPORT_CMD_SET;
1612 vport_request.dp_ifindex = dpif->dp_ifindex;
1613 vport_request.port_no = vport.port_no;
1614 vport_request.n_upcall_pids = dpif->n_handlers;
1615 vport_request.upcall_pids = upcall_pids;
1616 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1618 VLOG_WARN_RL(&error_rl,
1619 "%s: failed to set upcall pid on port: %s",
1620 dpif_name(&dpif->dpif), ovs_strerror(error));
1622 if (error != ENODEV && error != ENOENT) {
1625 /* The vport isn't really there, even though the dump says
1626 * it is. Probably we just hit a race after a port
1633 if (port_no < keep_channels_nbits) {
1634 bitmap_set1(keep_channels, port_no);
1641 vport_del_channels(dpif, vport.port_no);
1643 nl_dump_done(&dump);
1644 ofpbuf_uninit(&buf);
1646 /* Discard any saved channels that we didn't reuse. */
1647 for (i = 0; i < keep_channels_nbits; i++) {
1648 if (!bitmap_is_set(keep_channels, i)) {
1649 vport_del_channels(dpif, u32_to_odp(i));
1652 free(keep_channels);
1658 dpif_linux_recv_set__(struct dpif_linux *dpif, bool enable)
1659 OVS_REQ_WRLOCK(dpif->upcall_lock)
1661 if ((dpif->handlers != NULL) == enable) {
1663 } else if (!enable) {
1664 destroy_all_channels(dpif);
1667 return dpif_linux_refresh_channels(dpif, 1);
1672 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1674 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1677 fat_rwlock_wrlock(&dpif->upcall_lock);
1678 error = dpif_linux_recv_set__(dpif, enable);
1679 fat_rwlock_unlock(&dpif->upcall_lock);
1685 dpif_linux_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
1687 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1690 fat_rwlock_wrlock(&dpif->upcall_lock);
1691 if (dpif->handlers) {
1692 error = dpif_linux_refresh_channels(dpif, n_handlers);
1694 fat_rwlock_unlock(&dpif->upcall_lock);
1700 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1701 uint32_t queue_id, uint32_t *priority)
1703 if (queue_id < 0xf000) {
1704 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1712 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1715 static const struct nl_policy ovs_packet_policy[] = {
1716 /* Always present. */
1717 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1718 .min_len = ETH_HEADER_LEN },
1719 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1721 /* OVS_PACKET_CMD_ACTION only. */
1722 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1725 struct ovs_header *ovs_header;
1726 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1727 struct nlmsghdr *nlmsg;
1728 struct genlmsghdr *genl;
1732 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1734 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1735 genl = ofpbuf_try_pull(&b, sizeof *genl);
1736 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1737 if (!nlmsg || !genl || !ovs_header
1738 || nlmsg->nlmsg_type != ovs_packet_family
1739 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1740 ARRAY_SIZE(ovs_packet_policy))) {
1744 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1745 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1751 /* (Re)set ALL fields of '*upcall' on successful return. */
1752 upcall->type = type;
1753 upcall->key = CONST_CAST(struct nlattr *,
1754 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1755 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1756 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1758 /* Allow overwriting the netlink attribute header without reallocating. */
1759 ofpbuf_use_stub(&upcall->packet,
1760 CONST_CAST(struct nlattr *,
1761 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1762 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1763 sizeof(struct nlattr));
1764 ofpbuf_set_data(&upcall->packet,
1765 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
1766 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
1768 *dp_ifindex = ovs_header->dp_ifindex;
1774 dpif_linux_recv__(struct dpif_linux *dpif, uint32_t handler_id,
1775 struct dpif_upcall *upcall, struct ofpbuf *buf)
1776 OVS_REQ_RDLOCK(dpif->upcall_lock)
1778 struct dpif_handler *handler;
1781 if (!dpif->handlers || handler_id >= dpif->n_handlers) {
1785 handler = &dpif->handlers[handler_id];
1786 if (handler->event_offset >= handler->n_events) {
1789 handler->event_offset = handler->n_events = 0;
1792 retval = epoll_wait(handler->epoll_fd, handler->epoll_events,
1793 dpif->uc_array_size, 0);
1794 } while (retval < 0 && errno == EINTR);
1796 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1797 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1798 } else if (retval > 0) {
1799 handler->n_events = retval;
1803 while (handler->event_offset < handler->n_events) {
1804 int idx = handler->epoll_events[handler->event_offset].data.u32;
1805 struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
1807 handler->event_offset++;
1813 if (++read_tries > 50) {
1817 error = nl_sock_recv(ch->sock, buf, false);
1818 if (error == ENOBUFS) {
1819 /* ENOBUFS typically means that we've received so many
1820 * packets that the buffer overflowed. Try again
1821 * immediately because there's almost certainly a packet
1822 * waiting for us. */
1823 report_loss(dpif, ch, idx, handler_id);
1827 ch->last_poll = time_msec();
1829 if (error == EAGAIN) {
1835 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1836 if (!error && dp_ifindex == dpif->dp_ifindex) {
1848 dpif_linux_recv(struct dpif *dpif_, uint32_t handler_id,
1849 struct dpif_upcall *upcall, struct ofpbuf *buf)
1851 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1854 fat_rwlock_rdlock(&dpif->upcall_lock);
1855 error = dpif_linux_recv__(dpif, handler_id, upcall, buf);
1856 fat_rwlock_unlock(&dpif->upcall_lock);
1862 dpif_linux_recv_wait__(struct dpif_linux *dpif, uint32_t handler_id)
1863 OVS_REQ_RDLOCK(dpif->upcall_lock)
1865 if (dpif->handlers && handler_id < dpif->n_handlers) {
1866 struct dpif_handler *handler = &dpif->handlers[handler_id];
1868 poll_fd_wait(handler->epoll_fd, POLLIN);
1873 dpif_linux_recv_wait(struct dpif *dpif_, uint32_t handler_id)
1875 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1877 fat_rwlock_rdlock(&dpif->upcall_lock);
1878 dpif_linux_recv_wait__(dpif, handler_id);
1879 fat_rwlock_unlock(&dpif->upcall_lock);
1883 dpif_linux_recv_purge__(struct dpif_linux *dpif)
1884 OVS_REQ_WRLOCK(dpif->upcall_lock)
1886 if (dpif->handlers) {
1889 for (i = 0; i < dpif->uc_array_size; i++ ) {
1890 if (!dpif->handlers[0].channels[i].sock) {
1894 for (j = 0; j < dpif->n_handlers; j++) {
1895 nl_sock_drain(dpif->handlers[j].channels[i].sock);
1902 dpif_linux_recv_purge(struct dpif *dpif_)
1904 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1906 fat_rwlock_wrlock(&dpif->upcall_lock);
1907 dpif_linux_recv_purge__(dpif);
1908 fat_rwlock_unlock(&dpif->upcall_lock);
1911 const struct dpif_class dpif_linux_class = {
1913 dpif_linux_enumerate,
1920 dpif_linux_get_stats,
1921 dpif_linux_port_add,
1922 dpif_linux_port_del,
1923 dpif_linux_port_query_by_number,
1924 dpif_linux_port_query_by_name,
1925 dpif_linux_port_get_pid,
1926 dpif_linux_port_dump_start,
1927 dpif_linux_port_dump_next,
1928 dpif_linux_port_dump_done,
1929 dpif_linux_port_poll,
1930 dpif_linux_port_poll_wait,
1931 dpif_linux_flow_get,
1932 dpif_linux_flow_put,
1933 dpif_linux_flow_del,
1934 dpif_linux_flow_flush,
1935 dpif_linux_flow_dump_create,
1936 dpif_linux_flow_dump_destroy,
1937 dpif_linux_flow_dump_thread_create,
1938 dpif_linux_flow_dump_thread_destroy,
1939 dpif_linux_flow_dump_next,
1942 dpif_linux_recv_set,
1943 dpif_linux_handlers_set,
1944 dpif_linux_queue_to_priority,
1946 dpif_linux_recv_wait,
1947 dpif_linux_recv_purge,
1948 NULL, /* register_upcall_cb */
1949 NULL, /* enable_upcall */
1950 NULL, /* disable_upcall */
1954 dpif_linux_init(void)
1956 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1959 if (ovsthread_once_start(&once)) {
1960 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1961 &ovs_datapath_family);
1963 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1964 "The Open vSwitch kernel module is probably not loaded.",
1965 OVS_DATAPATH_FAMILY);
1968 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1971 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1974 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1975 &ovs_packet_family);
1978 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1979 &ovs_vport_mcgroup);
1982 ovsthread_once_done(&once);
1989 dpif_linux_is_internal_device(const char *name)
1991 struct dpif_linux_vport reply;
1995 error = dpif_linux_vport_get(name, &reply, &buf);
1998 } else if (error != ENODEV && error != ENOENT) {
1999 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
2000 name, ovs_strerror(error));
2003 return reply.type == OVS_VPORT_TYPE_INTERNAL;
2006 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2007 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
2008 * positive errno value.
2010 * 'vport' will contain pointers into 'buf', so the caller should not free
2011 * 'buf' while 'vport' is still in use. */
2013 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
2014 const struct ofpbuf *buf)
2016 static const struct nl_policy ovs_vport_policy[] = {
2017 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
2018 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
2019 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2020 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_UNSPEC },
2021 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
2023 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
2026 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
2027 struct ovs_header *ovs_header;
2028 struct nlmsghdr *nlmsg;
2029 struct genlmsghdr *genl;
2032 dpif_linux_vport_init(vport);
2034 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2035 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2036 genl = ofpbuf_try_pull(&b, sizeof *genl);
2037 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2038 if (!nlmsg || !genl || !ovs_header
2039 || nlmsg->nlmsg_type != ovs_vport_family
2040 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
2041 ARRAY_SIZE(ovs_vport_policy))) {
2045 vport->cmd = genl->cmd;
2046 vport->dp_ifindex = ovs_header->dp_ifindex;
2047 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
2048 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2049 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
2050 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2051 vport->n_upcall_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PID])
2052 / (sizeof *vport->upcall_pids);
2053 vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
2056 if (a[OVS_VPORT_ATTR_STATS]) {
2057 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
2059 if (a[OVS_VPORT_ATTR_OPTIONS]) {
2060 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
2061 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
2066 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2067 * followed by Netlink attributes corresponding to 'vport'. */
2069 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
2072 struct ovs_header *ovs_header;
2074 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
2075 vport->cmd, OVS_VPORT_VERSION);
2077 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2078 ovs_header->dp_ifindex = vport->dp_ifindex;
2080 if (vport->port_no != ODPP_NONE) {
2081 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
2084 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
2085 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
2089 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
2092 if (vport->upcall_pids) {
2093 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PID,
2095 vport->n_upcall_pids * sizeof *vport->upcall_pids);
2099 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
2100 vport->stats, sizeof *vport->stats);
2103 if (vport->options) {
2104 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
2105 vport->options, vport->options_len);
2109 /* Clears 'vport' to "empty" values. */
2111 dpif_linux_vport_init(struct dpif_linux_vport *vport)
2113 memset(vport, 0, sizeof *vport);
2114 vport->port_no = ODPP_NONE;
2117 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2118 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2119 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2120 * result of the command is expected to be an ovs_vport also, which is decoded
2121 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2122 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2124 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
2125 struct dpif_linux_vport *reply,
2126 struct ofpbuf **bufp)
2128 struct ofpbuf *request_buf;
2131 ovs_assert((reply != NULL) == (bufp != NULL));
2133 error = dpif_linux_init();
2137 dpif_linux_vport_init(reply);
2142 request_buf = ofpbuf_new(1024);
2143 dpif_linux_vport_to_ofpbuf(request, request_buf);
2144 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2145 ofpbuf_delete(request_buf);
2149 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
2152 dpif_linux_vport_init(reply);
2153 ofpbuf_delete(*bufp);
2160 /* Obtains information about the kernel vport named 'name' and stores it into
2161 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
2162 * longer needed ('reply' will contain pointers into '*bufp'). */
2164 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
2165 struct ofpbuf **bufp)
2167 struct dpif_linux_vport request;
2169 dpif_linux_vport_init(&request);
2170 request.cmd = OVS_VPORT_CMD_GET;
2171 request.name = name;
2173 return dpif_linux_vport_transact(&request, reply, bufp);
2176 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2177 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
2178 * positive errno value.
2180 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
2181 * while 'dp' is still in use. */
2183 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
2185 static const struct nl_policy ovs_datapath_policy[] = {
2186 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
2187 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
2189 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
2190 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
2194 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
2195 struct ovs_header *ovs_header;
2196 struct nlmsghdr *nlmsg;
2197 struct genlmsghdr *genl;
2200 dpif_linux_dp_init(dp);
2202 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2203 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2204 genl = ofpbuf_try_pull(&b, sizeof *genl);
2205 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2206 if (!nlmsg || !genl || !ovs_header
2207 || nlmsg->nlmsg_type != ovs_datapath_family
2208 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
2209 ARRAY_SIZE(ovs_datapath_policy))) {
2213 dp->cmd = genl->cmd;
2214 dp->dp_ifindex = ovs_header->dp_ifindex;
2215 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
2216 if (a[OVS_DP_ATTR_STATS]) {
2217 dp->stats = nl_attr_get(a[OVS_DP_ATTR_STATS]);
2220 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
2221 dp->megaflow_stats = nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]);
2227 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
2229 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
2231 struct ovs_header *ovs_header;
2233 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
2234 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
2235 OVS_DATAPATH_VERSION);
2237 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2238 ovs_header->dp_ifindex = dp->dp_ifindex;
2241 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
2244 if (dp->upcall_pid) {
2245 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
2248 if (dp->user_features) {
2249 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2252 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2255 /* Clears 'dp' to "empty" values. */
2257 dpif_linux_dp_init(struct dpif_linux_dp *dp)
2259 memset(dp, 0, sizeof *dp);
2263 dpif_linux_dp_dump_start(struct nl_dump *dump)
2265 struct dpif_linux_dp request;
2268 dpif_linux_dp_init(&request);
2269 request.cmd = OVS_DP_CMD_GET;
2271 buf = ofpbuf_new(1024);
2272 dpif_linux_dp_to_ofpbuf(&request, buf);
2273 nl_dump_start(dump, NETLINK_GENERIC, buf);
2277 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2278 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2279 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2280 * result of the command is expected to be of the same form, which is decoded
2281 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2282 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2284 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
2285 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
2287 struct ofpbuf *request_buf;
2290 ovs_assert((reply != NULL) == (bufp != NULL));
2292 request_buf = ofpbuf_new(1024);
2293 dpif_linux_dp_to_ofpbuf(request, request_buf);
2294 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2295 ofpbuf_delete(request_buf);
2298 dpif_linux_dp_init(reply);
2300 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
2303 ofpbuf_delete(*bufp);
2310 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2311 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2312 * will contain pointers into '*bufp'). */
2314 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2315 struct ofpbuf **bufp)
2317 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2318 struct dpif_linux_dp request;
2320 dpif_linux_dp_init(&request);
2321 request.cmd = OVS_DP_CMD_GET;
2322 request.dp_ifindex = dpif->dp_ifindex;
2324 return dpif_linux_dp_transact(&request, reply, bufp);
2327 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2328 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2329 * positive errno value.
2331 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2332 * while 'flow' is still in use. */
2334 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2335 const struct ofpbuf *buf)
2337 static const struct nl_policy ovs_flow_policy[] = {
2338 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2339 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2340 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2341 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2343 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2344 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2345 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2348 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2349 struct ovs_header *ovs_header;
2350 struct nlmsghdr *nlmsg;
2351 struct genlmsghdr *genl;
2354 dpif_linux_flow_init(flow);
2356 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2357 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2358 genl = ofpbuf_try_pull(&b, sizeof *genl);
2359 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2360 if (!nlmsg || !genl || !ovs_header
2361 || nlmsg->nlmsg_type != ovs_flow_family
2362 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2363 ARRAY_SIZE(ovs_flow_policy))) {
2367 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2368 flow->dp_ifindex = ovs_header->dp_ifindex;
2369 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2370 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2372 if (a[OVS_FLOW_ATTR_MASK]) {
2373 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2374 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2376 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2377 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2378 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2380 if (a[OVS_FLOW_ATTR_STATS]) {
2381 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2383 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2384 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2386 if (a[OVS_FLOW_ATTR_USED]) {
2387 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2392 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2393 * followed by Netlink attributes corresponding to 'flow'. */
2395 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2398 struct ovs_header *ovs_header;
2400 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2401 NLM_F_REQUEST | flow->nlmsg_flags,
2402 flow->cmd, OVS_FLOW_VERSION);
2404 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2405 ovs_header->dp_ifindex = flow->dp_ifindex;
2407 if (flow->key_len) {
2408 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2411 if (flow->mask_len) {
2412 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2415 if (flow->actions || flow->actions_len) {
2416 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2417 flow->actions, flow->actions_len);
2420 /* We never need to send these to the kernel. */
2421 ovs_assert(!flow->stats);
2422 ovs_assert(!flow->tcp_flags);
2423 ovs_assert(!flow->used);
2426 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2430 /* Clears 'flow' to "empty" values. */
2432 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2434 memset(flow, 0, sizeof *flow);
2437 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2438 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2439 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2440 * result of the command is expected to be a flow also, which is decoded and
2441 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2442 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2444 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2445 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2447 struct ofpbuf *request_buf;
2450 ovs_assert((reply != NULL) == (bufp != NULL));
2453 request->nlmsg_flags |= NLM_F_ECHO;
2456 request_buf = ofpbuf_new(1024);
2457 dpif_linux_flow_to_ofpbuf(request, request_buf);
2458 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2459 ofpbuf_delete(request_buf);
2463 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2466 dpif_linux_flow_init(reply);
2467 ofpbuf_delete(*bufp);
2475 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2476 struct dpif_flow_stats *stats)
2479 stats->n_packets = get_32aligned_u64(&flow->stats->n_packets);
2480 stats->n_bytes = get_32aligned_u64(&flow->stats->n_bytes);
2482 stats->n_packets = 0;
2485 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2486 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2489 /* Logs information about a packet that was recently lost in 'ch' (in
2492 report_loss(struct dpif_linux *dpif, struct dpif_channel *ch, uint32_t ch_idx,
2493 uint32_t handler_id)
2495 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2498 if (VLOG_DROP_WARN(&rl)) {
2503 if (ch->last_poll != LLONG_MIN) {
2504 ds_put_format(&s, " (last polled %lld ms ago)",
2505 time_msec() - ch->last_poll);
2508 VLOG_WARN("%s: lost packet on port channel %u of handler %u",
2509 dpif_name(&dpif->dpif), ch_idx, handler_id);