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>
31 #include <sys/epoll.h>
36 #include "dpif-provider.h"
37 #include "dynamic-string.h"
40 #include "netdev-linux.h"
41 #include "netdev-vport.h"
42 #include "netlink-notifier.h"
43 #include "netlink-socket.h"
48 #include "poll-loop.h"
53 #include "unaligned.h"
57 VLOG_DEFINE_THIS_MODULE(dpif_linux);
58 enum { MAX_PORTS = USHRT_MAX };
60 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
61 * missing if we have old headers. */
62 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
64 struct dpif_linux_dp {
65 /* Generic Netlink header. */
68 /* struct ovs_header. */
72 const char *name; /* OVS_DP_ATTR_NAME. */
73 const uint32_t *upcall_pid; /* OVS_DP_ATTR_UPCALL_PID. */
74 uint32_t user_features; /* OVS_DP_ATTR_USER_FEATURES */
75 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
76 struct ovs_dp_megaflow_stats megaflow_stats;
77 /* OVS_DP_ATTR_MEGAFLOW_STATS.*/
80 static void dpif_linux_dp_init(struct dpif_linux_dp *);
81 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
82 const struct ofpbuf *);
83 static void dpif_linux_dp_dump_start(struct nl_dump *);
84 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
85 struct dpif_linux_dp *reply,
86 struct ofpbuf **bufp);
87 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
88 struct ofpbuf **bufp);
90 struct dpif_linux_flow {
91 /* Generic Netlink header. */
94 /* struct ovs_header. */
95 unsigned int nlmsg_flags;
100 * The 'stats' member points to 64-bit data that might only be aligned on
101 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
104 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
105 * the Netlink version of the command, even if actions_len is zero. */
106 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
108 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
110 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
112 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
113 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
114 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
115 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
118 static void dpif_linux_flow_init(struct dpif_linux_flow *);
119 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
120 const struct ofpbuf *);
121 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
123 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
124 struct dpif_linux_flow *reply,
125 struct ofpbuf **bufp);
126 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
127 struct dpif_flow_stats *);
129 /* One of the dpif channels between the kernel and userspace. */
130 struct dpif_channel {
131 struct nl_sock *sock; /* Netlink socket. */
132 long long int last_poll; /* Last time this channel was polled. */
135 static void report_loss(struct dpif *, struct dpif_channel *);
137 /* Datapath interface for the openvswitch Linux kernel module. */
142 /* Upcall messages. */
143 struct ovs_mutex upcall_lock;
144 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
145 struct dpif_channel *channels;
146 struct epoll_event *epoll_events;
147 int epoll_fd; /* epoll fd that includes channel socks. */
148 int n_events; /* Num events returned by epoll_wait(). */
149 int event_offset; /* Offset into 'epoll_events'. */
151 /* Change notification. */
152 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
153 bool refresh_channels;
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 *,
175 static int dpif_linux_refresh_channels(struct dpif *);
177 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
179 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
180 const struct ofpbuf *);
182 static struct dpif_linux *
183 dpif_linux_cast(const struct dpif *dpif)
185 dpif_assert_class(dpif, &dpif_linux_class);
186 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
190 dpif_linux_enumerate(struct sset *all_dps)
196 error = dpif_linux_init();
201 dpif_linux_dp_dump_start(&dump);
202 while (nl_dump_next(&dump, &msg)) {
203 struct dpif_linux_dp dp;
205 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
206 sset_add(all_dps, dp.name);
209 return nl_dump_done(&dump);
213 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
214 bool create, struct dpif **dpifp)
216 struct dpif_linux_dp dp_request, dp;
221 error = dpif_linux_init();
226 /* Create or look up datapath. */
227 dpif_linux_dp_init(&dp_request);
229 dp_request.cmd = OVS_DP_CMD_NEW;
231 dp_request.upcall_pid = &upcall_pid;
233 /* Use OVS_DP_CMD_SET to report user features */
234 dp_request.cmd = OVS_DP_CMD_SET;
236 dp_request.name = name;
237 dp_request.user_features |= OVS_DP_F_UNALIGNED;
238 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
243 error = open_dpif(&dp, dpifp);
249 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
251 struct dpif_linux *dpif;
253 dpif = xzalloc(sizeof *dpif);
254 dpif->port_notifier = NULL;
255 ovs_mutex_init(&dpif->upcall_lock);
258 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
259 dp->dp_ifindex, dp->dp_ifindex);
261 dpif->dp_ifindex = dp->dp_ifindex;
262 *dpifp = &dpif->dpif;
268 destroy_channels(struct dpif_linux *dpif)
272 if (dpif->epoll_fd < 0) {
276 for (i = 0; i < dpif->uc_array_size; i++ ) {
277 struct dpif_linux_vport vport_request;
278 struct dpif_channel *ch = &dpif->channels[i];
279 uint32_t upcall_pid = 0;
285 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
287 /* Turn off upcalls. */
288 dpif_linux_vport_init(&vport_request);
289 vport_request.cmd = OVS_VPORT_CMD_SET;
290 vport_request.dp_ifindex = dpif->dp_ifindex;
291 vport_request.port_no = u32_to_odp(i);
292 vport_request.upcall_pid = &upcall_pid;
293 dpif_linux_vport_transact(&vport_request, NULL, NULL);
295 nl_sock_destroy(ch->sock);
298 free(dpif->channels);
299 dpif->channels = NULL;
300 dpif->uc_array_size = 0;
302 free(dpif->epoll_events);
303 dpif->epoll_events = NULL;
304 dpif->n_events = dpif->event_offset = 0;
306 /* Don't close dpif->epoll_fd since that would cause other threads that
307 * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */
311 add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
313 struct epoll_event event;
314 uint32_t port_idx = odp_to_u32(port_no);
316 if (dpif->epoll_fd < 0) {
320 /* We assume that the datapath densely chooses port numbers, which
321 * can therefore be used as an index into an array of channels. */
322 if (port_idx >= dpif->uc_array_size) {
323 uint32_t new_size = port_idx + 1;
326 if (new_size > MAX_PORTS) {
327 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
328 dpif_name(&dpif->dpif), port_no);
332 dpif->channels = xrealloc(dpif->channels,
333 new_size * sizeof *dpif->channels);
334 for (i = dpif->uc_array_size; i < new_size; i++) {
335 dpif->channels[i].sock = NULL;
338 dpif->epoll_events = xrealloc(dpif->epoll_events,
339 new_size * sizeof *dpif->epoll_events);
340 dpif->uc_array_size = new_size;
343 memset(&event, 0, sizeof event);
344 event.events = EPOLLIN;
345 event.data.u32 = port_idx;
346 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
351 nl_sock_destroy(dpif->channels[port_idx].sock);
352 dpif->channels[port_idx].sock = sock;
353 dpif->channels[port_idx].last_poll = LLONG_MIN;
359 del_channel(struct dpif_linux *dpif, odp_port_t port_no)
361 struct dpif_channel *ch;
362 uint32_t port_idx = odp_to_u32(port_no);
364 if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
368 ch = &dpif->channels[port_idx];
373 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
374 dpif->event_offset = dpif->n_events = 0;
376 nl_sock_destroy(ch->sock);
381 dpif_linux_close(struct dpif *dpif_)
383 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
385 nl_sock_destroy(dpif->port_notifier);
386 destroy_channels(dpif);
387 if (dpif->epoll_fd >= 0) {
388 close(dpif->epoll_fd);
390 ovs_mutex_destroy(&dpif->upcall_lock);
395 dpif_linux_destroy(struct dpif *dpif_)
397 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
398 struct dpif_linux_dp dp;
400 dpif_linux_dp_init(&dp);
401 dp.cmd = OVS_DP_CMD_DEL;
402 dp.dp_ifindex = dpif->dp_ifindex;
403 return dpif_linux_dp_transact(&dp, NULL, NULL);
407 dpif_linux_run(struct dpif *dpif_)
409 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
410 if (dpif->refresh_channels) {
411 dpif->refresh_channels = false;
412 dpif_linux_refresh_channels(dpif_);
417 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
419 struct dpif_linux_dp dp;
423 error = dpif_linux_dp_get(dpif_, &dp, &buf);
425 stats->n_hit = dp.stats.n_hit;
426 stats->n_missed = dp.stats.n_missed;
427 stats->n_lost = dp.stats.n_lost;
428 stats->n_flows = dp.stats.n_flows;
429 stats->n_masks = dp.megaflow_stats.n_masks;
430 stats->n_mask_hit = dp.megaflow_stats.n_mask_hit;
437 get_vport_type(const struct dpif_linux_vport *vport)
439 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
441 switch (vport->type) {
442 case OVS_VPORT_TYPE_NETDEV: {
443 const char *type = netdev_get_type_from_name(vport->name);
445 return type ? type : "system";
448 case OVS_VPORT_TYPE_INTERNAL:
451 case OVS_VPORT_TYPE_GRE:
454 case OVS_VPORT_TYPE_GRE64:
457 case OVS_VPORT_TYPE_VXLAN:
460 case OVS_VPORT_TYPE_LISP:
463 case OVS_VPORT_TYPE_UNSPEC:
464 case __OVS_VPORT_TYPE_MAX:
468 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
469 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
473 static enum ovs_vport_type
474 netdev_to_ovs_vport_type(const struct netdev *netdev)
476 const char *type = netdev_get_type(netdev);
478 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
479 return OVS_VPORT_TYPE_NETDEV;
480 } else if (!strcmp(type, "internal")) {
481 return OVS_VPORT_TYPE_INTERNAL;
482 } else if (strstr(type, "gre64")) {
483 return OVS_VPORT_TYPE_GRE64;
484 } else if (strstr(type, "gre")) {
485 return OVS_VPORT_TYPE_GRE;
486 } else if (!strcmp(type, "vxlan")) {
487 return OVS_VPORT_TYPE_VXLAN;
488 } else if (!strcmp(type, "lisp")) {
489 return OVS_VPORT_TYPE_LISP;
491 return OVS_VPORT_TYPE_UNSPEC;
496 dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
497 odp_port_t *port_nop)
499 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
500 const struct netdev_tunnel_config *tnl_cfg;
501 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
502 const char *name = netdev_vport_get_dpif_port(netdev,
503 namebuf, sizeof namebuf);
504 const char *type = netdev_get_type(netdev);
505 struct dpif_linux_vport request, reply;
506 struct nl_sock *sock = NULL;
509 uint64_t options_stub[64 / 8];
510 struct ofpbuf options;
513 if (dpif->epoll_fd >= 0) {
514 error = nl_sock_create(NETLINK_GENERIC, &sock);
520 dpif_linux_vport_init(&request);
521 request.cmd = OVS_VPORT_CMD_NEW;
522 request.dp_ifindex = dpif->dp_ifindex;
523 request.type = netdev_to_ovs_vport_type(netdev);
524 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
525 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
526 "unsupported type `%s'",
527 dpif_name(dpif_), name, type);
528 nl_sock_destroy(sock);
533 if (request.type == OVS_VPORT_TYPE_NETDEV) {
534 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
537 tnl_cfg = netdev_get_tunnel_config(netdev);
538 if (tnl_cfg && tnl_cfg->dst_port != 0) {
539 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
540 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
541 ntohs(tnl_cfg->dst_port));
542 request.options = options.data;
543 request.options_len = options.size;
546 request.port_no = *port_nop;
547 upcall_pid = sock ? nl_sock_pid(sock) : 0;
548 request.upcall_pid = &upcall_pid;
550 error = dpif_linux_vport_transact(&request, &reply, &buf);
552 *port_nop = reply.port_no;
553 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
554 dpif_name(dpif_), reply.port_no, upcall_pid);
556 if (error == EBUSY && *port_nop != ODPP_NONE) {
557 VLOG_INFO("%s: requested port %"PRIu32" is in use",
558 dpif_name(dpif_), *port_nop);
560 nl_sock_destroy(sock);
567 error = add_channel(dpif, *port_nop, sock);
569 VLOG_INFO("%s: could not add channel for port %s",
570 dpif_name(dpif_), name);
572 /* Delete the port. */
573 dpif_linux_vport_init(&request);
574 request.cmd = OVS_VPORT_CMD_DEL;
575 request.dp_ifindex = dpif->dp_ifindex;
576 request.port_no = *port_nop;
577 dpif_linux_vport_transact(&request, NULL, NULL);
579 nl_sock_destroy(sock);
588 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
589 odp_port_t *port_nop)
591 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
594 ovs_mutex_lock(&dpif->upcall_lock);
595 error = dpif_linux_port_add__(dpif_, netdev, port_nop);
596 ovs_mutex_unlock(&dpif->upcall_lock);
602 dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
604 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
605 struct dpif_linux_vport vport;
608 dpif_linux_vport_init(&vport);
609 vport.cmd = OVS_VPORT_CMD_DEL;
610 vport.dp_ifindex = dpif->dp_ifindex;
611 vport.port_no = port_no;
612 error = dpif_linux_vport_transact(&vport, NULL, NULL);
614 del_channel(dpif, port_no);
620 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
622 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
625 ovs_mutex_lock(&dpif->upcall_lock);
626 error = dpif_linux_port_del__(dpif_, port_no);
627 ovs_mutex_unlock(&dpif->upcall_lock);
633 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
634 const char *port_name, struct dpif_port *dpif_port)
636 struct dpif_linux_vport request;
637 struct dpif_linux_vport reply;
641 dpif_linux_vport_init(&request);
642 request.cmd = OVS_VPORT_CMD_GET;
643 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
644 request.port_no = port_no;
645 request.name = port_name;
647 error = dpif_linux_vport_transact(&request, &reply, &buf);
649 if (reply.dp_ifindex != request.dp_ifindex) {
650 /* A query by name reported that 'port_name' is in some datapath
651 * other than 'dpif', but the caller wants to know about 'dpif'. */
653 } else if (dpif_port) {
654 dpif_port->name = xstrdup(reply.name);
655 dpif_port->type = xstrdup(get_vport_type(&reply));
656 dpif_port->port_no = reply.port_no;
664 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
665 struct dpif_port *dpif_port)
667 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
671 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
672 struct dpif_port *dpif_port)
674 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
678 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
680 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
681 uint32_t port_idx = odp_to_u32(port_no);
684 ovs_mutex_lock(&dpif->upcall_lock);
685 if (dpif->epoll_fd >= 0) {
686 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
687 * channel, since it is not heavily loaded. */
688 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
689 const struct nl_sock *sock = dpif->channels[idx].sock;
690 pid = sock ? nl_sock_pid(sock) : 0;
692 ovs_mutex_unlock(&dpif->upcall_lock);
698 dpif_linux_flow_flush(struct dpif *dpif_)
700 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
701 struct dpif_linux_flow flow;
703 dpif_linux_flow_init(&flow);
704 flow.cmd = OVS_FLOW_CMD_DEL;
705 flow.dp_ifindex = dpif->dp_ifindex;
706 return dpif_linux_flow_transact(&flow, NULL, NULL);
709 struct dpif_linux_port_state {
714 dpif_linux_port_dump_start__(const struct dpif *dpif_, struct nl_dump *dump)
716 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
717 struct dpif_linux_vport request;
720 dpif_linux_vport_init(&request);
721 request.cmd = OVS_VPORT_CMD_GET;
722 request.dp_ifindex = dpif->dp_ifindex;
724 buf = ofpbuf_new(1024);
725 dpif_linux_vport_to_ofpbuf(&request, buf);
726 nl_dump_start(dump, NETLINK_GENERIC, buf);
731 dpif_linux_port_dump_start(const struct dpif *dpif, void **statep)
733 struct dpif_linux_port_state *state;
735 *statep = state = xmalloc(sizeof *state);
736 dpif_linux_port_dump_start__(dpif, &state->dump);
742 dpif_linux_port_dump_next__(const struct dpif *dpif_, struct nl_dump *dump,
743 struct dpif_linux_vport *vport)
745 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
749 if (!nl_dump_next(dump, &buf)) {
753 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
755 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
756 dpif_name(&dpif->dpif), ovs_strerror(error));
762 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
763 struct dpif_port *dpif_port)
765 struct dpif_linux_port_state *state = state_;
766 struct dpif_linux_vport vport;
769 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport);
773 dpif_port->name = CONST_CAST(char *, vport.name);
774 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
775 dpif_port->port_no = vport.port_no;
780 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
782 struct dpif_linux_port_state *state = state_;
783 int error = nl_dump_done(&state->dump);
790 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
792 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
794 /* Lazily create the Netlink socket to listen for notifications. */
795 if (!dpif->port_notifier) {
796 struct nl_sock *sock;
799 error = nl_sock_create(NETLINK_GENERIC, &sock);
804 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
806 nl_sock_destroy(sock);
809 dpif->port_notifier = sock;
811 /* We have no idea of the current state so report that everything
817 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
818 uint64_t buf_stub[4096 / 8];
822 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
823 error = nl_sock_recv(dpif->port_notifier, &buf, false);
825 struct dpif_linux_vport vport;
827 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
829 if (vport.dp_ifindex == dpif->dp_ifindex
830 && (vport.cmd == OVS_VPORT_CMD_NEW
831 || vport.cmd == OVS_VPORT_CMD_DEL
832 || vport.cmd == OVS_VPORT_CMD_SET)) {
833 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
834 dpif->dpif.full_name, vport.name, vport.cmd);
835 if (vport.cmd == OVS_VPORT_CMD_DEL) {
836 dpif->refresh_channels = true;
838 *devnamep = xstrdup(vport.name);
843 } else if (error != EAGAIN) {
844 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
845 ovs_strerror(error));
846 nl_sock_drain(dpif->port_notifier);
858 dpif_linux_port_poll_wait(const struct dpif *dpif_)
860 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
862 if (dpif->port_notifier) {
863 nl_sock_wait(dpif->port_notifier, POLLIN);
865 poll_immediate_wake();
870 dpif_linux_flow_get__(const struct dpif *dpif_,
871 const struct nlattr *key, size_t key_len,
872 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
874 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
875 struct dpif_linux_flow request;
877 dpif_linux_flow_init(&request);
878 request.cmd = OVS_FLOW_CMD_GET;
879 request.dp_ifindex = dpif->dp_ifindex;
881 request.key_len = key_len;
882 return dpif_linux_flow_transact(&request, reply, bufp);
886 dpif_linux_flow_get(const struct dpif *dpif_,
887 const struct nlattr *key, size_t key_len,
888 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
890 struct dpif_linux_flow reply;
894 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
897 dpif_linux_flow_get_stats(&reply, stats);
900 buf->data = CONST_CAST(struct nlattr *, reply.actions);
901 buf->size = reply.actions_len;
911 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
912 struct dpif_linux_flow *request)
914 static const struct nlattr dummy_action;
916 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
918 dpif_linux_flow_init(request);
919 request->cmd = (put->flags & DPIF_FP_CREATE
920 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
921 request->dp_ifindex = dpif->dp_ifindex;
922 request->key = put->key;
923 request->key_len = put->key_len;
924 request->mask = put->mask;
925 request->mask_len = put->mask_len;
926 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
927 request->actions = (put->actions
929 : CONST_CAST(struct nlattr *, &dummy_action));
930 request->actions_len = put->actions_len;
931 if (put->flags & DPIF_FP_ZERO_STATS) {
932 request->clear = true;
934 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
938 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
940 struct dpif_linux_flow request, reply;
944 dpif_linux_init_flow_put(dpif_, put, &request);
945 error = dpif_linux_flow_transact(&request,
946 put->stats ? &reply : NULL,
947 put->stats ? &buf : NULL);
948 if (!error && put->stats) {
949 dpif_linux_flow_get_stats(&reply, put->stats);
956 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
957 struct dpif_linux_flow *request)
959 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
961 dpif_linux_flow_init(request);
962 request->cmd = OVS_FLOW_CMD_DEL;
963 request->dp_ifindex = dpif->dp_ifindex;
964 request->key = del->key;
965 request->key_len = del->key_len;
969 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
971 struct dpif_linux_flow request, reply;
975 dpif_linux_init_flow_del(dpif_, del, &request);
976 error = dpif_linux_flow_transact(&request,
977 del->stats ? &reply : NULL,
978 del->stats ? &buf : NULL);
979 if (!error && del->stats) {
980 dpif_linux_flow_get_stats(&reply, del->stats);
986 struct dpif_linux_flow_state {
988 struct dpif_linux_flow flow;
989 struct dpif_flow_stats stats;
994 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
996 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
997 struct dpif_linux_flow_state *state;
998 struct dpif_linux_flow request;
1001 *statep = state = xmalloc(sizeof *state);
1003 dpif_linux_flow_init(&request);
1004 request.cmd = OVS_FLOW_CMD_GET;
1005 request.dp_ifindex = dpif->dp_ifindex;
1007 buf = ofpbuf_new(1024);
1008 dpif_linux_flow_to_ofpbuf(&request, buf);
1009 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
1018 dpif_linux_flow_dump_next(const struct dpif *dpif_, void *state_,
1019 const struct nlattr **key, size_t *key_len,
1020 const struct nlattr **mask, size_t *mask_len,
1021 const struct nlattr **actions, size_t *actions_len,
1022 const struct dpif_flow_stats **stats)
1024 struct dpif_linux_flow_state *state = state_;
1029 ofpbuf_delete(state->buf);
1032 if (!nl_dump_next(&state->dump, &buf)) {
1036 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1041 if (actions && !state->flow.actions) {
1042 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1043 state->flow.key_len,
1044 &state->flow, &state->buf);
1045 if (error == ENOENT) {
1046 VLOG_DBG("dumped flow disappeared on get");
1048 VLOG_WARN("error fetching dumped flow: %s",
1049 ovs_strerror(error));
1055 *actions = state->flow.actions;
1056 *actions_len = state->flow.actions_len;
1059 *key = state->flow.key;
1060 *key_len = state->flow.key_len;
1063 *mask = state->flow.mask;
1064 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1067 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1068 *stats = &state->stats;
1074 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1076 struct dpif_linux_flow_state *state = state_;
1077 int error = nl_dump_done(&state->dump);
1078 ofpbuf_delete(state->buf);
1084 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1087 struct ovs_header *k_exec;
1090 ofpbuf_prealloc_tailroom(buf, (64
1091 + d_exec->packet->size
1092 + ODP_KEY_METADATA_SIZE
1093 + d_exec->actions_len));
1095 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1096 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1098 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1099 k_exec->dp_ifindex = dp_ifindex;
1101 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1102 d_exec->packet->data, d_exec->packet->size);
1104 key_ofs = nl_msg_start_nested(buf, OVS_PACKET_ATTR_KEY);
1105 odp_key_from_pkt_metadata(buf, &d_exec->md);
1106 nl_msg_end_nested(buf, key_ofs);
1108 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1109 d_exec->actions, d_exec->actions_len);
1113 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1115 uint64_t request_stub[1024 / 8];
1116 struct ofpbuf request;
1119 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1120 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1121 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1122 ofpbuf_uninit(&request);
1128 dpif_linux_execute(struct dpif *dpif_, struct dpif_execute *execute)
1130 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1132 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1138 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1140 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1143 struct nl_transaction txn;
1145 struct ofpbuf request;
1146 uint64_t request_stub[1024 / 8];
1148 struct ofpbuf reply;
1149 uint64_t reply_stub[1024 / 8];
1152 struct nl_transaction *txnsp[MAX_OPS];
1155 ovs_assert(n_ops <= MAX_OPS);
1156 for (i = 0; i < n_ops; i++) {
1157 struct op_auxdata *aux = &auxes[i];
1158 struct dpif_op *op = ops[i];
1159 struct dpif_flow_put *put;
1160 struct dpif_flow_del *del;
1161 struct dpif_execute *execute;
1162 struct dpif_linux_flow flow;
1164 ofpbuf_use_stub(&aux->request,
1165 aux->request_stub, sizeof aux->request_stub);
1166 aux->txn.request = &aux->request;
1168 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1169 aux->txn.reply = NULL;
1172 case DPIF_OP_FLOW_PUT:
1173 put = &op->u.flow_put;
1174 dpif_linux_init_flow_put(dpif_, put, &flow);
1176 flow.nlmsg_flags |= NLM_F_ECHO;
1177 aux->txn.reply = &aux->reply;
1179 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1182 case DPIF_OP_FLOW_DEL:
1183 del = &op->u.flow_del;
1184 dpif_linux_init_flow_del(dpif_, del, &flow);
1186 flow.nlmsg_flags |= NLM_F_ECHO;
1187 aux->txn.reply = &aux->reply;
1189 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1192 case DPIF_OP_EXECUTE:
1193 execute = &op->u.execute;
1194 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1203 for (i = 0; i < n_ops; i++) {
1204 txnsp[i] = &auxes[i].txn;
1206 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1208 for (i = 0; i < n_ops; i++) {
1209 struct op_auxdata *aux = &auxes[i];
1210 struct nl_transaction *txn = &auxes[i].txn;
1211 struct dpif_op *op = ops[i];
1212 struct dpif_flow_put *put;
1213 struct dpif_flow_del *del;
1215 op->error = txn->error;
1218 case DPIF_OP_FLOW_PUT:
1219 put = &op->u.flow_put;
1222 struct dpif_linux_flow reply;
1224 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1227 dpif_linux_flow_get_stats(&reply, put->stats);
1232 memset(put->stats, 0, sizeof *put->stats);
1237 case DPIF_OP_FLOW_DEL:
1238 del = &op->u.flow_del;
1241 struct dpif_linux_flow reply;
1243 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1246 dpif_linux_flow_get_stats(&reply, del->stats);
1251 memset(del->stats, 0, sizeof *del->stats);
1256 case DPIF_OP_EXECUTE:
1263 ofpbuf_uninit(&aux->request);
1264 ofpbuf_uninit(&aux->reply);
1269 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1272 size_t chunk = MIN(n_ops, MAX_OPS);
1273 dpif_linux_operate__(dpif, ops, chunk);
1279 /* Synchronizes 'dpif->channels' with the set of vports currently in 'dpif' in
1280 * the kernel, by adding a new channel for any kernel vport that lacks one and
1281 * deleting any channels that have no backing kernel vports. */
1283 dpif_linux_refresh_channels(struct dpif *dpif_)
1285 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1286 unsigned long int *keep_channels;
1287 struct dpif_linux_vport vport;
1288 size_t keep_channels_nbits;
1289 struct nl_dump dump;
1293 /* To start with, we need an epoll fd. */
1294 if (dpif->epoll_fd < 0) {
1295 dpif->epoll_fd = epoll_create(10);
1296 if (dpif->epoll_fd < 0) {
1301 keep_channels_nbits = dpif->uc_array_size;
1302 keep_channels = bitmap_allocate(keep_channels_nbits);
1304 dpif->n_events = dpif->event_offset = 0;
1306 dpif_linux_port_dump_start__(dpif_, &dump);
1307 while (!dpif_linux_port_dump_next__(dpif_, &dump, &vport)) {
1308 uint32_t port_no = odp_to_u32(vport.port_no);
1309 struct nl_sock *sock = (port_no < dpif->uc_array_size
1310 ? dpif->channels[port_no].sock
1312 bool new_sock = !sock;
1316 error = nl_sock_create(NETLINK_GENERIC, &sock);
1323 /* Configure the vport to deliver misses to 'sock'. */
1324 if (!vport.upcall_pid || *vport.upcall_pid != nl_sock_pid(sock)) {
1325 uint32_t upcall_pid = nl_sock_pid(sock);
1326 struct dpif_linux_vport vport_request;
1328 dpif_linux_vport_init(&vport_request);
1329 vport_request.cmd = OVS_VPORT_CMD_SET;
1330 vport_request.dp_ifindex = dpif->dp_ifindex;
1331 vport_request.port_no = vport.port_no;
1332 vport_request.upcall_pid = &upcall_pid;
1333 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1335 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1336 dpif_name(&dpif->dpif), vport_request.port_no,
1339 VLOG_WARN_RL(&error_rl,
1340 "%s: failed to set upcall pid on port: %s",
1341 dpif_name(&dpif->dpif), ovs_strerror(error));
1343 if (error != ENODEV && error != ENOENT) {
1346 /* The vport isn't really there, even though the dump says
1347 * it is. Probably we just hit a race after a port
1355 error = add_channel(dpif, vport.port_no, sock);
1357 VLOG_INFO("%s: could not add channel for port %s",
1358 dpif_name(dpif_), vport.name);
1364 if (port_no < keep_channels_nbits) {
1365 bitmap_set1(keep_channels, port_no);
1370 nl_sock_destroy(sock);
1372 nl_dump_done(&dump);
1374 /* Discard any saved channels that we didn't reuse. */
1375 for (i = 0; i < keep_channels_nbits; i++) {
1376 if (!bitmap_is_set(keep_channels, i)) {
1377 nl_sock_destroy(dpif->channels[i].sock);
1378 dpif->channels[i].sock = NULL;
1381 free(keep_channels);
1387 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1389 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1391 if ((dpif->epoll_fd >= 0) == enable) {
1393 } else if (!enable) {
1394 destroy_channels(dpif);
1397 return dpif_linux_refresh_channels(dpif_);
1402 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1404 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1407 ovs_mutex_lock(&dpif->upcall_lock);
1408 error = dpif_linux_recv_set__(dpif_, enable);
1409 ovs_mutex_unlock(&dpif->upcall_lock);
1415 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1416 uint32_t queue_id, uint32_t *priority)
1418 if (queue_id < 0xf000) {
1419 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1427 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1430 static const struct nl_policy ovs_packet_policy[] = {
1431 /* Always present. */
1432 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1433 .min_len = ETH_HEADER_LEN },
1434 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1436 /* OVS_PACKET_CMD_ACTION only. */
1437 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1440 struct ovs_header *ovs_header;
1441 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1442 struct nlmsghdr *nlmsg;
1443 struct genlmsghdr *genl;
1447 ofpbuf_use_const(&b, buf->data, buf->size);
1449 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1450 genl = ofpbuf_try_pull(&b, sizeof *genl);
1451 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1452 if (!nlmsg || !genl || !ovs_header
1453 || nlmsg->nlmsg_type != ovs_packet_family
1454 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1455 ARRAY_SIZE(ovs_packet_policy))) {
1459 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1460 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1466 /* (Re)set ALL fields of '*upcall' on successful return. */
1467 upcall->type = type;
1468 upcall->key = CONST_CAST(struct nlattr *,
1469 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1470 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1471 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1473 /* Allow overwriting the netlink attribute header without reallocating. */
1474 ofpbuf_use_stub(&upcall->packet,
1475 CONST_CAST(struct nlattr *,
1476 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1477 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1478 sizeof(struct nlattr));
1479 upcall->packet.data = (char *)upcall->packet.data + sizeof(struct nlattr);
1480 upcall->packet.size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1482 *dp_ifindex = ovs_header->dp_ifindex;
1488 dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
1491 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1494 if (dpif->epoll_fd < 0) {
1498 if (dpif->event_offset >= dpif->n_events) {
1501 dpif->event_offset = dpif->n_events = 0;
1504 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1505 dpif->uc_array_size, 0);
1506 } while (retval < 0 && errno == EINTR);
1508 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1509 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1510 } else if (retval > 0) {
1511 dpif->n_events = retval;
1515 while (dpif->event_offset < dpif->n_events) {
1516 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1517 struct dpif_channel *ch = &dpif->channels[idx];
1519 dpif->event_offset++;
1525 if (++read_tries > 50) {
1529 error = nl_sock_recv(ch->sock, buf, false);
1530 if (error == ENOBUFS) {
1531 /* ENOBUFS typically means that we've received so many
1532 * packets that the buffer overflowed. Try again
1533 * immediately because there's almost certainly a packet
1534 * waiting for us. */
1535 report_loss(dpif_, ch);
1539 ch->last_poll = time_msec();
1541 if (error == EAGAIN) {
1547 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1548 if (!error && dp_ifindex == dpif->dp_ifindex) {
1560 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1563 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1566 ovs_mutex_lock(&dpif->upcall_lock);
1567 error = dpif_linux_recv__(dpif_, upcall, buf);
1568 ovs_mutex_unlock(&dpif->upcall_lock);
1574 dpif_linux_recv_wait(struct dpif *dpif_)
1576 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1578 ovs_mutex_lock(&dpif->upcall_lock);
1579 if (dpif->epoll_fd >= 0) {
1580 poll_fd_wait(dpif->epoll_fd, POLLIN);
1582 ovs_mutex_unlock(&dpif->upcall_lock);
1586 dpif_linux_recv_purge(struct dpif *dpif_)
1588 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1590 ovs_mutex_lock(&dpif->upcall_lock);
1591 if (dpif->epoll_fd >= 0) {
1592 struct dpif_channel *ch;
1594 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
1597 nl_sock_drain(ch->sock);
1601 ovs_mutex_unlock(&dpif->upcall_lock);
1604 const struct dpif_class dpif_linux_class = {
1606 dpif_linux_enumerate,
1613 dpif_linux_get_stats,
1614 dpif_linux_port_add,
1615 dpif_linux_port_del,
1616 dpif_linux_port_query_by_number,
1617 dpif_linux_port_query_by_name,
1618 dpif_linux_port_get_pid,
1619 dpif_linux_port_dump_start,
1620 dpif_linux_port_dump_next,
1621 dpif_linux_port_dump_done,
1622 dpif_linux_port_poll,
1623 dpif_linux_port_poll_wait,
1624 dpif_linux_flow_get,
1625 dpif_linux_flow_put,
1626 dpif_linux_flow_del,
1627 dpif_linux_flow_flush,
1628 dpif_linux_flow_dump_start,
1629 dpif_linux_flow_dump_next,
1630 dpif_linux_flow_dump_done,
1633 dpif_linux_recv_set,
1634 dpif_linux_queue_to_priority,
1636 dpif_linux_recv_wait,
1637 dpif_linux_recv_purge,
1641 dpif_linux_init(void)
1643 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1646 if (ovsthread_once_start(&once)) {
1647 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1648 &ovs_datapath_family);
1650 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1651 "The Open vSwitch kernel module is probably not loaded.",
1652 OVS_DATAPATH_FAMILY);
1655 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1658 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1661 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1662 &ovs_packet_family);
1665 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1666 &ovs_vport_mcgroup);
1669 ovsthread_once_done(&once);
1676 dpif_linux_is_internal_device(const char *name)
1678 struct dpif_linux_vport reply;
1682 error = dpif_linux_vport_get(name, &reply, &buf);
1685 } else if (error != ENODEV && error != ENOENT) {
1686 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1687 name, ovs_strerror(error));
1690 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1693 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1694 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1695 * positive errno value.
1697 * 'vport' will contain pointers into 'buf', so the caller should not free
1698 * 'buf' while 'vport' is still in use. */
1700 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1701 const struct ofpbuf *buf)
1703 static const struct nl_policy ovs_vport_policy[] = {
1704 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1705 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1706 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1707 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1708 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1710 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1713 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1714 struct ovs_header *ovs_header;
1715 struct nlmsghdr *nlmsg;
1716 struct genlmsghdr *genl;
1719 dpif_linux_vport_init(vport);
1721 ofpbuf_use_const(&b, buf->data, buf->size);
1722 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1723 genl = ofpbuf_try_pull(&b, sizeof *genl);
1724 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1725 if (!nlmsg || !genl || !ovs_header
1726 || nlmsg->nlmsg_type != ovs_vport_family
1727 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1728 ARRAY_SIZE(ovs_vport_policy))) {
1732 vport->cmd = genl->cmd;
1733 vport->dp_ifindex = ovs_header->dp_ifindex;
1734 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1735 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1736 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1737 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1738 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1740 if (a[OVS_VPORT_ATTR_STATS]) {
1741 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1743 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1744 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1745 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1750 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1751 * followed by Netlink attributes corresponding to 'vport'. */
1753 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1756 struct ovs_header *ovs_header;
1758 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1759 vport->cmd, OVS_VPORT_VERSION);
1761 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1762 ovs_header->dp_ifindex = vport->dp_ifindex;
1764 if (vport->port_no != ODPP_NONE) {
1765 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1768 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1769 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1773 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1776 if (vport->upcall_pid) {
1777 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1781 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1782 vport->stats, sizeof *vport->stats);
1785 if (vport->options) {
1786 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1787 vport->options, vport->options_len);
1791 /* Clears 'vport' to "empty" values. */
1793 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1795 memset(vport, 0, sizeof *vport);
1796 vport->port_no = ODPP_NONE;
1799 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1800 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1801 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1802 * result of the command is expected to be an ovs_vport also, which is decoded
1803 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1804 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1806 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1807 struct dpif_linux_vport *reply,
1808 struct ofpbuf **bufp)
1810 struct ofpbuf *request_buf;
1813 ovs_assert((reply != NULL) == (bufp != NULL));
1815 error = dpif_linux_init();
1819 dpif_linux_vport_init(reply);
1824 request_buf = ofpbuf_new(1024);
1825 dpif_linux_vport_to_ofpbuf(request, request_buf);
1826 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1827 ofpbuf_delete(request_buf);
1831 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1834 dpif_linux_vport_init(reply);
1835 ofpbuf_delete(*bufp);
1842 /* Obtains information about the kernel vport named 'name' and stores it into
1843 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1844 * longer needed ('reply' will contain pointers into '*bufp'). */
1846 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1847 struct ofpbuf **bufp)
1849 struct dpif_linux_vport request;
1851 dpif_linux_vport_init(&request);
1852 request.cmd = OVS_VPORT_CMD_GET;
1853 request.name = name;
1855 return dpif_linux_vport_transact(&request, reply, bufp);
1858 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1859 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1860 * positive errno value.
1862 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1863 * while 'dp' is still in use. */
1865 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1867 static const struct nl_policy ovs_datapath_policy[] = {
1868 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1869 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1871 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
1872 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
1876 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1877 struct ovs_header *ovs_header;
1878 struct nlmsghdr *nlmsg;
1879 struct genlmsghdr *genl;
1882 dpif_linux_dp_init(dp);
1884 ofpbuf_use_const(&b, buf->data, buf->size);
1885 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1886 genl = ofpbuf_try_pull(&b, sizeof *genl);
1887 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1888 if (!nlmsg || !genl || !ovs_header
1889 || nlmsg->nlmsg_type != ovs_datapath_family
1890 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1891 ARRAY_SIZE(ovs_datapath_policy))) {
1895 dp->cmd = genl->cmd;
1896 dp->dp_ifindex = ovs_header->dp_ifindex;
1897 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1898 if (a[OVS_DP_ATTR_STATS]) {
1899 /* Can't use structure assignment because Netlink doesn't ensure
1900 * sufficient alignment for 64-bit members. */
1901 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1905 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
1906 /* Can't use structure assignment because Netlink doesn't ensure
1907 * sufficient alignment for 64-bit members. */
1908 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
1909 sizeof dp->megaflow_stats);
1915 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1917 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1919 struct ovs_header *ovs_header;
1921 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1922 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1923 OVS_DATAPATH_VERSION);
1925 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1926 ovs_header->dp_ifindex = dp->dp_ifindex;
1929 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1932 if (dp->upcall_pid) {
1933 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1936 if (dp->user_features) {
1937 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
1940 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1943 /* Clears 'dp' to "empty" values. */
1945 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1947 memset(dp, 0, sizeof *dp);
1948 dp->megaflow_stats.n_masks = UINT32_MAX;
1949 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
1953 dpif_linux_dp_dump_start(struct nl_dump *dump)
1955 struct dpif_linux_dp request;
1958 dpif_linux_dp_init(&request);
1959 request.cmd = OVS_DP_CMD_GET;
1961 buf = ofpbuf_new(1024);
1962 dpif_linux_dp_to_ofpbuf(&request, buf);
1963 nl_dump_start(dump, NETLINK_GENERIC, buf);
1967 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1968 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1969 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1970 * result of the command is expected to be of the same form, which is decoded
1971 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1972 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1974 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1975 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1977 struct ofpbuf *request_buf;
1980 ovs_assert((reply != NULL) == (bufp != NULL));
1982 request_buf = ofpbuf_new(1024);
1983 dpif_linux_dp_to_ofpbuf(request, request_buf);
1984 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1985 ofpbuf_delete(request_buf);
1988 dpif_linux_dp_init(reply);
1990 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1993 ofpbuf_delete(*bufp);
2000 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2001 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2002 * will contain pointers into '*bufp'). */
2004 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2005 struct ofpbuf **bufp)
2007 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2008 struct dpif_linux_dp request;
2010 dpif_linux_dp_init(&request);
2011 request.cmd = OVS_DP_CMD_GET;
2012 request.dp_ifindex = dpif->dp_ifindex;
2014 return dpif_linux_dp_transact(&request, reply, bufp);
2017 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2018 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2019 * positive errno value.
2021 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2022 * while 'flow' is still in use. */
2024 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2025 const struct ofpbuf *buf)
2027 static const struct nl_policy ovs_flow_policy[] = {
2028 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2029 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2030 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2031 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2033 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2034 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2035 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2038 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2039 struct ovs_header *ovs_header;
2040 struct nlmsghdr *nlmsg;
2041 struct genlmsghdr *genl;
2044 dpif_linux_flow_init(flow);
2046 ofpbuf_use_const(&b, buf->data, buf->size);
2047 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2048 genl = ofpbuf_try_pull(&b, sizeof *genl);
2049 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2050 if (!nlmsg || !genl || !ovs_header
2051 || nlmsg->nlmsg_type != ovs_flow_family
2052 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2053 ARRAY_SIZE(ovs_flow_policy))) {
2057 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2058 flow->dp_ifindex = ovs_header->dp_ifindex;
2059 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2060 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2062 if (a[OVS_FLOW_ATTR_MASK]) {
2063 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2064 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2066 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2067 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2068 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2070 if (a[OVS_FLOW_ATTR_STATS]) {
2071 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2073 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2074 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2076 if (a[OVS_FLOW_ATTR_USED]) {
2077 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2082 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2083 * followed by Netlink attributes corresponding to 'flow'. */
2085 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2088 struct ovs_header *ovs_header;
2090 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2091 NLM_F_REQUEST | flow->nlmsg_flags,
2092 flow->cmd, OVS_FLOW_VERSION);
2094 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2095 ovs_header->dp_ifindex = flow->dp_ifindex;
2097 if (flow->key_len) {
2098 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2101 if (flow->mask_len) {
2102 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2105 if (flow->actions || flow->actions_len) {
2106 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2107 flow->actions, flow->actions_len);
2110 /* We never need to send these to the kernel. */
2111 ovs_assert(!flow->stats);
2112 ovs_assert(!flow->tcp_flags);
2113 ovs_assert(!flow->used);
2116 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2120 /* Clears 'flow' to "empty" values. */
2122 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2124 memset(flow, 0, sizeof *flow);
2127 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2128 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2129 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2130 * result of the command is expected to be a flow also, which is decoded and
2131 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2132 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2134 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2135 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2137 struct ofpbuf *request_buf;
2140 ovs_assert((reply != NULL) == (bufp != NULL));
2143 request->nlmsg_flags |= NLM_F_ECHO;
2146 request_buf = ofpbuf_new(1024);
2147 dpif_linux_flow_to_ofpbuf(request, request_buf);
2148 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2149 ofpbuf_delete(request_buf);
2153 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2156 dpif_linux_flow_init(reply);
2157 ofpbuf_delete(*bufp);
2165 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2166 struct dpif_flow_stats *stats)
2169 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2170 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2172 stats->n_packets = 0;
2175 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2176 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2179 /* Logs information about a packet that was recently lost in 'ch' (in
2182 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2184 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2185 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2188 if (VLOG_DROP_WARN(&rl)) {
2193 if (ch->last_poll != LLONG_MIN) {
2194 ds_put_format(&s, " (last polled %lld ms ago)",
2195 time_msec() - ch->last_poll);
2198 VLOG_WARN("%s: lost packet on channel %"PRIdPTR"%s",
2199 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));