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>
28 #include <linux/rtnetlink.h>
29 #include <linux/sockios.h>
33 #include <sys/epoll.h>
38 #include "dpif-provider.h"
39 #include "dynamic-string.h"
42 #include "netdev-linux.h"
43 #include "netdev-vport.h"
44 #include "netlink-notifier.h"
45 #include "netlink-socket.h"
50 #include "poll-loop.h"
55 #include "unaligned.h"
59 VLOG_DEFINE_THIS_MODULE(dpif_linux);
60 enum { MAX_PORTS = USHRT_MAX };
62 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
63 * missing if we have old headers. */
64 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
66 struct dpif_linux_dp {
67 /* Generic Netlink header. */
70 /* struct ovs_header. */
74 const char *name; /* OVS_DP_ATTR_NAME. */
75 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
76 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
77 struct ovs_dp_megaflow_stats megaflow_stats;
78 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
81 static void dpif_linux_dp_init(struct dpif_linux_dp *);
82 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
83 const struct ofpbuf *);
84 static void dpif_linux_dp_dump_start(struct nl_dump *);
85 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
86 struct dpif_linux_dp *reply,
87 struct ofpbuf **bufp);
88 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
89 struct ofpbuf **bufp);
91 struct dpif_linux_flow {
92 /* Generic Netlink header. */
95 /* struct ovs_header. */
96 unsigned int nlmsg_flags;
101 * The 'stats' member points to 64-bit data that might only be aligned on
102 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
105 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
106 * the Netlink version of the command, even if actions_len is zero. */
107 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
109 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
111 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
113 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
114 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
115 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
116 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
119 static void dpif_linux_flow_init(struct dpif_linux_flow *);
120 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
121 const struct ofpbuf *);
122 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
124 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
125 struct dpif_linux_flow *reply,
126 struct ofpbuf **bufp);
127 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
128 struct dpif_flow_stats *);
130 /* One of the dpif channels between the kernel and userspace. */
131 struct dpif_channel {
132 struct nl_sock *sock; /* Netlink socket. */
133 long long int last_poll; /* Last time this channel was polled. */
136 static void report_loss(struct dpif *, struct dpif_channel *);
138 /* Datapath interface for the openvswitch Linux kernel module. */
143 /* Upcall messages. */
144 struct ovs_mutex upcall_lock;
145 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
146 struct dpif_channel *channels;
147 struct epoll_event *epoll_events;
148 int epoll_fd; /* epoll fd that includes channel socks. */
149 int n_events; /* Num events returned by epoll_wait(). */
150 int event_offset; /* Offset into 'epoll_events'. */
152 /* Change notification. */
153 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
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 *,
176 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
178 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
179 const struct ofpbuf *);
181 static struct dpif_linux *
182 dpif_linux_cast(const struct dpif *dpif)
184 dpif_assert_class(dpif, &dpif_linux_class);
185 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
189 dpif_linux_enumerate(struct sset *all_dps)
195 error = dpif_linux_init();
200 dpif_linux_dp_dump_start(&dump);
201 while (nl_dump_next(&dump, &msg)) {
202 struct dpif_linux_dp dp;
204 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
205 sset_add(all_dps, dp.name);
208 return nl_dump_done(&dump);
212 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
213 bool create, struct dpif **dpifp)
215 struct dpif_linux_dp dp_request, dp;
220 error = dpif_linux_init();
225 /* Create or look up datapath. */
226 dpif_linux_dp_init(&dp_request);
228 dp_request.cmd = OVS_DP_CMD_NEW;
230 dp_request.upcall_pid = &upcall_pid;
232 dp_request.cmd = OVS_DP_CMD_GET;
234 dp_request.name = name;
235 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
240 error = open_dpif(&dp, dpifp);
246 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
248 struct dpif_linux *dpif;
250 dpif = xzalloc(sizeof *dpif);
251 dpif->port_notifier = NULL;
252 ovs_mutex_init(&dpif->upcall_lock);
255 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
256 dp->dp_ifindex, dp->dp_ifindex);
258 dpif->dp_ifindex = dp->dp_ifindex;
259 *dpifp = &dpif->dpif;
265 destroy_channels(struct dpif_linux *dpif)
269 if (dpif->epoll_fd < 0) {
273 for (i = 0; i < dpif->uc_array_size; i++ ) {
274 struct dpif_linux_vport vport_request;
275 struct dpif_channel *ch = &dpif->channels[i];
276 uint32_t upcall_pid = 0;
282 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
284 /* Turn off upcalls. */
285 dpif_linux_vport_init(&vport_request);
286 vport_request.cmd = OVS_VPORT_CMD_SET;
287 vport_request.dp_ifindex = dpif->dp_ifindex;
288 vport_request.port_no = u32_to_odp(i);
289 vport_request.upcall_pid = &upcall_pid;
290 dpif_linux_vport_transact(&vport_request, NULL, NULL);
292 nl_sock_destroy(ch->sock);
295 free(dpif->channels);
296 dpif->channels = NULL;
297 dpif->uc_array_size = 0;
299 free(dpif->epoll_events);
300 dpif->epoll_events = NULL;
301 dpif->n_events = dpif->event_offset = 0;
303 /* Don't close dpif->epoll_fd since that would cause other threads that
304 * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */
308 add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
310 struct epoll_event event;
311 uint32_t port_idx = odp_to_u32(port_no);
313 if (dpif->epoll_fd < 0) {
317 /* We assume that the datapath densely chooses port numbers, which
318 * can therefore be used as an index into an array of channels. */
319 if (port_idx >= dpif->uc_array_size) {
320 uint32_t new_size = port_idx + 1;
323 if (new_size > MAX_PORTS) {
324 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
325 dpif_name(&dpif->dpif), port_no);
329 dpif->channels = xrealloc(dpif->channels,
330 new_size * sizeof *dpif->channels);
331 for (i = dpif->uc_array_size; i < new_size; i++) {
332 dpif->channels[i].sock = NULL;
335 dpif->epoll_events = xrealloc(dpif->epoll_events,
336 new_size * sizeof *dpif->epoll_events);
337 dpif->uc_array_size = new_size;
340 memset(&event, 0, sizeof event);
341 event.events = EPOLLIN;
342 event.data.u32 = port_idx;
343 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
348 nl_sock_destroy(dpif->channels[port_idx].sock);
349 dpif->channels[port_idx].sock = sock;
350 dpif->channels[port_idx].last_poll = LLONG_MIN;
356 del_channel(struct dpif_linux *dpif, odp_port_t port_no)
358 struct dpif_channel *ch;
359 uint32_t port_idx = odp_to_u32(port_no);
361 if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
365 ch = &dpif->channels[port_idx];
370 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
371 dpif->event_offset = dpif->n_events = 0;
373 nl_sock_destroy(ch->sock);
378 dpif_linux_close(struct dpif *dpif_)
380 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
382 nl_sock_destroy(dpif->port_notifier);
383 destroy_channels(dpif);
384 if (dpif->epoll_fd >= 0) {
385 close(dpif->epoll_fd);
387 ovs_mutex_destroy(&dpif->upcall_lock);
392 dpif_linux_destroy(struct dpif *dpif_)
394 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
395 struct dpif_linux_dp dp;
397 dpif_linux_dp_init(&dp);
398 dp.cmd = OVS_DP_CMD_DEL;
399 dp.dp_ifindex = dpif->dp_ifindex;
400 return dpif_linux_dp_transact(&dp, NULL, NULL);
404 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
406 struct dpif_linux_dp dp;
410 error = dpif_linux_dp_get(dpif_, &dp, &buf);
412 stats->n_hit = dp.stats.n_hit;
413 stats->n_missed = dp.stats.n_missed;
414 stats->n_lost = dp.stats.n_lost;
415 stats->n_flows = dp.stats.n_flows;
416 stats->n_masks = dp.megaflow_stats.n_masks;
417 stats->n_mask_hit = dp.megaflow_stats.n_mask_hit;
424 get_vport_type(const struct dpif_linux_vport *vport)
426 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
428 switch (vport->type) {
429 case OVS_VPORT_TYPE_NETDEV:
432 case OVS_VPORT_TYPE_INTERNAL:
435 case OVS_VPORT_TYPE_GRE:
438 case OVS_VPORT_TYPE_GRE64:
441 case OVS_VPORT_TYPE_VXLAN:
444 case OVS_VPORT_TYPE_LISP:
447 case OVS_VPORT_TYPE_UNSPEC:
448 case __OVS_VPORT_TYPE_MAX:
452 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
453 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
457 static enum ovs_vport_type
458 netdev_to_ovs_vport_type(const struct netdev *netdev)
460 const char *type = netdev_get_type(netdev);
462 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
463 return OVS_VPORT_TYPE_NETDEV;
464 } else if (!strcmp(type, "internal")) {
465 return OVS_VPORT_TYPE_INTERNAL;
466 } else if (strstr(type, "gre64")) {
467 return OVS_VPORT_TYPE_GRE64;
468 } else if (strstr(type, "gre")) {
469 return OVS_VPORT_TYPE_GRE;
470 } else if (!strcmp(type, "vxlan")) {
471 return OVS_VPORT_TYPE_VXLAN;
472 } else if (!strcmp(type, "lisp")) {
473 return OVS_VPORT_TYPE_LISP;
475 return OVS_VPORT_TYPE_UNSPEC;
480 dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
481 odp_port_t *port_nop)
483 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
484 const struct netdev_tunnel_config *tnl_cfg;
485 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
486 const char *name = netdev_vport_get_dpif_port(netdev,
487 namebuf, sizeof namebuf);
488 const char *type = netdev_get_type(netdev);
489 struct dpif_linux_vport request, reply;
490 struct nl_sock *sock = NULL;
493 uint64_t options_stub[64 / 8];
494 struct ofpbuf options;
497 if (dpif->epoll_fd >= 0) {
498 error = nl_sock_create(NETLINK_GENERIC, &sock);
504 dpif_linux_vport_init(&request);
505 request.cmd = OVS_VPORT_CMD_NEW;
506 request.dp_ifindex = dpif->dp_ifindex;
507 request.type = netdev_to_ovs_vport_type(netdev);
508 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
509 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
510 "unsupported type `%s'",
511 dpif_name(dpif_), name, type);
512 nl_sock_destroy(sock);
517 if (request.type == OVS_VPORT_TYPE_NETDEV) {
518 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
521 tnl_cfg = netdev_get_tunnel_config(netdev);
522 if (tnl_cfg && tnl_cfg->dst_port != 0) {
523 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
524 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
525 ntohs(tnl_cfg->dst_port));
526 request.options = options.data;
527 request.options_len = options.size;
530 request.port_no = *port_nop;
531 upcall_pid = sock ? nl_sock_pid(sock) : 0;
532 request.upcall_pid = &upcall_pid;
534 error = dpif_linux_vport_transact(&request, &reply, &buf);
536 *port_nop = reply.port_no;
537 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
538 dpif_name(dpif_), reply.port_no, upcall_pid);
540 if (error == EBUSY && *port_nop != ODPP_NONE) {
541 VLOG_INFO("%s: requested port %"PRIu32" is in use",
542 dpif_name(dpif_), *port_nop);
544 nl_sock_destroy(sock);
551 error = add_channel(dpif, *port_nop, sock);
553 VLOG_INFO("%s: could not add channel for port %s",
554 dpif_name(dpif_), name);
556 /* Delete the port. */
557 dpif_linux_vport_init(&request);
558 request.cmd = OVS_VPORT_CMD_DEL;
559 request.dp_ifindex = dpif->dp_ifindex;
560 request.port_no = *port_nop;
561 dpif_linux_vport_transact(&request, NULL, NULL);
563 nl_sock_destroy(sock);
572 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
573 odp_port_t *port_nop)
575 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
578 ovs_mutex_lock(&dpif->upcall_lock);
579 error = dpif_linux_port_add__(dpif_, netdev, port_nop);
580 ovs_mutex_unlock(&dpif->upcall_lock);
586 dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
588 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
589 struct dpif_linux_vport vport;
592 dpif_linux_vport_init(&vport);
593 vport.cmd = OVS_VPORT_CMD_DEL;
594 vport.dp_ifindex = dpif->dp_ifindex;
595 vport.port_no = port_no;
596 error = dpif_linux_vport_transact(&vport, NULL, NULL);
598 del_channel(dpif, port_no);
604 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
606 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
609 ovs_mutex_lock(&dpif->upcall_lock);
610 error = dpif_linux_port_del__(dpif_, port_no);
611 ovs_mutex_unlock(&dpif->upcall_lock);
617 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
618 const char *port_name, struct dpif_port *dpif_port)
620 struct dpif_linux_vport request;
621 struct dpif_linux_vport reply;
625 dpif_linux_vport_init(&request);
626 request.cmd = OVS_VPORT_CMD_GET;
627 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
628 request.port_no = port_no;
629 request.name = port_name;
631 error = dpif_linux_vport_transact(&request, &reply, &buf);
633 if (reply.dp_ifindex != request.dp_ifindex) {
634 /* A query by name reported that 'port_name' is in some datapath
635 * other than 'dpif', but the caller wants to know about 'dpif'. */
637 } else if (dpif_port) {
638 dpif_port->name = xstrdup(reply.name);
639 dpif_port->type = xstrdup(get_vport_type(&reply));
640 dpif_port->port_no = reply.port_no;
648 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
649 struct dpif_port *dpif_port)
651 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
655 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
656 struct dpif_port *dpif_port)
658 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
662 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
668 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
670 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
671 uint32_t port_idx = odp_to_u32(port_no);
674 ovs_mutex_lock(&dpif->upcall_lock);
675 if (dpif->epoll_fd >= 0) {
676 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
677 * channel, since it is not heavily loaded. */
678 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
679 pid = nl_sock_pid(dpif->channels[idx].sock);
681 ovs_mutex_unlock(&dpif->upcall_lock);
687 dpif_linux_flow_flush(struct dpif *dpif_)
689 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
690 struct dpif_linux_flow flow;
692 dpif_linux_flow_init(&flow);
693 flow.cmd = OVS_FLOW_CMD_DEL;
694 flow.dp_ifindex = dpif->dp_ifindex;
695 return dpif_linux_flow_transact(&flow, NULL, NULL);
698 struct dpif_linux_port_state {
703 dpif_linux_port_dump_start__(const struct dpif *dpif_, struct nl_dump *dump)
705 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
706 struct dpif_linux_vport request;
709 dpif_linux_vport_init(&request);
710 request.cmd = OVS_VPORT_CMD_GET;
711 request.dp_ifindex = dpif->dp_ifindex;
713 buf = ofpbuf_new(1024);
714 dpif_linux_vport_to_ofpbuf(&request, buf);
715 nl_dump_start(dump, NETLINK_GENERIC, buf);
720 dpif_linux_port_dump_start(const struct dpif *dpif, void **statep)
722 struct dpif_linux_port_state *state;
724 *statep = state = xmalloc(sizeof *state);
725 dpif_linux_port_dump_start__(dpif, &state->dump);
731 dpif_linux_port_dump_next__(const struct dpif *dpif_, struct nl_dump *dump,
732 struct dpif_linux_vport *vport)
734 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
738 if (!nl_dump_next(dump, &buf)) {
742 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
744 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
745 dpif_name(&dpif->dpif), ovs_strerror(error));
751 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
752 struct dpif_port *dpif_port)
754 struct dpif_linux_port_state *state = state_;
755 struct dpif_linux_vport vport;
758 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport);
762 dpif_port->name = CONST_CAST(char *, vport.name);
763 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
764 dpif_port->port_no = vport.port_no;
769 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
771 struct dpif_linux_port_state *state = state_;
772 int error = nl_dump_done(&state->dump);
779 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
781 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
783 /* Lazily create the Netlink socket to listen for notifications. */
784 if (!dpif->port_notifier) {
785 struct nl_sock *sock;
788 error = nl_sock_create(NETLINK_GENERIC, &sock);
793 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
795 nl_sock_destroy(sock);
798 dpif->port_notifier = sock;
800 /* We have no idea of the current state so report that everything
806 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
807 uint64_t buf_stub[4096 / 8];
811 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
812 error = nl_sock_recv(dpif->port_notifier, &buf, false);
814 struct dpif_linux_vport vport;
816 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
818 if (vport.dp_ifindex == dpif->dp_ifindex
819 && (vport.cmd == OVS_VPORT_CMD_NEW
820 || vport.cmd == OVS_VPORT_CMD_DEL
821 || vport.cmd == OVS_VPORT_CMD_SET)) {
822 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
823 dpif->dpif.full_name, vport.name, vport.cmd);
824 *devnamep = xstrdup(vport.name);
829 } else if (error != EAGAIN) {
830 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
831 ovs_strerror(error));
832 nl_sock_drain(dpif->port_notifier);
844 dpif_linux_port_poll_wait(const struct dpif *dpif_)
846 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
848 if (dpif->port_notifier) {
849 nl_sock_wait(dpif->port_notifier, POLLIN);
851 poll_immediate_wake();
856 dpif_linux_flow_get__(const struct dpif *dpif_,
857 const struct nlattr *key, size_t key_len,
858 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
860 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
861 struct dpif_linux_flow request;
863 dpif_linux_flow_init(&request);
864 request.cmd = OVS_FLOW_CMD_GET;
865 request.dp_ifindex = dpif->dp_ifindex;
867 request.key_len = key_len;
868 return dpif_linux_flow_transact(&request, reply, bufp);
872 dpif_linux_flow_get(const struct dpif *dpif_,
873 const struct nlattr *key, size_t key_len,
874 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
876 struct dpif_linux_flow reply;
880 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
883 dpif_linux_flow_get_stats(&reply, stats);
886 buf->data = CONST_CAST(struct nlattr *, reply.actions);
887 buf->size = reply.actions_len;
897 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
898 struct dpif_linux_flow *request)
900 static const struct nlattr dummy_action;
902 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
904 dpif_linux_flow_init(request);
905 request->cmd = (put->flags & DPIF_FP_CREATE
906 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
907 request->dp_ifindex = dpif->dp_ifindex;
908 request->key = put->key;
909 request->key_len = put->key_len;
910 request->mask = put->mask;
911 request->mask_len = put->mask_len;
912 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
913 request->actions = (put->actions
915 : CONST_CAST(struct nlattr *, &dummy_action));
916 request->actions_len = put->actions_len;
917 if (put->flags & DPIF_FP_ZERO_STATS) {
918 request->clear = true;
920 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
924 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
926 struct dpif_linux_flow request, reply;
930 dpif_linux_init_flow_put(dpif_, put, &request);
931 error = dpif_linux_flow_transact(&request,
932 put->stats ? &reply : NULL,
933 put->stats ? &buf : NULL);
934 if (!error && put->stats) {
935 dpif_linux_flow_get_stats(&reply, put->stats);
942 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
943 struct dpif_linux_flow *request)
945 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
947 dpif_linux_flow_init(request);
948 request->cmd = OVS_FLOW_CMD_DEL;
949 request->dp_ifindex = dpif->dp_ifindex;
950 request->key = del->key;
951 request->key_len = del->key_len;
955 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
957 struct dpif_linux_flow request, reply;
961 dpif_linux_init_flow_del(dpif_, del, &request);
962 error = dpif_linux_flow_transact(&request,
963 del->stats ? &reply : NULL,
964 del->stats ? &buf : NULL);
965 if (!error && del->stats) {
966 dpif_linux_flow_get_stats(&reply, del->stats);
972 struct dpif_linux_flow_state {
974 struct dpif_linux_flow flow;
975 struct dpif_flow_stats stats;
980 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
982 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
983 struct dpif_linux_flow_state *state;
984 struct dpif_linux_flow request;
987 *statep = state = xmalloc(sizeof *state);
989 dpif_linux_flow_init(&request);
990 request.cmd = OVS_FLOW_CMD_GET;
991 request.dp_ifindex = dpif->dp_ifindex;
993 buf = ofpbuf_new(1024);
994 dpif_linux_flow_to_ofpbuf(&request, buf);
995 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
1004 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
1005 const struct nlattr **key, size_t *key_len,
1006 const struct nlattr **mask, size_t *mask_len,
1007 const struct nlattr **actions, size_t *actions_len,
1008 const struct dpif_flow_stats **stats)
1010 struct dpif_linux_flow_state *state = state_;
1015 ofpbuf_delete(state->buf);
1018 if (!nl_dump_next(&state->dump, &buf)) {
1022 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1027 if (actions && !state->flow.actions) {
1028 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1029 state->flow.key_len,
1030 &state->flow, &state->buf);
1031 if (error == ENOENT) {
1032 VLOG_DBG("dumped flow disappeared on get");
1034 VLOG_WARN("error fetching dumped flow: %s",
1035 ovs_strerror(error));
1041 *actions = state->flow.actions;
1042 *actions_len = state->flow.actions_len;
1045 *key = state->flow.key;
1046 *key_len = state->flow.key_len;
1049 *mask = state->flow.mask;
1050 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1053 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1054 *stats = &state->stats;
1060 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1062 struct dpif_linux_flow_state *state = state_;
1063 int error = nl_dump_done(&state->dump);
1064 ofpbuf_delete(state->buf);
1070 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1073 struct ovs_header *k_exec;
1075 ofpbuf_prealloc_tailroom(buf, (64
1076 + d_exec->packet->size
1078 + d_exec->actions_len));
1080 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1081 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1083 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1084 k_exec->dp_ifindex = dp_ifindex;
1086 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1087 d_exec->packet->data, d_exec->packet->size);
1088 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
1089 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1090 d_exec->actions, d_exec->actions_len);
1094 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1096 uint64_t request_stub[1024 / 8];
1097 struct ofpbuf request;
1100 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1101 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1102 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1103 ofpbuf_uninit(&request);
1109 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1111 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1113 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1119 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1121 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1124 struct nl_transaction txn;
1126 struct ofpbuf request;
1127 uint64_t request_stub[1024 / 8];
1129 struct ofpbuf reply;
1130 uint64_t reply_stub[1024 / 8];
1133 struct nl_transaction *txnsp[MAX_OPS];
1136 ovs_assert(n_ops <= MAX_OPS);
1137 for (i = 0; i < n_ops; i++) {
1138 struct op_auxdata *aux = &auxes[i];
1139 struct dpif_op *op = ops[i];
1140 struct dpif_flow_put *put;
1141 struct dpif_flow_del *del;
1142 struct dpif_execute *execute;
1143 struct dpif_linux_flow flow;
1145 ofpbuf_use_stub(&aux->request,
1146 aux->request_stub, sizeof aux->request_stub);
1147 aux->txn.request = &aux->request;
1149 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1150 aux->txn.reply = NULL;
1153 case DPIF_OP_FLOW_PUT:
1154 put = &op->u.flow_put;
1155 dpif_linux_init_flow_put(dpif_, put, &flow);
1157 flow.nlmsg_flags |= NLM_F_ECHO;
1158 aux->txn.reply = &aux->reply;
1160 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1163 case DPIF_OP_FLOW_DEL:
1164 del = &op->u.flow_del;
1165 dpif_linux_init_flow_del(dpif_, del, &flow);
1167 flow.nlmsg_flags |= NLM_F_ECHO;
1168 aux->txn.reply = &aux->reply;
1170 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1173 case DPIF_OP_EXECUTE:
1174 execute = &op->u.execute;
1175 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1184 for (i = 0; i < n_ops; i++) {
1185 txnsp[i] = &auxes[i].txn;
1187 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1189 for (i = 0; i < n_ops; i++) {
1190 struct op_auxdata *aux = &auxes[i];
1191 struct nl_transaction *txn = &auxes[i].txn;
1192 struct dpif_op *op = ops[i];
1193 struct dpif_flow_put *put;
1194 struct dpif_flow_del *del;
1196 op->error = txn->error;
1199 case DPIF_OP_FLOW_PUT:
1200 put = &op->u.flow_put;
1203 struct dpif_linux_flow reply;
1205 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1208 dpif_linux_flow_get_stats(&reply, put->stats);
1213 memset(put->stats, 0, sizeof *put->stats);
1218 case DPIF_OP_FLOW_DEL:
1219 del = &op->u.flow_del;
1222 struct dpif_linux_flow reply;
1224 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1227 dpif_linux_flow_get_stats(&reply, del->stats);
1232 memset(del->stats, 0, sizeof *del->stats);
1237 case DPIF_OP_EXECUTE:
1244 ofpbuf_uninit(&aux->request);
1245 ofpbuf_uninit(&aux->reply);
1250 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1253 size_t chunk = MIN(n_ops, MAX_OPS);
1254 dpif_linux_operate__(dpif, ops, chunk);
1260 /* Synchronizes 'dpif->channels' with the set of vports currently in 'dpif' in
1261 * the kernel, by adding a new channel for any kernel vport that lacks one and
1262 * deleting any channels that have no backing kernel vports. */
1264 dpif_linux_refresh_channels(struct dpif *dpif_)
1266 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1267 unsigned long int *keep_channels;
1268 struct dpif_linux_vport vport;
1269 size_t keep_channels_nbits;
1270 struct nl_dump dump;
1274 /* To start with, we need an epoll fd. */
1275 if (dpif->epoll_fd < 0) {
1276 dpif->epoll_fd = epoll_create(10);
1277 if (dpif->epoll_fd < 0) {
1282 keep_channels_nbits = dpif->uc_array_size;
1283 keep_channels = bitmap_allocate(keep_channels_nbits);
1285 dpif->n_events = dpif->event_offset = 0;
1287 dpif_linux_port_dump_start__(dpif_, &dump);
1288 while (dpif_linux_port_dump_next__(dpif_, &dump, &vport)) {
1289 uint32_t port_no = odp_to_u32(vport.port_no);
1290 struct nl_sock *sock = (port_no < dpif->uc_array_size
1291 ? dpif->channels[port_no].sock
1293 bool new_sock = !sock;
1297 error = nl_sock_create(NETLINK_GENERIC, &sock);
1304 /* Configure the vport to deliver misses to 'sock'. */
1305 if (!vport.upcall_pid || *vport.upcall_pid != nl_sock_pid(sock)) {
1306 uint32_t upcall_pid = nl_sock_pid(sock);
1307 struct dpif_linux_vport vport_request;
1309 dpif_linux_vport_init(&vport_request);
1310 vport_request.cmd = OVS_VPORT_CMD_SET;
1311 vport_request.dp_ifindex = dpif->dp_ifindex;
1312 vport_request.port_no = vport.port_no;
1313 vport_request.upcall_pid = &upcall_pid;
1314 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1316 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1317 dpif_name(&dpif->dpif), vport_request.port_no,
1320 VLOG_WARN_RL(&error_rl,
1321 "%s: failed to set upcall pid on port: %s",
1322 dpif_name(&dpif->dpif), ovs_strerror(error));
1324 if (error != ENODEV && error != ENOENT) {
1327 /* The vport isn't really there, even though the dump says
1328 * it is. Probably we just hit a race after a port
1336 error = add_channel(dpif, vport.port_no, sock);
1338 VLOG_INFO("%s: could not add channel for port %s",
1339 dpif_name(dpif_), vport.name);
1345 if (port_no < keep_channels_nbits) {
1346 bitmap_set1(keep_channels, port_no);
1351 nl_sock_destroy(sock);
1353 nl_dump_done(&dump);
1355 /* Discard any saved channels that we didn't reuse. */
1356 for (i = 0; i < keep_channels_nbits; i++) {
1357 if (!bitmap_is_set(keep_channels, i)) {
1358 nl_sock_destroy(dpif->channels[i].sock);
1359 dpif->channels[i].sock = NULL;
1362 free(keep_channels);
1368 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1370 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1372 if ((dpif->epoll_fd >= 0) == enable) {
1374 } else if (!enable) {
1375 destroy_channels(dpif);
1378 return dpif_linux_refresh_channels(dpif_);
1383 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1385 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1388 ovs_mutex_lock(&dpif->upcall_lock);
1389 error = dpif_linux_recv_set__(dpif_, enable);
1390 ovs_mutex_unlock(&dpif->upcall_lock);
1396 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1397 uint32_t queue_id, uint32_t *priority)
1399 if (queue_id < 0xf000) {
1400 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1408 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1411 static const struct nl_policy ovs_packet_policy[] = {
1412 /* Always present. */
1413 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1414 .min_len = ETH_HEADER_LEN },
1415 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1417 /* OVS_PACKET_CMD_ACTION only. */
1418 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1421 struct ovs_header *ovs_header;
1422 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1423 struct nlmsghdr *nlmsg;
1424 struct genlmsghdr *genl;
1428 ofpbuf_use_const(&b, buf->data, buf->size);
1430 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1431 genl = ofpbuf_try_pull(&b, sizeof *genl);
1432 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1433 if (!nlmsg || !genl || !ovs_header
1434 || nlmsg->nlmsg_type != ovs_packet_family
1435 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1436 ARRAY_SIZE(ovs_packet_policy))) {
1440 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1441 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1447 memset(upcall, 0, sizeof *upcall);
1448 upcall->type = type;
1449 upcall->packet = buf;
1450 upcall->packet->data = CONST_CAST(struct nlattr *,
1451 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1452 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1453 upcall->key = CONST_CAST(struct nlattr *,
1454 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1455 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1456 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1457 *dp_ifindex = ovs_header->dp_ifindex;
1463 dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
1466 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1469 if (dpif->epoll_fd < 0) {
1473 if (dpif->event_offset >= dpif->n_events) {
1476 dpif->event_offset = dpif->n_events = 0;
1479 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1480 dpif->uc_array_size, 0);
1481 } while (retval < 0 && errno == EINTR);
1483 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1484 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1485 } else if (retval > 0) {
1486 dpif->n_events = retval;
1490 while (dpif->event_offset < dpif->n_events) {
1491 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1492 struct dpif_channel *ch = &dpif->channels[idx];
1494 dpif->event_offset++;
1500 if (++read_tries > 50) {
1504 error = nl_sock_recv(ch->sock, buf, false);
1505 if (error == ENOBUFS) {
1506 /* ENOBUFS typically means that we've received so many
1507 * packets that the buffer overflowed. Try again
1508 * immediately because there's almost certainly a packet
1509 * waiting for us. */
1510 report_loss(dpif_, ch);
1514 ch->last_poll = time_msec();
1516 if (error == EAGAIN) {
1522 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1523 if (!error && dp_ifindex == dpif->dp_ifindex) {
1535 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1538 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1541 ovs_mutex_lock(&dpif->upcall_lock);
1542 error = dpif_linux_recv__(dpif_, upcall, buf);
1543 ovs_mutex_unlock(&dpif->upcall_lock);
1549 dpif_linux_recv_wait(struct dpif *dpif_)
1551 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1553 ovs_mutex_lock(&dpif->upcall_lock);
1554 if (dpif->epoll_fd >= 0) {
1555 poll_fd_wait(dpif->epoll_fd, POLLIN);
1557 ovs_mutex_unlock(&dpif->upcall_lock);
1561 dpif_linux_recv_purge(struct dpif *dpif_)
1563 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1565 ovs_mutex_lock(&dpif->upcall_lock);
1566 if (dpif->epoll_fd >= 0) {
1567 struct dpif_channel *ch;
1569 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
1572 nl_sock_drain(ch->sock);
1576 ovs_mutex_unlock(&dpif->upcall_lock);
1579 const struct dpif_class dpif_linux_class = {
1581 dpif_linux_enumerate,
1588 dpif_linux_get_stats,
1589 dpif_linux_port_add,
1590 dpif_linux_port_del,
1591 dpif_linux_port_query_by_number,
1592 dpif_linux_port_query_by_name,
1593 dpif_linux_get_max_ports,
1594 dpif_linux_port_get_pid,
1595 dpif_linux_port_dump_start,
1596 dpif_linux_port_dump_next,
1597 dpif_linux_port_dump_done,
1598 dpif_linux_port_poll,
1599 dpif_linux_port_poll_wait,
1600 dpif_linux_flow_get,
1601 dpif_linux_flow_put,
1602 dpif_linux_flow_del,
1603 dpif_linux_flow_flush,
1604 dpif_linux_flow_dump_start,
1605 dpif_linux_flow_dump_next,
1606 dpif_linux_flow_dump_done,
1609 dpif_linux_recv_set,
1610 dpif_linux_queue_to_priority,
1612 dpif_linux_recv_wait,
1613 dpif_linux_recv_purge,
1617 dpif_linux_init(void)
1619 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1622 if (ovsthread_once_start(&once)) {
1623 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1624 &ovs_datapath_family);
1626 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1627 "The Open vSwitch kernel module is probably not loaded.",
1628 OVS_DATAPATH_FAMILY);
1631 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1634 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1637 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1638 &ovs_packet_family);
1641 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1642 &ovs_vport_mcgroup);
1645 ovsthread_once_done(&once);
1652 dpif_linux_is_internal_device(const char *name)
1654 struct dpif_linux_vport reply;
1658 error = dpif_linux_vport_get(name, &reply, &buf);
1661 } else if (error != ENODEV && error != ENOENT) {
1662 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1663 name, ovs_strerror(error));
1666 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1669 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1670 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1671 * positive errno value.
1673 * 'vport' will contain pointers into 'buf', so the caller should not free
1674 * 'buf' while 'vport' is still in use. */
1676 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1677 const struct ofpbuf *buf)
1679 static const struct nl_policy ovs_vport_policy[] = {
1680 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1681 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1682 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1683 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1684 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1686 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1689 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1690 struct ovs_header *ovs_header;
1691 struct nlmsghdr *nlmsg;
1692 struct genlmsghdr *genl;
1695 dpif_linux_vport_init(vport);
1697 ofpbuf_use_const(&b, buf->data, buf->size);
1698 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1699 genl = ofpbuf_try_pull(&b, sizeof *genl);
1700 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1701 if (!nlmsg || !genl || !ovs_header
1702 || nlmsg->nlmsg_type != ovs_vport_family
1703 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1704 ARRAY_SIZE(ovs_vport_policy))) {
1708 vport->cmd = genl->cmd;
1709 vport->dp_ifindex = ovs_header->dp_ifindex;
1710 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1711 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1712 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1713 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1714 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1716 if (a[OVS_VPORT_ATTR_STATS]) {
1717 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1719 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1720 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1721 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1726 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1727 * followed by Netlink attributes corresponding to 'vport'. */
1729 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1732 struct ovs_header *ovs_header;
1734 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1735 vport->cmd, OVS_VPORT_VERSION);
1737 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1738 ovs_header->dp_ifindex = vport->dp_ifindex;
1740 if (vport->port_no != ODPP_NONE) {
1741 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1744 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1745 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1749 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1752 if (vport->upcall_pid) {
1753 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1757 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1758 vport->stats, sizeof *vport->stats);
1761 if (vport->options) {
1762 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1763 vport->options, vport->options_len);
1767 /* Clears 'vport' to "empty" values. */
1769 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1771 memset(vport, 0, sizeof *vport);
1772 vport->port_no = ODPP_NONE;
1775 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1776 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1777 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1778 * result of the command is expected to be an ovs_vport also, which is decoded
1779 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1780 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1782 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1783 struct dpif_linux_vport *reply,
1784 struct ofpbuf **bufp)
1786 struct ofpbuf *request_buf;
1789 ovs_assert((reply != NULL) == (bufp != NULL));
1791 error = dpif_linux_init();
1795 dpif_linux_vport_init(reply);
1800 request_buf = ofpbuf_new(1024);
1801 dpif_linux_vport_to_ofpbuf(request, request_buf);
1802 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1803 ofpbuf_delete(request_buf);
1807 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1810 dpif_linux_vport_init(reply);
1811 ofpbuf_delete(*bufp);
1818 /* Obtains information about the kernel vport named 'name' and stores it into
1819 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1820 * longer needed ('reply' will contain pointers into '*bufp'). */
1822 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1823 struct ofpbuf **bufp)
1825 struct dpif_linux_vport request;
1827 dpif_linux_vport_init(&request);
1828 request.cmd = OVS_VPORT_CMD_GET;
1829 request.name = name;
1831 return dpif_linux_vport_transact(&request, reply, bufp);
1834 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1835 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1836 * positive errno value.
1838 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1839 * while 'dp' is still in use. */
1841 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1843 static const struct nl_policy ovs_datapath_policy[] = {
1844 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1845 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1847 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
1848 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
1852 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1853 struct ovs_header *ovs_header;
1854 struct nlmsghdr *nlmsg;
1855 struct genlmsghdr *genl;
1858 dpif_linux_dp_init(dp);
1860 ofpbuf_use_const(&b, buf->data, buf->size);
1861 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1862 genl = ofpbuf_try_pull(&b, sizeof *genl);
1863 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1864 if (!nlmsg || !genl || !ovs_header
1865 || nlmsg->nlmsg_type != ovs_datapath_family
1866 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1867 ARRAY_SIZE(ovs_datapath_policy))) {
1871 dp->cmd = genl->cmd;
1872 dp->dp_ifindex = ovs_header->dp_ifindex;
1873 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1874 if (a[OVS_DP_ATTR_STATS]) {
1875 /* Can't use structure assignment because Netlink doesn't ensure
1876 * sufficient alignment for 64-bit members. */
1877 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1881 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
1882 /* Can't use structure assignment because Netlink doesn't ensure
1883 * sufficient alignment for 64-bit members. */
1884 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
1885 sizeof dp->megaflow_stats);
1891 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1893 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1895 struct ovs_header *ovs_header;
1897 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1898 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1899 OVS_DATAPATH_VERSION);
1901 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1902 ovs_header->dp_ifindex = dp->dp_ifindex;
1905 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1908 if (dp->upcall_pid) {
1909 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1912 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1915 /* Clears 'dp' to "empty" values. */
1917 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1919 memset(dp, 0, sizeof *dp);
1920 dp->megaflow_stats.n_masks = UINT32_MAX;
1921 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
1925 dpif_linux_dp_dump_start(struct nl_dump *dump)
1927 struct dpif_linux_dp request;
1930 dpif_linux_dp_init(&request);
1931 request.cmd = OVS_DP_CMD_GET;
1933 buf = ofpbuf_new(1024);
1934 dpif_linux_dp_to_ofpbuf(&request, buf);
1935 nl_dump_start(dump, NETLINK_GENERIC, buf);
1939 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1940 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1941 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1942 * result of the command is expected to be of the same form, which is decoded
1943 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1944 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1946 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1947 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1949 struct ofpbuf *request_buf;
1952 ovs_assert((reply != NULL) == (bufp != NULL));
1954 request_buf = ofpbuf_new(1024);
1955 dpif_linux_dp_to_ofpbuf(request, request_buf);
1956 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1957 ofpbuf_delete(request_buf);
1960 dpif_linux_dp_init(reply);
1962 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1965 ofpbuf_delete(*bufp);
1972 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1973 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1974 * will contain pointers into '*bufp'). */
1976 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1977 struct ofpbuf **bufp)
1979 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1980 struct dpif_linux_dp request;
1982 dpif_linux_dp_init(&request);
1983 request.cmd = OVS_DP_CMD_GET;
1984 request.dp_ifindex = dpif->dp_ifindex;
1986 return dpif_linux_dp_transact(&request, reply, bufp);
1989 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1990 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1991 * positive errno value.
1993 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1994 * while 'flow' is still in use. */
1996 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1997 const struct ofpbuf *buf)
1999 static const struct nl_policy ovs_flow_policy[] = {
2000 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2001 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2002 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2003 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2005 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2006 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2007 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2010 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2011 struct ovs_header *ovs_header;
2012 struct nlmsghdr *nlmsg;
2013 struct genlmsghdr *genl;
2016 dpif_linux_flow_init(flow);
2018 ofpbuf_use_const(&b, buf->data, buf->size);
2019 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2020 genl = ofpbuf_try_pull(&b, sizeof *genl);
2021 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2022 if (!nlmsg || !genl || !ovs_header
2023 || nlmsg->nlmsg_type != ovs_flow_family
2024 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2025 ARRAY_SIZE(ovs_flow_policy))) {
2029 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2030 flow->dp_ifindex = ovs_header->dp_ifindex;
2031 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2032 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2034 if (a[OVS_FLOW_ATTR_MASK]) {
2035 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2036 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2038 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2039 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2040 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2042 if (a[OVS_FLOW_ATTR_STATS]) {
2043 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2045 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2046 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2048 if (a[OVS_FLOW_ATTR_USED]) {
2049 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2054 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2055 * followed by Netlink attributes corresponding to 'flow'. */
2057 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2060 struct ovs_header *ovs_header;
2062 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2063 NLM_F_REQUEST | flow->nlmsg_flags,
2064 flow->cmd, OVS_FLOW_VERSION);
2066 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2067 ovs_header->dp_ifindex = flow->dp_ifindex;
2069 if (flow->key_len) {
2070 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2073 if (flow->mask_len) {
2074 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2077 if (flow->actions || flow->actions_len) {
2078 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2079 flow->actions, flow->actions_len);
2082 /* We never need to send these to the kernel. */
2083 ovs_assert(!flow->stats);
2084 ovs_assert(!flow->tcp_flags);
2085 ovs_assert(!flow->used);
2088 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2092 /* Clears 'flow' to "empty" values. */
2094 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2096 memset(flow, 0, sizeof *flow);
2099 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2100 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2101 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2102 * result of the command is expected to be a flow also, which is decoded and
2103 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2104 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2106 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2107 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2109 struct ofpbuf *request_buf;
2112 ovs_assert((reply != NULL) == (bufp != NULL));
2115 request->nlmsg_flags |= NLM_F_ECHO;
2118 request_buf = ofpbuf_new(1024);
2119 dpif_linux_flow_to_ofpbuf(request, request_buf);
2120 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2121 ofpbuf_delete(request_buf);
2125 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2128 dpif_linux_flow_init(reply);
2129 ofpbuf_delete(*bufp);
2137 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2138 struct dpif_flow_stats *stats)
2141 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2142 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2144 stats->n_packets = 0;
2147 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2148 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2151 /* Logs information about a packet that was recently lost in 'ch' (in
2154 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2156 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2157 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2160 if (VLOG_DROP_WARN(&rl)) {
2165 if (ch->last_poll != LLONG_MIN) {
2166 ds_put_format(&s, " (last polled %lld ms ago)",
2167 time_msec() - ch->last_poll);
2170 VLOG_WARN("%s: lost packet on channel %"PRIdPTR"%s",
2171 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));