2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "dpif-linux.h"
26 #include <linux/types.h>
27 #include <linux/pkt_sched.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/sockios.h>
33 #include <sys/epoll.h>
38 #include "dpif-provider.h"
39 #include "dynamic-string.h"
42 #include "netdev-linux.h"
43 #include "netdev-vport.h"
44 #include "netlink-notifier.h"
45 #include "netlink-socket.h"
50 #include "poll-loop.h"
55 #include "unaligned.h"
59 VLOG_DEFINE_THIS_MODULE(dpif_linux);
60 enum { MAX_PORTS = USHRT_MAX };
62 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
63 * missing if we have old headers. */
64 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
66 struct dpif_linux_dp {
67 /* Generic Netlink header. */
70 /* struct ovs_header. */
74 const char *name; /* OVS_DP_ATTR_NAME. */
75 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
76 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
77 struct ovs_dp_megaflow_stats megaflow_stats;
78 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
81 static void dpif_linux_dp_init(struct dpif_linux_dp *);
82 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
83 const struct ofpbuf *);
84 static void dpif_linux_dp_dump_start(struct nl_dump *);
85 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
86 struct dpif_linux_dp *reply,
87 struct ofpbuf **bufp);
88 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
89 struct ofpbuf **bufp);
91 struct dpif_linux_flow {
92 /* Generic Netlink header. */
95 /* struct ovs_header. */
96 unsigned int nlmsg_flags;
101 * The 'stats' member points to 64-bit data that might only be aligned on
102 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
105 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
106 * the Netlink version of the command, even if actions_len is zero. */
107 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
109 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
111 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
113 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
114 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
115 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
116 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
119 static void dpif_linux_flow_init(struct dpif_linux_flow *);
120 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
121 const struct ofpbuf *);
122 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
124 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
125 struct dpif_linux_flow *reply,
126 struct ofpbuf **bufp);
127 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
128 struct dpif_flow_stats *);
130 /* One of the dpif channels between the kernel and userspace. */
131 struct dpif_channel {
132 struct nl_sock *sock; /* Netlink socket. */
133 long long int last_poll; /* Last time this channel was polled. */
136 static void report_loss(struct dpif *, struct dpif_channel *);
138 /* Datapath interface for the openvswitch Linux kernel module. */
143 /* Upcall messages. */
144 struct ovs_mutex upcall_lock;
145 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
146 struct dpif_channel *channels;
147 struct epoll_event *epoll_events;
148 int epoll_fd; /* epoll fd that includes channel socks. */
149 int n_events; /* Num events returned by epoll_wait(). */
150 int event_offset; /* Offset into 'epoll_events'. */
152 /* Change notification. */
153 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
156 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
158 /* Generic Netlink family numbers for OVS.
160 * Initialized by dpif_linux_init(). */
161 static int ovs_datapath_family;
162 static int ovs_vport_family;
163 static int ovs_flow_family;
164 static int ovs_packet_family;
166 /* Generic Netlink multicast groups for OVS.
168 * Initialized by dpif_linux_init(). */
169 static unsigned int ovs_vport_mcgroup;
171 static int dpif_linux_init(void);
172 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
173 static uint32_t dpif_linux_port_get_pid(const struct dpif *,
176 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
178 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
179 const struct ofpbuf *);
181 static struct dpif_linux *
182 dpif_linux_cast(const struct dpif *dpif)
184 dpif_assert_class(dpif, &dpif_linux_class);
185 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
189 dpif_linux_enumerate(struct sset *all_dps)
195 error = dpif_linux_init();
200 dpif_linux_dp_dump_start(&dump);
201 while (nl_dump_next(&dump, &msg)) {
202 struct dpif_linux_dp dp;
204 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
205 sset_add(all_dps, dp.name);
208 return nl_dump_done(&dump);
212 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
213 bool create, struct dpif **dpifp)
215 struct dpif_linux_dp dp_request, dp;
220 error = dpif_linux_init();
225 /* Create or look up datapath. */
226 dpif_linux_dp_init(&dp_request);
228 dp_request.cmd = OVS_DP_CMD_NEW;
230 dp_request.upcall_pid = &upcall_pid;
232 dp_request.cmd = OVS_DP_CMD_GET;
234 dp_request.name = name;
235 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
240 error = open_dpif(&dp, dpifp);
246 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
248 struct dpif_linux *dpif;
250 dpif = xzalloc(sizeof *dpif);
251 dpif->port_notifier = NULL;
252 ovs_mutex_init(&dpif->upcall_lock);
255 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
256 dp->dp_ifindex, dp->dp_ifindex);
258 dpif->dp_ifindex = dp->dp_ifindex;
259 *dpifp = &dpif->dpif;
265 destroy_channels(struct dpif_linux *dpif)
269 if (dpif->epoll_fd < 0) {
273 for (i = 0; i < dpif->uc_array_size; i++ ) {
274 struct dpif_linux_vport vport_request;
275 struct dpif_channel *ch = &dpif->channels[i];
276 uint32_t upcall_pid = 0;
282 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
284 /* Turn off upcalls. */
285 dpif_linux_vport_init(&vport_request);
286 vport_request.cmd = OVS_VPORT_CMD_SET;
287 vport_request.dp_ifindex = dpif->dp_ifindex;
288 vport_request.port_no = u32_to_odp(i);
289 vport_request.upcall_pid = &upcall_pid;
290 dpif_linux_vport_transact(&vport_request, NULL, NULL);
292 nl_sock_destroy(ch->sock);
295 free(dpif->channels);
296 dpif->channels = NULL;
297 dpif->uc_array_size = 0;
299 free(dpif->epoll_events);
300 dpif->epoll_events = NULL;
301 dpif->n_events = dpif->event_offset = 0;
303 /* Don't close dpif->epoll_fd since that would cause other threads that
304 * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */
308 add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
310 struct epoll_event event;
311 uint32_t port_idx = odp_to_u32(port_no);
313 if (dpif->epoll_fd < 0) {
317 /* We assume that the datapath densely chooses port numbers, which
318 * can therefore be used as an index into an array of channels. */
319 if (port_idx >= dpif->uc_array_size) {
320 uint32_t new_size = port_idx + 1;
323 if (new_size > MAX_PORTS) {
324 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
325 dpif_name(&dpif->dpif), port_no);
329 dpif->channels = xrealloc(dpif->channels,
330 new_size * sizeof *dpif->channels);
331 for (i = dpif->uc_array_size; i < new_size; i++) {
332 dpif->channels[i].sock = NULL;
335 dpif->epoll_events = xrealloc(dpif->epoll_events,
336 new_size * sizeof *dpif->epoll_events);
337 dpif->uc_array_size = new_size;
340 memset(&event, 0, sizeof event);
341 event.events = EPOLLIN;
342 event.data.u32 = port_idx;
343 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
348 nl_sock_destroy(dpif->channels[port_idx].sock);
349 dpif->channels[port_idx].sock = sock;
350 dpif->channels[port_idx].last_poll = LLONG_MIN;
356 del_channel(struct dpif_linux *dpif, odp_port_t port_no)
358 struct dpif_channel *ch;
359 uint32_t port_idx = odp_to_u32(port_no);
361 if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
365 ch = &dpif->channels[port_idx];
370 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
371 dpif->event_offset = dpif->n_events = 0;
373 nl_sock_destroy(ch->sock);
378 dpif_linux_close(struct dpif *dpif_)
380 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
382 nl_sock_destroy(dpif->port_notifier);
383 destroy_channels(dpif);
384 if (dpif->epoll_fd >= 0) {
385 close(dpif->epoll_fd);
387 ovs_mutex_destroy(&dpif->upcall_lock);
392 dpif_linux_destroy(struct dpif *dpif_)
394 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
395 struct dpif_linux_dp dp;
397 dpif_linux_dp_init(&dp);
398 dp.cmd = OVS_DP_CMD_DEL;
399 dp.dp_ifindex = dpif->dp_ifindex;
400 return dpif_linux_dp_transact(&dp, NULL, NULL);
404 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
406 struct dpif_linux_dp dp;
410 error = dpif_linux_dp_get(dpif_, &dp, &buf);
412 stats->n_hit = dp.stats.n_hit;
413 stats->n_missed = dp.stats.n_missed;
414 stats->n_lost = dp.stats.n_lost;
415 stats->n_flows = dp.stats.n_flows;
416 stats->n_masks = dp.megaflow_stats.n_masks;
417 stats->n_mask_hit = dp.megaflow_stats.n_mask_hit;
424 get_vport_type(const struct dpif_linux_vport *vport)
426 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
428 switch (vport->type) {
429 case OVS_VPORT_TYPE_NETDEV:
432 case OVS_VPORT_TYPE_INTERNAL:
435 case OVS_VPORT_TYPE_GRE:
438 case OVS_VPORT_TYPE_GRE64:
441 case OVS_VPORT_TYPE_VXLAN:
444 case OVS_VPORT_TYPE_LISP:
447 case OVS_VPORT_TYPE_UNSPEC:
448 case __OVS_VPORT_TYPE_MAX:
452 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
453 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
457 static enum ovs_vport_type
458 netdev_to_ovs_vport_type(const struct netdev *netdev)
460 const char *type = netdev_get_type(netdev);
462 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
463 return OVS_VPORT_TYPE_NETDEV;
464 } else if (!strcmp(type, "internal")) {
465 return OVS_VPORT_TYPE_INTERNAL;
466 } else if (strstr(type, "gre64")) {
467 return OVS_VPORT_TYPE_GRE64;
468 } else if (strstr(type, "gre")) {
469 return OVS_VPORT_TYPE_GRE;
470 } else if (!strcmp(type, "vxlan")) {
471 return OVS_VPORT_TYPE_VXLAN;
472 } else if (!strcmp(type, "lisp")) {
473 return OVS_VPORT_TYPE_LISP;
475 return OVS_VPORT_TYPE_UNSPEC;
480 dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
481 odp_port_t *port_nop)
483 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
484 const struct netdev_tunnel_config *tnl_cfg;
485 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
486 const char *name = netdev_vport_get_dpif_port(netdev,
487 namebuf, sizeof namebuf);
488 const char *type = netdev_get_type(netdev);
489 struct dpif_linux_vport request, reply;
490 struct nl_sock *sock = NULL;
493 uint64_t options_stub[64 / 8];
494 struct ofpbuf options;
497 if (dpif->epoll_fd >= 0) {
498 error = nl_sock_create(NETLINK_GENERIC, &sock);
504 dpif_linux_vport_init(&request);
505 request.cmd = OVS_VPORT_CMD_NEW;
506 request.dp_ifindex = dpif->dp_ifindex;
507 request.type = netdev_to_ovs_vport_type(netdev);
508 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
509 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
510 "unsupported type `%s'",
511 dpif_name(dpif_), name, type);
512 nl_sock_destroy(sock);
517 if (request.type == OVS_VPORT_TYPE_NETDEV) {
518 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
521 tnl_cfg = netdev_get_tunnel_config(netdev);
522 if (tnl_cfg && tnl_cfg->dst_port != 0) {
523 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
524 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
525 ntohs(tnl_cfg->dst_port));
526 request.options = options.data;
527 request.options_len = options.size;
530 request.port_no = *port_nop;
531 upcall_pid = sock ? nl_sock_pid(sock) : 0;
532 request.upcall_pid = &upcall_pid;
534 error = dpif_linux_vport_transact(&request, &reply, &buf);
536 *port_nop = reply.port_no;
537 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
538 dpif_name(dpif_), reply.port_no, upcall_pid);
540 if (error == EBUSY && *port_nop != ODPP_NONE) {
541 VLOG_INFO("%s: requested port %"PRIu32" is in use",
542 dpif_name(dpif_), *port_nop);
544 nl_sock_destroy(sock);
551 error = add_channel(dpif, *port_nop, sock);
553 VLOG_INFO("%s: could not add channel for port %s",
554 dpif_name(dpif_), name);
556 /* Delete the port. */
557 dpif_linux_vport_init(&request);
558 request.cmd = OVS_VPORT_CMD_DEL;
559 request.dp_ifindex = dpif->dp_ifindex;
560 request.port_no = *port_nop;
561 dpif_linux_vport_transact(&request, NULL, NULL);
563 nl_sock_destroy(sock);
572 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
573 odp_port_t *port_nop)
575 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
578 ovs_mutex_lock(&dpif->upcall_lock);
579 error = dpif_linux_port_add__(dpif_, netdev, port_nop);
580 ovs_mutex_unlock(&dpif->upcall_lock);
586 dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
588 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
589 struct dpif_linux_vport vport;
592 dpif_linux_vport_init(&vport);
593 vport.cmd = OVS_VPORT_CMD_DEL;
594 vport.dp_ifindex = dpif->dp_ifindex;
595 vport.port_no = port_no;
596 error = dpif_linux_vport_transact(&vport, NULL, NULL);
598 del_channel(dpif, port_no);
604 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
606 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
609 ovs_mutex_lock(&dpif->upcall_lock);
610 error = dpif_linux_port_del__(dpif_, port_no);
611 ovs_mutex_unlock(&dpif->upcall_lock);
617 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
618 const char *port_name, struct dpif_port *dpif_port)
620 struct dpif_linux_vport request;
621 struct dpif_linux_vport reply;
625 dpif_linux_vport_init(&request);
626 request.cmd = OVS_VPORT_CMD_GET;
627 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
628 request.port_no = port_no;
629 request.name = port_name;
631 error = dpif_linux_vport_transact(&request, &reply, &buf);
633 if (reply.dp_ifindex != request.dp_ifindex) {
634 /* A query by name reported that 'port_name' is in some datapath
635 * other than 'dpif', but the caller wants to know about 'dpif'. */
637 } else if (dpif_port) {
638 dpif_port->name = xstrdup(reply.name);
639 dpif_port->type = xstrdup(get_vport_type(&reply));
640 dpif_port->port_no = reply.port_no;
648 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
649 struct dpif_port *dpif_port)
651 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
655 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
656 struct dpif_port *dpif_port)
658 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
662 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
668 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
670 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
671 uint32_t port_idx = odp_to_u32(port_no);
674 ovs_mutex_lock(&dpif->upcall_lock);
675 if (dpif->epoll_fd >= 0) {
676 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
677 * channel, since it is not heavily loaded. */
678 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
679 pid = nl_sock_pid(dpif->channels[idx].sock);
681 ovs_mutex_unlock(&dpif->upcall_lock);
687 dpif_linux_flow_flush(struct dpif *dpif_)
689 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
690 struct dpif_linux_flow flow;
692 dpif_linux_flow_init(&flow);
693 flow.cmd = OVS_FLOW_CMD_DEL;
694 flow.dp_ifindex = dpif->dp_ifindex;
695 return dpif_linux_flow_transact(&flow, NULL, NULL);
698 struct dpif_linux_port_state {
703 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
705 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
706 struct dpif_linux_port_state *state;
707 struct dpif_linux_vport request;
710 *statep = state = xmalloc(sizeof *state);
712 dpif_linux_vport_init(&request);
713 request.cmd = OVS_VPORT_CMD_GET;
714 request.dp_ifindex = dpif->dp_ifindex;
716 buf = ofpbuf_new(1024);
717 dpif_linux_vport_to_ofpbuf(&request, buf);
718 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
725 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
726 struct dpif_port *dpif_port)
728 struct dpif_linux_port_state *state = state_;
729 struct dpif_linux_vport vport;
733 if (!nl_dump_next(&state->dump, &buf)) {
737 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
742 dpif_port->name = CONST_CAST(char *, vport.name);
743 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
744 dpif_port->port_no = vport.port_no;
749 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
751 struct dpif_linux_port_state *state = state_;
752 int error = nl_dump_done(&state->dump);
759 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
761 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
763 /* Lazily create the Netlink socket to listen for notifications. */
764 if (!dpif->port_notifier) {
765 struct nl_sock *sock;
768 error = nl_sock_create(NETLINK_GENERIC, &sock);
773 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
775 nl_sock_destroy(sock);
778 dpif->port_notifier = sock;
780 /* We have no idea of the current state so report that everything
786 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
787 uint64_t buf_stub[4096 / 8];
791 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
792 error = nl_sock_recv(dpif->port_notifier, &buf, false);
794 struct dpif_linux_vport vport;
796 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
798 if (vport.dp_ifindex == dpif->dp_ifindex
799 && (vport.cmd == OVS_VPORT_CMD_NEW
800 || vport.cmd == OVS_VPORT_CMD_DEL
801 || vport.cmd == OVS_VPORT_CMD_SET)) {
802 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
803 dpif->dpif.full_name, vport.name, vport.cmd);
804 *devnamep = xstrdup(vport.name);
809 } else if (error != EAGAIN) {
810 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
811 ovs_strerror(error));
812 nl_sock_drain(dpif->port_notifier);
824 dpif_linux_port_poll_wait(const struct dpif *dpif_)
826 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
828 if (dpif->port_notifier) {
829 nl_sock_wait(dpif->port_notifier, POLLIN);
831 poll_immediate_wake();
836 dpif_linux_flow_get__(const struct dpif *dpif_,
837 const struct nlattr *key, size_t key_len,
838 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
840 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
841 struct dpif_linux_flow request;
843 dpif_linux_flow_init(&request);
844 request.cmd = OVS_FLOW_CMD_GET;
845 request.dp_ifindex = dpif->dp_ifindex;
847 request.key_len = key_len;
848 return dpif_linux_flow_transact(&request, reply, bufp);
852 dpif_linux_flow_get(const struct dpif *dpif_,
853 const struct nlattr *key, size_t key_len,
854 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
856 struct dpif_linux_flow reply;
860 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
863 dpif_linux_flow_get_stats(&reply, stats);
866 buf->data = CONST_CAST(struct nlattr *, reply.actions);
867 buf->size = reply.actions_len;
877 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
878 struct dpif_linux_flow *request)
880 static const struct nlattr dummy_action;
882 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
884 dpif_linux_flow_init(request);
885 request->cmd = (put->flags & DPIF_FP_CREATE
886 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
887 request->dp_ifindex = dpif->dp_ifindex;
888 request->key = put->key;
889 request->key_len = put->key_len;
890 request->mask = put->mask;
891 request->mask_len = put->mask_len;
892 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
893 request->actions = (put->actions
895 : CONST_CAST(struct nlattr *, &dummy_action));
896 request->actions_len = put->actions_len;
897 if (put->flags & DPIF_FP_ZERO_STATS) {
898 request->clear = true;
900 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
904 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
906 struct dpif_linux_flow request, reply;
910 dpif_linux_init_flow_put(dpif_, put, &request);
911 error = dpif_linux_flow_transact(&request,
912 put->stats ? &reply : NULL,
913 put->stats ? &buf : NULL);
914 if (!error && put->stats) {
915 dpif_linux_flow_get_stats(&reply, put->stats);
922 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
923 struct dpif_linux_flow *request)
925 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
927 dpif_linux_flow_init(request);
928 request->cmd = OVS_FLOW_CMD_DEL;
929 request->dp_ifindex = dpif->dp_ifindex;
930 request->key = del->key;
931 request->key_len = del->key_len;
935 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
937 struct dpif_linux_flow request, reply;
941 dpif_linux_init_flow_del(dpif_, del, &request);
942 error = dpif_linux_flow_transact(&request,
943 del->stats ? &reply : NULL,
944 del->stats ? &buf : NULL);
945 if (!error && del->stats) {
946 dpif_linux_flow_get_stats(&reply, del->stats);
952 struct dpif_linux_flow_state {
954 struct dpif_linux_flow flow;
955 struct dpif_flow_stats stats;
960 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
962 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
963 struct dpif_linux_flow_state *state;
964 struct dpif_linux_flow request;
967 *statep = state = xmalloc(sizeof *state);
969 dpif_linux_flow_init(&request);
970 request.cmd = OVS_FLOW_CMD_GET;
971 request.dp_ifindex = dpif->dp_ifindex;
973 buf = ofpbuf_new(1024);
974 dpif_linux_flow_to_ofpbuf(&request, buf);
975 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
984 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
985 const struct nlattr **key, size_t *key_len,
986 const struct nlattr **mask, size_t *mask_len,
987 const struct nlattr **actions, size_t *actions_len,
988 const struct dpif_flow_stats **stats)
990 struct dpif_linux_flow_state *state = state_;
995 ofpbuf_delete(state->buf);
998 if (!nl_dump_next(&state->dump, &buf)) {
1002 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1007 if (actions && !state->flow.actions) {
1008 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1009 state->flow.key_len,
1010 &state->flow, &state->buf);
1011 if (error == ENOENT) {
1012 VLOG_DBG("dumped flow disappeared on get");
1014 VLOG_WARN("error fetching dumped flow: %s",
1015 ovs_strerror(error));
1021 *actions = state->flow.actions;
1022 *actions_len = state->flow.actions_len;
1025 *key = state->flow.key;
1026 *key_len = state->flow.key_len;
1029 *mask = state->flow.mask;
1030 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1033 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1034 *stats = &state->stats;
1040 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1042 struct dpif_linux_flow_state *state = state_;
1043 int error = nl_dump_done(&state->dump);
1044 ofpbuf_delete(state->buf);
1050 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1053 struct ovs_header *k_exec;
1055 ofpbuf_prealloc_tailroom(buf, (64
1056 + d_exec->packet->size
1058 + d_exec->actions_len));
1060 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1061 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1063 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1064 k_exec->dp_ifindex = dp_ifindex;
1066 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1067 d_exec->packet->data, d_exec->packet->size);
1068 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
1069 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1070 d_exec->actions, d_exec->actions_len);
1074 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1076 uint64_t request_stub[1024 / 8];
1077 struct ofpbuf request;
1080 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1081 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1082 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1083 ofpbuf_uninit(&request);
1089 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1091 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1093 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1099 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1101 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1104 struct nl_transaction txn;
1106 struct ofpbuf request;
1107 uint64_t request_stub[1024 / 8];
1109 struct ofpbuf reply;
1110 uint64_t reply_stub[1024 / 8];
1113 struct nl_transaction *txnsp[MAX_OPS];
1116 ovs_assert(n_ops <= MAX_OPS);
1117 for (i = 0; i < n_ops; i++) {
1118 struct op_auxdata *aux = &auxes[i];
1119 struct dpif_op *op = ops[i];
1120 struct dpif_flow_put *put;
1121 struct dpif_flow_del *del;
1122 struct dpif_execute *execute;
1123 struct dpif_linux_flow flow;
1125 ofpbuf_use_stub(&aux->request,
1126 aux->request_stub, sizeof aux->request_stub);
1127 aux->txn.request = &aux->request;
1129 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1130 aux->txn.reply = NULL;
1133 case DPIF_OP_FLOW_PUT:
1134 put = &op->u.flow_put;
1135 dpif_linux_init_flow_put(dpif_, put, &flow);
1137 flow.nlmsg_flags |= NLM_F_ECHO;
1138 aux->txn.reply = &aux->reply;
1140 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1143 case DPIF_OP_FLOW_DEL:
1144 del = &op->u.flow_del;
1145 dpif_linux_init_flow_del(dpif_, del, &flow);
1147 flow.nlmsg_flags |= NLM_F_ECHO;
1148 aux->txn.reply = &aux->reply;
1150 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1153 case DPIF_OP_EXECUTE:
1154 execute = &op->u.execute;
1155 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1164 for (i = 0; i < n_ops; i++) {
1165 txnsp[i] = &auxes[i].txn;
1167 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1169 for (i = 0; i < n_ops; i++) {
1170 struct op_auxdata *aux = &auxes[i];
1171 struct nl_transaction *txn = &auxes[i].txn;
1172 struct dpif_op *op = ops[i];
1173 struct dpif_flow_put *put;
1174 struct dpif_flow_del *del;
1176 op->error = txn->error;
1179 case DPIF_OP_FLOW_PUT:
1180 put = &op->u.flow_put;
1183 struct dpif_linux_flow reply;
1185 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1188 dpif_linux_flow_get_stats(&reply, put->stats);
1193 memset(put->stats, 0, sizeof *put->stats);
1198 case DPIF_OP_FLOW_DEL:
1199 del = &op->u.flow_del;
1202 struct dpif_linux_flow reply;
1204 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1207 dpif_linux_flow_get_stats(&reply, del->stats);
1212 memset(del->stats, 0, sizeof *del->stats);
1217 case DPIF_OP_EXECUTE:
1224 ofpbuf_uninit(&aux->request);
1225 ofpbuf_uninit(&aux->reply);
1230 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1233 size_t chunk = MIN(n_ops, MAX_OPS);
1234 dpif_linux_operate__(dpif, ops, chunk);
1241 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1243 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1245 if ((dpif->epoll_fd >= 0) == enable) {
1250 destroy_channels(dpif);
1252 struct dpif_port_dump port_dump;
1253 struct dpif_port port;
1255 if (dpif->epoll_fd < 0) {
1256 dpif->epoll_fd = epoll_create(10);
1257 if (dpif->epoll_fd < 0) {
1262 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1263 struct dpif_linux_vport vport_request;
1264 struct nl_sock *sock;
1265 uint32_t upcall_pid;
1268 error = nl_sock_create(NETLINK_GENERIC, &sock);
1273 upcall_pid = nl_sock_pid(sock);
1275 dpif_linux_vport_init(&vport_request);
1276 vport_request.cmd = OVS_VPORT_CMD_SET;
1277 vport_request.dp_ifindex = dpif->dp_ifindex;
1278 vport_request.port_no = port.port_no;
1279 vport_request.upcall_pid = &upcall_pid;
1280 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1282 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1283 dpif_name(&dpif->dpif), vport_request.port_no,
1286 VLOG_WARN_RL(&error_rl,
1287 "%s: failed to set upcall pid on port: %s",
1288 dpif_name(&dpif->dpif), ovs_strerror(error));
1289 nl_sock_destroy(sock);
1291 if (error == ENODEV || error == ENOENT) {
1292 /* This device isn't there, but keep trying the others. */
1299 error = add_channel(dpif, port.port_no, sock);
1301 VLOG_INFO("%s: could not add channel for port %s",
1302 dpif_name(dpif_), port.name);
1303 nl_sock_destroy(sock);
1313 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1315 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1318 ovs_mutex_lock(&dpif->upcall_lock);
1319 error = dpif_linux_recv_set__(dpif_, enable);
1320 ovs_mutex_unlock(&dpif->upcall_lock);
1326 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1327 uint32_t queue_id, uint32_t *priority)
1329 if (queue_id < 0xf000) {
1330 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1338 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1341 static const struct nl_policy ovs_packet_policy[] = {
1342 /* Always present. */
1343 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1344 .min_len = ETH_HEADER_LEN },
1345 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1347 /* OVS_PACKET_CMD_ACTION only. */
1348 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1351 struct ovs_header *ovs_header;
1352 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1353 struct nlmsghdr *nlmsg;
1354 struct genlmsghdr *genl;
1358 ofpbuf_use_const(&b, buf->data, buf->size);
1360 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1361 genl = ofpbuf_try_pull(&b, sizeof *genl);
1362 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1363 if (!nlmsg || !genl || !ovs_header
1364 || nlmsg->nlmsg_type != ovs_packet_family
1365 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1366 ARRAY_SIZE(ovs_packet_policy))) {
1370 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1371 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1377 memset(upcall, 0, sizeof *upcall);
1378 upcall->type = type;
1379 upcall->packet = buf;
1380 upcall->packet->data = CONST_CAST(struct nlattr *,
1381 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1382 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1383 upcall->key = CONST_CAST(struct nlattr *,
1384 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1385 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1386 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1387 *dp_ifindex = ovs_header->dp_ifindex;
1393 dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
1396 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1399 if (dpif->epoll_fd < 0) {
1403 if (dpif->event_offset >= dpif->n_events) {
1406 dpif->event_offset = dpif->n_events = 0;
1409 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1410 dpif->uc_array_size, 0);
1411 } while (retval < 0 && errno == EINTR);
1413 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1414 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1415 } else if (retval > 0) {
1416 dpif->n_events = retval;
1420 while (dpif->event_offset < dpif->n_events) {
1421 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1422 struct dpif_channel *ch = &dpif->channels[idx];
1424 dpif->event_offset++;
1430 if (++read_tries > 50) {
1434 error = nl_sock_recv(ch->sock, buf, false);
1435 if (error == ENOBUFS) {
1436 /* ENOBUFS typically means that we've received so many
1437 * packets that the buffer overflowed. Try again
1438 * immediately because there's almost certainly a packet
1439 * waiting for us. */
1440 report_loss(dpif_, ch);
1444 ch->last_poll = time_msec();
1446 if (error == EAGAIN) {
1452 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1453 if (!error && dp_ifindex == dpif->dp_ifindex) {
1465 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1468 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1471 ovs_mutex_lock(&dpif->upcall_lock);
1472 error = dpif_linux_recv__(dpif_, upcall, buf);
1473 ovs_mutex_unlock(&dpif->upcall_lock);
1479 dpif_linux_recv_wait(struct dpif *dpif_)
1481 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1483 ovs_mutex_lock(&dpif->upcall_lock);
1484 if (dpif->epoll_fd >= 0) {
1485 poll_fd_wait(dpif->epoll_fd, POLLIN);
1487 ovs_mutex_unlock(&dpif->upcall_lock);
1491 dpif_linux_recv_purge(struct dpif *dpif_)
1493 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1495 ovs_mutex_lock(&dpif->upcall_lock);
1496 if (dpif->epoll_fd >= 0) {
1497 struct dpif_channel *ch;
1499 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
1502 nl_sock_drain(ch->sock);
1506 ovs_mutex_unlock(&dpif->upcall_lock);
1509 const struct dpif_class dpif_linux_class = {
1511 dpif_linux_enumerate,
1518 dpif_linux_get_stats,
1519 dpif_linux_port_add,
1520 dpif_linux_port_del,
1521 dpif_linux_port_query_by_number,
1522 dpif_linux_port_query_by_name,
1523 dpif_linux_get_max_ports,
1524 dpif_linux_port_get_pid,
1525 dpif_linux_port_dump_start,
1526 dpif_linux_port_dump_next,
1527 dpif_linux_port_dump_done,
1528 dpif_linux_port_poll,
1529 dpif_linux_port_poll_wait,
1530 dpif_linux_flow_get,
1531 dpif_linux_flow_put,
1532 dpif_linux_flow_del,
1533 dpif_linux_flow_flush,
1534 dpif_linux_flow_dump_start,
1535 dpif_linux_flow_dump_next,
1536 dpif_linux_flow_dump_done,
1539 dpif_linux_recv_set,
1540 dpif_linux_queue_to_priority,
1542 dpif_linux_recv_wait,
1543 dpif_linux_recv_purge,
1547 dpif_linux_init(void)
1549 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1552 if (ovsthread_once_start(&once)) {
1553 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1554 &ovs_datapath_family);
1556 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1557 "The Open vSwitch kernel module is probably not loaded.",
1558 OVS_DATAPATH_FAMILY);
1561 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1564 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1567 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1568 &ovs_packet_family);
1571 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1572 &ovs_vport_mcgroup);
1575 ovsthread_once_done(&once);
1582 dpif_linux_is_internal_device(const char *name)
1584 struct dpif_linux_vport reply;
1588 error = dpif_linux_vport_get(name, &reply, &buf);
1591 } else if (error != ENODEV && error != ENOENT) {
1592 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1593 name, ovs_strerror(error));
1596 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1599 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1600 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1601 * positive errno value.
1603 * 'vport' will contain pointers into 'buf', so the caller should not free
1604 * 'buf' while 'vport' is still in use. */
1606 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1607 const struct ofpbuf *buf)
1609 static const struct nl_policy ovs_vport_policy[] = {
1610 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1611 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1612 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1613 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1614 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1616 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1619 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1620 struct ovs_header *ovs_header;
1621 struct nlmsghdr *nlmsg;
1622 struct genlmsghdr *genl;
1625 dpif_linux_vport_init(vport);
1627 ofpbuf_use_const(&b, buf->data, buf->size);
1628 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1629 genl = ofpbuf_try_pull(&b, sizeof *genl);
1630 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1631 if (!nlmsg || !genl || !ovs_header
1632 || nlmsg->nlmsg_type != ovs_vport_family
1633 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1634 ARRAY_SIZE(ovs_vport_policy))) {
1638 vport->cmd = genl->cmd;
1639 vport->dp_ifindex = ovs_header->dp_ifindex;
1640 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1641 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1642 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1643 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1644 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1646 if (a[OVS_VPORT_ATTR_STATS]) {
1647 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1649 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1650 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1651 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1656 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1657 * followed by Netlink attributes corresponding to 'vport'. */
1659 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1662 struct ovs_header *ovs_header;
1664 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1665 vport->cmd, OVS_VPORT_VERSION);
1667 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1668 ovs_header->dp_ifindex = vport->dp_ifindex;
1670 if (vport->port_no != ODPP_NONE) {
1671 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1674 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1675 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1679 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1682 if (vport->upcall_pid) {
1683 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1687 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1688 vport->stats, sizeof *vport->stats);
1691 if (vport->options) {
1692 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1693 vport->options, vport->options_len);
1697 /* Clears 'vport' to "empty" values. */
1699 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1701 memset(vport, 0, sizeof *vport);
1702 vport->port_no = ODPP_NONE;
1705 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1706 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1707 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1708 * result of the command is expected to be an ovs_vport also, which is decoded
1709 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1710 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1712 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1713 struct dpif_linux_vport *reply,
1714 struct ofpbuf **bufp)
1716 struct ofpbuf *request_buf;
1719 ovs_assert((reply != NULL) == (bufp != NULL));
1721 error = dpif_linux_init();
1725 dpif_linux_vport_init(reply);
1730 request_buf = ofpbuf_new(1024);
1731 dpif_linux_vport_to_ofpbuf(request, request_buf);
1732 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1733 ofpbuf_delete(request_buf);
1737 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1740 dpif_linux_vport_init(reply);
1741 ofpbuf_delete(*bufp);
1748 /* Obtains information about the kernel vport named 'name' and stores it into
1749 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1750 * longer needed ('reply' will contain pointers into '*bufp'). */
1752 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1753 struct ofpbuf **bufp)
1755 struct dpif_linux_vport request;
1757 dpif_linux_vport_init(&request);
1758 request.cmd = OVS_VPORT_CMD_GET;
1759 request.name = name;
1761 return dpif_linux_vport_transact(&request, reply, bufp);
1764 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1765 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1766 * positive errno value.
1768 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1769 * while 'dp' is still in use. */
1771 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1773 static const struct nl_policy ovs_datapath_policy[] = {
1774 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1775 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1777 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
1778 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
1782 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1783 struct ovs_header *ovs_header;
1784 struct nlmsghdr *nlmsg;
1785 struct genlmsghdr *genl;
1788 dpif_linux_dp_init(dp);
1790 ofpbuf_use_const(&b, buf->data, buf->size);
1791 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1792 genl = ofpbuf_try_pull(&b, sizeof *genl);
1793 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1794 if (!nlmsg || !genl || !ovs_header
1795 || nlmsg->nlmsg_type != ovs_datapath_family
1796 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1797 ARRAY_SIZE(ovs_datapath_policy))) {
1801 dp->cmd = genl->cmd;
1802 dp->dp_ifindex = ovs_header->dp_ifindex;
1803 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1804 if (a[OVS_DP_ATTR_STATS]) {
1805 /* Can't use structure assignment because Netlink doesn't ensure
1806 * sufficient alignment for 64-bit members. */
1807 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1811 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
1812 /* Can't use structure assignment because Netlink doesn't ensure
1813 * sufficient alignment for 64-bit members. */
1814 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
1815 sizeof dp->megaflow_stats);
1821 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1823 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1825 struct ovs_header *ovs_header;
1827 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1828 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1829 OVS_DATAPATH_VERSION);
1831 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1832 ovs_header->dp_ifindex = dp->dp_ifindex;
1835 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1838 if (dp->upcall_pid) {
1839 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1842 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1845 /* Clears 'dp' to "empty" values. */
1847 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1849 memset(dp, 0, sizeof *dp);
1850 dp->megaflow_stats.n_masks = UINT32_MAX;
1851 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
1855 dpif_linux_dp_dump_start(struct nl_dump *dump)
1857 struct dpif_linux_dp request;
1860 dpif_linux_dp_init(&request);
1861 request.cmd = OVS_DP_CMD_GET;
1863 buf = ofpbuf_new(1024);
1864 dpif_linux_dp_to_ofpbuf(&request, buf);
1865 nl_dump_start(dump, NETLINK_GENERIC, buf);
1869 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1870 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1871 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1872 * result of the command is expected to be of the same form, which is decoded
1873 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1874 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1876 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1877 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1879 struct ofpbuf *request_buf;
1882 ovs_assert((reply != NULL) == (bufp != NULL));
1884 request_buf = ofpbuf_new(1024);
1885 dpif_linux_dp_to_ofpbuf(request, request_buf);
1886 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1887 ofpbuf_delete(request_buf);
1890 dpif_linux_dp_init(reply);
1892 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1895 ofpbuf_delete(*bufp);
1902 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1903 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1904 * will contain pointers into '*bufp'). */
1906 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1907 struct ofpbuf **bufp)
1909 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1910 struct dpif_linux_dp request;
1912 dpif_linux_dp_init(&request);
1913 request.cmd = OVS_DP_CMD_GET;
1914 request.dp_ifindex = dpif->dp_ifindex;
1916 return dpif_linux_dp_transact(&request, reply, bufp);
1919 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1920 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1921 * positive errno value.
1923 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1924 * while 'flow' is still in use. */
1926 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1927 const struct ofpbuf *buf)
1929 static const struct nl_policy ovs_flow_policy[] = {
1930 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1931 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
1932 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1933 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1935 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1936 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1937 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1940 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1941 struct ovs_header *ovs_header;
1942 struct nlmsghdr *nlmsg;
1943 struct genlmsghdr *genl;
1946 dpif_linux_flow_init(flow);
1948 ofpbuf_use_const(&b, buf->data, buf->size);
1949 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1950 genl = ofpbuf_try_pull(&b, sizeof *genl);
1951 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1952 if (!nlmsg || !genl || !ovs_header
1953 || nlmsg->nlmsg_type != ovs_flow_family
1954 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1955 ARRAY_SIZE(ovs_flow_policy))) {
1959 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1960 flow->dp_ifindex = ovs_header->dp_ifindex;
1961 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1962 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1964 if (a[OVS_FLOW_ATTR_MASK]) {
1965 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
1966 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
1968 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1969 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1970 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1972 if (a[OVS_FLOW_ATTR_STATS]) {
1973 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1975 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1976 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1978 if (a[OVS_FLOW_ATTR_USED]) {
1979 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1984 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1985 * followed by Netlink attributes corresponding to 'flow'. */
1987 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1990 struct ovs_header *ovs_header;
1992 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1993 NLM_F_REQUEST | flow->nlmsg_flags,
1994 flow->cmd, OVS_FLOW_VERSION);
1996 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1997 ovs_header->dp_ifindex = flow->dp_ifindex;
1999 if (flow->key_len) {
2000 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2003 if (flow->mask_len) {
2004 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2007 if (flow->actions || flow->actions_len) {
2008 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2009 flow->actions, flow->actions_len);
2012 /* We never need to send these to the kernel. */
2013 ovs_assert(!flow->stats);
2014 ovs_assert(!flow->tcp_flags);
2015 ovs_assert(!flow->used);
2018 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2022 /* Clears 'flow' to "empty" values. */
2024 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2026 memset(flow, 0, sizeof *flow);
2029 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2030 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2031 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2032 * result of the command is expected to be a flow also, which is decoded and
2033 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2034 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2036 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2037 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2039 struct ofpbuf *request_buf;
2042 ovs_assert((reply != NULL) == (bufp != NULL));
2045 request->nlmsg_flags |= NLM_F_ECHO;
2048 request_buf = ofpbuf_new(1024);
2049 dpif_linux_flow_to_ofpbuf(request, request_buf);
2050 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2051 ofpbuf_delete(request_buf);
2055 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2058 dpif_linux_flow_init(reply);
2059 ofpbuf_delete(*bufp);
2067 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2068 struct dpif_flow_stats *stats)
2071 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2072 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2074 stats->n_packets = 0;
2077 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2078 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2081 /* Logs information about a packet that was recently lost in 'ch' (in
2084 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2086 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2087 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2090 if (VLOG_DROP_WARN(&rl)) {
2095 if (ch->last_poll != LLONG_MIN) {
2096 ds_put_format(&s, " (last polled %lld ms ago)",
2097 time_msec() - ch->last_poll);
2100 VLOG_WARN("%s: lost packet on channel %"PRIdPTR"%s",
2101 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));