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: {
445 const char *type = netdev_get_type_from_name(vport->name);
447 return type ? type : "system";
450 case OVS_VPORT_TYPE_INTERNAL:
453 case OVS_VPORT_TYPE_GRE:
456 case OVS_VPORT_TYPE_GRE64:
459 case OVS_VPORT_TYPE_VXLAN:
462 case OVS_VPORT_TYPE_LISP:
465 case OVS_VPORT_TYPE_UNSPEC:
466 case __OVS_VPORT_TYPE_MAX:
470 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
471 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
475 static enum ovs_vport_type
476 netdev_to_ovs_vport_type(const struct netdev *netdev)
478 const char *type = netdev_get_type(netdev);
480 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
481 return OVS_VPORT_TYPE_NETDEV;
482 } else if (!strcmp(type, "internal")) {
483 return OVS_VPORT_TYPE_INTERNAL;
484 } else if (strstr(type, "gre64")) {
485 return OVS_VPORT_TYPE_GRE64;
486 } else if (strstr(type, "gre")) {
487 return OVS_VPORT_TYPE_GRE;
488 } else if (!strcmp(type, "vxlan")) {
489 return OVS_VPORT_TYPE_VXLAN;
490 } else if (!strcmp(type, "lisp")) {
491 return OVS_VPORT_TYPE_LISP;
493 return OVS_VPORT_TYPE_UNSPEC;
498 dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
499 odp_port_t *port_nop)
501 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
502 const struct netdev_tunnel_config *tnl_cfg;
503 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
504 const char *name = netdev_vport_get_dpif_port(netdev,
505 namebuf, sizeof namebuf);
506 const char *type = netdev_get_type(netdev);
507 struct dpif_linux_vport request, reply;
508 struct nl_sock *sock = NULL;
511 uint64_t options_stub[64 / 8];
512 struct ofpbuf options;
515 if (dpif->epoll_fd >= 0) {
516 error = nl_sock_create(NETLINK_GENERIC, &sock);
522 dpif_linux_vport_init(&request);
523 request.cmd = OVS_VPORT_CMD_NEW;
524 request.dp_ifindex = dpif->dp_ifindex;
525 request.type = netdev_to_ovs_vport_type(netdev);
526 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
527 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
528 "unsupported type `%s'",
529 dpif_name(dpif_), name, type);
530 nl_sock_destroy(sock);
535 if (request.type == OVS_VPORT_TYPE_NETDEV) {
536 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
539 tnl_cfg = netdev_get_tunnel_config(netdev);
540 if (tnl_cfg && tnl_cfg->dst_port != 0) {
541 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
542 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
543 ntohs(tnl_cfg->dst_port));
544 request.options = options.data;
545 request.options_len = options.size;
548 request.port_no = *port_nop;
549 upcall_pid = sock ? nl_sock_pid(sock) : 0;
550 request.upcall_pid = &upcall_pid;
552 error = dpif_linux_vport_transact(&request, &reply, &buf);
554 *port_nop = reply.port_no;
555 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
556 dpif_name(dpif_), reply.port_no, upcall_pid);
558 if (error == EBUSY && *port_nop != ODPP_NONE) {
559 VLOG_INFO("%s: requested port %"PRIu32" is in use",
560 dpif_name(dpif_), *port_nop);
562 nl_sock_destroy(sock);
569 error = add_channel(dpif, *port_nop, sock);
571 VLOG_INFO("%s: could not add channel for port %s",
572 dpif_name(dpif_), name);
574 /* Delete the port. */
575 dpif_linux_vport_init(&request);
576 request.cmd = OVS_VPORT_CMD_DEL;
577 request.dp_ifindex = dpif->dp_ifindex;
578 request.port_no = *port_nop;
579 dpif_linux_vport_transact(&request, NULL, NULL);
581 nl_sock_destroy(sock);
590 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
591 odp_port_t *port_nop)
593 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
596 ovs_mutex_lock(&dpif->upcall_lock);
597 error = dpif_linux_port_add__(dpif_, netdev, port_nop);
598 ovs_mutex_unlock(&dpif->upcall_lock);
604 dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
606 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
607 struct dpif_linux_vport vport;
610 dpif_linux_vport_init(&vport);
611 vport.cmd = OVS_VPORT_CMD_DEL;
612 vport.dp_ifindex = dpif->dp_ifindex;
613 vport.port_no = port_no;
614 error = dpif_linux_vport_transact(&vport, NULL, NULL);
616 del_channel(dpif, port_no);
622 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
624 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
627 ovs_mutex_lock(&dpif->upcall_lock);
628 error = dpif_linux_port_del__(dpif_, port_no);
629 ovs_mutex_unlock(&dpif->upcall_lock);
635 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
636 const char *port_name, struct dpif_port *dpif_port)
638 struct dpif_linux_vport request;
639 struct dpif_linux_vport reply;
643 dpif_linux_vport_init(&request);
644 request.cmd = OVS_VPORT_CMD_GET;
645 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
646 request.port_no = port_no;
647 request.name = port_name;
649 error = dpif_linux_vport_transact(&request, &reply, &buf);
651 if (reply.dp_ifindex != request.dp_ifindex) {
652 /* A query by name reported that 'port_name' is in some datapath
653 * other than 'dpif', but the caller wants to know about 'dpif'. */
655 } else if (dpif_port) {
656 dpif_port->name = xstrdup(reply.name);
657 dpif_port->type = xstrdup(get_vport_type(&reply));
658 dpif_port->port_no = reply.port_no;
666 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
667 struct dpif_port *dpif_port)
669 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
673 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
674 struct dpif_port *dpif_port)
676 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
680 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
686 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
688 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
689 uint32_t port_idx = odp_to_u32(port_no);
692 ovs_mutex_lock(&dpif->upcall_lock);
693 if (dpif->epoll_fd >= 0) {
694 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
695 * channel, since it is not heavily loaded. */
696 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
697 const struct nl_sock *sock = dpif->channels[idx].sock;
698 pid = sock ? nl_sock_pid(sock) : 0;
700 ovs_mutex_unlock(&dpif->upcall_lock);
706 dpif_linux_flow_flush(struct dpif *dpif_)
708 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
709 struct dpif_linux_flow flow;
711 dpif_linux_flow_init(&flow);
712 flow.cmd = OVS_FLOW_CMD_DEL;
713 flow.dp_ifindex = dpif->dp_ifindex;
714 return dpif_linux_flow_transact(&flow, NULL, NULL);
717 struct dpif_linux_port_state {
722 dpif_linux_port_dump_start__(const struct dpif *dpif_, struct nl_dump *dump)
724 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
725 struct dpif_linux_vport request;
728 dpif_linux_vport_init(&request);
729 request.cmd = OVS_VPORT_CMD_GET;
730 request.dp_ifindex = dpif->dp_ifindex;
732 buf = ofpbuf_new(1024);
733 dpif_linux_vport_to_ofpbuf(&request, buf);
734 nl_dump_start(dump, NETLINK_GENERIC, buf);
739 dpif_linux_port_dump_start(const struct dpif *dpif, void **statep)
741 struct dpif_linux_port_state *state;
743 *statep = state = xmalloc(sizeof *state);
744 dpif_linux_port_dump_start__(dpif, &state->dump);
750 dpif_linux_port_dump_next__(const struct dpif *dpif_, struct nl_dump *dump,
751 struct dpif_linux_vport *vport)
753 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
757 if (!nl_dump_next(dump, &buf)) {
761 error = dpif_linux_vport_from_ofpbuf(vport, &buf);
763 VLOG_WARN_RL(&error_rl, "%s: failed to parse vport record (%s)",
764 dpif_name(&dpif->dpif), ovs_strerror(error));
770 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
771 struct dpif_port *dpif_port)
773 struct dpif_linux_port_state *state = state_;
774 struct dpif_linux_vport vport;
777 error = dpif_linux_port_dump_next__(dpif, &state->dump, &vport);
781 dpif_port->name = CONST_CAST(char *, vport.name);
782 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
783 dpif_port->port_no = vport.port_no;
788 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
790 struct dpif_linux_port_state *state = state_;
791 int error = nl_dump_done(&state->dump);
798 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
800 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
802 /* Lazily create the Netlink socket to listen for notifications. */
803 if (!dpif->port_notifier) {
804 struct nl_sock *sock;
807 error = nl_sock_create(NETLINK_GENERIC, &sock);
812 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
814 nl_sock_destroy(sock);
817 dpif->port_notifier = sock;
819 /* We have no idea of the current state so report that everything
825 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
826 uint64_t buf_stub[4096 / 8];
830 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
831 error = nl_sock_recv(dpif->port_notifier, &buf, false);
833 struct dpif_linux_vport vport;
835 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
837 if (vport.dp_ifindex == dpif->dp_ifindex
838 && (vport.cmd == OVS_VPORT_CMD_NEW
839 || vport.cmd == OVS_VPORT_CMD_DEL
840 || vport.cmd == OVS_VPORT_CMD_SET)) {
841 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
842 dpif->dpif.full_name, vport.name, vport.cmd);
843 if (vport.cmd == OVS_VPORT_CMD_DEL) {
844 dpif->refresh_channels = true;
846 *devnamep = xstrdup(vport.name);
851 } else if (error != EAGAIN) {
852 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
853 ovs_strerror(error));
854 nl_sock_drain(dpif->port_notifier);
866 dpif_linux_port_poll_wait(const struct dpif *dpif_)
868 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
870 if (dpif->port_notifier) {
871 nl_sock_wait(dpif->port_notifier, POLLIN);
873 poll_immediate_wake();
878 dpif_linux_flow_get__(const struct dpif *dpif_,
879 const struct nlattr *key, size_t key_len,
880 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
882 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
883 struct dpif_linux_flow request;
885 dpif_linux_flow_init(&request);
886 request.cmd = OVS_FLOW_CMD_GET;
887 request.dp_ifindex = dpif->dp_ifindex;
889 request.key_len = key_len;
890 return dpif_linux_flow_transact(&request, reply, bufp);
894 dpif_linux_flow_get(const struct dpif *dpif_,
895 const struct nlattr *key, size_t key_len,
896 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
898 struct dpif_linux_flow reply;
902 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
905 dpif_linux_flow_get_stats(&reply, stats);
908 buf->data = CONST_CAST(struct nlattr *, reply.actions);
909 buf->size = reply.actions_len;
919 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
920 struct dpif_linux_flow *request)
922 static const struct nlattr dummy_action;
924 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
926 dpif_linux_flow_init(request);
927 request->cmd = (put->flags & DPIF_FP_CREATE
928 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
929 request->dp_ifindex = dpif->dp_ifindex;
930 request->key = put->key;
931 request->key_len = put->key_len;
932 request->mask = put->mask;
933 request->mask_len = put->mask_len;
934 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
935 request->actions = (put->actions
937 : CONST_CAST(struct nlattr *, &dummy_action));
938 request->actions_len = put->actions_len;
939 if (put->flags & DPIF_FP_ZERO_STATS) {
940 request->clear = true;
942 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
946 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
948 struct dpif_linux_flow request, reply;
952 dpif_linux_init_flow_put(dpif_, put, &request);
953 error = dpif_linux_flow_transact(&request,
954 put->stats ? &reply : NULL,
955 put->stats ? &buf : NULL);
956 if (!error && put->stats) {
957 dpif_linux_flow_get_stats(&reply, put->stats);
964 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
965 struct dpif_linux_flow *request)
967 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
969 dpif_linux_flow_init(request);
970 request->cmd = OVS_FLOW_CMD_DEL;
971 request->dp_ifindex = dpif->dp_ifindex;
972 request->key = del->key;
973 request->key_len = del->key_len;
977 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
979 struct dpif_linux_flow request, reply;
983 dpif_linux_init_flow_del(dpif_, del, &request);
984 error = dpif_linux_flow_transact(&request,
985 del->stats ? &reply : NULL,
986 del->stats ? &buf : NULL);
987 if (!error && del->stats) {
988 dpif_linux_flow_get_stats(&reply, del->stats);
994 struct dpif_linux_flow_state {
996 struct dpif_linux_flow flow;
997 struct dpif_flow_stats stats;
1002 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
1004 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1005 struct dpif_linux_flow_state *state;
1006 struct dpif_linux_flow request;
1009 *statep = state = xmalloc(sizeof *state);
1011 dpif_linux_flow_init(&request);
1012 request.cmd = OVS_FLOW_CMD_GET;
1013 request.dp_ifindex = dpif->dp_ifindex;
1015 buf = ofpbuf_new(1024);
1016 dpif_linux_flow_to_ofpbuf(&request, buf);
1017 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
1026 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
1027 const struct nlattr **key, size_t *key_len,
1028 const struct nlattr **mask, size_t *mask_len,
1029 const struct nlattr **actions, size_t *actions_len,
1030 const struct dpif_flow_stats **stats)
1032 struct dpif_linux_flow_state *state = state_;
1037 ofpbuf_delete(state->buf);
1040 if (!nl_dump_next(&state->dump, &buf)) {
1044 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1049 if (actions && !state->flow.actions) {
1050 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1051 state->flow.key_len,
1052 &state->flow, &state->buf);
1053 if (error == ENOENT) {
1054 VLOG_DBG("dumped flow disappeared on get");
1056 VLOG_WARN("error fetching dumped flow: %s",
1057 ovs_strerror(error));
1063 *actions = state->flow.actions;
1064 *actions_len = state->flow.actions_len;
1067 *key = state->flow.key;
1068 *key_len = state->flow.key_len;
1071 *mask = state->flow.mask;
1072 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1075 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1076 *stats = &state->stats;
1082 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1084 struct dpif_linux_flow_state *state = state_;
1085 int error = nl_dump_done(&state->dump);
1086 ofpbuf_delete(state->buf);
1092 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1095 struct ovs_header *k_exec;
1097 ofpbuf_prealloc_tailroom(buf, (64
1098 + d_exec->packet->size
1100 + d_exec->actions_len));
1102 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1103 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1105 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1106 k_exec->dp_ifindex = dp_ifindex;
1108 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1109 d_exec->packet->data, d_exec->packet->size);
1110 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
1111 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1112 d_exec->actions, d_exec->actions_len);
1116 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1118 uint64_t request_stub[1024 / 8];
1119 struct ofpbuf request;
1122 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1123 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1124 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1125 ofpbuf_uninit(&request);
1131 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1133 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1135 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1141 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1143 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1146 struct nl_transaction txn;
1148 struct ofpbuf request;
1149 uint64_t request_stub[1024 / 8];
1151 struct ofpbuf reply;
1152 uint64_t reply_stub[1024 / 8];
1155 struct nl_transaction *txnsp[MAX_OPS];
1158 ovs_assert(n_ops <= MAX_OPS);
1159 for (i = 0; i < n_ops; i++) {
1160 struct op_auxdata *aux = &auxes[i];
1161 struct dpif_op *op = ops[i];
1162 struct dpif_flow_put *put;
1163 struct dpif_flow_del *del;
1164 struct dpif_execute *execute;
1165 struct dpif_linux_flow flow;
1167 ofpbuf_use_stub(&aux->request,
1168 aux->request_stub, sizeof aux->request_stub);
1169 aux->txn.request = &aux->request;
1171 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1172 aux->txn.reply = NULL;
1175 case DPIF_OP_FLOW_PUT:
1176 put = &op->u.flow_put;
1177 dpif_linux_init_flow_put(dpif_, put, &flow);
1179 flow.nlmsg_flags |= NLM_F_ECHO;
1180 aux->txn.reply = &aux->reply;
1182 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1185 case DPIF_OP_FLOW_DEL:
1186 del = &op->u.flow_del;
1187 dpif_linux_init_flow_del(dpif_, del, &flow);
1189 flow.nlmsg_flags |= NLM_F_ECHO;
1190 aux->txn.reply = &aux->reply;
1192 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1195 case DPIF_OP_EXECUTE:
1196 execute = &op->u.execute;
1197 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1206 for (i = 0; i < n_ops; i++) {
1207 txnsp[i] = &auxes[i].txn;
1209 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1211 for (i = 0; i < n_ops; i++) {
1212 struct op_auxdata *aux = &auxes[i];
1213 struct nl_transaction *txn = &auxes[i].txn;
1214 struct dpif_op *op = ops[i];
1215 struct dpif_flow_put *put;
1216 struct dpif_flow_del *del;
1218 op->error = txn->error;
1221 case DPIF_OP_FLOW_PUT:
1222 put = &op->u.flow_put;
1225 struct dpif_linux_flow reply;
1227 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1230 dpif_linux_flow_get_stats(&reply, put->stats);
1235 memset(put->stats, 0, sizeof *put->stats);
1240 case DPIF_OP_FLOW_DEL:
1241 del = &op->u.flow_del;
1244 struct dpif_linux_flow reply;
1246 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1249 dpif_linux_flow_get_stats(&reply, del->stats);
1254 memset(del->stats, 0, sizeof *del->stats);
1259 case DPIF_OP_EXECUTE:
1266 ofpbuf_uninit(&aux->request);
1267 ofpbuf_uninit(&aux->reply);
1272 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1275 size_t chunk = MIN(n_ops, MAX_OPS);
1276 dpif_linux_operate__(dpif, ops, chunk);
1282 /* Synchronizes 'dpif->channels' with the set of vports currently in 'dpif' in
1283 * the kernel, by adding a new channel for any kernel vport that lacks one and
1284 * deleting any channels that have no backing kernel vports. */
1286 dpif_linux_refresh_channels(struct dpif *dpif_)
1288 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1289 unsigned long int *keep_channels;
1290 struct dpif_linux_vport vport;
1291 size_t keep_channels_nbits;
1292 struct nl_dump dump;
1296 /* To start with, we need an epoll fd. */
1297 if (dpif->epoll_fd < 0) {
1298 dpif->epoll_fd = epoll_create(10);
1299 if (dpif->epoll_fd < 0) {
1304 keep_channels_nbits = dpif->uc_array_size;
1305 keep_channels = bitmap_allocate(keep_channels_nbits);
1307 dpif->n_events = dpif->event_offset = 0;
1309 dpif_linux_port_dump_start__(dpif_, &dump);
1310 while (!dpif_linux_port_dump_next__(dpif_, &dump, &vport)) {
1311 uint32_t port_no = odp_to_u32(vport.port_no);
1312 struct nl_sock *sock = (port_no < dpif->uc_array_size
1313 ? dpif->channels[port_no].sock
1315 bool new_sock = !sock;
1319 error = nl_sock_create(NETLINK_GENERIC, &sock);
1326 /* Configure the vport to deliver misses to 'sock'. */
1327 if (!vport.upcall_pid || *vport.upcall_pid != nl_sock_pid(sock)) {
1328 uint32_t upcall_pid = nl_sock_pid(sock);
1329 struct dpif_linux_vport vport_request;
1331 dpif_linux_vport_init(&vport_request);
1332 vport_request.cmd = OVS_VPORT_CMD_SET;
1333 vport_request.dp_ifindex = dpif->dp_ifindex;
1334 vport_request.port_no = vport.port_no;
1335 vport_request.upcall_pid = &upcall_pid;
1336 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1338 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1339 dpif_name(&dpif->dpif), vport_request.port_no,
1342 VLOG_WARN_RL(&error_rl,
1343 "%s: failed to set upcall pid on port: %s",
1344 dpif_name(&dpif->dpif), ovs_strerror(error));
1346 if (error != ENODEV && error != ENOENT) {
1349 /* The vport isn't really there, even though the dump says
1350 * it is. Probably we just hit a race after a port
1358 error = add_channel(dpif, vport.port_no, sock);
1360 VLOG_INFO("%s: could not add channel for port %s",
1361 dpif_name(dpif_), vport.name);
1367 if (port_no < keep_channels_nbits) {
1368 bitmap_set1(keep_channels, port_no);
1373 nl_sock_destroy(sock);
1375 nl_dump_done(&dump);
1377 /* Discard any saved channels that we didn't reuse. */
1378 for (i = 0; i < keep_channels_nbits; i++) {
1379 if (!bitmap_is_set(keep_channels, i)) {
1380 nl_sock_destroy(dpif->channels[i].sock);
1381 dpif->channels[i].sock = NULL;
1384 free(keep_channels);
1390 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1392 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1394 if ((dpif->epoll_fd >= 0) == enable) {
1396 } else if (!enable) {
1397 destroy_channels(dpif);
1400 return dpif_linux_refresh_channels(dpif_);
1405 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1407 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1410 ovs_mutex_lock(&dpif->upcall_lock);
1411 error = dpif_linux_recv_set__(dpif_, enable);
1412 ovs_mutex_unlock(&dpif->upcall_lock);
1418 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1419 uint32_t queue_id, uint32_t *priority)
1421 if (queue_id < 0xf000) {
1422 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1430 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1433 static const struct nl_policy ovs_packet_policy[] = {
1434 /* Always present. */
1435 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1436 .min_len = ETH_HEADER_LEN },
1437 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1439 /* OVS_PACKET_CMD_ACTION only. */
1440 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1443 struct ovs_header *ovs_header;
1444 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1445 struct nlmsghdr *nlmsg;
1446 struct genlmsghdr *genl;
1450 ofpbuf_use_const(&b, buf->data, buf->size);
1452 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1453 genl = ofpbuf_try_pull(&b, sizeof *genl);
1454 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1455 if (!nlmsg || !genl || !ovs_header
1456 || nlmsg->nlmsg_type != ovs_packet_family
1457 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1458 ARRAY_SIZE(ovs_packet_policy))) {
1462 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1463 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1469 /* (Re)set ALL fields of '*upcall' on successful return. */
1470 upcall->type = type;
1471 upcall->key = CONST_CAST(struct nlattr *,
1472 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1473 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1474 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1476 /* Allow overwriting the netlink attribute header without reallocating. */
1477 ofpbuf_use_stub(&upcall->packet,
1478 CONST_CAST(struct nlattr *,
1479 nl_attr_get(a[OVS_PACKET_ATTR_PACKET])) - 1,
1480 nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]) +
1481 sizeof(struct nlattr));
1482 upcall->packet.data = (char *)upcall->packet.data + sizeof(struct nlattr);
1483 upcall->packet.size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1485 *dp_ifindex = ovs_header->dp_ifindex;
1491 dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
1494 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1497 if (dpif->epoll_fd < 0) {
1501 if (dpif->event_offset >= dpif->n_events) {
1504 dpif->event_offset = dpif->n_events = 0;
1507 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1508 dpif->uc_array_size, 0);
1509 } while (retval < 0 && errno == EINTR);
1511 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1512 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1513 } else if (retval > 0) {
1514 dpif->n_events = retval;
1518 while (dpif->event_offset < dpif->n_events) {
1519 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1520 struct dpif_channel *ch = &dpif->channels[idx];
1522 dpif->event_offset++;
1528 if (++read_tries > 50) {
1532 error = nl_sock_recv(ch->sock, buf, false);
1533 if (error == ENOBUFS) {
1534 /* ENOBUFS typically means that we've received so many
1535 * packets that the buffer overflowed. Try again
1536 * immediately because there's almost certainly a packet
1537 * waiting for us. */
1538 report_loss(dpif_, ch);
1542 ch->last_poll = time_msec();
1544 if (error == EAGAIN) {
1550 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1551 if (!error && dp_ifindex == dpif->dp_ifindex) {
1563 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1566 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1569 ovs_mutex_lock(&dpif->upcall_lock);
1570 error = dpif_linux_recv__(dpif_, upcall, buf);
1571 ovs_mutex_unlock(&dpif->upcall_lock);
1577 dpif_linux_recv_wait(struct dpif *dpif_)
1579 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1581 ovs_mutex_lock(&dpif->upcall_lock);
1582 if (dpif->epoll_fd >= 0) {
1583 poll_fd_wait(dpif->epoll_fd, POLLIN);
1585 ovs_mutex_unlock(&dpif->upcall_lock);
1589 dpif_linux_recv_purge(struct dpif *dpif_)
1591 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1593 ovs_mutex_lock(&dpif->upcall_lock);
1594 if (dpif->epoll_fd >= 0) {
1595 struct dpif_channel *ch;
1597 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
1600 nl_sock_drain(ch->sock);
1604 ovs_mutex_unlock(&dpif->upcall_lock);
1607 const struct dpif_class dpif_linux_class = {
1609 dpif_linux_enumerate,
1616 dpif_linux_get_stats,
1617 dpif_linux_port_add,
1618 dpif_linux_port_del,
1619 dpif_linux_port_query_by_number,
1620 dpif_linux_port_query_by_name,
1621 dpif_linux_get_max_ports,
1622 dpif_linux_port_get_pid,
1623 dpif_linux_port_dump_start,
1624 dpif_linux_port_dump_next,
1625 dpif_linux_port_dump_done,
1626 dpif_linux_port_poll,
1627 dpif_linux_port_poll_wait,
1628 dpif_linux_flow_get,
1629 dpif_linux_flow_put,
1630 dpif_linux_flow_del,
1631 dpif_linux_flow_flush,
1632 dpif_linux_flow_dump_start,
1633 dpif_linux_flow_dump_next,
1634 dpif_linux_flow_dump_done,
1637 dpif_linux_recv_set,
1638 dpif_linux_queue_to_priority,
1640 dpif_linux_recv_wait,
1641 dpif_linux_recv_purge,
1645 dpif_linux_init(void)
1647 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1650 if (ovsthread_once_start(&once)) {
1651 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1652 &ovs_datapath_family);
1654 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1655 "The Open vSwitch kernel module is probably not loaded.",
1656 OVS_DATAPATH_FAMILY);
1659 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1662 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1665 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1666 &ovs_packet_family);
1669 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1670 &ovs_vport_mcgroup);
1673 ovsthread_once_done(&once);
1680 dpif_linux_is_internal_device(const char *name)
1682 struct dpif_linux_vport reply;
1686 error = dpif_linux_vport_get(name, &reply, &buf);
1689 } else if (error != ENODEV && error != ENOENT) {
1690 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1691 name, ovs_strerror(error));
1694 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1697 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1698 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1699 * positive errno value.
1701 * 'vport' will contain pointers into 'buf', so the caller should not free
1702 * 'buf' while 'vport' is still in use. */
1704 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1705 const struct ofpbuf *buf)
1707 static const struct nl_policy ovs_vport_policy[] = {
1708 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1709 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1710 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1711 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1712 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1714 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1717 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1718 struct ovs_header *ovs_header;
1719 struct nlmsghdr *nlmsg;
1720 struct genlmsghdr *genl;
1723 dpif_linux_vport_init(vport);
1725 ofpbuf_use_const(&b, buf->data, buf->size);
1726 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1727 genl = ofpbuf_try_pull(&b, sizeof *genl);
1728 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1729 if (!nlmsg || !genl || !ovs_header
1730 || nlmsg->nlmsg_type != ovs_vport_family
1731 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1732 ARRAY_SIZE(ovs_vport_policy))) {
1736 vport->cmd = genl->cmd;
1737 vport->dp_ifindex = ovs_header->dp_ifindex;
1738 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1739 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1740 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1741 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1742 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1744 if (a[OVS_VPORT_ATTR_STATS]) {
1745 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1747 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1748 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1749 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1754 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1755 * followed by Netlink attributes corresponding to 'vport'. */
1757 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1760 struct ovs_header *ovs_header;
1762 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1763 vport->cmd, OVS_VPORT_VERSION);
1765 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1766 ovs_header->dp_ifindex = vport->dp_ifindex;
1768 if (vport->port_no != ODPP_NONE) {
1769 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1772 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1773 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1777 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1780 if (vport->upcall_pid) {
1781 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1785 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1786 vport->stats, sizeof *vport->stats);
1789 if (vport->options) {
1790 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1791 vport->options, vport->options_len);
1795 /* Clears 'vport' to "empty" values. */
1797 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1799 memset(vport, 0, sizeof *vport);
1800 vport->port_no = ODPP_NONE;
1803 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1804 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1805 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1806 * result of the command is expected to be an ovs_vport also, which is decoded
1807 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1808 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1810 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1811 struct dpif_linux_vport *reply,
1812 struct ofpbuf **bufp)
1814 struct ofpbuf *request_buf;
1817 ovs_assert((reply != NULL) == (bufp != NULL));
1819 error = dpif_linux_init();
1823 dpif_linux_vport_init(reply);
1828 request_buf = ofpbuf_new(1024);
1829 dpif_linux_vport_to_ofpbuf(request, request_buf);
1830 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1831 ofpbuf_delete(request_buf);
1835 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1838 dpif_linux_vport_init(reply);
1839 ofpbuf_delete(*bufp);
1846 /* Obtains information about the kernel vport named 'name' and stores it into
1847 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1848 * longer needed ('reply' will contain pointers into '*bufp'). */
1850 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1851 struct ofpbuf **bufp)
1853 struct dpif_linux_vport request;
1855 dpif_linux_vport_init(&request);
1856 request.cmd = OVS_VPORT_CMD_GET;
1857 request.name = name;
1859 return dpif_linux_vport_transact(&request, reply, bufp);
1862 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1863 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1864 * positive errno value.
1866 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1867 * while 'dp' is still in use. */
1869 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1871 static const struct nl_policy ovs_datapath_policy[] = {
1872 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1873 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1875 [OVS_DP_ATTR_MEGAFLOW_STATS] = {
1876 NL_POLICY_FOR(struct ovs_dp_megaflow_stats),
1880 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1881 struct ovs_header *ovs_header;
1882 struct nlmsghdr *nlmsg;
1883 struct genlmsghdr *genl;
1886 dpif_linux_dp_init(dp);
1888 ofpbuf_use_const(&b, buf->data, buf->size);
1889 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1890 genl = ofpbuf_try_pull(&b, sizeof *genl);
1891 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1892 if (!nlmsg || !genl || !ovs_header
1893 || nlmsg->nlmsg_type != ovs_datapath_family
1894 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1895 ARRAY_SIZE(ovs_datapath_policy))) {
1899 dp->cmd = genl->cmd;
1900 dp->dp_ifindex = ovs_header->dp_ifindex;
1901 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1902 if (a[OVS_DP_ATTR_STATS]) {
1903 /* Can't use structure assignment because Netlink doesn't ensure
1904 * sufficient alignment for 64-bit members. */
1905 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1909 if (a[OVS_DP_ATTR_MEGAFLOW_STATS]) {
1910 /* Can't use structure assignment because Netlink doesn't ensure
1911 * sufficient alignment for 64-bit members. */
1912 memcpy(&dp->megaflow_stats, nl_attr_get(a[OVS_DP_ATTR_MEGAFLOW_STATS]),
1913 sizeof dp->megaflow_stats);
1919 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1921 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1923 struct ovs_header *ovs_header;
1925 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1926 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1927 OVS_DATAPATH_VERSION);
1929 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1930 ovs_header->dp_ifindex = dp->dp_ifindex;
1933 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1936 if (dp->upcall_pid) {
1937 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1940 if (dp->user_features) {
1941 nl_msg_put_u32(buf, OVS_DP_ATTR_USER_FEATURES, dp->user_features);
1944 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1947 /* Clears 'dp' to "empty" values. */
1949 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1951 memset(dp, 0, sizeof *dp);
1952 dp->megaflow_stats.n_masks = UINT32_MAX;
1953 dp->megaflow_stats.n_mask_hit = UINT64_MAX;
1957 dpif_linux_dp_dump_start(struct nl_dump *dump)
1959 struct dpif_linux_dp request;
1962 dpif_linux_dp_init(&request);
1963 request.cmd = OVS_DP_CMD_GET;
1965 buf = ofpbuf_new(1024);
1966 dpif_linux_dp_to_ofpbuf(&request, buf);
1967 nl_dump_start(dump, NETLINK_GENERIC, buf);
1971 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1972 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1973 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1974 * result of the command is expected to be of the same form, which is decoded
1975 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1976 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1978 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1979 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1981 struct ofpbuf *request_buf;
1984 ovs_assert((reply != NULL) == (bufp != NULL));
1986 request_buf = ofpbuf_new(1024);
1987 dpif_linux_dp_to_ofpbuf(request, request_buf);
1988 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1989 ofpbuf_delete(request_buf);
1992 dpif_linux_dp_init(reply);
1994 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1997 ofpbuf_delete(*bufp);
2004 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
2005 * The caller must free '*bufp' when the reply is no longer needed ('reply'
2006 * will contain pointers into '*bufp'). */
2008 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
2009 struct ofpbuf **bufp)
2011 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2012 struct dpif_linux_dp request;
2014 dpif_linux_dp_init(&request);
2015 request.cmd = OVS_DP_CMD_GET;
2016 request.dp_ifindex = dpif->dp_ifindex;
2018 return dpif_linux_dp_transact(&request, reply, bufp);
2021 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
2022 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
2023 * positive errno value.
2025 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
2026 * while 'flow' is still in use. */
2028 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
2029 const struct ofpbuf *buf)
2031 static const struct nl_policy ovs_flow_policy[] = {
2032 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
2033 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
2034 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
2035 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
2037 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
2038 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
2039 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
2042 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
2043 struct ovs_header *ovs_header;
2044 struct nlmsghdr *nlmsg;
2045 struct genlmsghdr *genl;
2048 dpif_linux_flow_init(flow);
2050 ofpbuf_use_const(&b, buf->data, buf->size);
2051 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
2052 genl = ofpbuf_try_pull(&b, sizeof *genl);
2053 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
2054 if (!nlmsg || !genl || !ovs_header
2055 || nlmsg->nlmsg_type != ovs_flow_family
2056 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
2057 ARRAY_SIZE(ovs_flow_policy))) {
2061 flow->nlmsg_flags = nlmsg->nlmsg_flags;
2062 flow->dp_ifindex = ovs_header->dp_ifindex;
2063 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
2064 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
2066 if (a[OVS_FLOW_ATTR_MASK]) {
2067 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
2068 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
2070 if (a[OVS_FLOW_ATTR_ACTIONS]) {
2071 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
2072 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
2074 if (a[OVS_FLOW_ATTR_STATS]) {
2075 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
2077 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
2078 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
2080 if (a[OVS_FLOW_ATTR_USED]) {
2081 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
2086 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
2087 * followed by Netlink attributes corresponding to 'flow'. */
2089 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
2092 struct ovs_header *ovs_header;
2094 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
2095 NLM_F_REQUEST | flow->nlmsg_flags,
2096 flow->cmd, OVS_FLOW_VERSION);
2098 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
2099 ovs_header->dp_ifindex = flow->dp_ifindex;
2101 if (flow->key_len) {
2102 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
2105 if (flow->mask_len) {
2106 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
2109 if (flow->actions || flow->actions_len) {
2110 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
2111 flow->actions, flow->actions_len);
2114 /* We never need to send these to the kernel. */
2115 ovs_assert(!flow->stats);
2116 ovs_assert(!flow->tcp_flags);
2117 ovs_assert(!flow->used);
2120 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2124 /* Clears 'flow' to "empty" values. */
2126 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2128 memset(flow, 0, sizeof *flow);
2131 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2132 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2133 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2134 * result of the command is expected to be a flow also, which is decoded and
2135 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2136 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2138 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2139 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2141 struct ofpbuf *request_buf;
2144 ovs_assert((reply != NULL) == (bufp != NULL));
2147 request->nlmsg_flags |= NLM_F_ECHO;
2150 request_buf = ofpbuf_new(1024);
2151 dpif_linux_flow_to_ofpbuf(request, request_buf);
2152 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2153 ofpbuf_delete(request_buf);
2157 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2160 dpif_linux_flow_init(reply);
2161 ofpbuf_delete(*bufp);
2169 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2170 struct dpif_flow_stats *stats)
2173 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2174 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2176 stats->n_packets = 0;
2179 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2180 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2183 /* Logs information about a packet that was recently lost in 'ch' (in
2186 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2188 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2189 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2192 if (VLOG_DROP_WARN(&rl)) {
2197 if (ch->last_poll != LLONG_MIN) {
2198 ds_put_format(&s, " (last polled %lld ms ago)",
2199 time_msec() - ch->last_poll);
2202 VLOG_WARN("%s: lost packet on channel %"PRIdPTR"%s",
2203 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));