2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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"
40 #include "netdev-linux.h"
41 #include "netdev-vport.h"
42 #include "netlink-notifier.h"
43 #include "netlink-socket.h"
48 #include "poll-loop.h"
53 #include "unaligned.h"
57 VLOG_DEFINE_THIS_MODULE(dpif_linux);
58 enum { MAX_PORTS = USHRT_MAX };
60 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
61 * missing if we have old headers. */
62 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
64 struct dpif_linux_dp {
65 /* Generic Netlink header. */
68 /* struct ovs_header. */
72 const char *name; /* OVS_DP_ATTR_NAME. */
73 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
74 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
75 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
76 struct ovs_dp_megaflow_stats megaflow_stats;
77 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
80 static void dpif_linux_dp_init(struct dpif_linux_dp *);
81 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
82 const struct ofpbuf *);
83 static void dpif_linux_dp_dump_start(struct nl_dump *);
84 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
85 struct dpif_linux_dp *reply,
86 struct ofpbuf **bufp);
87 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
88 struct ofpbuf **bufp);
90 struct dpif_linux_flow {
91 /* Generic Netlink header. */
94 /* struct ovs_header. */
95 unsigned int nlmsg_flags;
100 * The 'stats' member points to 64-bit data that might only be aligned on
101 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
104 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
105 * the Netlink version of the command, even if actions_len is zero. */
106 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
108 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
110 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
112 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
113 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
114 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
115 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
118 static void dpif_linux_flow_init(struct dpif_linux_flow *);
119 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
120 const struct ofpbuf *);
121 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
123 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
124 struct dpif_linux_flow *reply,
125 struct ofpbuf **bufp);
126 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
127 struct dpif_flow_stats *);
129 /* One of the dpif channels between the kernel and userspace. */
130 struct dpif_channel {
131 struct nl_sock *sock; /* Netlink socket. */
132 long long int last_poll; /* Last time this channel was polled. */
135 static void report_loss(struct dpif *, struct dpif_channel *);
137 /* Datapath interface for the openvswitch Linux kernel module. */
142 /* Upcall messages. */
143 struct ovs_mutex upcall_lock;
144 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
145 struct dpif_channel *channels;
146 struct epoll_event *epoll_events;
147 int epoll_fd; /* epoll fd that includes channel socks. */
148 int n_events; /* Num events returned by epoll_wait(). */
149 int event_offset; /* Offset into 'epoll_events'. */
151 /* Change notification. */
152 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
153 bool refresh_channels;
156 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
158 /* Generic Netlink family numbers for OVS.
160 * Initialized by dpif_linux_init(). */
161 static int ovs_datapath_family;
162 static int ovs_vport_family;
163 static int ovs_flow_family;
164 static int ovs_packet_family;
166 /* Generic Netlink multicast groups for OVS.
168 * Initialized by dpif_linux_init(). */
169 static unsigned int ovs_vport_mcgroup;
171 static int dpif_linux_init(void);
172 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
173 static uint32_t dpif_linux_port_get_pid(const struct dpif *,
175 static int dpif_linux_refresh_channels(struct dpif *);
177 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
179 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
180 const struct ofpbuf *);
182 static struct dpif_linux *
183 dpif_linux_cast(const struct dpif *dpif)
185 dpif_assert_class(dpif, &dpif_linux_class);
186 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
190 dpif_linux_enumerate(struct sset *all_dps)
193 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
194 struct ofpbuf msg, buf;
197 error = dpif_linux_init();
202 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
203 dpif_linux_dp_dump_start(&dump);
204 while (nl_dump_next(&dump, &msg, &buf)) {
205 struct dpif_linux_dp dp;
207 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
208 sset_add(all_dps, dp.name);
212 return nl_dump_done(&dump);
216 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
217 bool create, struct dpif **dpifp)
219 struct dpif_linux_dp dp_request, dp;
224 error = dpif_linux_init();
229 /* Create or look up datapath. */
230 dpif_linux_dp_init(&dp_request);
232 dp_request.cmd = OVS_DP_CMD_NEW;
234 dp_request.upcall_pid = &upcall_pid;
236 /* Use OVS_DP_CMD_SET to report user features */
237 dp_request.cmd = OVS_DP_CMD_SET;
239 dp_request.name = name;
240 dp_request.user_features |= OVS_DP_F_UNALIGNED;
241 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
246 error = open_dpif(&dp, dpifp);
252 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
254 struct dpif_linux *dpif;
256 dpif = xzalloc(sizeof *dpif);
257 dpif->port_notifier = NULL;
258 ovs_mutex_init(&dpif->upcall_lock);
261 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
262 dp->dp_ifindex, dp->dp_ifindex);
264 dpif->dp_ifindex = dp->dp_ifindex;
265 *dpifp = &dpif->dpif;
271 destroy_channels(struct dpif_linux *dpif)
275 if (dpif->epoll_fd < 0) {
279 for (i = 0; i < dpif->uc_array_size; i++ ) {
280 struct dpif_linux_vport vport_request;
281 struct dpif_channel *ch = &dpif->channels[i];
282 uint32_t upcall_pid = 0;
288 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
290 /* Turn off upcalls. */
291 dpif_linux_vport_init(&vport_request);
292 vport_request.cmd = OVS_VPORT_CMD_SET;
293 vport_request.dp_ifindex = dpif->dp_ifindex;
294 vport_request.port_no = u32_to_odp(i);
295 vport_request.upcall_pid = &upcall_pid;
296 dpif_linux_vport_transact(&vport_request, NULL, NULL);
298 nl_sock_destroy(ch->sock);
301 free(dpif->channels);
302 dpif->channels = NULL;
303 dpif->uc_array_size = 0;
305 free(dpif->epoll_events);
306 dpif->epoll_events = NULL;
307 dpif->n_events = dpif->event_offset = 0;
309 /* Don't close dpif->epoll_fd since that would cause other threads that
310 * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */
314 add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
316 struct epoll_event event;
317 uint32_t port_idx = odp_to_u32(port_no);
319 if (dpif->epoll_fd < 0) {
323 /* We assume that the datapath densely chooses port numbers, which
324 * can therefore be used as an index into an array of channels. */
325 if (port_idx >= dpif->uc_array_size) {
326 uint32_t new_size = port_idx + 1;
329 if (new_size > MAX_PORTS) {
330 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
331 dpif_name(&dpif->dpif), port_no);
335 dpif->channels = xrealloc(dpif->channels,
336 new_size * sizeof *dpif->channels);
337 for (i = dpif->uc_array_size; i < new_size; i++) {
338 dpif->channels[i].sock = NULL;
341 dpif->epoll_events = xrealloc(dpif->epoll_events,
342 new_size * sizeof *dpif->epoll_events);
343 dpif->uc_array_size = new_size;
346 memset(&event, 0, sizeof event);
347 event.events = EPOLLIN;
348 event.data.u32 = port_idx;
349 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
354 nl_sock_destroy(dpif->channels[port_idx].sock);
355 dpif->channels[port_idx].sock = sock;
356 dpif->channels[port_idx].last_poll = LLONG_MIN;
362 del_channel(struct dpif_linux *dpif, odp_port_t port_no)
364 struct dpif_channel *ch;
365 uint32_t port_idx = odp_to_u32(port_no);
367 if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
371 ch = &dpif->channels[port_idx];
376 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
377 dpif->event_offset = dpif->n_events = 0;
379 nl_sock_destroy(ch->sock);
384 dpif_linux_close(struct dpif *dpif_)
386 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
388 nl_sock_destroy(dpif->port_notifier);
389 destroy_channels(dpif);
390 if (dpif->epoll_fd >= 0) {
391 close(dpif->epoll_fd);
393 ovs_mutex_destroy(&dpif->upcall_lock);
398 dpif_linux_destroy(struct dpif *dpif_)
400 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
401 struct dpif_linux_dp dp;
403 dpif_linux_dp_init(&dp);
404 dp.cmd = OVS_DP_CMD_DEL;
405 dp.dp_ifindex = dpif->dp_ifindex;
406 return dpif_linux_dp_transact(&dp, NULL, NULL);
410 dpif_linux_run(struct dpif *dpif_)
412 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
413 if (dpif->refresh_channels) {
414 dpif->refresh_channels = false;
415 dpif_linux_refresh_channels(dpif_);
420 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
422 struct dpif_linux_dp dp;
426 error = dpif_linux_dp_get(dpif_, &dp, &buf);
428 stats->n_hit = dp.stats.n_hit;
429 stats->n_missed = dp.stats.n_missed;
430 stats->n_lost = dp.stats.n_lost;
431 stats->n_flows = dp.stats.n_flows;
432 stats->n_masks = dp.megaflow_stats.n_masks;
433 stats->n_mask_hit = dp.megaflow_stats.n_mask_hit;
440 get_vport_type(const struct dpif_linux_vport *vport)
442 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
444 switch (vport->type) {
445 case OVS_VPORT_TYPE_NETDEV: {
446 const char *type = netdev_get_type_from_name(vport->name);
448 return type ? type : "system";
451 case OVS_VPORT_TYPE_INTERNAL:
454 case OVS_VPORT_TYPE_GRE:
457 case OVS_VPORT_TYPE_GRE64:
460 case OVS_VPORT_TYPE_VXLAN:
463 case OVS_VPORT_TYPE_LISP:
466 case OVS_VPORT_TYPE_UNSPEC:
467 case __OVS_VPORT_TYPE_MAX:
471 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
472 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
476 static enum ovs_vport_type
477 netdev_to_ovs_vport_type(const struct netdev *netdev)
479 const char *type = netdev_get_type(netdev);
481 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
482 return OVS_VPORT_TYPE_NETDEV;
483 } else if (!strcmp(type, "internal")) {
484 return OVS_VPORT_TYPE_INTERNAL;
485 } else if (strstr(type, "gre64")) {
486 return OVS_VPORT_TYPE_GRE64;
487 } else if (strstr(type, "gre")) {
488 return OVS_VPORT_TYPE_GRE;
489 } else if (!strcmp(type, "vxlan")) {
490 return OVS_VPORT_TYPE_VXLAN;
491 } else if (!strcmp(type, "lisp")) {
492 return OVS_VPORT_TYPE_LISP;
494 return OVS_VPORT_TYPE_UNSPEC;
499 dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
500 odp_port_t *port_nop)
502 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
503 const struct netdev_tunnel_config *tnl_cfg;
504 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
505 const char *name = netdev_vport_get_dpif_port(netdev,
506 namebuf, sizeof namebuf);
507 const char *type = netdev_get_type(netdev);
508 struct dpif_linux_vport request, reply;
509 struct nl_sock *sock = NULL;
512 uint64_t options_stub[64 / 8];
513 struct ofpbuf options;
516 if (dpif->epoll_fd >= 0) {
517 error = nl_sock_create(NETLINK_GENERIC, &sock);
523 dpif_linux_vport_init(&request);
524 request.cmd = OVS_VPORT_CMD_NEW;
525 request.dp_ifindex = dpif->dp_ifindex;
526 request.type = netdev_to_ovs_vport_type(netdev);
527 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
528 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
529 "unsupported type `%s'",
530 dpif_name(dpif_), name, type);
531 nl_sock_destroy(sock);
536 if (request.type == OVS_VPORT_TYPE_NETDEV) {
537 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
540 tnl_cfg = netdev_get_tunnel_config(netdev);
541 if (tnl_cfg && tnl_cfg->dst_port != 0) {
542 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
543 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
544 ntohs(tnl_cfg->dst_port));
545 request.options = options.data;
546 request.options_len = options.size;
549 request.port_no = *port_nop;
550 upcall_pid = sock ? nl_sock_pid(sock) : 0;
551 request.upcall_pid = &upcall_pid;
553 error = dpif_linux_vport_transact(&request, &reply, &buf);
555 *port_nop = reply.port_no;
556 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
557 dpif_name(dpif_), reply.port_no, upcall_pid);
559 if (error == EBUSY && *port_nop != ODPP_NONE) {
560 VLOG_INFO("%s: requested port %"PRIu32" is in use",
561 dpif_name(dpif_), *port_nop);
563 nl_sock_destroy(sock);
570 error = add_channel(dpif, *port_nop, sock);
572 VLOG_INFO("%s: could not add channel for port %s",
573 dpif_name(dpif_), name);
575 /* Delete the port. */
576 dpif_linux_vport_init(&request);
577 request.cmd = OVS_VPORT_CMD_DEL;
578 request.dp_ifindex = dpif->dp_ifindex;
579 request.port_no = *port_nop;
580 dpif_linux_vport_transact(&request, NULL, NULL);
582 nl_sock_destroy(sock);
591 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
592 odp_port_t *port_nop)
594 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
597 ovs_mutex_lock(&dpif->upcall_lock);
598 error = dpif_linux_port_add__(dpif_, netdev, port_nop);
599 ovs_mutex_unlock(&dpif->upcall_lock);
605 dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
607 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
608 struct dpif_linux_vport vport;
611 dpif_linux_vport_init(&vport);
612 vport.cmd = OVS_VPORT_CMD_DEL;
613 vport.dp_ifindex = dpif->dp_ifindex;
614 vport.port_no = port_no;
615 error = dpif_linux_vport_transact(&vport, NULL, NULL);
617 del_channel(dpif, port_no);
623 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
625 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
628 ovs_mutex_lock(&dpif->upcall_lock);
629 error = dpif_linux_port_del__(dpif_, port_no);
630 ovs_mutex_unlock(&dpif->upcall_lock);
636 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
637 const char *port_name, struct dpif_port *dpif_port)
639 struct dpif_linux_vport request;
640 struct dpif_linux_vport reply;
644 dpif_linux_vport_init(&request);
645 request.cmd = OVS_VPORT_CMD_GET;
646 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
647 request.port_no = port_no;
648 request.name = port_name;
650 error = dpif_linux_vport_transact(&request, &reply, &buf);
652 if (reply.dp_ifindex != request.dp_ifindex) {
653 /* A query by name reported that 'port_name' is in some datapath
654 * other than 'dpif', but the caller wants to know about 'dpif'. */
656 } else if (dpif_port) {
657 dpif_port->name = xstrdup(reply.name);
658 dpif_port->type = xstrdup(get_vport_type(&reply));
659 dpif_port->port_no = reply.port_no;
667 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
668 struct dpif_port *dpif_port)
670 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
674 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
675 struct dpif_port *dpif_port)
677 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
681 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
683 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
684 uint32_t port_idx = odp_to_u32(port_no);
687 ovs_mutex_lock(&dpif->upcall_lock);
688 if (dpif->epoll_fd >= 0) {
689 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
690 * channel, since it is not heavily loaded. */
691 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
692 const struct nl_sock *sock = dpif->channels[idx].sock;
693 pid = sock ? nl_sock_pid(sock) : 0;
695 ovs_mutex_unlock(&dpif->upcall_lock);
701 dpif_linux_flow_flush(struct dpif *dpif_)
703 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
704 struct dpif_linux_flow flow;
706 dpif_linux_flow_init(&flow);
707 flow.cmd = OVS_FLOW_CMD_DEL;
708 flow.dp_ifindex = dpif->dp_ifindex;
709 return dpif_linux_flow_transact(&flow, NULL, NULL);
712 struct dpif_linux_port_state {
718 dpif_linux_port_dump_start__(const struct dpif *dpif_, struct nl_dump *dump)
720 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
721 struct dpif_linux_vport request;
724 dpif_linux_vport_init(&request);
725 request.cmd = OVS_VPORT_CMD_GET;
726 request.dp_ifindex = dpif->dp_ifindex;
728 buf = ofpbuf_new(1024);
729 dpif_linux_vport_to_ofpbuf(&request, buf);
730 nl_dump_start(dump, NETLINK_GENERIC, buf);
735 dpif_linux_port_dump_start(const struct dpif *dpif, void **statep)
737 struct dpif_linux_port_state *state;
739 *statep = state = xmalloc(sizeof *state);
740 dpif_linux_port_dump_start__(dpif, &state->dump);
742 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
747 dpif_linux_port_dump_next__(const struct dpif *dpif_, struct nl_dump *dump,
748 struct dpif_linux_vport *vport,
749 struct ofpbuf *buffer)
751 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
755 if (!nl_dump_next(dump, &buf, buffer)) {
759 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
761 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
762 dpif_name(&dpif->dpif), ovs_strerror(error));
768 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
769 struct dpif_port *dpif_port)
771 struct dpif_linux_port_state *state = state_;
772 struct dpif_linux_vport vport;
775 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport,
780 dpif_port->name = CONST_CAST(char *, vport.name);
781 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
782 dpif_port->port_no = vport.port_no;
787 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
789 struct dpif_linux_port_state *state = state_;
790 int error = nl_dump_done(&state->dump);
792 ofpbuf_uninit(&state->buf);
798 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
800 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
802 /* Lazily create the Netlink socket to listen for notifications. */
803 if (!dpif->port_notifier) {
804 struct nl_sock *sock;
807 error = nl_sock_create(NETLINK_GENERIC, &sock);
812 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
814 nl_sock_destroy(sock);
817 dpif->port_notifier = sock;
819 /* We have no idea of the current state so report that everything
825 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
826 uint64_t buf_stub[4096 / 8];
830 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
831 error = nl_sock_recv(dpif->port_notifier, &buf, false);
833 struct dpif_linux_vport vport;
835 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
837 if (vport.dp_ifindex == dpif->dp_ifindex
838 && (vport.cmd == OVS_VPORT_CMD_NEW
839 || vport.cmd == OVS_VPORT_CMD_DEL
840 || vport.cmd == OVS_VPORT_CMD_SET)) {
841 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
842 dpif->dpif.full_name, vport.name, vport.cmd);
843 if (vport.cmd == OVS_VPORT_CMD_DEL) {
844 dpif->refresh_channels = true;
846 *devnamep = xstrdup(vport.name);
851 } else if (error != EAGAIN) {
852 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
853 ovs_strerror(error));
854 nl_sock_drain(dpif->port_notifier);
866 dpif_linux_port_poll_wait(const struct dpif *dpif_)
868 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
870 if (dpif->port_notifier) {
871 nl_sock_wait(dpif->port_notifier, POLLIN);
873 poll_immediate_wake();
878 dpif_linux_flow_get__(const struct dpif *dpif_,
879 const struct nlattr *key, size_t key_len,
880 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
882 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
883 struct dpif_linux_flow request;
885 dpif_linux_flow_init(&request);
886 request.cmd = OVS_FLOW_CMD_GET;
887 request.dp_ifindex = dpif->dp_ifindex;
889 request.key_len = key_len;
890 return dpif_linux_flow_transact(&request, reply, bufp);
894 dpif_linux_flow_get(const struct dpif *dpif_,
895 const struct nlattr *key, size_t key_len,
896 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
898 struct dpif_linux_flow reply;
902 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
905 dpif_linux_flow_get_stats(&reply, stats);
908 buf->data = CONST_CAST(struct nlattr *, reply.actions);
909 buf->size = reply.actions_len;
919 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
920 struct dpif_linux_flow *request)
922 static const struct nlattr dummy_action;
924 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
926 dpif_linux_flow_init(request);
927 request->cmd = (put->flags & DPIF_FP_CREATE
928 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
929 request->dp_ifindex = dpif->dp_ifindex;
930 request->key = put->key;
931 request->key_len = put->key_len;
932 request->mask = put->mask;
933 request->mask_len = put->mask_len;
934 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
935 request->actions = (put->actions
937 : CONST_CAST(struct nlattr *, &dummy_action));
938 request->actions_len = put->actions_len;
939 if (put->flags & DPIF_FP_ZERO_STATS) {
940 request->clear = true;
942 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
946 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
948 struct dpif_linux_flow request, reply;
952 dpif_linux_init_flow_put(dpif_, put, &request);
953 error = dpif_linux_flow_transact(&request,
954 put->stats ? &reply : NULL,
955 put->stats ? &buf : NULL);
956 if (!error && put->stats) {
957 dpif_linux_flow_get_stats(&reply, put->stats);
964 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
965 struct dpif_linux_flow *request)
967 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
969 dpif_linux_flow_init(request);
970 request->cmd = OVS_FLOW_CMD_DEL;
971 request->dp_ifindex = dpif->dp_ifindex;
972 request->key = del->key;
973 request->key_len = del->key_len;
977 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
979 struct dpif_linux_flow request, reply;
983 dpif_linux_init_flow_del(dpif_, del, &request);
984 error = dpif_linux_flow_transact(&request,
985 del->stats ? &reply : NULL,
986 del->stats ? &buf : NULL);
987 if (!error && del->stats) {
988 dpif_linux_flow_get_stats(&reply, del->stats);
994 struct dpif_linux_flow_state {
995 struct dpif_linux_flow flow;
996 struct dpif_flow_stats stats;
997 struct ofpbuf buffer; /* Always used to store flows. */
998 struct ofpbuf *tmp; /* Used if kernel does not supply actions. */
1001 struct dpif_linux_flow_iter {
1002 struct nl_dump dump;
1007 dpif_linux_flow_dump_state_init(void **statep)
1009 struct dpif_linux_flow_state *state;
1011 *statep = state = xmalloc(sizeof *state);
1012 ofpbuf_init(&state->buffer, NL_DUMP_BUFSIZE);
1017 dpif_linux_flow_dump_state_uninit(void *state_)
1019 struct dpif_linux_flow_state *state = state_;
1021 ofpbuf_uninit(&state->buffer);
1022 ofpbuf_delete(state->tmp);
1027 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **iterp)
1029 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1030 struct dpif_linux_flow_iter *iter;
1031 struct dpif_linux_flow request;
1034 *iterp = iter = xmalloc(sizeof *iter);
1036 dpif_linux_flow_init(&request);
1037 request.cmd = OVS_FLOW_CMD_GET;
1038 request.dp_ifindex = dpif->dp_ifindex;
1040 buf = ofpbuf_new(1024);
1041 dpif_linux_flow_to_ofpbuf(&request, buf);
1042 nl_dump_start(&iter->dump, NETLINK_GENERIC, buf);
1045 dpif_linux_flow_dump_state_init(&iter->state);
1051 dpif_linux_flow_dump_next(const struct dpif *dpif_, void *iter_,
1052 const struct nlattr **key, size_t *key_len,
1053 const struct nlattr **mask, size_t *mask_len,
1054 const struct nlattr **actions, size_t *actions_len,
1055 const struct dpif_flow_stats **stats)
1057 struct dpif_linux_flow_iter *iter = iter_;
1058 struct dpif_linux_flow_state *state = iter->state;
1063 ofpbuf_delete(state->tmp);
1066 if (!nl_dump_next(&iter->dump, &buf, &state->buffer)) {
1070 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1075 if (actions && !state->flow.actions) {
1076 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1077 state->flow.key_len,
1078 &state->flow, &state->tmp);
1079 if (error == ENOENT) {
1080 VLOG_DBG("dumped flow disappeared on get");
1082 VLOG_WARN("error fetching dumped flow: %s",
1083 ovs_strerror(error));
1089 *actions = state->flow.actions;
1090 *actions_len = state->flow.actions_len;
1093 *key = state->flow.key;
1094 *key_len = state->flow.key_len;
1097 *mask = state->flow.mask;
1098 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1101 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1102 *stats = &state->stats;
1108 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1110 struct dpif_linux_flow_iter *iter = iter_;
1111 int error = nl_dump_done(&iter->dump);
1112 dpif_linux_flow_dump_state_uninit(iter->state);
1118 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1121 struct ovs_header *k_exec;
1124 ofpbuf_prealloc_tailroom(buf, (64
1125 + d_exec->packet->size
1126 + ODP_KEY_METADATA_SIZE
1127 + d_exec->actions_len));
1129 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1130 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1132 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1133 k_exec->dp_ifindex = dp_ifindex;
1135 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1136 d_exec->packet->data, d_exec->packet->size);
1138 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1139 odp_key_from_pkt_metadata(buf, &d_exec->md);
1140 nl_msg_end_nested(buf, key_ofs);
1142 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1143 d_exec->actions, d_exec->actions_len);
1147 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1149 uint64_t request_stub[1024 / 8];
1150 struct ofpbuf request;
1153 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1154 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1155 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1156 ofpbuf_uninit(&request);
1162 dpif_linux_execute(struct dpif *dpif_, struct dpif_execute *execute)
1164 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1166 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1172 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1174 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1177 struct nl_transaction txn;
1179 struct ofpbuf request;
1180 uint64_t request_stub[1024 / 8];
1182 struct ofpbuf reply;
1183 uint64_t reply_stub[1024 / 8];
1186 struct nl_transaction *txnsp[MAX_OPS];
1189 ovs_assert(n_ops <= MAX_OPS);
1190 for (i = 0; i < n_ops; i++) {
1191 struct op_auxdata *aux = &auxes[i];
1192 struct dpif_op *op = ops[i];
1193 struct dpif_flow_put *put;
1194 struct dpif_flow_del *del;
1195 struct dpif_execute *execute;
1196 struct dpif_linux_flow flow;
1198 ofpbuf_use_stub(&aux->request,
1199 aux->request_stub, sizeof aux->request_stub);
1200 aux->txn.request = &aux->request;
1202 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1203 aux->txn.reply = NULL;
1206 case DPIF_OP_FLOW_PUT:
1207 put = &op->u.flow_put;
1208 dpif_linux_init_flow_put(dpif_, put, &flow);
1210 flow.nlmsg_flags |= NLM_F_ECHO;
1211 aux->txn.reply = &aux->reply;
1213 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1216 case DPIF_OP_FLOW_DEL:
1217 del = &op->u.flow_del;
1218 dpif_linux_init_flow_del(dpif_, del, &flow);
1220 flow.nlmsg_flags |= NLM_F_ECHO;
1221 aux->txn.reply = &aux->reply;
1223 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1226 case DPIF_OP_EXECUTE:
1227 execute = &op->u.execute;
1228 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1237 for (i = 0; i < n_ops; i++) {
1238 txnsp[i] = &auxes[i].txn;
1240 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1242 for (i = 0; i < n_ops; i++) {
1243 struct op_auxdata *aux = &auxes[i];
1244 struct nl_transaction *txn = &auxes[i].txn;
1245 struct dpif_op *op = ops[i];
1246 struct dpif_flow_put *put;
1247 struct dpif_flow_del *del;
1249 op->error = txn->error;
1252 case DPIF_OP_FLOW_PUT:
1253 put = &op->u.flow_put;
1256 struct dpif_linux_flow reply;
1258 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1261 dpif_linux_flow_get_stats(&reply, put->stats);
1266 memset(put->stats, 0, sizeof *put->stats);
1271 case DPIF_OP_FLOW_DEL:
1272 del = &op->u.flow_del;
1275 struct dpif_linux_flow reply;
1277 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1280 dpif_linux_flow_get_stats(&reply, del->stats);
1285 memset(del->stats, 0, sizeof *del->stats);
1290 case DPIF_OP_EXECUTE:
1297 ofpbuf_uninit(&aux->request);
1298 ofpbuf_uninit(&aux->reply);
1303 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1306 size_t chunk = MIN(n_ops, MAX_OPS);
1307 dpif_linux_operate__(dpif, ops, chunk);
1313 /* Synchronizes 'dpif->channels' with the set of vports currently in 'dpif' in
1314 * the kernel, by adding a new channel for any kernel vport that lacks one and
1315 * deleting any channels that have no backing kernel vports. */
1317 dpif_linux_refresh_channels(struct dpif *dpif_)
1319 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1320 unsigned long int *keep_channels;
1321 struct dpif_linux_vport vport;
1322 size_t keep_channels_nbits;
1323 struct nl_dump dump;
1324 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1329 /* To start with, we need an epoll fd. */
1330 if (dpif->epoll_fd < 0) {
1331 dpif->epoll_fd = epoll_create(10);
1332 if (dpif->epoll_fd < 0) {
1337 keep_channels_nbits = dpif->uc_array_size;
1338 keep_channels = bitmap_allocate(keep_channels_nbits);
1340 dpif->n_events = dpif->event_offset = 0;
1342 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1343 dpif_linux_port_dump_start__(dpif_, &dump);
1344 while (!dpif_linux_port_dump_next__(dpif_, &dump, &vport, &buf)) {
1345 uint32_t port_no = odp_to_u32(vport.port_no);
1346 struct nl_sock *sock = (port_no < dpif->uc_array_size
1347 ? dpif->channels[port_no].sock
1349 bool new_sock = !sock;
1353 error = nl_sock_create(NETLINK_GENERIC, &sock);
1360 /* Configure the vport to deliver misses to 'sock'. */
1361 if (!vport.upcall_pid || *vport.upcall_pid != nl_sock_pid(sock)) {
1362 uint32_t upcall_pid = nl_sock_pid(sock);
1363 struct dpif_linux_vport vport_request;
1365 dpif_linux_vport_init(&vport_request);
1366 vport_request.cmd = OVS_VPORT_CMD_SET;
1367 vport_request.dp_ifindex = dpif->dp_ifindex;
1368 vport_request.port_no = vport.port_no;
1369 vport_request.upcall_pid = &upcall_pid;
1370 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1372 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1373 dpif_name(&dpif->dpif), vport_request.port_no,
1376 VLOG_WARN_RL(&error_rl,
1377 "%s: failed to set upcall pid on port: %s",
1378 dpif_name(&dpif->dpif), ovs_strerror(error));
1380 if (error != ENODEV && error != ENOENT) {
1383 /* The vport isn't really there, even though the dump says
1384 * it is. Probably we just hit a race after a port
1392 error = add_channel(dpif, vport.port_no, sock);
1394 VLOG_INFO("%s: could not add channel for port %s",
1395 dpif_name(dpif_), vport.name);
1401 if (port_no < keep_channels_nbits) {
1402 bitmap_set1(keep_channels, port_no);
1407 nl_sock_destroy(sock);
1409 nl_dump_done(&dump);
1410 ofpbuf_uninit(&buf);
1412 /* Discard any saved channels that we didn't reuse. */
1413 for (i = 0; i < keep_channels_nbits; i++) {
1414 if (!bitmap_is_set(keep_channels, i)) {
1415 nl_sock_destroy(dpif->channels[i].sock);
1416 dpif->channels[i].sock = NULL;
1419 free(keep_channels);
1425 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1427 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1429 if ((dpif->epoll_fd >= 0) == enable) {
1431 } else if (!enable) {
1432 destroy_channels(dpif);
1435 return dpif_linux_refresh_channels(dpif_);
1440 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1442 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1445 ovs_mutex_lock(&dpif->upcall_lock);
1446 error = dpif_linux_recv_set__(dpif_, enable);
1447 ovs_mutex_unlock(&dpif->upcall_lock);
1453 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1454 uint32_t queue_id, uint32_t *priority)
1456 if (queue_id < 0xf000) {
1457 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1465 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1468 static const struct nl_policy ovs_packet_policy[] = {
1469 /* Always present. */
1470 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1471 .min_len = ETH_HEADER_LEN },
1472 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1474 /* OVS_PACKET_CMD_ACTION only. */
1475 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1478 struct ovs_header *ovs_header;
1479 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1480 struct nlmsghdr *nlmsg;
1481 struct genlmsghdr *genl;
1485 ofpbuf_use_const(&b, buf->data, buf->size);
1487 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1488 genl = ofpbuf_try_pull(&b, sizeof *genl);
1489 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1490 if (!nlmsg || !genl || !ovs_header
1491 || nlmsg->nlmsg_type != ovs_packet_family
1492 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1493 ARRAY_SIZE(ovs_packet_policy))) {
1497 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1498 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1504 /* (Re)set ALL fields of '*upcall' on successful return. */
1505 upcall->type = type;
1506 upcall->key = CONST_CAST(struct nlattr *,
1507 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1508 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1509 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1511 /* Allow overwriting the netlink attribute header without reallocating. */
1512 ofpbuf_use_stub(&upcall->packet,
1513 CONST_CAST(struct nlattr *,
1514 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1515 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1516 sizeof(struct nlattr));
1517 upcall->packet.data = (char *)upcall->packet.data + sizeof(struct nlattr);
1518 upcall->packet.size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1520 *dp_ifindex = ovs_header->dp_ifindex;
1526 dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
1529 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1532 if (dpif->epoll_fd < 0) {
1536 if (dpif->event_offset >= dpif->n_events) {
1539 dpif->event_offset = dpif->n_events = 0;
1542 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1543 dpif->uc_array_size, 0);
1544 } while (retval < 0 && errno == EINTR);
1546 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1547 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1548 } else if (retval > 0) {
1549 dpif->n_events = retval;
1553 while (dpif->event_offset < dpif->n_events) {
1554 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1555 struct dpif_channel *ch = &dpif->channels[idx];
1557 dpif->event_offset++;
1563 if (++read_tries > 50) {
1567 error = nl_sock_recv(ch->sock, buf, false);
1568 if (error == ENOBUFS) {
1569 /* ENOBUFS typically means that we've received so many
1570 * packets that the buffer overflowed. Try again
1571 * immediately because there's almost certainly a packet
1572 * waiting for us. */
1573 report_loss(dpif_, ch);
1577 ch->last_poll = time_msec();
1579 if (error == EAGAIN) {
1585 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1586 if (!error && dp_ifindex == dpif->dp_ifindex) {
1598 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1601 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1604 ovs_mutex_lock(&dpif->upcall_lock);
1605 error = dpif_linux_recv__(dpif_, upcall, buf);
1606 ovs_mutex_unlock(&dpif->upcall_lock);
1612 dpif_linux_recv_wait(struct dpif *dpif_)
1614 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1616 ovs_mutex_lock(&dpif->upcall_lock);
1617 if (dpif->epoll_fd >= 0) {
1618 poll_fd_wait(dpif->epoll_fd, POLLIN);
1620 ovs_mutex_unlock(&dpif->upcall_lock);
1624 dpif_linux_recv_purge(struct dpif *dpif_)
1626 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1628 ovs_mutex_lock(&dpif->upcall_lock);
1629 if (dpif->epoll_fd >= 0) {
1630 struct dpif_channel *ch;
1632 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
1635 nl_sock_drain(ch->sock);
1639 ovs_mutex_unlock(&dpif->upcall_lock);
1642 const struct dpif_class dpif_linux_class = {
1644 dpif_linux_enumerate,
1651 dpif_linux_get_stats,
1652 dpif_linux_port_add,
1653 dpif_linux_port_del,
1654 dpif_linux_port_query_by_number,
1655 dpif_linux_port_query_by_name,
1656 dpif_linux_port_get_pid,
1657 dpif_linux_port_dump_start,
1658 dpif_linux_port_dump_next,
1659 dpif_linux_port_dump_done,
1660 dpif_linux_port_poll,
1661 dpif_linux_port_poll_wait,
1662 dpif_linux_flow_get,
1663 dpif_linux_flow_put,
1664 dpif_linux_flow_del,
1665 dpif_linux_flow_flush,
1666 dpif_linux_flow_dump_state_init,
1667 dpif_linux_flow_dump_start,
1668 dpif_linux_flow_dump_next,
1669 dpif_linux_flow_dump_done,
1670 dpif_linux_flow_dump_state_uninit,
1673 dpif_linux_recv_set,
1674 dpif_linux_queue_to_priority,
1676 dpif_linux_recv_wait,
1677 dpif_linux_recv_purge,
1681 dpif_linux_init(void)
1683 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1686 if (ovsthread_once_start(&once)) {
1687 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1688 &ovs_datapath_family);
1690 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1691 "The Open vSwitch kernel module is probably not loaded.",
1692 OVS_DATAPATH_FAMILY);
1695 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1698 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1701 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1702 &ovs_packet_family);
1705 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1706 &ovs_vport_mcgroup);
1709 ovsthread_once_done(&once);
1716 dpif_linux_is_internal_device(const char *name)
1718 struct dpif_linux_vport reply;
1722 error = dpif_linux_vport_get(name, &reply, &buf);
1725 } else if (error != ENODEV && error != ENOENT) {
1726 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1727 name, ovs_strerror(error));
1730 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1733 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1734 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1735 * positive errno value.
1737 * 'vport' will contain pointers into 'buf', so the caller should not free
1738 * 'buf' while 'vport' is still in use. */
1740 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1741 const struct ofpbuf *buf)
1743 static const struct nl_policy ovs_vport_policy[] = {
1744 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1745 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1746 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1747 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1748 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1750 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1753 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1754 struct ovs_header *ovs_header;
1755 struct nlmsghdr *nlmsg;
1756 struct genlmsghdr *genl;
1759 dpif_linux_vport_init(vport);
1761 ofpbuf_use_const(&b, buf->data, buf->size);
1762 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1763 genl = ofpbuf_try_pull(&b, sizeof *genl);
1764 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1765 if (!nlmsg || !genl || !ovs_header
1766 || nlmsg->nlmsg_type != ovs_vport_family
1767 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1768 ARRAY_SIZE(ovs_vport_policy))) {
1772 vport->cmd = genl->cmd;
1773 vport->dp_ifindex = ovs_header->dp_ifindex;
1774 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1775 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1776 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1777 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1778 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1780 if (a[OVS_VPORT_ATTR_STATS]) {
1781 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1783 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1784 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1785 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1790 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1791 * followed by Netlink attributes corresponding to 'vport'. */
1793 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1796 struct ovs_header *ovs_header;
1798 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1799 vport->cmd, OVS_VPORT_VERSION);
1801 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1802 ovs_header->dp_ifindex = vport->dp_ifindex;
1804 if (vport->port_no != ODPP_NONE) {
1805 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1808 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1809 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1813 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1816 if (vport->upcall_pid) {
1817 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1821 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1822 vport->stats, sizeof *vport->stats);
1825 if (vport->options) {
1826 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1827 vport->options, vport->options_len);
1831 /* Clears 'vport' to "empty" values. */
1833 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1835 memset(vport, 0, sizeof *vport);
1836 vport->port_no = ODPP_NONE;
1839 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1840 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1841 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1842 * result of the command is expected to be an ovs_vport also, which is decoded
1843 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1844 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1846 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1847 struct dpif_linux_vport *reply,
1848 struct ofpbuf **bufp)
1850 struct ofpbuf *request_buf;
1853 ovs_assert((reply != NULL) == (bufp != NULL));
1855 error = dpif_linux_init();
1859 dpif_linux_vport_init(reply);
1864 request_buf = ofpbuf_new(1024);
1865 dpif_linux_vport_to_ofpbuf(request, request_buf);
1866 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1867 ofpbuf_delete(request_buf);
1871 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1874 dpif_linux_vport_init(reply);
1875 ofpbuf_delete(*bufp);
1882 /* Obtains information about the kernel vport named 'name' and stores it into
1883 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1884 * longer needed ('reply' will contain pointers into '*bufp'). */
1886 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1887 struct ofpbuf **bufp)
1889 struct dpif_linux_vport request;
1891 dpif_linux_vport_init(&request);
1892 request.cmd = OVS_VPORT_CMD_GET;
1893 request.name = name;
1895 return dpif_linux_vport_transact(&request, reply, bufp);
1898 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1899 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1900 * positive errno value.
1902 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1903 * while 'dp' is still in use. */
1905 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1907 static const struct nl_policy ovs_datapath_policy[] = {
1908 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1909 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1911 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
1912 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
1916 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1917 struct ovs_header *ovs_header;
1918 struct nlmsghdr *nlmsg;
1919 struct genlmsghdr *genl;
1922 dpif_linux_dp_init(dp);
1924 ofpbuf_use_const(&b, buf->data, buf->size);
1925 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1926 genl = ofpbuf_try_pull(&b, sizeof *genl);
1927 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1928 if (!nlmsg || !genl || !ovs_header
1929 || nlmsg->nlmsg_type != ovs_datapath_family
1930 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1931 ARRAY_SIZE(ovs_datapath_policy))) {
1935 dp->cmd = genl->cmd;
1936 dp->dp_ifindex = ovs_header->dp_ifindex;
1937 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1938 if (a[OVS_DP_ATTR_STATS]) {
1939 /* Can't use structure assignment because Netlink doesn't ensure
1940 * sufficient alignment for 64-bit members. */
1941 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1945 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
1946 /* Can't use structure assignment because Netlink doesn't ensure
1947 * sufficient alignment for 64-bit members. */
1948 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
1949 sizeof dp->megaflow_stats);
1955 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1957 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1959 struct ovs_header *ovs_header;
1961 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1962 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1963 OVS_DATAPATH_VERSION);
1965 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1966 ovs_header->dp_ifindex = dp->dp_ifindex;
1969 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1972 if (dp->upcall_pid) {
1973 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1976 if (dp->user_features) {
1977 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
1980 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1983 /* Clears 'dp' to "empty" values. */
1985 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1987 memset(dp, 0, sizeof *dp);
1988 dp->megaflow_stats.n_masks = UINT32_MAX;
1989 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
1993 dpif_linux_dp_dump_start(struct nl_dump *dump)
1995 struct dpif_linux_dp request;
1998 dpif_linux_dp_init(&request);
1999 request.cmd = OVS_DP_CMD_GET;
2001 buf = ofpbuf_new(1024);
2002 dpif_linux_dp_to_ofpbuf(&request, buf);
2003 nl_dump_start(dump, NETLINK_GENERIC, buf);
2007 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2008 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2009 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2010 * result of the command is expected to be of the same form, which is decoded
2011 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2012 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2014 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
2015 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
2017 struct ofpbuf *request_buf;
2020 ovs_assert((reply != NULL) == (bufp != NULL));
2022 request_buf = ofpbuf_new(1024);
2023 dpif_linux_dp_to_ofpbuf(request, request_buf);
2024 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2025 ofpbuf_delete(request_buf);
2028 dpif_linux_dp_init(reply);
2030 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
2033 ofpbuf_delete(*bufp);
2040 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2041 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2042 * will contain pointers into '*bufp'). */
2044 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2045 struct ofpbuf **bufp)
2047 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2048 struct dpif_linux_dp request;
2050 dpif_linux_dp_init(&request);
2051 request.cmd = OVS_DP_CMD_GET;
2052 request.dp_ifindex = dpif->dp_ifindex;
2054 return dpif_linux_dp_transact(&request, reply, bufp);
2057 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2058 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2059 * positive errno value.
2061 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2062 * while 'flow' is still in use. */
2064 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2065 const struct ofpbuf *buf)
2067 static const struct nl_policy ovs_flow_policy[] = {
2068 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2069 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2070 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2071 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2073 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2074 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2075 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2078 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2079 struct ovs_header *ovs_header;
2080 struct nlmsghdr *nlmsg;
2081 struct genlmsghdr *genl;
2084 dpif_linux_flow_init(flow);
2086 ofpbuf_use_const(&b, buf->data, buf->size);
2087 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2088 genl = ofpbuf_try_pull(&b, sizeof *genl);
2089 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2090 if (!nlmsg || !genl || !ovs_header
2091 || nlmsg->nlmsg_type != ovs_flow_family
2092 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2093 ARRAY_SIZE(ovs_flow_policy))) {
2097 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2098 flow->dp_ifindex = ovs_header->dp_ifindex;
2099 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2100 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2102 if (a[OVS_FLOW_ATTR_MASK]) {
2103 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2104 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2106 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2107 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2108 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2110 if (a[OVS_FLOW_ATTR_STATS]) {
2111 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2113 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2114 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2116 if (a[OVS_FLOW_ATTR_USED]) {
2117 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2122 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2123 * followed by Netlink attributes corresponding to 'flow'. */
2125 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2128 struct ovs_header *ovs_header;
2130 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2131 NLM_F_REQUEST | flow->nlmsg_flags,
2132 flow->cmd, OVS_FLOW_VERSION);
2134 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2135 ovs_header->dp_ifindex = flow->dp_ifindex;
2137 if (flow->key_len) {
2138 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2141 if (flow->mask_len) {
2142 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2145 if (flow->actions || flow->actions_len) {
2146 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2147 flow->actions, flow->actions_len);
2150 /* We never need to send these to the kernel. */
2151 ovs_assert(!flow->stats);
2152 ovs_assert(!flow->tcp_flags);
2153 ovs_assert(!flow->used);
2156 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2160 /* Clears 'flow' to "empty" values. */
2162 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2164 memset(flow, 0, sizeof *flow);
2167 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2168 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2169 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2170 * result of the command is expected to be a flow also, which is decoded and
2171 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2172 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2174 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2175 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2177 struct ofpbuf *request_buf;
2180 ovs_assert((reply != NULL) == (bufp != NULL));
2183 request->nlmsg_flags |= NLM_F_ECHO;
2186 request_buf = ofpbuf_new(1024);
2187 dpif_linux_flow_to_ofpbuf(request, request_buf);
2188 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2189 ofpbuf_delete(request_buf);
2193 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2196 dpif_linux_flow_init(reply);
2197 ofpbuf_delete(*bufp);
2205 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2206 struct dpif_flow_stats *stats)
2209 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2210 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2212 stats->n_packets = 0;
2215 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2216 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2219 /* Logs information about a packet that was recently lost in 'ch' (in
2222 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2224 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2225 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2228 if (VLOG_DROP_WARN(&rl)) {
2233 if (ch->last_poll != LLONG_MIN) {
2234 ds_put_format(&s, " (last polled %lld ms ago)",
2235 time_msec() - ch->last_poll);
2238 VLOG_WARN("%s: lost packet on channel %"PRIdPTR"%s",
2239 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));