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"
49 #include "openvswitch/datapath-compat.h"
51 #include "poll-loop.h"
56 #include "unaligned.h"
60 VLOG_DEFINE_THIS_MODULE(dpif_linux);
61 enum { MAX_PORTS = USHRT_MAX };
63 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
64 * missing if we have old headers. */
65 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
67 struct dpif_linux_dp {
68 /* Generic Netlink header. */
71 /* struct ovs_header. */
75 const char *name; /* OVS_DP_ATTR_NAME. */
76 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
77 struct ovs_dp_stats stats; /* OVS_DP_ATTR_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. */
155 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
157 /* Generic Netlink family numbers for OVS.
159 * Initialized by dpif_linux_init(). */
160 static int ovs_datapath_family;
161 static int ovs_vport_family;
162 static int ovs_flow_family;
163 static int ovs_packet_family;
165 /* Generic Netlink multicast groups for OVS.
167 * Initialized by dpif_linux_init(). */
168 static unsigned int ovs_vport_mcgroup;
170 static int dpif_linux_init(void);
171 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
172 static uint32_t dpif_linux_port_get_pid(const struct dpif *,
175 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
177 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
178 const struct ofpbuf *);
180 static struct dpif_linux *
181 dpif_linux_cast(const struct dpif *dpif)
183 dpif_assert_class(dpif, &dpif_linux_class);
184 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
188 dpif_linux_enumerate(struct sset *all_dps)
194 error = dpif_linux_init();
199 dpif_linux_dp_dump_start(&dump);
200 while (nl_dump_next(&dump, &msg)) {
201 struct dpif_linux_dp dp;
203 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
204 sset_add(all_dps, dp.name);
207 return nl_dump_done(&dump);
211 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
212 bool create, struct dpif **dpifp)
214 struct dpif_linux_dp dp_request, dp;
219 error = dpif_linux_init();
224 /* Create or look up datapath. */
225 dpif_linux_dp_init(&dp_request);
227 dp_request.cmd = OVS_DP_CMD_NEW;
229 dp_request.upcall_pid = &upcall_pid;
231 dp_request.cmd = OVS_DP_CMD_GET;
233 dp_request.name = name;
234 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
239 error = open_dpif(&dp, dpifp);
245 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
247 struct dpif_linux *dpif;
249 dpif = xzalloc(sizeof *dpif);
250 dpif->port_notifier = NULL;
251 ovs_mutex_init(&dpif->upcall_lock, PTHREAD_MUTEX_DEFAULT);
254 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
255 dp->dp_ifindex, dp->dp_ifindex);
257 dpif->dp_ifindex = dp->dp_ifindex;
258 *dpifp = &dpif->dpif;
264 destroy_channels(struct dpif_linux *dpif)
268 if (dpif->epoll_fd < 0) {
272 for (i = 0; i < dpif->uc_array_size; i++ ) {
273 struct dpif_linux_vport vport_request;
274 struct dpif_channel *ch = &dpif->channels[i];
275 uint32_t upcall_pid = 0;
281 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
283 /* Turn off upcalls. */
284 dpif_linux_vport_init(&vport_request);
285 vport_request.cmd = OVS_VPORT_CMD_SET;
286 vport_request.dp_ifindex = dpif->dp_ifindex;
287 vport_request.port_no = u32_to_odp(i);
288 vport_request.upcall_pid = &upcall_pid;
289 dpif_linux_vport_transact(&vport_request, NULL, NULL);
291 nl_sock_destroy(ch->sock);
294 free(dpif->channels);
295 dpif->channels = NULL;
296 dpif->uc_array_size = 0;
298 free(dpif->epoll_events);
299 dpif->epoll_events = NULL;
300 dpif->n_events = dpif->event_offset = 0;
302 /* Don't close dpif->epoll_fd since that would cause other threads that
303 * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */
307 add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
309 struct epoll_event event;
310 uint32_t port_idx = odp_to_u32(port_no);
312 if (dpif->epoll_fd < 0) {
316 /* We assume that the datapath densely chooses port numbers, which
317 * can therefore be used as an index into an array of channels. */
318 if (port_idx >= dpif->uc_array_size) {
319 uint32_t new_size = port_idx + 1;
322 if (new_size > MAX_PORTS) {
323 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
324 dpif_name(&dpif->dpif), port_no);
328 dpif->channels = xrealloc(dpif->channels,
329 new_size * sizeof *dpif->channels);
330 for (i = dpif->uc_array_size; i < new_size; i++) {
331 dpif->channels[i].sock = NULL;
334 dpif->epoll_events = xrealloc(dpif->epoll_events,
335 new_size * sizeof *dpif->epoll_events);
336 dpif->uc_array_size = new_size;
339 memset(&event, 0, sizeof event);
340 event.events = EPOLLIN;
341 event.data.u32 = port_idx;
342 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
347 nl_sock_destroy(dpif->channels[port_idx].sock);
348 dpif->channels[port_idx].sock = sock;
349 dpif->channels[port_idx].last_poll = LLONG_MIN;
355 del_channel(struct dpif_linux *dpif, odp_port_t port_no)
357 struct dpif_channel *ch;
358 uint32_t port_idx = odp_to_u32(port_no);
360 if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
364 ch = &dpif->channels[port_idx];
369 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
370 dpif->event_offset = dpif->n_events = 0;
372 nl_sock_destroy(ch->sock);
377 dpif_linux_close(struct dpif *dpif_)
379 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
381 nl_sock_destroy(dpif->port_notifier);
382 destroy_channels(dpif);
383 if (dpif->epoll_fd >= 0) {
384 close(dpif->epoll_fd);
386 ovs_mutex_destroy(&dpif->upcall_lock);
391 dpif_linux_destroy(struct dpif *dpif_)
393 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
394 struct dpif_linux_dp dp;
396 dpif_linux_dp_init(&dp);
397 dp.cmd = OVS_DP_CMD_DEL;
398 dp.dp_ifindex = dpif->dp_ifindex;
399 return dpif_linux_dp_transact(&dp, NULL, NULL);
403 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
405 struct dpif_linux_dp dp;
409 error = dpif_linux_dp_get(dpif_, &dp, &buf);
411 stats->n_hit = dp.stats.n_hit;
412 stats->n_missed = dp.stats.n_missed;
413 stats->n_lost = dp.stats.n_lost;
414 stats->n_flows = dp.stats.n_flows;
421 get_vport_type(const struct dpif_linux_vport *vport)
423 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
425 switch (vport->type) {
426 case OVS_VPORT_TYPE_NETDEV:
429 case OVS_VPORT_TYPE_INTERNAL:
432 case OVS_VPORT_TYPE_GRE:
435 case OVS_VPORT_TYPE_GRE64:
438 case OVS_VPORT_TYPE_VXLAN:
441 case OVS_VPORT_TYPE_LISP:
444 case OVS_VPORT_TYPE_UNSPEC:
445 case __OVS_VPORT_TYPE_MAX:
449 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
450 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
454 static enum ovs_vport_type
455 netdev_to_ovs_vport_type(const struct netdev *netdev)
457 const char *type = netdev_get_type(netdev);
459 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
460 return OVS_VPORT_TYPE_NETDEV;
461 } else if (!strcmp(type, "internal")) {
462 return OVS_VPORT_TYPE_INTERNAL;
463 } else if (strstr(type, "gre64")) {
464 return OVS_VPORT_TYPE_GRE64;
465 } else if (strstr(type, "gre")) {
466 return OVS_VPORT_TYPE_GRE;
467 } else if (!strcmp(type, "vxlan")) {
468 return OVS_VPORT_TYPE_VXLAN;
469 } else if (!strcmp(type, "lisp")) {
470 return OVS_VPORT_TYPE_LISP;
472 return OVS_VPORT_TYPE_UNSPEC;
477 dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
478 odp_port_t *port_nop)
480 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
481 const struct netdev_tunnel_config *tnl_cfg;
482 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
483 const char *name = netdev_vport_get_dpif_port(netdev,
484 namebuf, sizeof namebuf);
485 const char *type = netdev_get_type(netdev);
486 struct dpif_linux_vport request, reply;
487 struct nl_sock *sock = NULL;
490 uint64_t options_stub[64 / 8];
491 struct ofpbuf options;
494 if (dpif->epoll_fd >= 0) {
495 error = nl_sock_create(NETLINK_GENERIC, &sock);
501 dpif_linux_vport_init(&request);
502 request.cmd = OVS_VPORT_CMD_NEW;
503 request.dp_ifindex = dpif->dp_ifindex;
504 request.type = netdev_to_ovs_vport_type(netdev);
505 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
506 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
507 "unsupported type `%s'",
508 dpif_name(dpif_), name, type);
509 nl_sock_destroy(sock);
514 if (request.type == OVS_VPORT_TYPE_NETDEV) {
515 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
518 tnl_cfg = netdev_get_tunnel_config(netdev);
519 if (tnl_cfg && tnl_cfg->dst_port != 0) {
520 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
521 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
522 ntohs(tnl_cfg->dst_port));
523 request.options = options.data;
524 request.options_len = options.size;
527 request.port_no = *port_nop;
528 upcall_pid = sock ? nl_sock_pid(sock) : 0;
529 request.upcall_pid = &upcall_pid;
531 error = dpif_linux_vport_transact(&request, &reply, &buf);
533 *port_nop = reply.port_no;
534 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
535 dpif_name(dpif_), reply.port_no, upcall_pid);
537 if (error == EBUSY && *port_nop != ODPP_NONE) {
538 VLOG_INFO("%s: requested port %"PRIu32" is in use",
539 dpif_name(dpif_), *port_nop);
541 nl_sock_destroy(sock);
548 error = add_channel(dpif, *port_nop, sock);
550 VLOG_INFO("%s: could not add channel for port %s",
551 dpif_name(dpif_), name);
553 /* Delete the port. */
554 dpif_linux_vport_init(&request);
555 request.cmd = OVS_VPORT_CMD_DEL;
556 request.dp_ifindex = dpif->dp_ifindex;
557 request.port_no = *port_nop;
558 dpif_linux_vport_transact(&request, NULL, NULL);
560 nl_sock_destroy(sock);
569 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
570 odp_port_t *port_nop)
572 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
575 ovs_mutex_lock(&dpif->upcall_lock);
576 error = dpif_linux_port_add__(dpif_, netdev, port_nop);
577 ovs_mutex_unlock(&dpif->upcall_lock);
583 dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
585 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
586 struct dpif_linux_vport vport;
589 dpif_linux_vport_init(&vport);
590 vport.cmd = OVS_VPORT_CMD_DEL;
591 vport.dp_ifindex = dpif->dp_ifindex;
592 vport.port_no = port_no;
593 error = dpif_linux_vport_transact(&vport, NULL, NULL);
595 del_channel(dpif, port_no);
601 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
603 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
606 ovs_mutex_lock(&dpif->upcall_lock);
607 error = dpif_linux_port_del__(dpif_, port_no);
608 ovs_mutex_unlock(&dpif->upcall_lock);
614 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
615 const char *port_name, struct dpif_port *dpif_port)
617 struct dpif_linux_vport request;
618 struct dpif_linux_vport reply;
622 dpif_linux_vport_init(&request);
623 request.cmd = OVS_VPORT_CMD_GET;
624 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
625 request.port_no = port_no;
626 request.name = port_name;
628 error = dpif_linux_vport_transact(&request, &reply, &buf);
630 if (reply.dp_ifindex != request.dp_ifindex) {
631 /* A query by name reported that 'port_name' is in some datapath
632 * other than 'dpif', but the caller wants to know about 'dpif'. */
634 } else if (dpif_port) {
635 dpif_port->name = xstrdup(reply.name);
636 dpif_port->type = xstrdup(get_vport_type(&reply));
637 dpif_port->port_no = reply.port_no;
645 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
646 struct dpif_port *dpif_port)
648 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
652 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
653 struct dpif_port *dpif_port)
655 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
659 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
661 return u32_to_odp(MAX_PORTS);
665 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
667 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
668 uint32_t port_idx = odp_to_u32(port_no);
671 ovs_mutex_lock(&dpif->upcall_lock);
672 if (dpif->epoll_fd >= 0) {
673 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
674 * channel, since it is not heavily loaded. */
675 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
676 pid = nl_sock_pid(dpif->channels[idx].sock);
678 ovs_mutex_unlock(&dpif->upcall_lock);
684 dpif_linux_flow_flush(struct dpif *dpif_)
686 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
687 struct dpif_linux_flow flow;
689 dpif_linux_flow_init(&flow);
690 flow.cmd = OVS_FLOW_CMD_DEL;
691 flow.dp_ifindex = dpif->dp_ifindex;
692 return dpif_linux_flow_transact(&flow, NULL, NULL);
695 struct dpif_linux_port_state {
700 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
702 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
703 struct dpif_linux_port_state *state;
704 struct dpif_linux_vport request;
707 *statep = state = xmalloc(sizeof *state);
709 dpif_linux_vport_init(&request);
710 request.cmd = OVS_DP_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(&state->dump, NETLINK_GENERIC, buf);
722 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
723 struct dpif_port *dpif_port)
725 struct dpif_linux_port_state *state = state_;
726 struct dpif_linux_vport vport;
730 if (!nl_dump_next(&state->dump, &buf)) {
734 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
739 dpif_port->name = CONST_CAST(char *, vport.name);
740 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
741 dpif_port->port_no = vport.port_no;
746 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
748 struct dpif_linux_port_state *state = state_;
749 int error = nl_dump_done(&state->dump);
756 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
758 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
760 /* Lazily create the Netlink socket to listen for notifications. */
761 if (!dpif->port_notifier) {
762 struct nl_sock *sock;
765 error = nl_sock_create(NETLINK_GENERIC, &sock);
770 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
772 nl_sock_destroy(sock);
775 dpif->port_notifier = sock;
777 /* We have no idea of the current state so report that everything
783 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
784 uint64_t buf_stub[4096 / 8];
788 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
789 error = nl_sock_recv(dpif->port_notifier, &buf, false);
791 struct dpif_linux_vport vport;
793 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
795 if (vport.dp_ifindex == dpif->dp_ifindex
796 && (vport.cmd == OVS_VPORT_CMD_NEW
797 || vport.cmd == OVS_VPORT_CMD_DEL
798 || vport.cmd == OVS_VPORT_CMD_SET)) {
799 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
800 dpif->dpif.full_name, vport.name, vport.cmd);
801 *devnamep = xstrdup(vport.name);
807 } else if (error == EAGAIN) {
811 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
812 ovs_strerror(error));
813 nl_sock_drain(dpif->port_notifier);
819 dpif_linux_port_poll_wait(const struct dpif *dpif_)
821 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
823 if (dpif->port_notifier) {
824 nl_sock_wait(dpif->port_notifier, POLLIN);
826 poll_immediate_wake();
831 dpif_linux_flow_get__(const struct dpif *dpif_,
832 const struct nlattr *key, size_t key_len,
833 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
835 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
836 struct dpif_linux_flow request;
838 dpif_linux_flow_init(&request);
839 request.cmd = OVS_FLOW_CMD_GET;
840 request.dp_ifindex = dpif->dp_ifindex;
842 request.key_len = key_len;
843 return dpif_linux_flow_transact(&request, reply, bufp);
847 dpif_linux_flow_get(const struct dpif *dpif_,
848 const struct nlattr *key, size_t key_len,
849 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
851 struct dpif_linux_flow reply;
855 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
858 dpif_linux_flow_get_stats(&reply, stats);
861 buf->data = CONST_CAST(struct nlattr *, reply.actions);
862 buf->size = reply.actions_len;
872 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
873 struct dpif_linux_flow *request)
875 static const struct nlattr dummy_action;
877 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
879 dpif_linux_flow_init(request);
880 request->cmd = (put->flags & DPIF_FP_CREATE
881 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
882 request->dp_ifindex = dpif->dp_ifindex;
883 request->key = put->key;
884 request->key_len = put->key_len;
885 request->mask = put->mask;
886 request->mask_len = put->mask_len;
887 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
888 request->actions = (put->actions
890 : CONST_CAST(struct nlattr *, &dummy_action));
891 request->actions_len = put->actions_len;
892 if (put->flags & DPIF_FP_ZERO_STATS) {
893 request->clear = true;
895 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
899 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
901 struct dpif_linux_flow request, reply;
905 dpif_linux_init_flow_put(dpif_, put, &request);
906 error = dpif_linux_flow_transact(&request,
907 put->stats ? &reply : NULL,
908 put->stats ? &buf : NULL);
909 if (!error && put->stats) {
910 dpif_linux_flow_get_stats(&reply, put->stats);
917 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
918 struct dpif_linux_flow *request)
920 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
922 dpif_linux_flow_init(request);
923 request->cmd = OVS_FLOW_CMD_DEL;
924 request->dp_ifindex = dpif->dp_ifindex;
925 request->key = del->key;
926 request->key_len = del->key_len;
930 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
932 struct dpif_linux_flow request, reply;
936 dpif_linux_init_flow_del(dpif_, del, &request);
937 error = dpif_linux_flow_transact(&request,
938 del->stats ? &reply : NULL,
939 del->stats ? &buf : NULL);
940 if (!error && del->stats) {
941 dpif_linux_flow_get_stats(&reply, del->stats);
947 struct dpif_linux_flow_state {
949 struct dpif_linux_flow flow;
950 struct dpif_flow_stats stats;
955 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
957 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
958 struct dpif_linux_flow_state *state;
959 struct dpif_linux_flow request;
962 *statep = state = xmalloc(sizeof *state);
964 dpif_linux_flow_init(&request);
965 request.cmd = OVS_DP_CMD_GET;
966 request.dp_ifindex = dpif->dp_ifindex;
968 buf = ofpbuf_new(1024);
969 dpif_linux_flow_to_ofpbuf(&request, buf);
970 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
979 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
980 const struct nlattr **key, size_t *key_len,
981 const struct nlattr **mask, size_t *mask_len,
982 const struct nlattr **actions, size_t *actions_len,
983 const struct dpif_flow_stats **stats)
985 struct dpif_linux_flow_state *state = state_;
990 ofpbuf_delete(state->buf);
993 if (!nl_dump_next(&state->dump, &buf)) {
997 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1002 if (actions && !state->flow.actions) {
1003 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1004 state->flow.key_len,
1005 &state->flow, &state->buf);
1006 if (error == ENOENT) {
1007 VLOG_DBG("dumped flow disappeared on get");
1009 VLOG_WARN("error fetching dumped flow: %s",
1010 ovs_strerror(error));
1016 *actions = state->flow.actions;
1017 *actions_len = state->flow.actions_len;
1020 *key = state->flow.key;
1021 *key_len = state->flow.key_len;
1024 *mask = state->flow.mask;
1025 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1028 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1029 *stats = &state->stats;
1035 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1037 struct dpif_linux_flow_state *state = state_;
1038 int error = nl_dump_done(&state->dump);
1039 ofpbuf_delete(state->buf);
1045 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1048 struct ovs_header *k_exec;
1050 ofpbuf_prealloc_tailroom(buf, (64
1051 + d_exec->packet->size
1053 + d_exec->actions_len));
1055 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1056 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1058 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1059 k_exec->dp_ifindex = dp_ifindex;
1061 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1062 d_exec->packet->data, d_exec->packet->size);
1063 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
1064 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1065 d_exec->actions, d_exec->actions_len);
1069 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1071 uint64_t request_stub[1024 / 8];
1072 struct ofpbuf request;
1075 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1076 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1077 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1078 ofpbuf_uninit(&request);
1084 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1086 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1088 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1094 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1096 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1099 struct nl_transaction txn;
1101 struct ofpbuf request;
1102 uint64_t request_stub[1024 / 8];
1104 struct ofpbuf reply;
1105 uint64_t reply_stub[1024 / 8];
1108 struct nl_transaction *txnsp[MAX_OPS];
1111 ovs_assert(n_ops <= MAX_OPS);
1112 for (i = 0; i < n_ops; i++) {
1113 struct op_auxdata *aux = &auxes[i];
1114 struct dpif_op *op = ops[i];
1115 struct dpif_flow_put *put;
1116 struct dpif_flow_del *del;
1117 struct dpif_execute *execute;
1118 struct dpif_linux_flow flow;
1120 ofpbuf_use_stub(&aux->request,
1121 aux->request_stub, sizeof aux->request_stub);
1122 aux->txn.request = &aux->request;
1124 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1125 aux->txn.reply = NULL;
1128 case DPIF_OP_FLOW_PUT:
1129 put = &op->u.flow_put;
1130 dpif_linux_init_flow_put(dpif_, put, &flow);
1132 flow.nlmsg_flags |= NLM_F_ECHO;
1133 aux->txn.reply = &aux->reply;
1135 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1138 case DPIF_OP_FLOW_DEL:
1139 del = &op->u.flow_del;
1140 dpif_linux_init_flow_del(dpif_, del, &flow);
1142 flow.nlmsg_flags |= NLM_F_ECHO;
1143 aux->txn.reply = &aux->reply;
1145 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1148 case DPIF_OP_EXECUTE:
1149 execute = &op->u.execute;
1150 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1159 for (i = 0; i < n_ops; i++) {
1160 txnsp[i] = &auxes[i].txn;
1162 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1164 for (i = 0; i < n_ops; i++) {
1165 struct op_auxdata *aux = &auxes[i];
1166 struct nl_transaction *txn = &auxes[i].txn;
1167 struct dpif_op *op = ops[i];
1168 struct dpif_flow_put *put;
1169 struct dpif_flow_del *del;
1171 op->error = txn->error;
1174 case DPIF_OP_FLOW_PUT:
1175 put = &op->u.flow_put;
1178 struct dpif_linux_flow reply;
1180 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1183 dpif_linux_flow_get_stats(&reply, put->stats);
1188 memset(put->stats, 0, sizeof *put->stats);
1193 case DPIF_OP_FLOW_DEL:
1194 del = &op->u.flow_del;
1197 struct dpif_linux_flow reply;
1199 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1202 dpif_linux_flow_get_stats(&reply, del->stats);
1207 memset(del->stats, 0, sizeof *del->stats);
1212 case DPIF_OP_EXECUTE:
1219 ofpbuf_uninit(&aux->request);
1220 ofpbuf_uninit(&aux->reply);
1225 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1228 size_t chunk = MIN(n_ops, MAX_OPS);
1229 dpif_linux_operate__(dpif, ops, chunk);
1236 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1238 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1240 if ((dpif->epoll_fd >= 0) == enable) {
1245 destroy_channels(dpif);
1247 struct dpif_port_dump port_dump;
1248 struct dpif_port port;
1250 if (dpif->epoll_fd < 0) {
1251 dpif->epoll_fd = epoll_create(10);
1252 if (dpif->epoll_fd < 0) {
1257 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1258 struct dpif_linux_vport vport_request;
1259 struct nl_sock *sock;
1260 uint32_t upcall_pid;
1263 error = nl_sock_create(NETLINK_GENERIC, &sock);
1268 upcall_pid = nl_sock_pid(sock);
1270 dpif_linux_vport_init(&vport_request);
1271 vport_request.cmd = OVS_VPORT_CMD_SET;
1272 vport_request.dp_ifindex = dpif->dp_ifindex;
1273 vport_request.port_no = port.port_no;
1274 vport_request.upcall_pid = &upcall_pid;
1275 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1277 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1278 dpif_name(&dpif->dpif), vport_request.port_no,
1281 VLOG_WARN_RL(&error_rl,
1282 "%s: failed to set upcall pid on port: %s",
1283 dpif_name(&dpif->dpif), ovs_strerror(error));
1284 nl_sock_destroy(sock);
1286 if (error == ENODEV || error == ENOENT) {
1287 /* This device isn't there, but keep trying the others. */
1294 error = add_channel(dpif, port.port_no, sock);
1296 VLOG_INFO("%s: could not add channel for port %s",
1297 dpif_name(dpif_), port.name);
1298 nl_sock_destroy(sock);
1308 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1310 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1313 ovs_mutex_lock(&dpif->upcall_lock);
1314 error = dpif_linux_recv_set__(dpif_, enable);
1315 ovs_mutex_unlock(&dpif->upcall_lock);
1321 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1322 uint32_t queue_id, uint32_t *priority)
1324 if (queue_id < 0xf000) {
1325 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1333 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1336 static const struct nl_policy ovs_packet_policy[] = {
1337 /* Always present. */
1338 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1339 .min_len = ETH_HEADER_LEN },
1340 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1342 /* OVS_PACKET_CMD_ACTION only. */
1343 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1346 struct ovs_header *ovs_header;
1347 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1348 struct nlmsghdr *nlmsg;
1349 struct genlmsghdr *genl;
1353 ofpbuf_use_const(&b, buf->data, buf->size);
1355 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1356 genl = ofpbuf_try_pull(&b, sizeof *genl);
1357 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1358 if (!nlmsg || !genl || !ovs_header
1359 || nlmsg->nlmsg_type != ovs_packet_family
1360 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1361 ARRAY_SIZE(ovs_packet_policy))) {
1365 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1366 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1372 memset(upcall, 0, sizeof *upcall);
1373 upcall->type = type;
1374 upcall->packet = buf;
1375 upcall->packet->data = CONST_CAST(struct nlattr *,
1376 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1377 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1378 upcall->key = CONST_CAST(struct nlattr *,
1379 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1380 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1381 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1382 *dp_ifindex = ovs_header->dp_ifindex;
1388 dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
1391 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1394 if (dpif->epoll_fd < 0) {
1398 if (dpif->event_offset >= dpif->n_events) {
1401 dpif->event_offset = dpif->n_events = 0;
1404 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1405 dpif->uc_array_size, 0);
1406 } while (retval < 0 && errno == EINTR);
1408 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1409 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1410 } else if (retval > 0) {
1411 dpif->n_events = retval;
1415 while (dpif->event_offset < dpif->n_events) {
1416 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1417 struct dpif_channel *ch = &dpif->channels[idx];
1419 dpif->event_offset++;
1425 if (++read_tries > 50) {
1429 error = nl_sock_recv(ch->sock, buf, false);
1430 if (error == ENOBUFS) {
1431 /* ENOBUFS typically means that we've received so many
1432 * packets that the buffer overflowed. Try again
1433 * immediately because there's almost certainly a packet
1434 * waiting for us. */
1435 report_loss(dpif_, ch);
1439 ch->last_poll = time_msec();
1441 if (error == EAGAIN) {
1447 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1448 if (!error && dp_ifindex == dpif->dp_ifindex) {
1460 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1463 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1466 ovs_mutex_lock(&dpif->upcall_lock);
1467 error = dpif_linux_recv__(dpif_, upcall, buf);
1468 ovs_mutex_unlock(&dpif->upcall_lock);
1474 dpif_linux_recv_wait(struct dpif *dpif_)
1476 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1478 ovs_mutex_lock(&dpif->upcall_lock);
1479 if (dpif->epoll_fd >= 0) {
1480 poll_fd_wait(dpif->epoll_fd, POLLIN);
1482 ovs_mutex_unlock(&dpif->upcall_lock);
1486 dpif_linux_recv_purge(struct dpif *dpif_)
1488 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1490 ovs_mutex_lock(&dpif->upcall_lock);
1491 if (dpif->epoll_fd >= 0) {
1492 struct dpif_channel *ch;
1494 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
1497 nl_sock_drain(ch->sock);
1501 ovs_mutex_unlock(&dpif->upcall_lock);
1504 const struct dpif_class dpif_linux_class = {
1506 dpif_linux_enumerate,
1513 dpif_linux_get_stats,
1514 dpif_linux_port_add,
1515 dpif_linux_port_del,
1516 dpif_linux_port_query_by_number,
1517 dpif_linux_port_query_by_name,
1518 dpif_linux_get_max_ports,
1519 dpif_linux_port_get_pid,
1520 dpif_linux_port_dump_start,
1521 dpif_linux_port_dump_next,
1522 dpif_linux_port_dump_done,
1523 dpif_linux_port_poll,
1524 dpif_linux_port_poll_wait,
1525 dpif_linux_flow_get,
1526 dpif_linux_flow_put,
1527 dpif_linux_flow_del,
1528 dpif_linux_flow_flush,
1529 dpif_linux_flow_dump_start,
1530 dpif_linux_flow_dump_next,
1531 dpif_linux_flow_dump_done,
1534 dpif_linux_recv_set,
1535 dpif_linux_queue_to_priority,
1537 dpif_linux_recv_wait,
1538 dpif_linux_recv_purge,
1542 dpif_linux_init(void)
1544 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1547 if (ovsthread_once_start(&once)) {
1548 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1549 &ovs_datapath_family);
1551 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1552 "The Open vSwitch kernel module is probably not loaded.",
1553 OVS_DATAPATH_FAMILY);
1556 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1559 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1562 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1563 &ovs_packet_family);
1566 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1568 OVS_VPORT_MCGROUP_FALLBACK_ID);
1571 ovsthread_once_done(&once);
1578 dpif_linux_is_internal_device(const char *name)
1580 struct dpif_linux_vport reply;
1584 error = dpif_linux_vport_get(name, &reply, &buf);
1587 } else if (error != ENODEV && error != ENOENT) {
1588 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1589 name, ovs_strerror(error));
1592 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1595 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1596 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1597 * positive errno value.
1599 * 'vport' will contain pointers into 'buf', so the caller should not free
1600 * 'buf' while 'vport' is still in use. */
1602 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1603 const struct ofpbuf *buf)
1605 static const struct nl_policy ovs_vport_policy[] = {
1606 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1607 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1608 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1609 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1610 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1612 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1615 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1616 struct ovs_header *ovs_header;
1617 struct nlmsghdr *nlmsg;
1618 struct genlmsghdr *genl;
1621 dpif_linux_vport_init(vport);
1623 ofpbuf_use_const(&b, buf->data, buf->size);
1624 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1625 genl = ofpbuf_try_pull(&b, sizeof *genl);
1626 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1627 if (!nlmsg || !genl || !ovs_header
1628 || nlmsg->nlmsg_type != ovs_vport_family
1629 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1630 ARRAY_SIZE(ovs_vport_policy))) {
1634 vport->cmd = genl->cmd;
1635 vport->dp_ifindex = ovs_header->dp_ifindex;
1636 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1637 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1638 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1639 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1640 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1642 if (a[OVS_VPORT_ATTR_STATS]) {
1643 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1645 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1646 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1647 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1652 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1653 * followed by Netlink attributes corresponding to 'vport'. */
1655 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1658 struct ovs_header *ovs_header;
1660 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1661 vport->cmd, OVS_VPORT_VERSION);
1663 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1664 ovs_header->dp_ifindex = vport->dp_ifindex;
1666 if (vport->port_no != ODPP_NONE) {
1667 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1670 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1671 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1675 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1678 if (vport->upcall_pid) {
1679 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1683 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1684 vport->stats, sizeof *vport->stats);
1687 if (vport->options) {
1688 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1689 vport->options, vport->options_len);
1693 /* Clears 'vport' to "empty" values. */
1695 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1697 memset(vport, 0, sizeof *vport);
1698 vport->port_no = ODPP_NONE;
1701 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1702 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1703 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1704 * result of the command is expected to be an ovs_vport also, which is decoded
1705 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1706 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1708 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1709 struct dpif_linux_vport *reply,
1710 struct ofpbuf **bufp)
1712 struct ofpbuf *request_buf;
1715 ovs_assert((reply != NULL) == (bufp != NULL));
1717 error = dpif_linux_init();
1721 dpif_linux_vport_init(reply);
1726 request_buf = ofpbuf_new(1024);
1727 dpif_linux_vport_to_ofpbuf(request, request_buf);
1728 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1729 ofpbuf_delete(request_buf);
1733 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1736 dpif_linux_vport_init(reply);
1737 ofpbuf_delete(*bufp);
1744 /* Obtains information about the kernel vport named 'name' and stores it into
1745 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1746 * longer needed ('reply' will contain pointers into '*bufp'). */
1748 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1749 struct ofpbuf **bufp)
1751 struct dpif_linux_vport request;
1753 dpif_linux_vport_init(&request);
1754 request.cmd = OVS_VPORT_CMD_GET;
1755 request.name = name;
1757 return dpif_linux_vport_transact(&request, reply, bufp);
1760 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1761 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1762 * positive errno value.
1764 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1765 * while 'dp' is still in use. */
1767 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1769 static const struct nl_policy ovs_datapath_policy[] = {
1770 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1771 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1775 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1776 struct ovs_header *ovs_header;
1777 struct nlmsghdr *nlmsg;
1778 struct genlmsghdr *genl;
1781 dpif_linux_dp_init(dp);
1783 ofpbuf_use_const(&b, buf->data, buf->size);
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_datapath_family
1789 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1790 ARRAY_SIZE(ovs_datapath_policy))) {
1794 dp->cmd = genl->cmd;
1795 dp->dp_ifindex = ovs_header->dp_ifindex;
1796 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1797 if (a[OVS_DP_ATTR_STATS]) {
1798 /* Can't use structure assignment because Netlink doesn't ensure
1799 * sufficient alignment for 64-bit members. */
1800 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1807 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1809 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1811 struct ovs_header *ovs_header;
1813 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1814 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1815 OVS_DATAPATH_VERSION);
1817 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1818 ovs_header->dp_ifindex = dp->dp_ifindex;
1821 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1824 if (dp->upcall_pid) {
1825 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1828 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1831 /* Clears 'dp' to "empty" values. */
1833 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1835 memset(dp, 0, sizeof *dp);
1839 dpif_linux_dp_dump_start(struct nl_dump *dump)
1841 struct dpif_linux_dp request;
1844 dpif_linux_dp_init(&request);
1845 request.cmd = OVS_DP_CMD_GET;
1847 buf = ofpbuf_new(1024);
1848 dpif_linux_dp_to_ofpbuf(&request, buf);
1849 nl_dump_start(dump, NETLINK_GENERIC, buf);
1853 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1854 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1855 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1856 * result of the command is expected to be of the same form, which is decoded
1857 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1858 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1860 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1861 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1863 struct ofpbuf *request_buf;
1866 ovs_assert((reply != NULL) == (bufp != NULL));
1868 request_buf = ofpbuf_new(1024);
1869 dpif_linux_dp_to_ofpbuf(request, request_buf);
1870 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1871 ofpbuf_delete(request_buf);
1875 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1878 dpif_linux_dp_init(reply);
1879 ofpbuf_delete(*bufp);
1886 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1887 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1888 * will contain pointers into '*bufp'). */
1890 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1891 struct ofpbuf **bufp)
1893 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1894 struct dpif_linux_dp request;
1896 dpif_linux_dp_init(&request);
1897 request.cmd = OVS_DP_CMD_GET;
1898 request.dp_ifindex = dpif->dp_ifindex;
1900 return dpif_linux_dp_transact(&request, reply, bufp);
1903 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1904 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1905 * positive errno value.
1907 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1908 * while 'flow' is still in use. */
1910 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1911 const struct ofpbuf *buf)
1913 static const struct nl_policy ovs_flow_policy[] = {
1914 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1915 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
1916 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1917 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1919 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1920 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1921 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1924 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1925 struct ovs_header *ovs_header;
1926 struct nlmsghdr *nlmsg;
1927 struct genlmsghdr *genl;
1930 dpif_linux_flow_init(flow);
1932 ofpbuf_use_const(&b, buf->data, buf->size);
1933 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1934 genl = ofpbuf_try_pull(&b, sizeof *genl);
1935 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1936 if (!nlmsg || !genl || !ovs_header
1937 || nlmsg->nlmsg_type != ovs_flow_family
1938 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1939 ARRAY_SIZE(ovs_flow_policy))) {
1943 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1944 flow->dp_ifindex = ovs_header->dp_ifindex;
1945 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1946 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1948 if (a[OVS_FLOW_ATTR_MASK]) {
1949 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
1950 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
1952 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1953 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1954 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1956 if (a[OVS_FLOW_ATTR_STATS]) {
1957 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1959 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1960 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1962 if (a[OVS_FLOW_ATTR_USED]) {
1963 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1968 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1969 * followed by Netlink attributes corresponding to 'flow'. */
1971 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1974 struct ovs_header *ovs_header;
1976 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1977 NLM_F_REQUEST | flow->nlmsg_flags,
1978 flow->cmd, OVS_FLOW_VERSION);
1980 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1981 ovs_header->dp_ifindex = flow->dp_ifindex;
1983 if (flow->key_len) {
1984 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1987 if (flow->mask_len) {
1988 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
1991 if (flow->actions || flow->actions_len) {
1992 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1993 flow->actions, flow->actions_len);
1996 /* We never need to send these to the kernel. */
1997 ovs_assert(!flow->stats);
1998 ovs_assert(!flow->tcp_flags);
1999 ovs_assert(!flow->used);
2002 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2006 /* Clears 'flow' to "empty" values. */
2008 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2010 memset(flow, 0, sizeof *flow);
2013 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2014 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2015 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2016 * result of the command is expected to be a flow also, which is decoded and
2017 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2018 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2020 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2021 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2023 struct ofpbuf *request_buf;
2026 ovs_assert((reply != NULL) == (bufp != NULL));
2029 request->nlmsg_flags |= NLM_F_ECHO;
2032 request_buf = ofpbuf_new(1024);
2033 dpif_linux_flow_to_ofpbuf(request, request_buf);
2034 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2035 ofpbuf_delete(request_buf);
2039 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2042 dpif_linux_flow_init(reply);
2043 ofpbuf_delete(*bufp);
2051 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2052 struct dpif_flow_stats *stats)
2055 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2056 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2058 stats->n_packets = 0;
2061 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2062 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2065 /* Logs information about a packet that was recently lost in 'ch' (in
2068 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2070 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2071 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2074 if (VLOG_DROP_WARN(&rl)) {
2079 if (ch->last_poll != LLONG_MIN) {
2080 ds_put_format(&s, " (last polled %lld ms ago)",
2081 time_msec() - ch->last_poll);
2084 VLOG_WARN("%s: lost packet on channel %td%s",
2085 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));