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"
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 *,
174 odp_port_t port_no, uint32_t hash);
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() 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 = ofpbuf_data(&options);
546 request.options_len = ofpbuf_size(&options);
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,
682 uint32_t hash OVS_UNUSED)
684 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
685 uint32_t port_idx = odp_to_u32(port_no);
688 ovs_mutex_lock(&dpif->upcall_lock);
689 if (dpif->epoll_fd >= 0) {
690 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
691 * channel, since it is not heavily loaded. */
692 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
693 const struct nl_sock *sock = dpif->channels[idx].sock;
694 pid = sock ? nl_sock_pid(sock) : 0;
696 ovs_mutex_unlock(&dpif->upcall_lock);
702 dpif_linux_flow_flush(struct dpif *dpif_)
704 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
705 struct dpif_linux_flow flow;
707 dpif_linux_flow_init(&flow);
708 flow.cmd = OVS_FLOW_CMD_DEL;
709 flow.dp_ifindex = dpif->dp_ifindex;
710 return dpif_linux_flow_transact(&flow, NULL, NULL);
713 struct dpif_linux_port_state {
719 dpif_linux_port_dump_start__(const struct dpif *dpif_, struct nl_dump *dump)
721 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
722 struct dpif_linux_vport request;
725 dpif_linux_vport_init(&request);
726 request.cmd = OVS_VPORT_CMD_GET;
727 request.dp_ifindex = dpif->dp_ifindex;
729 buf = ofpbuf_new(1024);
730 dpif_linux_vport_to_ofpbuf(&request, buf);
731 nl_dump_start(dump, NETLINK_GENERIC, buf);
736 dpif_linux_port_dump_start(const struct dpif *dpif, void **statep)
738 struct dpif_linux_port_state *state;
740 *statep = state = xmalloc(sizeof *state);
741 dpif_linux_port_dump_start__(dpif, &state->dump);
743 ofpbuf_init(&state->buf, NL_DUMP_BUFSIZE);
748 dpif_linux_port_dump_next__(const struct dpif *dpif_, struct nl_dump *dump,
749 struct dpif_linux_vport *vport,
750 struct ofpbuf *buffer)
752 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
756 if (!nl_dump_next(dump, &buf, buffer)) {
760 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
762 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
763 dpif_name(&dpif->dpif), ovs_strerror(error));
769 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
770 struct dpif_port *dpif_port)
772 struct dpif_linux_port_state *state = state_;
773 struct dpif_linux_vport vport;
776 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport,
781 dpif_port->name = CONST_CAST(char *, vport.name);
782 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
783 dpif_port->port_no = vport.port_no;
788 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
790 struct dpif_linux_port_state *state = state_;
791 int error = nl_dump_done(&state->dump);
793 ofpbuf_uninit(&state->buf);
799 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
801 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
803 /* Lazily create the Netlink socket to listen for notifications. */
804 if (!dpif->port_notifier) {
805 struct nl_sock *sock;
808 error = nl_sock_create(NETLINK_GENERIC, &sock);
813 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
815 nl_sock_destroy(sock);
818 dpif->port_notifier = sock;
820 /* We have no idea of the current state so report that everything
826 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
827 uint64_t buf_stub[4096 / 8];
831 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
832 error = nl_sock_recv(dpif->port_notifier, &buf, false);
834 struct dpif_linux_vport vport;
836 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
838 if (vport.dp_ifindex == dpif->dp_ifindex
839 && (vport.cmd == OVS_VPORT_CMD_NEW
840 || vport.cmd == OVS_VPORT_CMD_DEL
841 || vport.cmd == OVS_VPORT_CMD_SET)) {
842 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
843 dpif->dpif.full_name, vport.name, vport.cmd);
844 if (vport.cmd == OVS_VPORT_CMD_DEL) {
845 dpif->refresh_channels = true;
847 *devnamep = xstrdup(vport.name);
852 } else if (error != EAGAIN) {
853 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
854 ovs_strerror(error));
855 nl_sock_drain(dpif->port_notifier);
867 dpif_linux_port_poll_wait(const struct dpif *dpif_)
869 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
871 if (dpif->port_notifier) {
872 nl_sock_wait(dpif->port_notifier, POLLIN);
874 poll_immediate_wake();
879 dpif_linux_flow_get__(const struct dpif *dpif_,
880 const struct nlattr *key, size_t key_len,
881 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
883 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
884 struct dpif_linux_flow request;
886 dpif_linux_flow_init(&request);
887 request.cmd = OVS_FLOW_CMD_GET;
888 request.dp_ifindex = dpif->dp_ifindex;
890 request.key_len = key_len;
891 return dpif_linux_flow_transact(&request, reply, bufp);
895 dpif_linux_flow_get(const struct dpif *dpif_,
896 const struct nlattr *key, size_t key_len,
897 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
899 struct dpif_linux_flow reply;
903 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
906 dpif_linux_flow_get_stats(&reply, stats);
909 ofpbuf_set_data(buf, CONST_CAST(struct nlattr *, reply.actions));
910 ofpbuf_set_size(buf, reply.actions_len);
920 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
921 struct dpif_linux_flow *request)
923 static const struct nlattr dummy_action;
925 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
927 dpif_linux_flow_init(request);
928 request->cmd = (put->flags & DPIF_FP_CREATE
929 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
930 request->dp_ifindex = dpif->dp_ifindex;
931 request->key = put->key;
932 request->key_len = put->key_len;
933 request->mask = put->mask;
934 request->mask_len = put->mask_len;
935 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
936 request->actions = (put->actions
938 : CONST_CAST(struct nlattr *, &dummy_action));
939 request->actions_len = put->actions_len;
940 if (put->flags & DPIF_FP_ZERO_STATS) {
941 request->clear = true;
943 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
947 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
949 struct dpif_linux_flow request, reply;
953 dpif_linux_init_flow_put(dpif_, put, &request);
954 error = dpif_linux_flow_transact(&request,
955 put->stats ? &reply : NULL,
956 put->stats ? &buf : NULL);
957 if (!error && put->stats) {
958 dpif_linux_flow_get_stats(&reply, put->stats);
965 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
966 struct dpif_linux_flow *request)
968 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
970 dpif_linux_flow_init(request);
971 request->cmd = OVS_FLOW_CMD_DEL;
972 request->dp_ifindex = dpif->dp_ifindex;
973 request->key = del->key;
974 request->key_len = del->key_len;
978 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
980 struct dpif_linux_flow request, reply;
984 dpif_linux_init_flow_del(dpif_, del, &request);
985 error = dpif_linux_flow_transact(&request,
986 del->stats ? &reply : NULL,
987 del->stats ? &buf : NULL);
988 if (!error && del->stats) {
989 dpif_linux_flow_get_stats(&reply, del->stats);
995 struct dpif_linux_flow_state {
996 struct dpif_linux_flow flow;
997 struct dpif_flow_stats stats;
998 struct ofpbuf buffer; /* Always used to store flows. */
999 struct ofpbuf *tmp; /* Used if kernel does not supply actions. */
1002 struct dpif_linux_flow_iter {
1003 struct nl_dump dump;
1008 dpif_linux_flow_dump_state_init(void **statep)
1010 struct dpif_linux_flow_state *state;
1012 *statep = state = xmalloc(sizeof *state);
1013 ofpbuf_init(&state->buffer, NL_DUMP_BUFSIZE);
1018 dpif_linux_flow_dump_state_uninit(void *state_)
1020 struct dpif_linux_flow_state *state = state_;
1022 ofpbuf_uninit(&state->buffer);
1023 ofpbuf_delete(state->tmp);
1028 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **iterp)
1030 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1031 struct dpif_linux_flow_iter *iter;
1032 struct dpif_linux_flow request;
1035 *iterp = iter = xmalloc(sizeof *iter);
1037 dpif_linux_flow_init(&request);
1038 request.cmd = OVS_FLOW_CMD_GET;
1039 request.dp_ifindex = dpif->dp_ifindex;
1041 buf = ofpbuf_new(1024);
1042 dpif_linux_flow_to_ofpbuf(&request, buf);
1043 nl_dump_start(&iter->dump, NETLINK_GENERIC, buf);
1045 atomic_init(&iter->status, 0);
1051 dpif_linux_flow_dump_next(const struct dpif *dpif_, void *iter_, void *state_,
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 = 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);
1072 atomic_store(&iter->status, error);
1076 if (actions && !state->flow.actions) {
1077 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1078 state->flow.key_len,
1079 &state->flow, &state->tmp);
1080 if (error == ENOENT) {
1081 VLOG_DBG("dumped flow disappeared on get");
1083 VLOG_WARN("error fetching dumped flow: %s",
1084 ovs_strerror(error));
1090 *actions = state->flow.actions;
1091 *actions_len = state->flow.actions_len;
1094 *key = state->flow.key;
1095 *key_len = state->flow.key_len;
1098 *mask = state->flow.mask;
1099 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1102 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1103 *stats = &state->stats;
1109 dpif_linux_flow_dump_next_may_destroy_keys(void *state_)
1111 struct dpif_linux_flow_state *state = state_;
1113 return ofpbuf_size(&state->buffer) ? false : true;
1117 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1119 struct dpif_linux_flow_iter *iter = iter_;
1121 unsigned int nl_status = nl_dump_done(&iter->dump);
1123 atomic_read(&iter->status, &dump_status);
1125 return dump_status ? dump_status : nl_status;
1129 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1132 struct ovs_header *k_exec;
1135 ofpbuf_prealloc_tailroom(buf, (64
1136 + ofpbuf_size(d_exec->packet)
1137 + ODP_KEY_METADATA_SIZE
1138 + d_exec->actions_len));
1140 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1141 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1143 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1144 k_exec->dp_ifindex = dp_ifindex;
1146 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1147 ofpbuf_data(d_exec->packet),
1148 ofpbuf_size(d_exec->packet));
1150 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1151 odp_key_from_pkt_metadata(buf, &d_exec->md);
1152 nl_msg_end_nested(buf, key_ofs);
1154 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1155 d_exec->actions, d_exec->actions_len);
1159 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1161 uint64_t request_stub[1024 / 8];
1162 struct ofpbuf request;
1165 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1166 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1167 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1168 ofpbuf_uninit(&request);
1174 dpif_linux_execute(struct dpif *dpif_, struct dpif_execute *execute)
1176 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1178 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1184 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1186 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1189 struct nl_transaction txn;
1191 struct ofpbuf request;
1192 uint64_t request_stub[1024 / 8];
1194 struct ofpbuf reply;
1195 uint64_t reply_stub[1024 / 8];
1198 struct nl_transaction *txnsp[MAX_OPS];
1201 ovs_assert(n_ops <= MAX_OPS);
1202 for (i = 0; i < n_ops; i++) {
1203 struct op_auxdata *aux = &auxes[i];
1204 struct dpif_op *op = ops[i];
1205 struct dpif_flow_put *put;
1206 struct dpif_flow_del *del;
1207 struct dpif_execute *execute;
1208 struct dpif_linux_flow flow;
1210 ofpbuf_use_stub(&aux->request,
1211 aux->request_stub, sizeof aux->request_stub);
1212 aux->txn.request = &aux->request;
1214 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1215 aux->txn.reply = NULL;
1218 case DPIF_OP_FLOW_PUT:
1219 put = &op->u.flow_put;
1220 dpif_linux_init_flow_put(dpif_, put, &flow);
1222 flow.nlmsg_flags |= NLM_F_ECHO;
1223 aux->txn.reply = &aux->reply;
1225 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1228 case DPIF_OP_FLOW_DEL:
1229 del = &op->u.flow_del;
1230 dpif_linux_init_flow_del(dpif_, del, &flow);
1232 flow.nlmsg_flags |= NLM_F_ECHO;
1233 aux->txn.reply = &aux->reply;
1235 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1238 case DPIF_OP_EXECUTE:
1239 execute = &op->u.execute;
1240 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1249 for (i = 0; i < n_ops; i++) {
1250 txnsp[i] = &auxes[i].txn;
1252 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1254 for (i = 0; i < n_ops; i++) {
1255 struct op_auxdata *aux = &auxes[i];
1256 struct nl_transaction *txn = &auxes[i].txn;
1257 struct dpif_op *op = ops[i];
1258 struct dpif_flow_put *put;
1259 struct dpif_flow_del *del;
1261 op->error = txn->error;
1264 case DPIF_OP_FLOW_PUT:
1265 put = &op->u.flow_put;
1268 struct dpif_linux_flow reply;
1270 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1273 dpif_linux_flow_get_stats(&reply, put->stats);
1278 memset(put->stats, 0, sizeof *put->stats);
1283 case DPIF_OP_FLOW_DEL:
1284 del = &op->u.flow_del;
1287 struct dpif_linux_flow reply;
1289 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1292 dpif_linux_flow_get_stats(&reply, del->stats);
1297 memset(del->stats, 0, sizeof *del->stats);
1302 case DPIF_OP_EXECUTE:
1309 ofpbuf_uninit(&aux->request);
1310 ofpbuf_uninit(&aux->reply);
1315 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1318 size_t chunk = MIN(n_ops, MAX_OPS);
1319 dpif_linux_operate__(dpif, ops, chunk);
1325 /* Synchronizes 'dpif->channels' with the set of vports currently in 'dpif' in
1326 * the kernel, by adding a new channel for any kernel vport that lacks one and
1327 * deleting any channels that have no backing kernel vports. */
1329 dpif_linux_refresh_channels(struct dpif *dpif_)
1331 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1332 unsigned long int *keep_channels;
1333 struct dpif_linux_vport vport;
1334 size_t keep_channels_nbits;
1335 struct nl_dump dump;
1336 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
1341 /* To start with, we need an epoll fd. */
1342 if (dpif->epoll_fd < 0) {
1343 dpif->epoll_fd = epoll_create(10);
1344 if (dpif->epoll_fd < 0) {
1349 keep_channels_nbits = dpif->uc_array_size;
1350 keep_channels = bitmap_allocate(keep_channels_nbits);
1352 dpif->n_events = dpif->event_offset = 0;
1354 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
1355 dpif_linux_port_dump_start__(dpif_, &dump);
1356 while (!dpif_linux_port_dump_next__(dpif_, &dump, &vport, &buf)) {
1357 uint32_t port_no = odp_to_u32(vport.port_no);
1358 struct nl_sock *sock = (port_no < dpif->uc_array_size
1359 ? dpif->channels[port_no].sock
1361 bool new_sock = !sock;
1365 error = nl_sock_create(NETLINK_GENERIC, &sock);
1372 /* Configure the vport to deliver misses to 'sock'. */
1373 if (!vport.upcall_pid || *vport.upcall_pid != nl_sock_pid(sock)) {
1374 uint32_t upcall_pid = nl_sock_pid(sock);
1375 struct dpif_linux_vport vport_request;
1377 dpif_linux_vport_init(&vport_request);
1378 vport_request.cmd = OVS_VPORT_CMD_SET;
1379 vport_request.dp_ifindex = dpif->dp_ifindex;
1380 vport_request.port_no = vport.port_no;
1381 vport_request.upcall_pid = &upcall_pid;
1382 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1384 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1385 dpif_name(&dpif->dpif), vport_request.port_no,
1388 VLOG_WARN_RL(&error_rl,
1389 "%s: failed to set upcall pid on port: %s",
1390 dpif_name(&dpif->dpif), ovs_strerror(error));
1392 if (error != ENODEV && error != ENOENT) {
1395 /* The vport isn't really there, even though the dump says
1396 * it is. Probably we just hit a race after a port
1404 error = add_channel(dpif, vport.port_no, sock);
1406 VLOG_INFO("%s: could not add channel for port %s",
1407 dpif_name(dpif_), vport.name);
1413 if (port_no < keep_channels_nbits) {
1414 bitmap_set1(keep_channels, port_no);
1419 nl_sock_destroy(sock);
1421 nl_dump_done(&dump);
1422 ofpbuf_uninit(&buf);
1424 /* Discard any saved channels that we didn't reuse. */
1425 for (i = 0; i < keep_channels_nbits; i++) {
1426 if (!bitmap_is_set(keep_channels, i)) {
1427 nl_sock_destroy(dpif->channels[i].sock);
1428 dpif->channels[i].sock = NULL;
1431 free(keep_channels);
1437 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1439 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1441 if ((dpif->epoll_fd >= 0) == enable) {
1443 } else if (!enable) {
1444 destroy_channels(dpif);
1447 return dpif_linux_refresh_channels(dpif_);
1452 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1454 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1457 ovs_mutex_lock(&dpif->upcall_lock);
1458 error = dpif_linux_recv_set__(dpif_, enable);
1459 ovs_mutex_unlock(&dpif->upcall_lock);
1465 dpif_linux_handlers_set(struct dpif *dpif_ OVS_UNUSED,
1466 uint32_t n_handlers OVS_UNUSED)
1472 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1473 uint32_t queue_id, uint32_t *priority)
1475 if (queue_id < 0xf000) {
1476 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1484 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1487 static const struct nl_policy ovs_packet_policy[] = {
1488 /* Always present. */
1489 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1490 .min_len = ETH_HEADER_LEN },
1491 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1493 /* OVS_PACKET_CMD_ACTION only. */
1494 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1497 struct ovs_header *ovs_header;
1498 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1499 struct nlmsghdr *nlmsg;
1500 struct genlmsghdr *genl;
1504 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1506 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1507 genl = ofpbuf_try_pull(&b, sizeof *genl);
1508 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1509 if (!nlmsg || !genl || !ovs_header
1510 || nlmsg->nlmsg_type != ovs_packet_family
1511 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1512 ARRAY_SIZE(ovs_packet_policy))) {
1516 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1517 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1523 /* (Re)set ALL fields of '*upcall' on successful return. */
1524 upcall->type = type;
1525 upcall->key = CONST_CAST(struct nlattr *,
1526 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1527 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1528 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1530 /* Allow overwriting the netlink attribute header without reallocating. */
1531 ofpbuf_use_stub(&upcall->packet,
1532 CONST_CAST(struct nlattr *,
1533 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1534 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1535 sizeof(struct nlattr));
1536 ofpbuf_set_data(&upcall->packet,
1537 (char *)ofpbuf_data(&upcall->packet) + sizeof(struct nlattr));
1538 ofpbuf_set_size(&upcall->packet, nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]));
1540 *dp_ifindex = ovs_header->dp_ifindex;
1546 dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
1549 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1552 if (dpif->epoll_fd < 0) {
1556 if (dpif->event_offset >= dpif->n_events) {
1559 dpif->event_offset = dpif->n_events = 0;
1562 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1563 dpif->uc_array_size, 0);
1564 } while (retval < 0 && errno == EINTR);
1566 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1567 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1568 } else if (retval > 0) {
1569 dpif->n_events = retval;
1573 while (dpif->event_offset < dpif->n_events) {
1574 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1575 struct dpif_channel *ch = &dpif->channels[idx];
1577 dpif->event_offset++;
1583 if (++read_tries > 50) {
1587 error = nl_sock_recv(ch->sock, buf, false);
1588 if (error == ENOBUFS) {
1589 /* ENOBUFS typically means that we've received so many
1590 * packets that the buffer overflowed. Try again
1591 * immediately because there's almost certainly a packet
1592 * waiting for us. */
1593 report_loss(dpif_, ch);
1597 ch->last_poll = time_msec();
1599 if (error == EAGAIN) {
1605 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1606 if (!error && dp_ifindex == dpif->dp_ifindex) {
1618 dpif_linux_recv(struct dpif *dpif_, uint32_t handler_id OVS_UNUSED,
1619 struct dpif_upcall *upcall, struct ofpbuf *buf)
1621 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1624 ovs_mutex_lock(&dpif->upcall_lock);
1625 error = dpif_linux_recv__(dpif_, upcall, buf);
1626 ovs_mutex_unlock(&dpif->upcall_lock);
1632 dpif_linux_recv_wait(struct dpif *dpif_, uint32_t handler_id OVS_UNUSED)
1634 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1636 ovs_mutex_lock(&dpif->upcall_lock);
1637 if (dpif->epoll_fd >= 0) {
1638 poll_fd_wait(dpif->epoll_fd, POLLIN);
1640 ovs_mutex_unlock(&dpif->upcall_lock);
1644 dpif_linux_recv_purge(struct dpif *dpif_)
1646 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1648 ovs_mutex_lock(&dpif->upcall_lock);
1649 if (dpif->epoll_fd >= 0) {
1650 struct dpif_channel *ch;
1652 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
1655 nl_sock_drain(ch->sock);
1659 ovs_mutex_unlock(&dpif->upcall_lock);
1662 const struct dpif_class dpif_linux_class = {
1664 dpif_linux_enumerate,
1671 dpif_linux_get_stats,
1672 dpif_linux_port_add,
1673 dpif_linux_port_del,
1674 dpif_linux_port_query_by_number,
1675 dpif_linux_port_query_by_name,
1676 dpif_linux_port_get_pid,
1677 dpif_linux_port_dump_start,
1678 dpif_linux_port_dump_next,
1679 dpif_linux_port_dump_done,
1680 dpif_linux_port_poll,
1681 dpif_linux_port_poll_wait,
1682 dpif_linux_flow_get,
1683 dpif_linux_flow_put,
1684 dpif_linux_flow_del,
1685 dpif_linux_flow_flush,
1686 dpif_linux_flow_dump_state_init,
1687 dpif_linux_flow_dump_start,
1688 dpif_linux_flow_dump_next,
1689 dpif_linux_flow_dump_next_may_destroy_keys,
1690 dpif_linux_flow_dump_done,
1691 dpif_linux_flow_dump_state_uninit,
1694 dpif_linux_recv_set,
1695 dpif_linux_handlers_set,
1696 dpif_linux_queue_to_priority,
1698 dpif_linux_recv_wait,
1699 dpif_linux_recv_purge,
1703 dpif_linux_init(void)
1705 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1708 if (ovsthread_once_start(&once)) {
1709 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1710 &ovs_datapath_family);
1712 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1713 "The Open vSwitch kernel module is probably not loaded.",
1714 OVS_DATAPATH_FAMILY);
1717 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1720 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1723 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1724 &ovs_packet_family);
1727 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1728 &ovs_vport_mcgroup);
1731 ovsthread_once_done(&once);
1738 dpif_linux_is_internal_device(const char *name)
1740 struct dpif_linux_vport reply;
1744 error = dpif_linux_vport_get(name, &reply, &buf);
1747 } else if (error != ENODEV && error != ENOENT) {
1748 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1749 name, ovs_strerror(error));
1752 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1755 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1756 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1757 * positive errno value.
1759 * 'vport' will contain pointers into 'buf', so the caller should not free
1760 * 'buf' while 'vport' is still in use. */
1762 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1763 const struct ofpbuf *buf)
1765 static const struct nl_policy ovs_vport_policy[] = {
1766 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1767 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1768 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1769 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1770 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1772 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1775 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1776 struct ovs_header *ovs_header;
1777 struct nlmsghdr *nlmsg;
1778 struct genlmsghdr *genl;
1781 dpif_linux_vport_init(vport);
1783 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1784 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1785 genl = ofpbuf_try_pull(&b, sizeof *genl);
1786 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1787 if (!nlmsg || !genl || !ovs_header
1788 || nlmsg->nlmsg_type != ovs_vport_family
1789 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1790 ARRAY_SIZE(ovs_vport_policy))) {
1794 vport->cmd = genl->cmd;
1795 vport->dp_ifindex = ovs_header->dp_ifindex;
1796 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1797 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1798 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1799 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1800 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1802 if (a[OVS_VPORT_ATTR_STATS]) {
1803 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1805 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1806 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1807 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1812 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1813 * followed by Netlink attributes corresponding to 'vport'. */
1815 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1818 struct ovs_header *ovs_header;
1820 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1821 vport->cmd, OVS_VPORT_VERSION);
1823 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1824 ovs_header->dp_ifindex = vport->dp_ifindex;
1826 if (vport->port_no != ODPP_NONE) {
1827 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1830 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1831 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1835 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1838 if (vport->upcall_pid) {
1839 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1843 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1844 vport->stats, sizeof *vport->stats);
1847 if (vport->options) {
1848 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1849 vport->options, vport->options_len);
1853 /* Clears 'vport' to "empty" values. */
1855 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1857 memset(vport, 0, sizeof *vport);
1858 vport->port_no = ODPP_NONE;
1861 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1862 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1863 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1864 * result of the command is expected to be an ovs_vport also, which is decoded
1865 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1866 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1868 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1869 struct dpif_linux_vport *reply,
1870 struct ofpbuf **bufp)
1872 struct ofpbuf *request_buf;
1875 ovs_assert((reply != NULL) == (bufp != NULL));
1877 error = dpif_linux_init();
1881 dpif_linux_vport_init(reply);
1886 request_buf = ofpbuf_new(1024);
1887 dpif_linux_vport_to_ofpbuf(request, request_buf);
1888 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1889 ofpbuf_delete(request_buf);
1893 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1896 dpif_linux_vport_init(reply);
1897 ofpbuf_delete(*bufp);
1904 /* Obtains information about the kernel vport named 'name' and stores it into
1905 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1906 * longer needed ('reply' will contain pointers into '*bufp'). */
1908 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1909 struct ofpbuf **bufp)
1911 struct dpif_linux_vport request;
1913 dpif_linux_vport_init(&request);
1914 request.cmd = OVS_VPORT_CMD_GET;
1915 request.name = name;
1917 return dpif_linux_vport_transact(&request, reply, bufp);
1920 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1921 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1922 * positive errno value.
1924 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1925 * while 'dp' is still in use. */
1927 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1929 static const struct nl_policy ovs_datapath_policy[] = {
1930 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1931 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1933 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
1934 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
1938 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1939 struct ovs_header *ovs_header;
1940 struct nlmsghdr *nlmsg;
1941 struct genlmsghdr *genl;
1944 dpif_linux_dp_init(dp);
1946 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
1947 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1948 genl = ofpbuf_try_pull(&b, sizeof *genl);
1949 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1950 if (!nlmsg || !genl || !ovs_header
1951 || nlmsg->nlmsg_type != ovs_datapath_family
1952 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1953 ARRAY_SIZE(ovs_datapath_policy))) {
1957 dp->cmd = genl->cmd;
1958 dp->dp_ifindex = ovs_header->dp_ifindex;
1959 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1960 if (a[OVS_DP_ATTR_STATS]) {
1961 /* Can't use structure assignment because Netlink doesn't ensure
1962 * sufficient alignment for 64-bit members. */
1963 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1967 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
1968 /* Can't use structure assignment because Netlink doesn't ensure
1969 * sufficient alignment for 64-bit members. */
1970 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
1971 sizeof dp->megaflow_stats);
1977 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1979 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1981 struct ovs_header *ovs_header;
1983 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1984 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1985 OVS_DATAPATH_VERSION);
1987 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1988 ovs_header->dp_ifindex = dp->dp_ifindex;
1991 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1994 if (dp->upcall_pid) {
1995 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1998 if (dp->user_features) {
1999 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
2002 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
2005 /* Clears 'dp' to "empty" values. */
2007 dpif_linux_dp_init(struct dpif_linux_dp *dp)
2009 memset(dp, 0, sizeof *dp);
2010 dp->megaflow_stats.n_masks = UINT32_MAX;
2011 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
2015 dpif_linux_dp_dump_start(struct nl_dump *dump)
2017 struct dpif_linux_dp request;
2020 dpif_linux_dp_init(&request);
2021 request.cmd = OVS_DP_CMD_GET;
2023 buf = ofpbuf_new(1024);
2024 dpif_linux_dp_to_ofpbuf(&request, buf);
2025 nl_dump_start(dump, NETLINK_GENERIC, buf);
2029 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2030 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2031 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2032 * result of the command is expected to be of the same form, which is decoded
2033 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
2034 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
2036 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
2037 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
2039 struct ofpbuf *request_buf;
2042 ovs_assert((reply != NULL) == (bufp != NULL));
2044 request_buf = ofpbuf_new(1024);
2045 dpif_linux_dp_to_ofpbuf(request, request_buf);
2046 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2047 ofpbuf_delete(request_buf);
2050 dpif_linux_dp_init(reply);
2052 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
2055 ofpbuf_delete(*bufp);
2062 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2063 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2064 * will contain pointers into '*bufp'). */
2066 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2067 struct ofpbuf **bufp)
2069 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2070 struct dpif_linux_dp request;
2072 dpif_linux_dp_init(&request);
2073 request.cmd = OVS_DP_CMD_GET;
2074 request.dp_ifindex = dpif->dp_ifindex;
2076 return dpif_linux_dp_transact(&request, reply, bufp);
2079 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2080 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2081 * positive errno value.
2083 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2084 * while 'flow' is still in use. */
2086 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2087 const struct ofpbuf *buf)
2089 static const struct nl_policy ovs_flow_policy[] = {
2090 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2091 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2092 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2093 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2095 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2096 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2097 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2100 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2101 struct ovs_header *ovs_header;
2102 struct nlmsghdr *nlmsg;
2103 struct genlmsghdr *genl;
2106 dpif_linux_flow_init(flow);
2108 ofpbuf_use_const(&b, ofpbuf_data(buf), ofpbuf_size(buf));
2109 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2110 genl = ofpbuf_try_pull(&b, sizeof *genl);
2111 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2112 if (!nlmsg || !genl || !ovs_header
2113 || nlmsg->nlmsg_type != ovs_flow_family
2114 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2115 ARRAY_SIZE(ovs_flow_policy))) {
2119 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2120 flow->dp_ifindex = ovs_header->dp_ifindex;
2121 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2122 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2124 if (a[OVS_FLOW_ATTR_MASK]) {
2125 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2126 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2128 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2129 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2130 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2132 if (a[OVS_FLOW_ATTR_STATS]) {
2133 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2135 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2136 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2138 if (a[OVS_FLOW_ATTR_USED]) {
2139 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2144 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2145 * followed by Netlink attributes corresponding to 'flow'. */
2147 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2150 struct ovs_header *ovs_header;
2152 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2153 NLM_F_REQUEST | flow->nlmsg_flags,
2154 flow->cmd, OVS_FLOW_VERSION);
2156 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2157 ovs_header->dp_ifindex = flow->dp_ifindex;
2159 if (flow->key_len) {
2160 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2163 if (flow->mask_len) {
2164 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2167 if (flow->actions || flow->actions_len) {
2168 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2169 flow->actions, flow->actions_len);
2172 /* We never need to send these to the kernel. */
2173 ovs_assert(!flow->stats);
2174 ovs_assert(!flow->tcp_flags);
2175 ovs_assert(!flow->used);
2178 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2182 /* Clears 'flow' to "empty" values. */
2184 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2186 memset(flow, 0, sizeof *flow);
2189 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2190 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2191 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2192 * result of the command is expected to be a flow also, which is decoded and
2193 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2194 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2196 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2197 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2199 struct ofpbuf *request_buf;
2202 ovs_assert((reply != NULL) == (bufp != NULL));
2205 request->nlmsg_flags |= NLM_F_ECHO;
2208 request_buf = ofpbuf_new(1024);
2209 dpif_linux_flow_to_ofpbuf(request, request_buf);
2210 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2211 ofpbuf_delete(request_buf);
2215 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2218 dpif_linux_flow_init(reply);
2219 ofpbuf_delete(*bufp);
2227 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2228 struct dpif_flow_stats *stats)
2231 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2232 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2234 stats->n_packets = 0;
2237 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2238 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2241 /* Logs information about a packet that was recently lost in 'ch' (in
2244 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2246 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2247 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2250 if (VLOG_DROP_WARN(&rl)) {
2255 if (ch->last_poll != LLONG_MIN) {
2256 ds_put_format(&s, " (last polled %lld ms ago)",
2257 time_msec() - ch->last_poll);
2260 VLOG_WARN("%s: lost packet on channel %"PRIdPTR"%s",
2261 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));