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 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
77 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
78 struct ovs_dp_megaflow_stats megaflow_stats;
79 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
82 static void dpif_linux_dp_init(struct dpif_linux_dp *);
83 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
84 const struct ofpbuf *);
85 static void dpif_linux_dp_dump_start(struct nl_dump *);
86 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
87 struct dpif_linux_dp *reply,
88 struct ofpbuf **bufp);
89 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
90 struct ofpbuf **bufp);
92 struct dpif_linux_flow {
93 /* Generic Netlink header. */
96 /* struct ovs_header. */
97 unsigned int nlmsg_flags;
102 * The 'stats' member points to 64-bit data that might only be aligned on
103 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
106 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
107 * the Netlink version of the command, even if actions_len is zero. */
108 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
110 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
112 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
114 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
115 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
116 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
117 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
120 static void dpif_linux_flow_init(struct dpif_linux_flow *);
121 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
122 const struct ofpbuf *);
123 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
125 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
126 struct dpif_linux_flow *reply,
127 struct ofpbuf **bufp);
128 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
129 struct dpif_flow_stats *);
131 /* One of the dpif channels between the kernel and userspace. */
132 struct dpif_channel {
133 struct nl_sock *sock; /* Netlink socket. */
134 long long int last_poll; /* Last time this channel was polled. */
137 static void report_loss(struct dpif *, struct dpif_channel *);
139 /* Datapath interface for the openvswitch Linux kernel module. */
144 /* Upcall messages. */
145 struct ovs_mutex upcall_lock;
146 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
147 struct dpif_channel *channels;
148 struct epoll_event *epoll_events;
149 int epoll_fd; /* epoll fd that includes channel socks. */
150 int n_events; /* Num events returned by epoll_wait(). */
151 int event_offset; /* Offset into 'epoll_events'. */
153 /* Change notification. */
154 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
155 bool refresh_channels;
158 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
160 /* Generic Netlink family numbers for OVS.
162 * Initialized by dpif_linux_init(). */
163 static int ovs_datapath_family;
164 static int ovs_vport_family;
165 static int ovs_flow_family;
166 static int ovs_packet_family;
168 /* Generic Netlink multicast groups for OVS.
170 * Initialized by dpif_linux_init(). */
171 static unsigned int ovs_vport_mcgroup;
173 static int dpif_linux_init(void);
174 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
175 static uint32_t dpif_linux_port_get_pid(const struct dpif *,
177 static int dpif_linux_refresh_channels(struct dpif *);
179 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
181 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
182 const struct ofpbuf *);
184 static struct dpif_linux *
185 dpif_linux_cast(const struct dpif *dpif)
187 dpif_assert_class(dpif, &dpif_linux_class);
188 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
192 dpif_linux_enumerate(struct sset *all_dps)
198 error = dpif_linux_init();
203 dpif_linux_dp_dump_start(&dump);
204 while (nl_dump_next(&dump, &msg)) {
205 struct dpif_linux_dp dp;
207 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
208 sset_add(all_dps, dp.name);
211 return nl_dump_done(&dump);
215 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
216 bool create, struct dpif **dpifp)
218 struct dpif_linux_dp dp_request, dp;
223 error = dpif_linux_init();
228 /* Create or look up datapath. */
229 dpif_linux_dp_init(&dp_request);
231 dp_request.cmd = OVS_DP_CMD_NEW;
233 dp_request.upcall_pid = &upcall_pid;
235 /* Use OVS_DP_CMD_SET to report user features */
236 dp_request.cmd = OVS_DP_CMD_SET;
238 dp_request.name = name;
239 dp_request.user_features |= OVS_DP_F_UNALIGNED;
240 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
245 error = open_dpif(&dp, dpifp);
251 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
253 struct dpif_linux *dpif;
255 dpif = xzalloc(sizeof *dpif);
256 dpif->port_notifier = NULL;
257 ovs_mutex_init(&dpif->upcall_lock);
260 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
261 dp->dp_ifindex, dp->dp_ifindex);
263 dpif->dp_ifindex = dp->dp_ifindex;
264 *dpifp = &dpif->dpif;
270 destroy_channels(struct dpif_linux *dpif)
274 if (dpif->epoll_fd < 0) {
278 for (i = 0; i < dpif->uc_array_size; i++ ) {
279 struct dpif_linux_vport vport_request;
280 struct dpif_channel *ch = &dpif->channels[i];
281 uint32_t upcall_pid = 0;
287 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
289 /* Turn off upcalls. */
290 dpif_linux_vport_init(&vport_request);
291 vport_request.cmd = OVS_VPORT_CMD_SET;
292 vport_request.dp_ifindex = dpif->dp_ifindex;
293 vport_request.port_no = u32_to_odp(i);
294 vport_request.upcall_pid = &upcall_pid;
295 dpif_linux_vport_transact(&vport_request, NULL, NULL);
297 nl_sock_destroy(ch->sock);
300 free(dpif->channels);
301 dpif->channels = NULL;
302 dpif->uc_array_size = 0;
304 free(dpif->epoll_events);
305 dpif->epoll_events = NULL;
306 dpif->n_events = dpif->event_offset = 0;
308 /* Don't close dpif->epoll_fd since that would cause other threads that
309 * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */
313 add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
315 struct epoll_event event;
316 uint32_t port_idx = odp_to_u32(port_no);
318 if (dpif->epoll_fd < 0) {
322 /* We assume that the datapath densely chooses port numbers, which
323 * can therefore be used as an index into an array of channels. */
324 if (port_idx >= dpif->uc_array_size) {
325 uint32_t new_size = port_idx + 1;
328 if (new_size > MAX_PORTS) {
329 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
330 dpif_name(&dpif->dpif), port_no);
334 dpif->channels = xrealloc(dpif->channels,
335 new_size * sizeof *dpif->channels);
336 for (i = dpif->uc_array_size; i < new_size; i++) {
337 dpif->channels[i].sock = NULL;
340 dpif->epoll_events = xrealloc(dpif->epoll_events,
341 new_size * sizeof *dpif->epoll_events);
342 dpif->uc_array_size = new_size;
345 memset(&event, 0, sizeof event);
346 event.events = EPOLLIN;
347 event.data.u32 = port_idx;
348 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
353 nl_sock_destroy(dpif->channels[port_idx].sock);
354 dpif->channels[port_idx].sock = sock;
355 dpif->channels[port_idx].last_poll = LLONG_MIN;
361 del_channel(struct dpif_linux *dpif, odp_port_t port_no)
363 struct dpif_channel *ch;
364 uint32_t port_idx = odp_to_u32(port_no);
366 if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
370 ch = &dpif->channels[port_idx];
375 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
376 dpif->event_offset = dpif->n_events = 0;
378 nl_sock_destroy(ch->sock);
383 dpif_linux_close(struct dpif *dpif_)
385 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
387 nl_sock_destroy(dpif->port_notifier);
388 destroy_channels(dpif);
389 if (dpif->epoll_fd >= 0) {
390 close(dpif->epoll_fd);
392 ovs_mutex_destroy(&dpif->upcall_lock);
397 dpif_linux_destroy(struct dpif *dpif_)
399 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
400 struct dpif_linux_dp dp;
402 dpif_linux_dp_init(&dp);
403 dp.cmd = OVS_DP_CMD_DEL;
404 dp.dp_ifindex = dpif->dp_ifindex;
405 return dpif_linux_dp_transact(&dp, NULL, NULL);
409 dpif_linux_run(struct dpif *dpif_)
411 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
412 if (dpif->refresh_channels) {
413 dpif->refresh_channels = false;
414 dpif_linux_refresh_channels(dpif_);
419 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
421 struct dpif_linux_dp dp;
425 error = dpif_linux_dp_get(dpif_, &dp, &buf);
427 stats->n_hit = dp.stats.n_hit;
428 stats->n_missed = dp.stats.n_missed;
429 stats->n_lost = dp.stats.n_lost;
430 stats->n_flows = dp.stats.n_flows;
431 stats->n_masks = dp.megaflow_stats.n_masks;
432 stats->n_mask_hit = dp.megaflow_stats.n_mask_hit;
439 get_vport_type(const struct dpif_linux_vport *vport)
441 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
443 switch (vport->type) {
444 case OVS_VPORT_TYPE_NETDEV:
447 case OVS_VPORT_TYPE_INTERNAL:
450 case OVS_VPORT_TYPE_GRE:
453 case OVS_VPORT_TYPE_GRE64:
456 case OVS_VPORT_TYPE_VXLAN:
459 case OVS_VPORT_TYPE_LISP:
462 case OVS_VPORT_TYPE_UNSPEC:
463 case __OVS_VPORT_TYPE_MAX:
467 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
468 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
472 static enum ovs_vport_type
473 netdev_to_ovs_vport_type(const struct netdev *netdev)
475 const char *type = netdev_get_type(netdev);
477 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
478 return OVS_VPORT_TYPE_NETDEV;
479 } else if (!strcmp(type, "internal")) {
480 return OVS_VPORT_TYPE_INTERNAL;
481 } else if (strstr(type, "gre64")) {
482 return OVS_VPORT_TYPE_GRE64;
483 } else if (strstr(type, "gre")) {
484 return OVS_VPORT_TYPE_GRE;
485 } else if (!strcmp(type, "vxlan")) {
486 return OVS_VPORT_TYPE_VXLAN;
487 } else if (!strcmp(type, "lisp")) {
488 return OVS_VPORT_TYPE_LISP;
490 return OVS_VPORT_TYPE_UNSPEC;
495 dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
496 odp_port_t *port_nop)
498 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
499 const struct netdev_tunnel_config *tnl_cfg;
500 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
501 const char *name = netdev_vport_get_dpif_port(netdev,
502 namebuf, sizeof namebuf);
503 const char *type = netdev_get_type(netdev);
504 struct dpif_linux_vport request, reply;
505 struct nl_sock *sock = NULL;
508 uint64_t options_stub[64 / 8];
509 struct ofpbuf options;
512 if (dpif->epoll_fd >= 0) {
513 error = nl_sock_create(NETLINK_GENERIC, &sock);
519 dpif_linux_vport_init(&request);
520 request.cmd = OVS_VPORT_CMD_NEW;
521 request.dp_ifindex = dpif->dp_ifindex;
522 request.type = netdev_to_ovs_vport_type(netdev);
523 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
524 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
525 "unsupported type `%s'",
526 dpif_name(dpif_), name, type);
527 nl_sock_destroy(sock);
532 if (request.type == OVS_VPORT_TYPE_NETDEV) {
533 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
536 tnl_cfg = netdev_get_tunnel_config(netdev);
537 if (tnl_cfg && tnl_cfg->dst_port != 0) {
538 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
539 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
540 ntohs(tnl_cfg->dst_port));
541 request.options = options.data;
542 request.options_len = options.size;
545 request.port_no = *port_nop;
546 upcall_pid = sock ? nl_sock_pid(sock) : 0;
547 request.upcall_pid = &upcall_pid;
549 error = dpif_linux_vport_transact(&request, &reply, &buf);
551 *port_nop = reply.port_no;
552 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
553 dpif_name(dpif_), reply.port_no, upcall_pid);
555 if (error == EBUSY && *port_nop != ODPP_NONE) {
556 VLOG_INFO("%s: requested port %"PRIu32" is in use",
557 dpif_name(dpif_), *port_nop);
559 nl_sock_destroy(sock);
566 error = add_channel(dpif, *port_nop, sock);
568 VLOG_INFO("%s: could not add channel for port %s",
569 dpif_name(dpif_), name);
571 /* Delete the port. */
572 dpif_linux_vport_init(&request);
573 request.cmd = OVS_VPORT_CMD_DEL;
574 request.dp_ifindex = dpif->dp_ifindex;
575 request.port_no = *port_nop;
576 dpif_linux_vport_transact(&request, NULL, NULL);
578 nl_sock_destroy(sock);
587 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
588 odp_port_t *port_nop)
590 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
593 ovs_mutex_lock(&dpif->upcall_lock);
594 error = dpif_linux_port_add__(dpif_, netdev, port_nop);
595 ovs_mutex_unlock(&dpif->upcall_lock);
601 dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
603 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
604 struct dpif_linux_vport vport;
607 dpif_linux_vport_init(&vport);
608 vport.cmd = OVS_VPORT_CMD_DEL;
609 vport.dp_ifindex = dpif->dp_ifindex;
610 vport.port_no = port_no;
611 error = dpif_linux_vport_transact(&vport, NULL, NULL);
613 del_channel(dpif, port_no);
619 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
621 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
624 ovs_mutex_lock(&dpif->upcall_lock);
625 error = dpif_linux_port_del__(dpif_, port_no);
626 ovs_mutex_unlock(&dpif->upcall_lock);
632 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
633 const char *port_name, struct dpif_port *dpif_port)
635 struct dpif_linux_vport request;
636 struct dpif_linux_vport reply;
640 dpif_linux_vport_init(&request);
641 request.cmd = OVS_VPORT_CMD_GET;
642 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
643 request.port_no = port_no;
644 request.name = port_name;
646 error = dpif_linux_vport_transact(&request, &reply, &buf);
648 if (reply.dp_ifindex != request.dp_ifindex) {
649 /* A query by name reported that 'port_name' is in some datapath
650 * other than 'dpif', but the caller wants to know about 'dpif'. */
652 } else if (dpif_port) {
653 dpif_port->name = xstrdup(reply.name);
654 dpif_port->type = xstrdup(get_vport_type(&reply));
655 dpif_port->port_no = reply.port_no;
663 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
664 struct dpif_port *dpif_port)
666 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
670 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
671 struct dpif_port *dpif_port)
673 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
677 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
683 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
685 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
686 uint32_t port_idx = odp_to_u32(port_no);
689 ovs_mutex_lock(&dpif->upcall_lock);
690 if (dpif->epoll_fd >= 0) {
691 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
692 * channel, since it is not heavily loaded. */
693 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
694 const struct nl_sock *sock = dpif->channels[idx].sock;
695 pid = sock ? nl_sock_pid(sock) : 0;
697 ovs_mutex_unlock(&dpif->upcall_lock);
703 dpif_linux_flow_flush(struct dpif *dpif_)
705 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
706 struct dpif_linux_flow flow;
708 dpif_linux_flow_init(&flow);
709 flow.cmd = OVS_FLOW_CMD_DEL;
710 flow.dp_ifindex = dpif->dp_ifindex;
711 return dpif_linux_flow_transact(&flow, NULL, NULL);
714 struct dpif_linux_port_state {
719 dpif_linux_port_dump_start__(const struct dpif *dpif_, struct nl_dump *dump)
721 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
722 struct dpif_linux_vport request;
725 dpif_linux_vport_init(&request);
726 request.cmd = OVS_VPORT_CMD_GET;
727 request.dp_ifindex = dpif->dp_ifindex;
729 buf = ofpbuf_new(1024);
730 dpif_linux_vport_to_ofpbuf(&request, buf);
731 nl_dump_start(dump, NETLINK_GENERIC, buf);
736 dpif_linux_port_dump_start(const struct dpif *dpif, void **statep)
738 struct dpif_linux_port_state *state;
740 *statep = state = xmalloc(sizeof *state);
741 dpif_linux_port_dump_start__(dpif, &state->dump);
747 dpif_linux_port_dump_next__(const struct dpif *dpif_, struct nl_dump *dump,
748 struct dpif_linux_vport *vport)
750 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
754 if (!nl_dump_next(dump, &buf)) {
758 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
760 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
761 dpif_name(&dpif->dpif), ovs_strerror(error));
767 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
768 struct dpif_port *dpif_port)
770 struct dpif_linux_port_state *state = state_;
771 struct dpif_linux_vport vport;
774 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport);
778 dpif_port->name = CONST_CAST(char *, vport.name);
779 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
780 dpif_port->port_no = vport.port_no;
785 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
787 struct dpif_linux_port_state *state = state_;
788 int error = nl_dump_done(&state->dump);
795 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
797 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
799 /* Lazily create the Netlink socket to listen for notifications. */
800 if (!dpif->port_notifier) {
801 struct nl_sock *sock;
804 error = nl_sock_create(NETLINK_GENERIC, &sock);
809 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
811 nl_sock_destroy(sock);
814 dpif->port_notifier = sock;
816 /* We have no idea of the current state so report that everything
822 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
823 uint64_t buf_stub[4096 / 8];
827 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
828 error = nl_sock_recv(dpif->port_notifier, &buf, false);
830 struct dpif_linux_vport vport;
832 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
834 if (vport.dp_ifindex == dpif->dp_ifindex
835 && (vport.cmd == OVS_VPORT_CMD_NEW
836 || vport.cmd == OVS_VPORT_CMD_DEL
837 || vport.cmd == OVS_VPORT_CMD_SET)) {
838 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
839 dpif->dpif.full_name, vport.name, vport.cmd);
840 if (vport.cmd == OVS_VPORT_CMD_DEL) {
841 dpif->refresh_channels = true;
843 *devnamep = xstrdup(vport.name);
848 } else if (error != EAGAIN) {
849 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
850 ovs_strerror(error));
851 nl_sock_drain(dpif->port_notifier);
863 dpif_linux_port_poll_wait(const struct dpif *dpif_)
865 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
867 if (dpif->port_notifier) {
868 nl_sock_wait(dpif->port_notifier, POLLIN);
870 poll_immediate_wake();
875 dpif_linux_flow_get__(const struct dpif *dpif_,
876 const struct nlattr *key, size_t key_len,
877 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
879 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
880 struct dpif_linux_flow request;
882 dpif_linux_flow_init(&request);
883 request.cmd = OVS_FLOW_CMD_GET;
884 request.dp_ifindex = dpif->dp_ifindex;
886 request.key_len = key_len;
887 return dpif_linux_flow_transact(&request, reply, bufp);
891 dpif_linux_flow_get(const struct dpif *dpif_,
892 const struct nlattr *key, size_t key_len,
893 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
895 struct dpif_linux_flow reply;
899 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
902 dpif_linux_flow_get_stats(&reply, stats);
905 buf->data = CONST_CAST(struct nlattr *, reply.actions);
906 buf->size = reply.actions_len;
916 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
917 struct dpif_linux_flow *request)
919 static const struct nlattr dummy_action;
921 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
923 dpif_linux_flow_init(request);
924 request->cmd = (put->flags & DPIF_FP_CREATE
925 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
926 request->dp_ifindex = dpif->dp_ifindex;
927 request->key = put->key;
928 request->key_len = put->key_len;
929 request->mask = put->mask;
930 request->mask_len = put->mask_len;
931 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
932 request->actions = (put->actions
934 : CONST_CAST(struct nlattr *, &dummy_action));
935 request->actions_len = put->actions_len;
936 if (put->flags & DPIF_FP_ZERO_STATS) {
937 request->clear = true;
939 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
943 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
945 struct dpif_linux_flow request, reply;
949 dpif_linux_init_flow_put(dpif_, put, &request);
950 error = dpif_linux_flow_transact(&request,
951 put->stats ? &reply : NULL,
952 put->stats ? &buf : NULL);
953 if (!error && put->stats) {
954 dpif_linux_flow_get_stats(&reply, put->stats);
961 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
962 struct dpif_linux_flow *request)
964 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
966 dpif_linux_flow_init(request);
967 request->cmd = OVS_FLOW_CMD_DEL;
968 request->dp_ifindex = dpif->dp_ifindex;
969 request->key = del->key;
970 request->key_len = del->key_len;
974 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
976 struct dpif_linux_flow request, reply;
980 dpif_linux_init_flow_del(dpif_, del, &request);
981 error = dpif_linux_flow_transact(&request,
982 del->stats ? &reply : NULL,
983 del->stats ? &buf : NULL);
984 if (!error && del->stats) {
985 dpif_linux_flow_get_stats(&reply, del->stats);
991 struct dpif_linux_flow_state {
993 struct dpif_linux_flow flow;
994 struct dpif_flow_stats stats;
999 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
1001 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1002 struct dpif_linux_flow_state *state;
1003 struct dpif_linux_flow request;
1006 *statep = state = xmalloc(sizeof *state);
1008 dpif_linux_flow_init(&request);
1009 request.cmd = OVS_FLOW_CMD_GET;
1010 request.dp_ifindex = dpif->dp_ifindex;
1012 buf = ofpbuf_new(1024);
1013 dpif_linux_flow_to_ofpbuf(&request, buf);
1014 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
1023 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
1024 const struct nlattr **key, size_t *key_len,
1025 const struct nlattr **mask, size_t *mask_len,
1026 const struct nlattr **actions, size_t *actions_len,
1027 const struct dpif_flow_stats **stats)
1029 struct dpif_linux_flow_state *state = state_;
1034 ofpbuf_delete(state->buf);
1037 if (!nl_dump_next(&state->dump, &buf)) {
1041 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1046 if (actions && !state->flow.actions) {
1047 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1048 state->flow.key_len,
1049 &state->flow, &state->buf);
1050 if (error == ENOENT) {
1051 VLOG_DBG("dumped flow disappeared on get");
1053 VLOG_WARN("error fetching dumped flow: %s",
1054 ovs_strerror(error));
1060 *actions = state->flow.actions;
1061 *actions_len = state->flow.actions_len;
1064 *key = state->flow.key;
1065 *key_len = state->flow.key_len;
1068 *mask = state->flow.mask;
1069 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1072 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1073 *stats = &state->stats;
1079 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1081 struct dpif_linux_flow_state *state = state_;
1082 int error = nl_dump_done(&state->dump);
1083 ofpbuf_delete(state->buf);
1089 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1092 struct ovs_header *k_exec;
1095 ofpbuf_prealloc_tailroom(buf, (64
1096 + d_exec->packet->size
1097 + ODP_KEY_METADATA_SIZE
1098 + d_exec->actions_len));
1100 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1101 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1103 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1104 k_exec->dp_ifindex = dp_ifindex;
1106 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1107 d_exec->packet->data, d_exec->packet->size);
1109 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1110 odp_key_from_pkt_metadata(buf, &d_exec->md);
1111 nl_msg_end_nested(buf, key_ofs);
1113 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1114 d_exec->actions, d_exec->actions_len);
1118 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1120 uint64_t request_stub[1024 / 8];
1121 struct ofpbuf request;
1124 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1125 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1126 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1127 ofpbuf_uninit(&request);
1133 dpif_linux_execute(struct dpif *dpif_, struct dpif_execute *execute)
1135 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1137 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1143 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1145 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1148 struct nl_transaction txn;
1150 struct ofpbuf request;
1151 uint64_t request_stub[1024 / 8];
1153 struct ofpbuf reply;
1154 uint64_t reply_stub[1024 / 8];
1157 struct nl_transaction *txnsp[MAX_OPS];
1160 ovs_assert(n_ops <= MAX_OPS);
1161 for (i = 0; i < n_ops; i++) {
1162 struct op_auxdata *aux = &auxes[i];
1163 struct dpif_op *op = ops[i];
1164 struct dpif_flow_put *put;
1165 struct dpif_flow_del *del;
1166 struct dpif_execute *execute;
1167 struct dpif_linux_flow flow;
1169 ofpbuf_use_stub(&aux->request,
1170 aux->request_stub, sizeof aux->request_stub);
1171 aux->txn.request = &aux->request;
1173 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1174 aux->txn.reply = NULL;
1177 case DPIF_OP_FLOW_PUT:
1178 put = &op->u.flow_put;
1179 dpif_linux_init_flow_put(dpif_, put, &flow);
1181 flow.nlmsg_flags |= NLM_F_ECHO;
1182 aux->txn.reply = &aux->reply;
1184 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1187 case DPIF_OP_FLOW_DEL:
1188 del = &op->u.flow_del;
1189 dpif_linux_init_flow_del(dpif_, del, &flow);
1191 flow.nlmsg_flags |= NLM_F_ECHO;
1192 aux->txn.reply = &aux->reply;
1194 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1197 case DPIF_OP_EXECUTE:
1198 execute = &op->u.execute;
1199 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1208 for (i = 0; i < n_ops; i++) {
1209 txnsp[i] = &auxes[i].txn;
1211 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1213 for (i = 0; i < n_ops; i++) {
1214 struct op_auxdata *aux = &auxes[i];
1215 struct nl_transaction *txn = &auxes[i].txn;
1216 struct dpif_op *op = ops[i];
1217 struct dpif_flow_put *put;
1218 struct dpif_flow_del *del;
1220 op->error = txn->error;
1223 case DPIF_OP_FLOW_PUT:
1224 put = &op->u.flow_put;
1227 struct dpif_linux_flow reply;
1229 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1232 dpif_linux_flow_get_stats(&reply, put->stats);
1237 memset(put->stats, 0, sizeof *put->stats);
1242 case DPIF_OP_FLOW_DEL:
1243 del = &op->u.flow_del;
1246 struct dpif_linux_flow reply;
1248 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1251 dpif_linux_flow_get_stats(&reply, del->stats);
1256 memset(del->stats, 0, sizeof *del->stats);
1261 case DPIF_OP_EXECUTE:
1268 ofpbuf_uninit(&aux->request);
1269 ofpbuf_uninit(&aux->reply);
1274 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1277 size_t chunk = MIN(n_ops, MAX_OPS);
1278 dpif_linux_operate__(dpif, ops, chunk);
1284 /* Synchronizes 'dpif->channels' with the set of vports currently in 'dpif' in
1285 * the kernel, by adding a new channel for any kernel vport that lacks one and
1286 * deleting any channels that have no backing kernel vports. */
1288 dpif_linux_refresh_channels(struct dpif *dpif_)
1290 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1291 unsigned long int *keep_channels;
1292 struct dpif_linux_vport vport;
1293 size_t keep_channels_nbits;
1294 struct nl_dump dump;
1298 /* To start with, we need an epoll fd. */
1299 if (dpif->epoll_fd < 0) {
1300 dpif->epoll_fd = epoll_create(10);
1301 if (dpif->epoll_fd < 0) {
1306 keep_channels_nbits = dpif->uc_array_size;
1307 keep_channels = bitmap_allocate(keep_channels_nbits);
1309 dpif->n_events = dpif->event_offset = 0;
1311 dpif_linux_port_dump_start__(dpif_, &dump);
1312 while (!dpif_linux_port_dump_next__(dpif_, &dump, &vport)) {
1313 uint32_t port_no = odp_to_u32(vport.port_no);
1314 struct nl_sock *sock = (port_no < dpif->uc_array_size
1315 ? dpif->channels[port_no].sock
1317 bool new_sock = !sock;
1321 error = nl_sock_create(NETLINK_GENERIC, &sock);
1328 /* Configure the vport to deliver misses to 'sock'. */
1329 if (!vport.upcall_pid || *vport.upcall_pid != nl_sock_pid(sock)) {
1330 uint32_t upcall_pid = nl_sock_pid(sock);
1331 struct dpif_linux_vport vport_request;
1333 dpif_linux_vport_init(&vport_request);
1334 vport_request.cmd = OVS_VPORT_CMD_SET;
1335 vport_request.dp_ifindex = dpif->dp_ifindex;
1336 vport_request.port_no = vport.port_no;
1337 vport_request.upcall_pid = &upcall_pid;
1338 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1340 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1341 dpif_name(&dpif->dpif), vport_request.port_no,
1344 VLOG_WARN_RL(&error_rl,
1345 "%s: failed to set upcall pid on port: %s",
1346 dpif_name(&dpif->dpif), ovs_strerror(error));
1348 if (error != ENODEV && error != ENOENT) {
1351 /* The vport isn't really there, even though the dump says
1352 * it is. Probably we just hit a race after a port
1360 error = add_channel(dpif, vport.port_no, sock);
1362 VLOG_INFO("%s: could not add channel for port %s",
1363 dpif_name(dpif_), vport.name);
1369 if (port_no < keep_channels_nbits) {
1370 bitmap_set1(keep_channels, port_no);
1375 nl_sock_destroy(sock);
1377 nl_dump_done(&dump);
1379 /* Discard any saved channels that we didn't reuse. */
1380 for (i = 0; i < keep_channels_nbits; i++) {
1381 if (!bitmap_is_set(keep_channels, i)) {
1382 nl_sock_destroy(dpif->channels[i].sock);
1383 dpif->channels[i].sock = NULL;
1386 free(keep_channels);
1392 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1394 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1396 if ((dpif->epoll_fd >= 0) == enable) {
1398 } else if (!enable) {
1399 destroy_channels(dpif);
1402 return dpif_linux_refresh_channels(dpif_);
1407 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1409 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1412 ovs_mutex_lock(&dpif->upcall_lock);
1413 error = dpif_linux_recv_set__(dpif_, enable);
1414 ovs_mutex_unlock(&dpif->upcall_lock);
1420 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1421 uint32_t queue_id, uint32_t *priority)
1423 if (queue_id < 0xf000) {
1424 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1432 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1435 static const struct nl_policy ovs_packet_policy[] = {
1436 /* Always present. */
1437 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1438 .min_len = ETH_HEADER_LEN },
1439 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1441 /* OVS_PACKET_CMD_ACTION only. */
1442 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1445 struct ovs_header *ovs_header;
1446 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1447 struct nlmsghdr *nlmsg;
1448 struct genlmsghdr *genl;
1452 ofpbuf_use_const(&b, buf->data, buf->size);
1454 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1455 genl = ofpbuf_try_pull(&b, sizeof *genl);
1456 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1457 if (!nlmsg || !genl || !ovs_header
1458 || nlmsg->nlmsg_type != ovs_packet_family
1459 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1460 ARRAY_SIZE(ovs_packet_policy))) {
1464 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1465 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1471 /* (Re)set ALL fields of '*upcall' on successful return. */
1472 upcall->type = type;
1473 upcall->key = CONST_CAST(struct nlattr *,
1474 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1475 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1476 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1478 /* Allow overwriting the netlink attribute header without reallocating. */
1479 ofpbuf_use_stub(&upcall->packet,
1480 CONST_CAST(struct nlattr *,
1481 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1482 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1483 sizeof(struct nlattr));
1484 upcall->packet.data = (char *)upcall->packet.data + sizeof(struct nlattr);
1485 upcall->packet.size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1487 *dp_ifindex = ovs_header->dp_ifindex;
1493 dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
1496 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1499 if (dpif->epoll_fd < 0) {
1503 if (dpif->event_offset >= dpif->n_events) {
1506 dpif->event_offset = dpif->n_events = 0;
1509 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1510 dpif->uc_array_size, 0);
1511 } while (retval < 0 && errno == EINTR);
1513 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1514 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1515 } else if (retval > 0) {
1516 dpif->n_events = retval;
1520 while (dpif->event_offset < dpif->n_events) {
1521 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1522 struct dpif_channel *ch = &dpif->channels[idx];
1524 dpif->event_offset++;
1530 if (++read_tries > 50) {
1534 error = nl_sock_recv(ch->sock, buf, false);
1535 if (error == ENOBUFS) {
1536 /* ENOBUFS typically means that we've received so many
1537 * packets that the buffer overflowed. Try again
1538 * immediately because there's almost certainly a packet
1539 * waiting for us. */
1540 report_loss(dpif_, ch);
1544 ch->last_poll = time_msec();
1546 if (error == EAGAIN) {
1552 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1553 if (!error && dp_ifindex == dpif->dp_ifindex) {
1565 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1568 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1571 ovs_mutex_lock(&dpif->upcall_lock);
1572 error = dpif_linux_recv__(dpif_, upcall, buf);
1573 ovs_mutex_unlock(&dpif->upcall_lock);
1579 dpif_linux_recv_wait(struct dpif *dpif_)
1581 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1583 ovs_mutex_lock(&dpif->upcall_lock);
1584 if (dpif->epoll_fd >= 0) {
1585 poll_fd_wait(dpif->epoll_fd, POLLIN);
1587 ovs_mutex_unlock(&dpif->upcall_lock);
1591 dpif_linux_recv_purge(struct dpif *dpif_)
1593 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1595 ovs_mutex_lock(&dpif->upcall_lock);
1596 if (dpif->epoll_fd >= 0) {
1597 struct dpif_channel *ch;
1599 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
1602 nl_sock_drain(ch->sock);
1606 ovs_mutex_unlock(&dpif->upcall_lock);
1609 const struct dpif_class dpif_linux_class = {
1611 dpif_linux_enumerate,
1618 dpif_linux_get_stats,
1619 dpif_linux_port_add,
1620 dpif_linux_port_del,
1621 dpif_linux_port_query_by_number,
1622 dpif_linux_port_query_by_name,
1623 dpif_linux_get_max_ports,
1624 dpif_linux_port_get_pid,
1625 dpif_linux_port_dump_start,
1626 dpif_linux_port_dump_next,
1627 dpif_linux_port_dump_done,
1628 dpif_linux_port_poll,
1629 dpif_linux_port_poll_wait,
1630 dpif_linux_flow_get,
1631 dpif_linux_flow_put,
1632 dpif_linux_flow_del,
1633 dpif_linux_flow_flush,
1634 dpif_linux_flow_dump_start,
1635 dpif_linux_flow_dump_next,
1636 dpif_linux_flow_dump_done,
1639 dpif_linux_recv_set,
1640 dpif_linux_queue_to_priority,
1642 dpif_linux_recv_wait,
1643 dpif_linux_recv_purge,
1647 dpif_linux_init(void)
1649 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1652 if (ovsthread_once_start(&once)) {
1653 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1654 &ovs_datapath_family);
1656 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1657 "The Open vSwitch kernel module is probably not loaded.",
1658 OVS_DATAPATH_FAMILY);
1661 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1664 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1667 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1668 &ovs_packet_family);
1671 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1672 &ovs_vport_mcgroup);
1675 ovsthread_once_done(&once);
1682 dpif_linux_is_internal_device(const char *name)
1684 struct dpif_linux_vport reply;
1688 error = dpif_linux_vport_get(name, &reply, &buf);
1691 } else if (error != ENODEV && error != ENOENT) {
1692 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1693 name, ovs_strerror(error));
1696 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1699 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1700 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1701 * positive errno value.
1703 * 'vport' will contain pointers into 'buf', so the caller should not free
1704 * 'buf' while 'vport' is still in use. */
1706 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1707 const struct ofpbuf *buf)
1709 static const struct nl_policy ovs_vport_policy[] = {
1710 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1711 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1712 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1713 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1714 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1716 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1719 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1720 struct ovs_header *ovs_header;
1721 struct nlmsghdr *nlmsg;
1722 struct genlmsghdr *genl;
1725 dpif_linux_vport_init(vport);
1727 ofpbuf_use_const(&b, buf->data, buf->size);
1728 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1729 genl = ofpbuf_try_pull(&b, sizeof *genl);
1730 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1731 if (!nlmsg || !genl || !ovs_header
1732 || nlmsg->nlmsg_type != ovs_vport_family
1733 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1734 ARRAY_SIZE(ovs_vport_policy))) {
1738 vport->cmd = genl->cmd;
1739 vport->dp_ifindex = ovs_header->dp_ifindex;
1740 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1741 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1742 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1743 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1744 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1746 if (a[OVS_VPORT_ATTR_STATS]) {
1747 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1749 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1750 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1751 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1756 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1757 * followed by Netlink attributes corresponding to 'vport'. */
1759 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1762 struct ovs_header *ovs_header;
1764 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1765 vport->cmd, OVS_VPORT_VERSION);
1767 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1768 ovs_header->dp_ifindex = vport->dp_ifindex;
1770 if (vport->port_no != ODPP_NONE) {
1771 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1774 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1775 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1779 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1782 if (vport->upcall_pid) {
1783 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1787 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1788 vport->stats, sizeof *vport->stats);
1791 if (vport->options) {
1792 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1793 vport->options, vport->options_len);
1797 /* Clears 'vport' to "empty" values. */
1799 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1801 memset(vport, 0, sizeof *vport);
1802 vport->port_no = ODPP_NONE;
1805 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1806 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1807 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1808 * result of the command is expected to be an ovs_vport also, which is decoded
1809 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1810 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1812 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1813 struct dpif_linux_vport *reply,
1814 struct ofpbuf **bufp)
1816 struct ofpbuf *request_buf;
1819 ovs_assert((reply != NULL) == (bufp != NULL));
1821 error = dpif_linux_init();
1825 dpif_linux_vport_init(reply);
1830 request_buf = ofpbuf_new(1024);
1831 dpif_linux_vport_to_ofpbuf(request, request_buf);
1832 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1833 ofpbuf_delete(request_buf);
1837 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1840 dpif_linux_vport_init(reply);
1841 ofpbuf_delete(*bufp);
1848 /* Obtains information about the kernel vport named 'name' and stores it into
1849 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1850 * longer needed ('reply' will contain pointers into '*bufp'). */
1852 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1853 struct ofpbuf **bufp)
1855 struct dpif_linux_vport request;
1857 dpif_linux_vport_init(&request);
1858 request.cmd = OVS_VPORT_CMD_GET;
1859 request.name = name;
1861 return dpif_linux_vport_transact(&request, reply, bufp);
1864 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1865 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1866 * positive errno value.
1868 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1869 * while 'dp' is still in use. */
1871 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1873 static const struct nl_policy ovs_datapath_policy[] = {
1874 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1875 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1877 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
1878 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
1882 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1883 struct ovs_header *ovs_header;
1884 struct nlmsghdr *nlmsg;
1885 struct genlmsghdr *genl;
1888 dpif_linux_dp_init(dp);
1890 ofpbuf_use_const(&b, buf->data, buf->size);
1891 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1892 genl = ofpbuf_try_pull(&b, sizeof *genl);
1893 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1894 if (!nlmsg || !genl || !ovs_header
1895 || nlmsg->nlmsg_type != ovs_datapath_family
1896 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1897 ARRAY_SIZE(ovs_datapath_policy))) {
1901 dp->cmd = genl->cmd;
1902 dp->dp_ifindex = ovs_header->dp_ifindex;
1903 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1904 if (a[OVS_DP_ATTR_STATS]) {
1905 /* Can't use structure assignment because Netlink doesn't ensure
1906 * sufficient alignment for 64-bit members. */
1907 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1911 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
1912 /* Can't use structure assignment because Netlink doesn't ensure
1913 * sufficient alignment for 64-bit members. */
1914 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
1915 sizeof dp->megaflow_stats);
1921 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1923 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1925 struct ovs_header *ovs_header;
1927 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1928 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1929 OVS_DATAPATH_VERSION);
1931 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1932 ovs_header->dp_ifindex = dp->dp_ifindex;
1935 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1938 if (dp->upcall_pid) {
1939 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1942 if (dp->user_features) {
1943 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
1946 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1949 /* Clears 'dp' to "empty" values. */
1951 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1953 memset(dp, 0, sizeof *dp);
1954 dp->megaflow_stats.n_masks = UINT32_MAX;
1955 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
1959 dpif_linux_dp_dump_start(struct nl_dump *dump)
1961 struct dpif_linux_dp request;
1964 dpif_linux_dp_init(&request);
1965 request.cmd = OVS_DP_CMD_GET;
1967 buf = ofpbuf_new(1024);
1968 dpif_linux_dp_to_ofpbuf(&request, buf);
1969 nl_dump_start(dump, NETLINK_GENERIC, buf);
1973 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1974 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1975 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1976 * result of the command is expected to be of the same form, which is decoded
1977 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1978 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1980 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1981 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1983 struct ofpbuf *request_buf;
1986 ovs_assert((reply != NULL) == (bufp != NULL));
1988 request_buf = ofpbuf_new(1024);
1989 dpif_linux_dp_to_ofpbuf(request, request_buf);
1990 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1991 ofpbuf_delete(request_buf);
1994 dpif_linux_dp_init(reply);
1996 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1999 ofpbuf_delete(*bufp);
2006 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2007 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2008 * will contain pointers into '*bufp'). */
2010 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2011 struct ofpbuf **bufp)
2013 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2014 struct dpif_linux_dp request;
2016 dpif_linux_dp_init(&request);
2017 request.cmd = OVS_DP_CMD_GET;
2018 request.dp_ifindex = dpif->dp_ifindex;
2020 return dpif_linux_dp_transact(&request, reply, bufp);
2023 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2024 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2025 * positive errno value.
2027 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2028 * while 'flow' is still in use. */
2030 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2031 const struct ofpbuf *buf)
2033 static const struct nl_policy ovs_flow_policy[] = {
2034 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2035 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2036 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2037 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2039 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2040 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2041 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2044 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2045 struct ovs_header *ovs_header;
2046 struct nlmsghdr *nlmsg;
2047 struct genlmsghdr *genl;
2050 dpif_linux_flow_init(flow);
2052 ofpbuf_use_const(&b, buf->data, buf->size);
2053 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2054 genl = ofpbuf_try_pull(&b, sizeof *genl);
2055 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2056 if (!nlmsg || !genl || !ovs_header
2057 || nlmsg->nlmsg_type != ovs_flow_family
2058 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2059 ARRAY_SIZE(ovs_flow_policy))) {
2063 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2064 flow->dp_ifindex = ovs_header->dp_ifindex;
2065 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2066 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2068 if (a[OVS_FLOW_ATTR_MASK]) {
2069 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2070 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2072 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2073 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2074 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2076 if (a[OVS_FLOW_ATTR_STATS]) {
2077 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2079 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2080 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2082 if (a[OVS_FLOW_ATTR_USED]) {
2083 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2088 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2089 * followed by Netlink attributes corresponding to 'flow'. */
2091 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2094 struct ovs_header *ovs_header;
2096 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2097 NLM_F_REQUEST | flow->nlmsg_flags,
2098 flow->cmd, OVS_FLOW_VERSION);
2100 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2101 ovs_header->dp_ifindex = flow->dp_ifindex;
2103 if (flow->key_len) {
2104 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2107 if (flow->mask_len) {
2108 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2111 if (flow->actions || flow->actions_len) {
2112 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2113 flow->actions, flow->actions_len);
2116 /* We never need to send these to the kernel. */
2117 ovs_assert(!flow->stats);
2118 ovs_assert(!flow->tcp_flags);
2119 ovs_assert(!flow->used);
2122 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2126 /* Clears 'flow' to "empty" values. */
2128 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2130 memset(flow, 0, sizeof *flow);
2133 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2134 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2135 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2136 * result of the command is expected to be a flow also, which is decoded and
2137 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2138 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2140 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2141 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2143 struct ofpbuf *request_buf;
2146 ovs_assert((reply != NULL) == (bufp != NULL));
2149 request->nlmsg_flags |= NLM_F_ECHO;
2152 request_buf = ofpbuf_new(1024);
2153 dpif_linux_flow_to_ofpbuf(request, request_buf);
2154 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2155 ofpbuf_delete(request_buf);
2159 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2162 dpif_linux_flow_init(reply);
2163 ofpbuf_delete(*bufp);
2171 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2172 struct dpif_flow_stats *stats)
2175 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2176 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2178 stats->n_packets = 0;
2181 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2182 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2185 /* Logs information about a packet that was recently lost in 'ch' (in
2188 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2190 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2191 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2194 if (VLOG_DROP_WARN(&rl)) {
2199 if (ch->last_poll != LLONG_MIN) {
2200 ds_put_format(&s, " (last polled %lld ms ago)",
2201 time_msec() - ch->last_poll);
2204 VLOG_WARN("%s: lost packet on channel %"PRIdPTR"%s",
2205 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));