2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "dpif-linux.h"
26 #include <linux/types.h>
27 #include <linux/pkt_sched.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/sockios.h>
33 #include <sys/epoll.h>
38 #include "dpif-provider.h"
39 #include "dynamic-string.h"
42 #include "netdev-linux.h"
43 #include "netdev-vport.h"
44 #include "netlink-notifier.h"
45 #include "netlink-socket.h"
50 #include "poll-loop.h"
55 #include "unaligned.h"
59 VLOG_DEFINE_THIS_MODULE(dpif_linux);
60 enum { MAX_PORTS = USHRT_MAX };
62 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
63 * missing if we have old headers. */
64 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
66 struct dpif_linux_dp {
67 /* Generic Netlink header. */
70 /* struct ovs_header. */
74 const char *name; /* OVS_DP_ATTR_NAME. */
75 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
76 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
79 static void dpif_linux_dp_init(struct dpif_linux_dp *);
80 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
81 const struct ofpbuf *);
82 static void dpif_linux_dp_dump_start(struct nl_dump *);
83 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
84 struct dpif_linux_dp *reply,
85 struct ofpbuf **bufp);
86 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
87 struct ofpbuf **bufp);
89 struct dpif_linux_flow {
90 /* Generic Netlink header. */
93 /* struct ovs_header. */
94 unsigned int nlmsg_flags;
99 * The 'stats' member points to 64-bit data that might only be aligned on
100 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
103 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
104 * the Netlink version of the command, even if actions_len is zero. */
105 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
107 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
109 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
111 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
112 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
113 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
114 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
117 static void dpif_linux_flow_init(struct dpif_linux_flow *);
118 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
119 const struct ofpbuf *);
120 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
122 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
123 struct dpif_linux_flow *reply,
124 struct ofpbuf **bufp);
125 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
126 struct dpif_flow_stats *);
128 /* One of the dpif channels between the kernel and userspace. */
129 struct dpif_channel {
130 struct nl_sock *sock; /* Netlink socket. */
131 long long int last_poll; /* Last time this channel was polled. */
134 static void report_loss(struct dpif *, struct dpif_channel *);
136 /* Datapath interface for the openvswitch Linux kernel module. */
141 /* Upcall messages. */
142 struct ovs_mutex upcall_lock;
143 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
144 struct dpif_channel *channels;
145 struct epoll_event *epoll_events;
146 int epoll_fd; /* epoll fd that includes channel socks. */
147 int n_events; /* Num events returned by epoll_wait(). */
148 int event_offset; /* Offset into 'epoll_events'. */
150 /* Change notification. */
151 struct nl_sock *port_notifier; /* vport multicast group subscriber. */
154 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
156 /* Generic Netlink family numbers for OVS.
158 * Initialized by dpif_linux_init(). */
159 static int ovs_datapath_family;
160 static int ovs_vport_family;
161 static int ovs_flow_family;
162 static int ovs_packet_family;
164 /* Generic Netlink multicast groups for OVS.
166 * Initialized by dpif_linux_init(). */
167 static unsigned int ovs_vport_mcgroup;
169 static int dpif_linux_init(void);
170 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
171 static uint32_t dpif_linux_port_get_pid(const struct dpif *,
174 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
176 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
177 const struct ofpbuf *);
179 static struct dpif_linux *
180 dpif_linux_cast(const struct dpif *dpif)
182 dpif_assert_class(dpif, &dpif_linux_class);
183 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
187 dpif_linux_enumerate(struct sset *all_dps)
193 error = dpif_linux_init();
198 dpif_linux_dp_dump_start(&dump);
199 while (nl_dump_next(&dump, &msg)) {
200 struct dpif_linux_dp dp;
202 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
203 sset_add(all_dps, dp.name);
206 return nl_dump_done(&dump);
210 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
211 bool create, struct dpif **dpifp)
213 struct dpif_linux_dp dp_request, dp;
218 error = dpif_linux_init();
223 /* Create or look up datapath. */
224 dpif_linux_dp_init(&dp_request);
226 dp_request.cmd = OVS_DP_CMD_NEW;
228 dp_request.upcall_pid = &upcall_pid;
230 dp_request.cmd = OVS_DP_CMD_GET;
232 dp_request.name = name;
233 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
238 error = open_dpif(&dp, dpifp);
244 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
246 struct dpif_linux *dpif;
248 dpif = xzalloc(sizeof *dpif);
249 dpif->port_notifier = NULL;
250 ovs_mutex_init(&dpif->upcall_lock);
253 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
254 dp->dp_ifindex, dp->dp_ifindex);
256 dpif->dp_ifindex = dp->dp_ifindex;
257 *dpifp = &dpif->dpif;
263 destroy_channels(struct dpif_linux *dpif)
267 if (dpif->epoll_fd < 0) {
271 for (i = 0; i < dpif->uc_array_size; i++ ) {
272 struct dpif_linux_vport vport_request;
273 struct dpif_channel *ch = &dpif->channels[i];
274 uint32_t upcall_pid = 0;
280 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
282 /* Turn off upcalls. */
283 dpif_linux_vport_init(&vport_request);
284 vport_request.cmd = OVS_VPORT_CMD_SET;
285 vport_request.dp_ifindex = dpif->dp_ifindex;
286 vport_request.port_no = u32_to_odp(i);
287 vport_request.upcall_pid = &upcall_pid;
288 dpif_linux_vport_transact(&vport_request, NULL, NULL);
290 nl_sock_destroy(ch->sock);
293 free(dpif->channels);
294 dpif->channels = NULL;
295 dpif->uc_array_size = 0;
297 free(dpif->epoll_events);
298 dpif->epoll_events = NULL;
299 dpif->n_events = dpif->event_offset = 0;
301 /* Don't close dpif->epoll_fd since that would cause other threads that
302 * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */
306 add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
308 struct epoll_event event;
309 uint32_t port_idx = odp_to_u32(port_no);
311 if (dpif->epoll_fd < 0) {
315 /* We assume that the datapath densely chooses port numbers, which
316 * can therefore be used as an index into an array of channels. */
317 if (port_idx >= dpif->uc_array_size) {
318 uint32_t new_size = port_idx + 1;
321 if (new_size > MAX_PORTS) {
322 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
323 dpif_name(&dpif->dpif), port_no);
327 dpif->channels = xrealloc(dpif->channels,
328 new_size * sizeof *dpif->channels);
329 for (i = dpif->uc_array_size; i < new_size; i++) {
330 dpif->channels[i].sock = NULL;
333 dpif->epoll_events = xrealloc(dpif->epoll_events,
334 new_size * sizeof *dpif->epoll_events);
335 dpif->uc_array_size = new_size;
338 memset(&event, 0, sizeof event);
339 event.events = EPOLLIN;
340 event.data.u32 = port_idx;
341 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
346 nl_sock_destroy(dpif->channels[port_idx].sock);
347 dpif->channels[port_idx].sock = sock;
348 dpif->channels[port_idx].last_poll = LLONG_MIN;
354 del_channel(struct dpif_linux *dpif, odp_port_t port_no)
356 struct dpif_channel *ch;
357 uint32_t port_idx = odp_to_u32(port_no);
359 if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
363 ch = &dpif->channels[port_idx];
368 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
369 dpif->event_offset = dpif->n_events = 0;
371 nl_sock_destroy(ch->sock);
376 dpif_linux_close(struct dpif *dpif_)
378 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
380 nl_sock_destroy(dpif->port_notifier);
381 destroy_channels(dpif);
382 if (dpif->epoll_fd >= 0) {
383 close(dpif->epoll_fd);
385 ovs_mutex_destroy(&dpif->upcall_lock);
390 dpif_linux_destroy(struct dpif *dpif_)
392 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
393 struct dpif_linux_dp dp;
395 dpif_linux_dp_init(&dp);
396 dp.cmd = OVS_DP_CMD_DEL;
397 dp.dp_ifindex = dpif->dp_ifindex;
398 return dpif_linux_dp_transact(&dp, NULL, NULL);
402 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
404 struct dpif_linux_dp dp;
408 error = dpif_linux_dp_get(dpif_, &dp, &buf);
410 stats->n_hit = dp.stats.n_hit;
411 stats->n_missed = dp.stats.n_missed;
412 stats->n_lost = dp.stats.n_lost;
413 stats->n_flows = dp.stats.n_flows;
420 get_vport_type(const struct dpif_linux_vport *vport)
422 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
424 switch (vport->type) {
425 case OVS_VPORT_TYPE_NETDEV: {
426 const char *type = netdev_get_type_from_name(vport->name);
428 return type ? type : "system";
431 case OVS_VPORT_TYPE_INTERNAL:
434 case OVS_VPORT_TYPE_GRE:
437 case OVS_VPORT_TYPE_GRE64:
440 case OVS_VPORT_TYPE_VXLAN:
443 case OVS_VPORT_TYPE_LISP:
446 case OVS_VPORT_TYPE_UNSPEC:
447 case __OVS_VPORT_TYPE_MAX:
451 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
452 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
456 static enum ovs_vport_type
457 netdev_to_ovs_vport_type(const struct netdev *netdev)
459 const char *type = netdev_get_type(netdev);
461 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
462 return OVS_VPORT_TYPE_NETDEV;
463 } else if (!strcmp(type, "internal")) {
464 return OVS_VPORT_TYPE_INTERNAL;
465 } else if (strstr(type, "gre64")) {
466 return OVS_VPORT_TYPE_GRE64;
467 } else if (strstr(type, "gre")) {
468 return OVS_VPORT_TYPE_GRE;
469 } else if (!strcmp(type, "vxlan")) {
470 return OVS_VPORT_TYPE_VXLAN;
471 } else if (!strcmp(type, "lisp")) {
472 return OVS_VPORT_TYPE_LISP;
474 return OVS_VPORT_TYPE_UNSPEC;
479 dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
480 odp_port_t *port_nop)
482 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
483 const struct netdev_tunnel_config *tnl_cfg;
484 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
485 const char *name = netdev_vport_get_dpif_port(netdev,
486 namebuf, sizeof namebuf);
487 const char *type = netdev_get_type(netdev);
488 struct dpif_linux_vport request, reply;
489 struct nl_sock *sock = NULL;
492 uint64_t options_stub[64 / 8];
493 struct ofpbuf options;
496 if (dpif->epoll_fd >= 0) {
497 error = nl_sock_create(NETLINK_GENERIC, &sock);
503 dpif_linux_vport_init(&request);
504 request.cmd = OVS_VPORT_CMD_NEW;
505 request.dp_ifindex = dpif->dp_ifindex;
506 request.type = netdev_to_ovs_vport_type(netdev);
507 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
508 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
509 "unsupported type `%s'",
510 dpif_name(dpif_), name, type);
511 nl_sock_destroy(sock);
516 if (request.type == OVS_VPORT_TYPE_NETDEV) {
517 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
520 tnl_cfg = netdev_get_tunnel_config(netdev);
521 if (tnl_cfg && tnl_cfg->dst_port != 0) {
522 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
523 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
524 ntohs(tnl_cfg->dst_port));
525 request.options = options.data;
526 request.options_len = options.size;
529 request.port_no = *port_nop;
530 upcall_pid = sock ? nl_sock_pid(sock) : 0;
531 request.upcall_pid = &upcall_pid;
533 error = dpif_linux_vport_transact(&request, &reply, &buf);
535 *port_nop = reply.port_no;
536 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
537 dpif_name(dpif_), reply.port_no, upcall_pid);
539 if (error == EBUSY && *port_nop != ODPP_NONE) {
540 VLOG_INFO("%s: requested port %"PRIu32" is in use",
541 dpif_name(dpif_), *port_nop);
543 nl_sock_destroy(sock);
550 error = add_channel(dpif, *port_nop, sock);
552 VLOG_INFO("%s: could not add channel for port %s",
553 dpif_name(dpif_), name);
555 /* Delete the port. */
556 dpif_linux_vport_init(&request);
557 request.cmd = OVS_VPORT_CMD_DEL;
558 request.dp_ifindex = dpif->dp_ifindex;
559 request.port_no = *port_nop;
560 dpif_linux_vport_transact(&request, NULL, NULL);
562 nl_sock_destroy(sock);
571 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
572 odp_port_t *port_nop)
574 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
577 ovs_mutex_lock(&dpif->upcall_lock);
578 error = dpif_linux_port_add__(dpif_, netdev, port_nop);
579 ovs_mutex_unlock(&dpif->upcall_lock);
585 dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
587 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
588 struct dpif_linux_vport vport;
591 dpif_linux_vport_init(&vport);
592 vport.cmd = OVS_VPORT_CMD_DEL;
593 vport.dp_ifindex = dpif->dp_ifindex;
594 vport.port_no = port_no;
595 error = dpif_linux_vport_transact(&vport, NULL, NULL);
597 del_channel(dpif, port_no);
603 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
605 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
608 ovs_mutex_lock(&dpif->upcall_lock);
609 error = dpif_linux_port_del__(dpif_, port_no);
610 ovs_mutex_unlock(&dpif->upcall_lock);
616 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
617 const char *port_name, struct dpif_port *dpif_port)
619 struct dpif_linux_vport request;
620 struct dpif_linux_vport reply;
624 dpif_linux_vport_init(&request);
625 request.cmd = OVS_VPORT_CMD_GET;
626 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
627 request.port_no = port_no;
628 request.name = port_name;
630 error = dpif_linux_vport_transact(&request, &reply, &buf);
632 if (reply.dp_ifindex != request.dp_ifindex) {
633 /* A query by name reported that 'port_name' is in some datapath
634 * other than 'dpif', but the caller wants to know about 'dpif'. */
636 } else if (dpif_port) {
637 dpif_port->name = xstrdup(reply.name);
638 dpif_port->type = xstrdup(get_vport_type(&reply));
639 dpif_port->port_no = reply.port_no;
647 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
648 struct dpif_port *dpif_port)
650 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
654 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
655 struct dpif_port *dpif_port)
657 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
661 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
667 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
669 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
670 uint32_t port_idx = odp_to_u32(port_no);
673 ovs_mutex_lock(&dpif->upcall_lock);
674 if (dpif->epoll_fd >= 0) {
675 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
676 * channel, since it is not heavily loaded. */
677 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
678 pid = nl_sock_pid(dpif->channels[idx].sock);
680 ovs_mutex_unlock(&dpif->upcall_lock);
686 dpif_linux_flow_flush(struct dpif *dpif_)
688 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
689 struct dpif_linux_flow flow;
691 dpif_linux_flow_init(&flow);
692 flow.cmd = OVS_FLOW_CMD_DEL;
693 flow.dp_ifindex = dpif->dp_ifindex;
694 return dpif_linux_flow_transact(&flow, NULL, NULL);
697 struct dpif_linux_port_state {
702 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
704 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
705 struct dpif_linux_port_state *state;
706 struct dpif_linux_vport request;
709 *statep = state = xmalloc(sizeof *state);
711 dpif_linux_vport_init(&request);
712 request.cmd = OVS_DP_CMD_GET;
713 request.dp_ifindex = dpif->dp_ifindex;
715 buf = ofpbuf_new(1024);
716 dpif_linux_vport_to_ofpbuf(&request, buf);
717 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
724 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
725 struct dpif_port *dpif_port)
727 struct dpif_linux_port_state *state = state_;
728 struct dpif_linux_vport vport;
732 if (!nl_dump_next(&state->dump, &buf)) {
736 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
741 dpif_port->name = CONST_CAST(char *, vport.name);
742 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
743 dpif_port->port_no = vport.port_no;
748 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
750 struct dpif_linux_port_state *state = state_;
751 int error = nl_dump_done(&state->dump);
758 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
760 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
762 /* Lazily create the Netlink socket to listen for notifications. */
763 if (!dpif->port_notifier) {
764 struct nl_sock *sock;
767 error = nl_sock_create(NETLINK_GENERIC, &sock);
772 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
774 nl_sock_destroy(sock);
777 dpif->port_notifier = sock;
779 /* We have no idea of the current state so report that everything
785 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
786 uint64_t buf_stub[4096 / 8];
790 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
791 error = nl_sock_recv(dpif->port_notifier, &buf, false);
793 struct dpif_linux_vport vport;
795 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
797 if (vport.dp_ifindex == dpif->dp_ifindex
798 && (vport.cmd == OVS_VPORT_CMD_NEW
799 || vport.cmd == OVS_VPORT_CMD_DEL
800 || vport.cmd == OVS_VPORT_CMD_SET)) {
801 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
802 dpif->dpif.full_name, vport.name, vport.cmd);
803 *devnamep = xstrdup(vport.name);
808 } else if (error != EAGAIN) {
809 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
810 ovs_strerror(error));
811 nl_sock_drain(dpif->port_notifier);
823 dpif_linux_port_poll_wait(const struct dpif *dpif_)
825 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
827 if (dpif->port_notifier) {
828 nl_sock_wait(dpif->port_notifier, POLLIN);
830 poll_immediate_wake();
835 dpif_linux_flow_get__(const struct dpif *dpif_,
836 const struct nlattr *key, size_t key_len,
837 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
839 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
840 struct dpif_linux_flow request;
842 dpif_linux_flow_init(&request);
843 request.cmd = OVS_FLOW_CMD_GET;
844 request.dp_ifindex = dpif->dp_ifindex;
846 request.key_len = key_len;
847 return dpif_linux_flow_transact(&request, reply, bufp);
851 dpif_linux_flow_get(const struct dpif *dpif_,
852 const struct nlattr *key, size_t key_len,
853 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
855 struct dpif_linux_flow reply;
859 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
862 dpif_linux_flow_get_stats(&reply, stats);
865 buf->data = CONST_CAST(struct nlattr *, reply.actions);
866 buf->size = reply.actions_len;
876 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
877 struct dpif_linux_flow *request)
879 static const struct nlattr dummy_action;
881 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
883 dpif_linux_flow_init(request);
884 request->cmd = (put->flags & DPIF_FP_CREATE
885 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
886 request->dp_ifindex = dpif->dp_ifindex;
887 request->key = put->key;
888 request->key_len = put->key_len;
889 request->mask = put->mask;
890 request->mask_len = put->mask_len;
891 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
892 request->actions = (put->actions
894 : CONST_CAST(struct nlattr *, &dummy_action));
895 request->actions_len = put->actions_len;
896 if (put->flags & DPIF_FP_ZERO_STATS) {
897 request->clear = true;
899 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
903 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
905 struct dpif_linux_flow request, reply;
909 dpif_linux_init_flow_put(dpif_, put, &request);
910 error = dpif_linux_flow_transact(&request,
911 put->stats ? &reply : NULL,
912 put->stats ? &buf : NULL);
913 if (!error && put->stats) {
914 dpif_linux_flow_get_stats(&reply, put->stats);
921 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
922 struct dpif_linux_flow *request)
924 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
926 dpif_linux_flow_init(request);
927 request->cmd = OVS_FLOW_CMD_DEL;
928 request->dp_ifindex = dpif->dp_ifindex;
929 request->key = del->key;
930 request->key_len = del->key_len;
934 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
936 struct dpif_linux_flow request, reply;
940 dpif_linux_init_flow_del(dpif_, del, &request);
941 error = dpif_linux_flow_transact(&request,
942 del->stats ? &reply : NULL,
943 del->stats ? &buf : NULL);
944 if (!error && del->stats) {
945 dpif_linux_flow_get_stats(&reply, del->stats);
951 struct dpif_linux_flow_state {
953 struct dpif_linux_flow flow;
954 struct dpif_flow_stats stats;
959 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
961 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
962 struct dpif_linux_flow_state *state;
963 struct dpif_linux_flow request;
966 *statep = state = xmalloc(sizeof *state);
968 dpif_linux_flow_init(&request);
969 request.cmd = OVS_DP_CMD_GET;
970 request.dp_ifindex = dpif->dp_ifindex;
972 buf = ofpbuf_new(1024);
973 dpif_linux_flow_to_ofpbuf(&request, buf);
974 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
983 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
984 const struct nlattr **key, size_t *key_len,
985 const struct nlattr **mask, size_t *mask_len,
986 const struct nlattr **actions, size_t *actions_len,
987 const struct dpif_flow_stats **stats)
989 struct dpif_linux_flow_state *state = state_;
994 ofpbuf_delete(state->buf);
997 if (!nl_dump_next(&state->dump, &buf)) {
1001 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
1006 if (actions && !state->flow.actions) {
1007 error = dpif_linux_flow_get__(dpif_, state->flow.key,
1008 state->flow.key_len,
1009 &state->flow, &state->buf);
1010 if (error == ENOENT) {
1011 VLOG_DBG("dumped flow disappeared on get");
1013 VLOG_WARN("error fetching dumped flow: %s",
1014 ovs_strerror(error));
1020 *actions = state->flow.actions;
1021 *actions_len = state->flow.actions_len;
1024 *key = state->flow.key;
1025 *key_len = state->flow.key_len;
1028 *mask = state->flow.mask;
1029 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
1032 dpif_linux_flow_get_stats(&state->flow, &state->stats);
1033 *stats = &state->stats;
1039 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1041 struct dpif_linux_flow_state *state = state_;
1042 int error = nl_dump_done(&state->dump);
1043 ofpbuf_delete(state->buf);
1049 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1052 struct ovs_header *k_exec;
1054 ofpbuf_prealloc_tailroom(buf, (64
1055 + d_exec->packet->size
1057 + d_exec->actions_len));
1059 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1060 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1062 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1063 k_exec->dp_ifindex = dp_ifindex;
1065 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1066 d_exec->packet->data, d_exec->packet->size);
1067 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
1068 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1069 d_exec->actions, d_exec->actions_len);
1073 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1075 uint64_t request_stub[1024 / 8];
1076 struct ofpbuf request;
1079 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1080 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1081 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1082 ofpbuf_uninit(&request);
1088 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1090 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1092 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1098 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1100 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1103 struct nl_transaction txn;
1105 struct ofpbuf request;
1106 uint64_t request_stub[1024 / 8];
1108 struct ofpbuf reply;
1109 uint64_t reply_stub[1024 / 8];
1112 struct nl_transaction *txnsp[MAX_OPS];
1115 ovs_assert(n_ops <= MAX_OPS);
1116 for (i = 0; i < n_ops; i++) {
1117 struct op_auxdata *aux = &auxes[i];
1118 struct dpif_op *op = ops[i];
1119 struct dpif_flow_put *put;
1120 struct dpif_flow_del *del;
1121 struct dpif_execute *execute;
1122 struct dpif_linux_flow flow;
1124 ofpbuf_use_stub(&aux->request,
1125 aux->request_stub, sizeof aux->request_stub);
1126 aux->txn.request = &aux->request;
1128 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1129 aux->txn.reply = NULL;
1132 case DPIF_OP_FLOW_PUT:
1133 put = &op->u.flow_put;
1134 dpif_linux_init_flow_put(dpif_, put, &flow);
1136 flow.nlmsg_flags |= NLM_F_ECHO;
1137 aux->txn.reply = &aux->reply;
1139 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1142 case DPIF_OP_FLOW_DEL:
1143 del = &op->u.flow_del;
1144 dpif_linux_init_flow_del(dpif_, del, &flow);
1146 flow.nlmsg_flags |= NLM_F_ECHO;
1147 aux->txn.reply = &aux->reply;
1149 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1152 case DPIF_OP_EXECUTE:
1153 execute = &op->u.execute;
1154 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1163 for (i = 0; i < n_ops; i++) {
1164 txnsp[i] = &auxes[i].txn;
1166 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1168 for (i = 0; i < n_ops; i++) {
1169 struct op_auxdata *aux = &auxes[i];
1170 struct nl_transaction *txn = &auxes[i].txn;
1171 struct dpif_op *op = ops[i];
1172 struct dpif_flow_put *put;
1173 struct dpif_flow_del *del;
1175 op->error = txn->error;
1178 case DPIF_OP_FLOW_PUT:
1179 put = &op->u.flow_put;
1182 struct dpif_linux_flow reply;
1184 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1187 dpif_linux_flow_get_stats(&reply, put->stats);
1192 memset(put->stats, 0, sizeof *put->stats);
1197 case DPIF_OP_FLOW_DEL:
1198 del = &op->u.flow_del;
1201 struct dpif_linux_flow reply;
1203 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1206 dpif_linux_flow_get_stats(&reply, del->stats);
1211 memset(del->stats, 0, sizeof *del->stats);
1216 case DPIF_OP_EXECUTE:
1223 ofpbuf_uninit(&aux->request);
1224 ofpbuf_uninit(&aux->reply);
1229 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1232 size_t chunk = MIN(n_ops, MAX_OPS);
1233 dpif_linux_operate__(dpif, ops, chunk);
1240 dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
1242 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1244 if ((dpif->epoll_fd >= 0) == enable) {
1249 destroy_channels(dpif);
1251 struct dpif_port_dump port_dump;
1252 struct dpif_port port;
1254 if (dpif->epoll_fd < 0) {
1255 dpif->epoll_fd = epoll_create(10);
1256 if (dpif->epoll_fd < 0) {
1261 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1262 struct dpif_linux_vport vport_request;
1263 struct nl_sock *sock;
1264 uint32_t upcall_pid;
1267 error = nl_sock_create(NETLINK_GENERIC, &sock);
1272 upcall_pid = nl_sock_pid(sock);
1274 dpif_linux_vport_init(&vport_request);
1275 vport_request.cmd = OVS_VPORT_CMD_SET;
1276 vport_request.dp_ifindex = dpif->dp_ifindex;
1277 vport_request.port_no = port.port_no;
1278 vport_request.upcall_pid = &upcall_pid;
1279 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1281 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1282 dpif_name(&dpif->dpif), vport_request.port_no,
1285 VLOG_WARN_RL(&error_rl,
1286 "%s: failed to set upcall pid on port: %s",
1287 dpif_name(&dpif->dpif), ovs_strerror(error));
1288 nl_sock_destroy(sock);
1290 if (error == ENODEV || error == ENOENT) {
1291 /* This device isn't there, but keep trying the others. */
1298 error = add_channel(dpif, port.port_no, sock);
1300 VLOG_INFO("%s: could not add channel for port %s",
1301 dpif_name(dpif_), port.name);
1302 nl_sock_destroy(sock);
1312 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1314 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1317 ovs_mutex_lock(&dpif->upcall_lock);
1318 error = dpif_linux_recv_set__(dpif_, enable);
1319 ovs_mutex_unlock(&dpif->upcall_lock);
1325 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1326 uint32_t queue_id, uint32_t *priority)
1328 if (queue_id < 0xf000) {
1329 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1337 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1340 static const struct nl_policy ovs_packet_policy[] = {
1341 /* Always present. */
1342 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1343 .min_len = ETH_HEADER_LEN },
1344 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1346 /* OVS_PACKET_CMD_ACTION only. */
1347 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1350 struct ovs_header *ovs_header;
1351 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1352 struct nlmsghdr *nlmsg;
1353 struct genlmsghdr *genl;
1357 ofpbuf_use_const(&b, buf->data, buf->size);
1359 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1360 genl = ofpbuf_try_pull(&b, sizeof *genl);
1361 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1362 if (!nlmsg || !genl || !ovs_header
1363 || nlmsg->nlmsg_type != ovs_packet_family
1364 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1365 ARRAY_SIZE(ovs_packet_policy))) {
1369 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1370 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1376 memset(upcall, 0, sizeof *upcall);
1377 upcall->type = type;
1378 upcall->packet = buf;
1379 upcall->packet->data = CONST_CAST(struct nlattr *,
1380 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1381 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1382 upcall->key = CONST_CAST(struct nlattr *,
1383 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1384 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1385 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1386 *dp_ifindex = ovs_header->dp_ifindex;
1392 dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
1395 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1398 if (dpif->epoll_fd < 0) {
1402 if (dpif->event_offset >= dpif->n_events) {
1405 dpif->event_offset = dpif->n_events = 0;
1408 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1409 dpif->uc_array_size, 0);
1410 } while (retval < 0 && errno == EINTR);
1412 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1413 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1414 } else if (retval > 0) {
1415 dpif->n_events = retval;
1419 while (dpif->event_offset < dpif->n_events) {
1420 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1421 struct dpif_channel *ch = &dpif->channels[idx];
1423 dpif->event_offset++;
1429 if (++read_tries > 50) {
1433 error = nl_sock_recv(ch->sock, buf, false);
1434 if (error == ENOBUFS) {
1435 /* ENOBUFS typically means that we've received so many
1436 * packets that the buffer overflowed. Try again
1437 * immediately because there's almost certainly a packet
1438 * waiting for us. */
1439 report_loss(dpif_, ch);
1443 ch->last_poll = time_msec();
1445 if (error == EAGAIN) {
1451 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1452 if (!error && dp_ifindex == dpif->dp_ifindex) {
1464 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1467 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1470 ovs_mutex_lock(&dpif->upcall_lock);
1471 error = dpif_linux_recv__(dpif_, upcall, buf);
1472 ovs_mutex_unlock(&dpif->upcall_lock);
1478 dpif_linux_recv_wait(struct dpif *dpif_)
1480 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1482 ovs_mutex_lock(&dpif->upcall_lock);
1483 if (dpif->epoll_fd >= 0) {
1484 poll_fd_wait(dpif->epoll_fd, POLLIN);
1486 ovs_mutex_unlock(&dpif->upcall_lock);
1490 dpif_linux_recv_purge(struct dpif *dpif_)
1492 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1494 ovs_mutex_lock(&dpif->upcall_lock);
1495 if (dpif->epoll_fd >= 0) {
1496 struct dpif_channel *ch;
1498 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
1501 nl_sock_drain(ch->sock);
1505 ovs_mutex_unlock(&dpif->upcall_lock);
1508 const struct dpif_class dpif_linux_class = {
1510 dpif_linux_enumerate,
1517 dpif_linux_get_stats,
1518 dpif_linux_port_add,
1519 dpif_linux_port_del,
1520 dpif_linux_port_query_by_number,
1521 dpif_linux_port_query_by_name,
1522 dpif_linux_get_max_ports,
1523 dpif_linux_port_get_pid,
1524 dpif_linux_port_dump_start,
1525 dpif_linux_port_dump_next,
1526 dpif_linux_port_dump_done,
1527 dpif_linux_port_poll,
1528 dpif_linux_port_poll_wait,
1529 dpif_linux_flow_get,
1530 dpif_linux_flow_put,
1531 dpif_linux_flow_del,
1532 dpif_linux_flow_flush,
1533 dpif_linux_flow_dump_start,
1534 dpif_linux_flow_dump_next,
1535 dpif_linux_flow_dump_done,
1538 dpif_linux_recv_set,
1539 dpif_linux_queue_to_priority,
1541 dpif_linux_recv_wait,
1542 dpif_linux_recv_purge,
1546 dpif_linux_init(void)
1548 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1551 if (ovsthread_once_start(&once)) {
1552 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1553 &ovs_datapath_family);
1555 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1556 "The Open vSwitch kernel module is probably not loaded.",
1557 OVS_DATAPATH_FAMILY);
1560 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1563 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1566 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1567 &ovs_packet_family);
1570 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1571 &ovs_vport_mcgroup);
1574 ovsthread_once_done(&once);
1581 dpif_linux_is_internal_device(const char *name)
1583 struct dpif_linux_vport reply;
1587 error = dpif_linux_vport_get(name, &reply, &buf);
1590 } else if (error != ENODEV && error != ENOENT) {
1591 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1592 name, ovs_strerror(error));
1595 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1598 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1599 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1600 * positive errno value.
1602 * 'vport' will contain pointers into 'buf', so the caller should not free
1603 * 'buf' while 'vport' is still in use. */
1605 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1606 const struct ofpbuf *buf)
1608 static const struct nl_policy ovs_vport_policy[] = {
1609 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1610 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1611 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1612 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1613 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1615 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1618 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1619 struct ovs_header *ovs_header;
1620 struct nlmsghdr *nlmsg;
1621 struct genlmsghdr *genl;
1624 dpif_linux_vport_init(vport);
1626 ofpbuf_use_const(&b, buf->data, buf->size);
1627 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1628 genl = ofpbuf_try_pull(&b, sizeof *genl);
1629 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1630 if (!nlmsg || !genl || !ovs_header
1631 || nlmsg->nlmsg_type != ovs_vport_family
1632 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1633 ARRAY_SIZE(ovs_vport_policy))) {
1637 vport->cmd = genl->cmd;
1638 vport->dp_ifindex = ovs_header->dp_ifindex;
1639 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1640 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1641 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1642 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1643 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1645 if (a[OVS_VPORT_ATTR_STATS]) {
1646 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1648 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1649 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1650 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1655 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1656 * followed by Netlink attributes corresponding to 'vport'. */
1658 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1661 struct ovs_header *ovs_header;
1663 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1664 vport->cmd, OVS_VPORT_VERSION);
1666 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1667 ovs_header->dp_ifindex = vport->dp_ifindex;
1669 if (vport->port_no != ODPP_NONE) {
1670 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1673 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1674 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1678 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1681 if (vport->upcall_pid) {
1682 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1686 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1687 vport->stats, sizeof *vport->stats);
1690 if (vport->options) {
1691 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1692 vport->options, vport->options_len);
1696 /* Clears 'vport' to "empty" values. */
1698 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1700 memset(vport, 0, sizeof *vport);
1701 vport->port_no = ODPP_NONE;
1704 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1705 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1706 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1707 * result of the command is expected to be an ovs_vport also, which is decoded
1708 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1709 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1711 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1712 struct dpif_linux_vport *reply,
1713 struct ofpbuf **bufp)
1715 struct ofpbuf *request_buf;
1718 ovs_assert((reply != NULL) == (bufp != NULL));
1720 error = dpif_linux_init();
1724 dpif_linux_vport_init(reply);
1729 request_buf = ofpbuf_new(1024);
1730 dpif_linux_vport_to_ofpbuf(request, request_buf);
1731 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1732 ofpbuf_delete(request_buf);
1736 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1739 dpif_linux_vport_init(reply);
1740 ofpbuf_delete(*bufp);
1747 /* Obtains information about the kernel vport named 'name' and stores it into
1748 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1749 * longer needed ('reply' will contain pointers into '*bufp'). */
1751 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1752 struct ofpbuf **bufp)
1754 struct dpif_linux_vport request;
1756 dpif_linux_vport_init(&request);
1757 request.cmd = OVS_VPORT_CMD_GET;
1758 request.name = name;
1760 return dpif_linux_vport_transact(&request, reply, bufp);
1763 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1764 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1765 * positive errno value.
1767 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1768 * while 'dp' is still in use. */
1770 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1772 static const struct nl_policy ovs_datapath_policy[] = {
1773 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1774 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1778 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1779 struct ovs_header *ovs_header;
1780 struct nlmsghdr *nlmsg;
1781 struct genlmsghdr *genl;
1784 dpif_linux_dp_init(dp);
1786 ofpbuf_use_const(&b, buf->data, buf->size);
1787 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1788 genl = ofpbuf_try_pull(&b, sizeof *genl);
1789 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1790 if (!nlmsg || !genl || !ovs_header
1791 || nlmsg->nlmsg_type != ovs_datapath_family
1792 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1793 ARRAY_SIZE(ovs_datapath_policy))) {
1797 dp->cmd = genl->cmd;
1798 dp->dp_ifindex = ovs_header->dp_ifindex;
1799 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1800 if (a[OVS_DP_ATTR_STATS]) {
1801 /* Can't use structure assignment because Netlink doesn't ensure
1802 * sufficient alignment for 64-bit members. */
1803 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1810 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1812 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1814 struct ovs_header *ovs_header;
1816 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1817 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1818 OVS_DATAPATH_VERSION);
1820 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1821 ovs_header->dp_ifindex = dp->dp_ifindex;
1824 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1827 if (dp->upcall_pid) {
1828 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1831 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1834 /* Clears 'dp' to "empty" values. */
1836 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1838 memset(dp, 0, sizeof *dp);
1842 dpif_linux_dp_dump_start(struct nl_dump *dump)
1844 struct dpif_linux_dp request;
1847 dpif_linux_dp_init(&request);
1848 request.cmd = OVS_DP_CMD_GET;
1850 buf = ofpbuf_new(1024);
1851 dpif_linux_dp_to_ofpbuf(&request, buf);
1852 nl_dump_start(dump, NETLINK_GENERIC, buf);
1856 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1857 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1858 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1859 * result of the command is expected to be of the same form, which is decoded
1860 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1861 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1863 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1864 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1866 struct ofpbuf *request_buf;
1869 ovs_assert((reply != NULL) == (bufp != NULL));
1871 request_buf = ofpbuf_new(1024);
1872 dpif_linux_dp_to_ofpbuf(request, request_buf);
1873 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1874 ofpbuf_delete(request_buf);
1878 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1881 dpif_linux_dp_init(reply);
1882 ofpbuf_delete(*bufp);
1889 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1890 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1891 * will contain pointers into '*bufp'). */
1893 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1894 struct ofpbuf **bufp)
1896 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1897 struct dpif_linux_dp request;
1899 dpif_linux_dp_init(&request);
1900 request.cmd = OVS_DP_CMD_GET;
1901 request.dp_ifindex = dpif->dp_ifindex;
1903 return dpif_linux_dp_transact(&request, reply, bufp);
1906 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1907 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1908 * positive errno value.
1910 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1911 * while 'flow' is still in use. */
1913 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1914 const struct ofpbuf *buf)
1916 static const struct nl_policy ovs_flow_policy[] = {
1917 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1918 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
1919 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1920 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1922 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1923 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1924 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1927 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1928 struct ovs_header *ovs_header;
1929 struct nlmsghdr *nlmsg;
1930 struct genlmsghdr *genl;
1933 dpif_linux_flow_init(flow);
1935 ofpbuf_use_const(&b, buf->data, buf->size);
1936 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1937 genl = ofpbuf_try_pull(&b, sizeof *genl);
1938 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1939 if (!nlmsg || !genl || !ovs_header
1940 || nlmsg->nlmsg_type != ovs_flow_family
1941 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1942 ARRAY_SIZE(ovs_flow_policy))) {
1946 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1947 flow->dp_ifindex = ovs_header->dp_ifindex;
1948 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1949 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1951 if (a[OVS_FLOW_ATTR_MASK]) {
1952 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
1953 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
1955 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1956 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1957 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1959 if (a[OVS_FLOW_ATTR_STATS]) {
1960 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1962 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1963 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1965 if (a[OVS_FLOW_ATTR_USED]) {
1966 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1971 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1972 * followed by Netlink attributes corresponding to 'flow'. */
1974 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1977 struct ovs_header *ovs_header;
1979 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1980 NLM_F_REQUEST | flow->nlmsg_flags,
1981 flow->cmd, OVS_FLOW_VERSION);
1983 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1984 ovs_header->dp_ifindex = flow->dp_ifindex;
1986 if (flow->key_len) {
1987 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1990 if (flow->mask_len) {
1991 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
1994 if (flow->actions || flow->actions_len) {
1995 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1996 flow->actions, flow->actions_len);
1999 /* We never need to send these to the kernel. */
2000 ovs_assert(!flow->stats);
2001 ovs_assert(!flow->tcp_flags);
2002 ovs_assert(!flow->used);
2005 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
2009 /* Clears 'flow' to "empty" values. */
2011 dpif_linux_flow_init(struct dpif_linux_flow *flow)
2013 memset(flow, 0, sizeof *flow);
2016 /* Executes 'request' in the kernel datapath. If the command fails, returns a
2017 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
2018 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
2019 * result of the command is expected to be a flow also, which is decoded and
2020 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
2021 * is no longer needed ('reply' will contain pointers into '*bufp'). */
2023 dpif_linux_flow_transact(struct dpif_linux_flow *request,
2024 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
2026 struct ofpbuf *request_buf;
2029 ovs_assert((reply != NULL) == (bufp != NULL));
2032 request->nlmsg_flags |= NLM_F_ECHO;
2035 request_buf = ofpbuf_new(1024);
2036 dpif_linux_flow_to_ofpbuf(request, request_buf);
2037 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
2038 ofpbuf_delete(request_buf);
2042 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
2045 dpif_linux_flow_init(reply);
2046 ofpbuf_delete(*bufp);
2054 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
2055 struct dpif_flow_stats *stats)
2058 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
2059 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
2061 stats->n_packets = 0;
2064 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
2065 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
2068 /* Logs information about a packet that was recently lost in 'ch' (in
2071 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2073 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2074 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2077 if (VLOG_DROP_WARN(&rl)) {
2082 if (ch->last_poll != LLONG_MIN) {
2083 ds_put_format(&s, " (last polled %lld ms ago)",
2084 time_msec() - ch->last_poll);
2087 VLOG_WARN("%s: lost packet on channel %td%s",
2088 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));