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_UPCALL_PID. */
76 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
79 static void dpif_linux_dp_init(struct dpif_linux_dp *);
80 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
81 const struct ofpbuf *);
82 static void dpif_linux_dp_dump_start(struct nl_dump *);
83 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
84 struct dpif_linux_dp *reply,
85 struct ofpbuf **bufp);
86 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
87 struct ofpbuf **bufp);
89 struct dpif_linux_flow {
90 /* Generic Netlink header. */
93 /* struct ovs_header. */
94 unsigned int nlmsg_flags;
99 * The 'stats' member points to 64-bit data that might only be aligned on
100 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
103 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
104 * the Netlink version of the command, even if actions_len is zero. */
105 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
107 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
109 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
111 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
112 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
113 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
114 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
117 static void dpif_linux_flow_init(struct dpif_linux_flow *);
118 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
119 const struct ofpbuf *);
120 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
122 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
123 struct dpif_linux_flow *reply,
124 struct ofpbuf **bufp);
125 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
126 struct dpif_flow_stats *);
128 /* One of the dpif channels between the kernel and userspace. */
129 struct dpif_channel {
130 struct nl_sock *sock; /* Netlink socket. */
131 long long int last_poll; /* Last time this channel was polled. */
134 static void report_loss(struct dpif *, struct dpif_channel *);
136 /* Datapath interface for the openvswitch Linux kernel module. */
141 /* Upcall messages. */
142 struct ovs_mutex upcall_lock;
143 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
144 struct dpif_channel *channels;
145 struct epoll_event *epoll_events;
146 int epoll_fd; /* epoll fd that includes channel socks. */
147 int n_events; /* Num events returned by epoll_wait(). */
148 int event_offset; /* Offset into 'epoll_events'. */
150 /* Change notification. */
151 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
154 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
156 /* Generic Netlink family numbers for OVS.
158 * Initialized by dpif_linux_init(). */
159 static int ovs_datapath_family;
160 static int ovs_vport_family;
161 static int ovs_flow_family;
162 static int ovs_packet_family;
164 /* Generic Netlink multicast groups for OVS.
166 * Initialized by dpif_linux_init(). */
167 static unsigned int ovs_vport_mcgroup;
169 static int dpif_linux_init(void);
170 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
171 static uint32_t dpif_linux_port_get_pid(const struct dpif *,
174 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
176 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
177 const struct ofpbuf *);
179 static struct dpif_linux *
180 dpif_linux_cast(const struct dpif *dpif)
182 dpif_assert_class(dpif, &dpif_linux_class);
183 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
187 dpif_linux_enumerate(struct sset *all_dps)
193 error = dpif_linux_init();
198 dpif_linux_dp_dump_start(&dump);
199 while (nl_dump_next(&dump, &msg)) {
200 struct dpif_linux_dp dp;
202 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
203 sset_add(all_dps, dp.name);
206 return nl_dump_done(&dump);
210 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
211 bool create, struct dpif **dpifp)
213 struct dpif_linux_dp dp_request, dp;
218 error = dpif_linux_init();
223 /* Create or look up datapath. */
224 dpif_linux_dp_init(&dp_request);
226 dp_request.cmd = OVS_DP_CMD_NEW;
228 dp_request.upcall_pid = &upcall_pid;
230 dp_request.cmd = OVS_DP_CMD_GET;
232 dp_request.name = name;
233 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
238 error = open_dpif(&dp, dpifp);
244 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
246 struct dpif_linux *dpif;
248 dpif = xzalloc(sizeof *dpif);
249 dpif->port_notifier = NULL;
250 ovs_mutex_init(&dpif->upcall_lock);
253 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
254 dp->dp_ifindex, dp->dp_ifindex);
256 dpif->dp_ifindex = dp->dp_ifindex;
257 *dpifp = &dpif->dpif;
263 destroy_channels(struct dpif_linux *dpif)
267 if (dpif->epoll_fd < 0) {
271 for (i = 0; i < dpif->uc_array_size; i++ ) {
272 struct dpif_linux_vport vport_request;
273 struct dpif_channel *ch = &dpif->channels[i];
274 uint32_t upcall_pid = 0;
280 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
282 /* Turn off upcalls. */
283 dpif_linux_vport_init(&vport_request);
284 vport_request.cmd = OVS_VPORT_CMD_SET;
285 vport_request.dp_ifindex = dpif->dp_ifindex;
286 vport_request.port_no = u32_to_odp(i);
287 vport_request.upcall_pid = &upcall_pid;
288 dpif_linux_vport_transact(&vport_request, NULL, NULL);
290 nl_sock_destroy(ch->sock);
293 free(dpif->channels);
294 dpif->channels = NULL;
295 dpif->uc_array_size = 0;
297 free(dpif->epoll_events);
298 dpif->epoll_events = NULL;
299 dpif->n_events = dpif->event_offset = 0;
301 /* Don't close dpif->epoll_fd since that would cause other threads that
302 * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */
306 add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
308 struct epoll_event event;
309 uint32_t port_idx = odp_to_u32(port_no);
311 if (dpif->epoll_fd < 0) {
315 /* We assume that the datapath densely chooses port numbers, which
316 * can therefore be used as an index into an array of channels. */
317 if (port_idx >= dpif->uc_array_size) {
318 uint32_t new_size = port_idx + 1;
321 if (new_size > MAX_PORTS) {
322 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
323 dpif_name(&dpif->dpif), port_no);
327 dpif->channels = xrealloc(dpif->channels,
328 new_size * sizeof *dpif->channels);
329 for (i = dpif->uc_array_size; i < new_size; i++) {
330 dpif->channels[i].sock = NULL;
333 dpif->epoll_events = xrealloc(dpif->epoll_events,
334 new_size * sizeof *dpif->epoll_events);
335 dpif->uc_array_size = new_size;
338 memset(&event, 0, sizeof event);
339 event.events = EPOLLIN;
340 event.data.u32 = port_idx;
341 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
346 nl_sock_destroy(dpif->channels[port_idx].sock);
347 dpif->channels[port_idx].sock = sock;
348 dpif->channels[port_idx].last_poll = LLONG_MIN;
354 del_channel(struct dpif_linux *dpif, odp_port_t port_no)
356 struct dpif_channel *ch;
357 uint32_t port_idx = odp_to_u32(port_no);
359 if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
363 ch = &dpif->channels[port_idx];
368 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
369 dpif->event_offset = dpif->n_events = 0;
371 nl_sock_destroy(ch->sock);
376 dpif_linux_close(struct dpif *dpif_)
378 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
380 nl_sock_destroy(dpif->port_notifier);
381 destroy_channels(dpif);
382 if (dpif->epoll_fd >= 0) {
383 close(dpif->epoll_fd);
385 ovs_mutex_destroy(&dpif->upcall_lock);
390 dpif_linux_destroy(struct dpif *dpif_)
392 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
393 struct dpif_linux_dp dp;
395 dpif_linux_dp_init(&dp);
396 dp.cmd = OVS_DP_CMD_DEL;
397 dp.dp_ifindex = dpif->dp_ifindex;
398 return dpif_linux_dp_transact(&dp, NULL, NULL);
402 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
404 struct dpif_linux_dp dp;
408 error = dpif_linux_dp_get(dpif_, &dp, &buf);
410 stats->n_hit = dp.stats.n_hit;
411 stats->n_missed = dp.stats.n_missed;
412 stats->n_lost = dp.stats.n_lost;
413 stats->n_flows = dp.stats.n_flows;
420 get_vport_type(const struct dpif_linux_vport *vport)
422 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
424 switch (vport->type) {
425 case OVS_VPORT_TYPE_NETDEV:
428 case OVS_VPORT_TYPE_INTERNAL:
431 case OVS_VPORT_TYPE_GRE:
434 case OVS_VPORT_TYPE_GRE64:
437 case OVS_VPORT_TYPE_VXLAN:
440 case OVS_VPORT_TYPE_LISP:
443 case OVS_VPORT_TYPE_UNSPEC:
444 case __OVS_VPORT_TYPE_MAX:
448 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
449 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
453 static enum ovs_vport_type
454 netdev_to_ovs_vport_type(const struct netdev *netdev)
456 const char *type = netdev_get_type(netdev);
458 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
459 return OVS_VPORT_TYPE_NETDEV;
460 } else if (!strcmp(type, "internal")) {
461 return OVS_VPORT_TYPE_INTERNAL;
462 } else if (strstr(type, "gre64")) {
463 return OVS_VPORT_TYPE_GRE64;
464 } else if (strstr(type, "gre")) {
465 return OVS_VPORT_TYPE_GRE;
466 } else if (!strcmp(type, "vxlan")) {
467 return OVS_VPORT_TYPE_VXLAN;
468 } else if (!strcmp(type, "lisp")) {
469 return OVS_VPORT_TYPE_LISP;
471 return OVS_VPORT_TYPE_UNSPEC;
476 dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
477 odp_port_t *port_nop)
479 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
480 const struct netdev_tunnel_config *tnl_cfg;
481 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
482 const char *name = netdev_vport_get_dpif_port(netdev,
483 namebuf, sizeof namebuf);
484 const char *type = netdev_get_type(netdev);
485 struct dpif_linux_vport request, reply;
486 struct nl_sock *sock = NULL;
489 uint64_t options_stub[64 / 8];
490 struct ofpbuf options;
493 if (dpif->epoll_fd >= 0) {
494 error = nl_sock_create(NETLINK_GENERIC, &sock);
500 dpif_linux_vport_init(&request);
501 request.cmd = OVS_VPORT_CMD_NEW;
502 request.dp_ifindex = dpif->dp_ifindex;
503 request.type = netdev_to_ovs_vport_type(netdev);
504 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
505 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
506 "unsupported type `%s'",
507 dpif_name(dpif_), name, type);
508 nl_sock_destroy(sock);
513 if (request.type == OVS_VPORT_TYPE_NETDEV) {
514 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
517 tnl_cfg = netdev_get_tunnel_config(netdev);
518 if (tnl_cfg && tnl_cfg->dst_port != 0) {
519 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
520 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
521 ntohs(tnl_cfg->dst_port));
522 request.options = options.data;
523 request.options_len = options.size;
526 request.port_no = *port_nop;
527 upcall_pid = sock ? nl_sock_pid(sock) : 0;
528 request.upcall_pid = &upcall_pid;
530 error = dpif_linux_vport_transact(&request, &reply, &buf);
532 *port_nop = reply.port_no;
533 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
534 dpif_name(dpif_), reply.port_no, upcall_pid);
536 if (error == EBUSY && *port_nop != ODPP_NONE) {
537 VLOG_INFO("%s: requested port %"PRIu32" is in use",
538 dpif_name(dpif_), *port_nop);
540 nl_sock_destroy(sock);
547 error = add_channel(dpif, *port_nop, sock);
549 VLOG_INFO("%s: could not add channel for port %s",
550 dpif_name(dpif_), name);
552 /* Delete the port. */
553 dpif_linux_vport_init(&request);
554 request.cmd = OVS_VPORT_CMD_DEL;
555 request.dp_ifindex = dpif->dp_ifindex;
556 request.port_no = *port_nop;
557 dpif_linux_vport_transact(&request, NULL, NULL);
559 nl_sock_destroy(sock);
568 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
569 odp_port_t *port_nop)
571 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
574 ovs_mutex_lock(&dpif->upcall_lock);
575 error = dpif_linux_port_add__(dpif_, netdev, port_nop);
576 ovs_mutex_unlock(&dpif->upcall_lock);
582 dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
584 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
585 struct dpif_linux_vport vport;
588 dpif_linux_vport_init(&vport);
589 vport.cmd = OVS_VPORT_CMD_DEL;
590 vport.dp_ifindex = dpif->dp_ifindex;
591 vport.port_no = port_no;
592 error = dpif_linux_vport_transact(&vport, NULL, NULL);
594 del_channel(dpif, port_no);
600 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
602 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
605 ovs_mutex_lock(&dpif->upcall_lock);
606 error = dpif_linux_port_del__(dpif_, port_no);
607 ovs_mutex_unlock(&dpif->upcall_lock);
613 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
614 const char *port_name, struct dpif_port *dpif_port)
616 struct dpif_linux_vport request;
617 struct dpif_linux_vport reply;
621 dpif_linux_vport_init(&request);
622 request.cmd = OVS_VPORT_CMD_GET;
623 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
624 request.port_no = port_no;
625 request.name = port_name;
627 error = dpif_linux_vport_transact(&request, &reply, &buf);
629 if (reply.dp_ifindex != request.dp_ifindex) {
630 /* A query by name reported that 'port_name' is in some datapath
631 * other than 'dpif', but the caller wants to know about 'dpif'. */
633 } else if (dpif_port) {
634 dpif_port->name = xstrdup(reply.name);
635 dpif_port->type = xstrdup(get_vport_type(&reply));
636 dpif_port->port_no = reply.port_no;
644 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
645 struct dpif_port *dpif_port)
647 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
651 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
652 struct dpif_port *dpif_port)
654 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
658 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
664 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
666 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
667 uint32_t port_idx = odp_to_u32(port_no);
670 ovs_mutex_lock(&dpif->upcall_lock);
671 if (dpif->epoll_fd >= 0) {
672 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
673 * channel, since it is not heavily loaded. */
674 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
675 pid = nl_sock_pid(dpif->channels[idx].sock);
677 ovs_mutex_unlock(&dpif->upcall_lock);
683 dpif_linux_flow_flush(struct dpif *dpif_)
685 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
686 struct dpif_linux_flow flow;
688 dpif_linux_flow_init(&flow);
689 flow.cmd = OVS_FLOW_CMD_DEL;
690 flow.dp_ifindex = dpif->dp_ifindex;
691 return dpif_linux_flow_transact(&flow, NULL, NULL);
694 struct dpif_linux_port_state {
699 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
701 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
702 struct dpif_linux_port_state *state;
703 struct dpif_linux_vport request;
706 *statep = state = xmalloc(sizeof *state);
708 dpif_linux_vport_init(&request);
709 request.cmd = OVS_VPORT_CMD_GET;
710 request.dp_ifindex = dpif->dp_ifindex;
712 buf = ofpbuf_new(1024);
713 dpif_linux_vport_to_ofpbuf(&request, buf);
714 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
721 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
722 struct dpif_port *dpif_port)
724 struct dpif_linux_port_state *state = state_;
725 struct dpif_linux_vport vport;
729 if (!nl_dump_next(&state->dump, &buf)) {
733 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
738 dpif_port->name = CONST_CAST(char *, vport.name);
739 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
740 dpif_port->port_no = vport.port_no;
745 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
747 struct dpif_linux_port_state *state = state_;
748 int error = nl_dump_done(&state->dump);
755 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
757 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
759 /* Lazily create the Netlink socket to listen for notifications. */
760 if (!dpif->port_notifier) {
761 struct nl_sock *sock;
764 error = nl_sock_create(NETLINK_GENERIC, &sock);
769 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
771 nl_sock_destroy(sock);
774 dpif->port_notifier = sock;
776 /* We have no idea of the current state so report that everything
782 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
783 uint64_t buf_stub[4096 / 8];
787 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
788 error = nl_sock_recv(dpif->port_notifier, &buf, false);
790 struct dpif_linux_vport vport;
792 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
794 if (vport.dp_ifindex == dpif->dp_ifindex
795 && (vport.cmd == OVS_VPORT_CMD_NEW
796 || vport.cmd == OVS_VPORT_CMD_DEL
797 || vport.cmd == OVS_VPORT_CMD_SET)) {
798 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
799 dpif->dpif.full_name, vport.name, vport.cmd);
800 *devnamep = xstrdup(vport.name);
805 } else if (error != EAGAIN) {
806 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
807 ovs_strerror(error));
808 nl_sock_drain(dpif->port_notifier);
820 dpif_linux_port_poll_wait(const struct dpif *dpif_)
822 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
824 if (dpif->port_notifier) {
825 nl_sock_wait(dpif->port_notifier, POLLIN);
827 poll_immediate_wake();
832 dpif_linux_flow_get__(const struct dpif *dpif_,
833 const struct nlattr *key, size_t key_len,
834 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
836 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
837 struct dpif_linux_flow request;
839 dpif_linux_flow_init(&request);
840 request.cmd = OVS_FLOW_CMD_GET;
841 request.dp_ifindex = dpif->dp_ifindex;
843 request.key_len = key_len;
844 return dpif_linux_flow_transact(&request, reply, bufp);
848 dpif_linux_flow_get(const struct dpif *dpif_,
849 const struct nlattr *key, size_t key_len,
850 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
852 struct dpif_linux_flow reply;
856 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
859 dpif_linux_flow_get_stats(&reply, stats);
862 buf->data = CONST_CAST(struct nlattr *, reply.actions);
863 buf->size = reply.actions_len;
873 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
874 struct dpif_linux_flow *request)
876 static const struct nlattr dummy_action;
878 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
880 dpif_linux_flow_init(request);
881 request->cmd = (put->flags & DPIF_FP_CREATE
882 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
883 request->dp_ifindex = dpif->dp_ifindex;
884 request->key = put->key;
885 request->key_len = put->key_len;
886 request->mask = put->mask;
887 request->mask_len = put->mask_len;
888 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
889 request->actions = (put->actions
891 : CONST_CAST(struct nlattr *, &dummy_action));
892 request->actions_len = put->actions_len;
893 if (put->flags & DPIF_FP_ZERO_STATS) {
894 request->clear = true;
896 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
900 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
902 struct dpif_linux_flow request, reply;
906 dpif_linux_init_flow_put(dpif_, put, &request);
907 error = dpif_linux_flow_transact(&request,
908 put->stats ? &reply : NULL,
909 put->stats ? &buf : NULL);
910 if (!error && put->stats) {
911 dpif_linux_flow_get_stats(&reply, put->stats);
918 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
919 struct dpif_linux_flow *request)
921 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
923 dpif_linux_flow_init(request);
924 request->cmd = OVS_FLOW_CMD_DEL;
925 request->dp_ifindex = dpif->dp_ifindex;
926 request->key = del->key;
927 request->key_len = del->key_len;
931 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
933 struct dpif_linux_flow request, reply;
937 dpif_linux_init_flow_del(dpif_, del, &request);
938 error = dpif_linux_flow_transact(&request,
939 del->stats ? &reply : NULL,
940 del->stats ? &buf : NULL);
941 if (!error && del->stats) {
942 dpif_linux_flow_get_stats(&reply, del->stats);
948 struct dpif_linux_flow_state {
950 struct dpif_linux_flow flow;
951 struct dpif_flow_stats stats;
956 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
958 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
959 struct dpif_linux_flow_state *state;
960 struct dpif_linux_flow request;
963 *statep = state = xmalloc(sizeof *state);
965 dpif_linux_flow_init(&request);
966 request.cmd = OVS_FLOW_CMD_GET;
967 request.dp_ifindex = dpif->dp_ifindex;
969 buf = ofpbuf_new(1024);
970 dpif_linux_flow_to_ofpbuf(&request, buf);
971 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
980 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
981 const struct nlattr **key, size_t *key_len,
982 const struct nlattr **mask, size_t *mask_len,
983 const struct nlattr **actions, size_t *actions_len,
984 const struct dpif_flow_stats **stats)
986 struct dpif_linux_flow_state *state = state_;
991 ofpbuf_delete(state->buf);
994 if (!nl_dump_next(&state->dump, &buf)) {
998 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1003 if (actions && !state->flow.actions) {
1004 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1005 state->flow.key_len,
1006 &state->flow, &state->buf);
1007 if (error == ENOENT) {
1008 VLOG_DBG("dumped flow disappeared on get");
1010 VLOG_WARN("error fetching dumped flow: %s",
1011 ovs_strerror(error));
1017 *actions = state->flow.actions;
1018 *actions_len = state->flow.actions_len;
1021 *key = state->flow.key;
1022 *key_len = state->flow.key_len;
1025 *mask = state->flow.mask;
1026 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1029 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1030 *stats = &state->stats;
1036 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1038 struct dpif_linux_flow_state *state = state_;
1039 int error = nl_dump_done(&state->dump);
1040 ofpbuf_delete(state->buf);
1046 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1049 struct ovs_header *k_exec;
1051 ofpbuf_prealloc_tailroom(buf, (64
1052 + d_exec->packet->size
1054 + d_exec->actions_len));
1056 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1057 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1059 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1060 k_exec->dp_ifindex = dp_ifindex;
1062 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1063 d_exec->packet->data, d_exec->packet->size);
1064 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
1065 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1066 d_exec->actions, d_exec->actions_len);
1070 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1072 uint64_t request_stub[1024 / 8];
1073 struct ofpbuf request;
1076 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1077 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1078 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1079 ofpbuf_uninit(&request);
1085 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1087 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1089 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1095 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1097 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1100 struct nl_transaction txn;
1102 struct ofpbuf request;
1103 uint64_t request_stub[1024 / 8];
1105 struct ofpbuf reply;
1106 uint64_t reply_stub[1024 / 8];
1109 struct nl_transaction *txnsp[MAX_OPS];
1112 ovs_assert(n_ops <= MAX_OPS);
1113 for (i = 0; i < n_ops; i++) {
1114 struct op_auxdata *aux = &auxes[i];
1115 struct dpif_op *op = ops[i];
1116 struct dpif_flow_put *put;
1117 struct dpif_flow_del *del;
1118 struct dpif_execute *execute;
1119 struct dpif_linux_flow flow;
1121 ofpbuf_use_stub(&aux->request,
1122 aux->request_stub, sizeof aux->request_stub);
1123 aux->txn.request = &aux->request;
1125 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1126 aux->txn.reply = NULL;
1129 case DPIF_OP_FLOW_PUT:
1130 put = &op->u.flow_put;
1131 dpif_linux_init_flow_put(dpif_, put, &flow);
1133 flow.nlmsg_flags |= NLM_F_ECHO;
1134 aux->txn.reply = &aux->reply;
1136 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1139 case DPIF_OP_FLOW_DEL:
1140 del = &op->u.flow_del;
1141 dpif_linux_init_flow_del(dpif_, del, &flow);
1143 flow.nlmsg_flags |= NLM_F_ECHO;
1144 aux->txn.reply = &aux->reply;
1146 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1149 case DPIF_OP_EXECUTE:
1150 execute = &op->u.execute;
1151 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1160 for (i = 0; i < n_ops; i++) {
1161 txnsp[i] = &auxes[i].txn;
1163 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1165 for (i = 0; i < n_ops; i++) {
1166 struct op_auxdata *aux = &auxes[i];
1167 struct nl_transaction *txn = &auxes[i].txn;
1168 struct dpif_op *op = ops[i];
1169 struct dpif_flow_put *put;
1170 struct dpif_flow_del *del;
1172 op->error = txn->error;
1175 case DPIF_OP_FLOW_PUT:
1176 put = &op->u.flow_put;
1179 struct dpif_linux_flow reply;
1181 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1184 dpif_linux_flow_get_stats(&reply, put->stats);
1189 memset(put->stats, 0, sizeof *put->stats);
1194 case DPIF_OP_FLOW_DEL:
1195 del = &op->u.flow_del;
1198 struct dpif_linux_flow reply;
1200 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1203 dpif_linux_flow_get_stats(&reply, del->stats);
1208 memset(del->stats, 0, sizeof *del->stats);
1213 case DPIF_OP_EXECUTE:
1220 ofpbuf_uninit(&aux->request);
1221 ofpbuf_uninit(&aux->reply);
1226 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1229 size_t chunk = MIN(n_ops, MAX_OPS);
1230 dpif_linux_operate__(dpif, ops, chunk);
1237 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1239 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1241 if ((dpif->epoll_fd >= 0) == enable) {
1246 destroy_channels(dpif);
1248 struct dpif_port_dump port_dump;
1249 struct dpif_port port;
1251 if (dpif->epoll_fd < 0) {
1252 dpif->epoll_fd = epoll_create(10);
1253 if (dpif->epoll_fd < 0) {
1258 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1259 struct dpif_linux_vport vport_request;
1260 struct nl_sock *sock;
1261 uint32_t upcall_pid;
1264 error = nl_sock_create(NETLINK_GENERIC, &sock);
1269 upcall_pid = nl_sock_pid(sock);
1271 dpif_linux_vport_init(&vport_request);
1272 vport_request.cmd = OVS_VPORT_CMD_SET;
1273 vport_request.dp_ifindex = dpif->dp_ifindex;
1274 vport_request.port_no = port.port_no;
1275 vport_request.upcall_pid = &upcall_pid;
1276 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1278 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1279 dpif_name(&dpif->dpif), vport_request.port_no,
1282 VLOG_WARN_RL(&error_rl,
1283 "%s: failed to set upcall pid on port: %s",
1284 dpif_name(&dpif->dpif), ovs_strerror(error));
1285 nl_sock_destroy(sock);
1287 if (error == ENODEV || error == ENOENT) {
1288 /* This device isn't there, but keep trying the others. */
1295 error = add_channel(dpif, port.port_no, sock);
1297 VLOG_INFO("%s: could not add channel for port %s",
1298 dpif_name(dpif_), port.name);
1299 nl_sock_destroy(sock);
1309 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1311 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1314 ovs_mutex_lock(&dpif->upcall_lock);
1315 error = dpif_linux_recv_set__(dpif_, enable);
1316 ovs_mutex_unlock(&dpif->upcall_lock);
1322 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1323 uint32_t queue_id, uint32_t *priority)
1325 if (queue_id < 0xf000) {
1326 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1334 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1337 static const struct nl_policy ovs_packet_policy[] = {
1338 /* Always present. */
1339 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1340 .min_len = ETH_HEADER_LEN },
1341 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1343 /* OVS_PACKET_CMD_ACTION only. */
1344 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1347 struct ovs_header *ovs_header;
1348 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1349 struct nlmsghdr *nlmsg;
1350 struct genlmsghdr *genl;
1354 ofpbuf_use_const(&b, buf->data, buf->size);
1356 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1357 genl = ofpbuf_try_pull(&b, sizeof *genl);
1358 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1359 if (!nlmsg || !genl || !ovs_header
1360 || nlmsg->nlmsg_type != ovs_packet_family
1361 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1362 ARRAY_SIZE(ovs_packet_policy))) {
1366 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1367 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1373 memset(upcall, 0, sizeof *upcall);
1374 upcall->type = type;
1375 upcall->packet = buf;
1376 upcall->packet->data = CONST_CAST(struct nlattr *,
1377 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1378 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1379 upcall->key = CONST_CAST(struct nlattr *,
1380 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1381 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1382 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1383 *dp_ifindex = ovs_header->dp_ifindex;
1389 dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
1392 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1395 if (dpif->epoll_fd < 0) {
1399 if (dpif->event_offset >= dpif->n_events) {
1402 dpif->event_offset = dpif->n_events = 0;
1405 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1406 dpif->uc_array_size, 0);
1407 } while (retval < 0 && errno == EINTR);
1409 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1410 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1411 } else if (retval > 0) {
1412 dpif->n_events = retval;
1416 while (dpif->event_offset < dpif->n_events) {
1417 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1418 struct dpif_channel *ch = &dpif->channels[idx];
1420 dpif->event_offset++;
1426 if (++read_tries > 50) {
1430 error = nl_sock_recv(ch->sock, buf, false);
1431 if (error == ENOBUFS) {
1432 /* ENOBUFS typically means that we've received so many
1433 * packets that the buffer overflowed. Try again
1434 * immediately because there's almost certainly a packet
1435 * waiting for us. */
1436 report_loss(dpif_, ch);
1440 ch->last_poll = time_msec();
1442 if (error == EAGAIN) {
1448 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1449 if (!error && dp_ifindex == dpif->dp_ifindex) {
1461 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1464 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1467 ovs_mutex_lock(&dpif->upcall_lock);
1468 error = dpif_linux_recv__(dpif_, upcall, buf);
1469 ovs_mutex_unlock(&dpif->upcall_lock);
1475 dpif_linux_recv_wait(struct dpif *dpif_)
1477 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1479 ovs_mutex_lock(&dpif->upcall_lock);
1480 if (dpif->epoll_fd >= 0) {
1481 poll_fd_wait(dpif->epoll_fd, POLLIN);
1483 ovs_mutex_unlock(&dpif->upcall_lock);
1487 dpif_linux_recv_purge(struct dpif *dpif_)
1489 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1491 ovs_mutex_lock(&dpif->upcall_lock);
1492 if (dpif->epoll_fd >= 0) {
1493 struct dpif_channel *ch;
1495 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
1498 nl_sock_drain(ch->sock);
1502 ovs_mutex_unlock(&dpif->upcall_lock);
1505 const struct dpif_class dpif_linux_class = {
1507 dpif_linux_enumerate,
1514 dpif_linux_get_stats,
1515 dpif_linux_port_add,
1516 dpif_linux_port_del,
1517 dpif_linux_port_query_by_number,
1518 dpif_linux_port_query_by_name,
1519 dpif_linux_get_max_ports,
1520 dpif_linux_port_get_pid,
1521 dpif_linux_port_dump_start,
1522 dpif_linux_port_dump_next,
1523 dpif_linux_port_dump_done,
1524 dpif_linux_port_poll,
1525 dpif_linux_port_poll_wait,
1526 dpif_linux_flow_get,
1527 dpif_linux_flow_put,
1528 dpif_linux_flow_del,
1529 dpif_linux_flow_flush,
1530 dpif_linux_flow_dump_start,
1531 dpif_linux_flow_dump_next,
1532 dpif_linux_flow_dump_done,
1535 dpif_linux_recv_set,
1536 dpif_linux_queue_to_priority,
1538 dpif_linux_recv_wait,
1539 dpif_linux_recv_purge,
1543 dpif_linux_init(void)
1545 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1548 if (ovsthread_once_start(&once)) {
1549 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1550 &ovs_datapath_family);
1552 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1553 "The Open vSwitch kernel module is probably not loaded.",
1554 OVS_DATAPATH_FAMILY);
1557 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1560 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1563 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1564 &ovs_packet_family);
1567 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1568 &ovs_vport_mcgroup);
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));