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"
49 #include "openvswitch/datapath-compat.h"
51 #include "poll-loop.h"
56 #include "unaligned.h"
60 VLOG_DEFINE_THIS_MODULE(dpif_linux);
61 enum { MAX_PORTS = USHRT_MAX };
63 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
64 * missing if we have old headers. */
65 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
67 struct dpif_linux_dp {
68 /* Generic Netlink header. */
71 /* struct ovs_header. */
75 const char *name; /* OVS_DP_ATTR_NAME. */
76 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
77 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
80 static void dpif_linux_dp_init(struct dpif_linux_dp *);
81 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
82 const struct ofpbuf *);
83 static void dpif_linux_dp_dump_start(struct nl_dump *);
84 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
85 struct dpif_linux_dp *reply,
86 struct ofpbuf **bufp);
87 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
88 struct ofpbuf **bufp);
90 struct dpif_linux_flow {
91 /* Generic Netlink header. */
94 /* struct ovs_header. */
95 unsigned int nlmsg_flags;
100 * The 'stats' member points to 64-bit data that might only be aligned on
101 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
104 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
105 * the Netlink version of the command, even if actions_len is zero. */
106 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
108 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
110 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
111 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
112 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
113 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
116 static void dpif_linux_flow_init(struct dpif_linux_flow *);
117 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
118 const struct ofpbuf *);
119 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
121 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
122 struct dpif_linux_flow *reply,
123 struct ofpbuf **bufp);
124 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
125 struct dpif_flow_stats *);
127 /* One of the dpif channels between the kernel and userspace. */
128 struct dpif_channel {
129 struct nl_sock *sock; /* Netlink socket. */
130 long long int last_poll; /* Last time this channel was polled. */
133 static void report_loss(struct dpif *, struct dpif_channel *);
135 /* Datapath interface for the openvswitch Linux kernel module. */
140 /* Upcall messages. */
141 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
142 struct dpif_channel *channels;
143 struct epoll_event *epoll_events;
144 int epoll_fd; /* epoll fd that includes channel socks. */
145 int n_events; /* Num events returned by epoll_wait(). */
146 int event_offset; /* Offset into 'epoll_events'. */
148 /* Change notification. */
149 struct sset changed_ports; /* Ports that have changed. */
150 struct nln_notifier *port_notifier;
154 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
156 /* Generic Netlink family numbers for OVS. */
157 static int ovs_datapath_family;
158 static int ovs_vport_family;
159 static int ovs_flow_family;
160 static int ovs_packet_family;
162 /* Generic Netlink socket. */
163 static struct nl_sock *genl_sock;
164 static struct nln *nln = NULL;
166 static int dpif_linux_init(void);
167 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
168 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
169 static void dpif_linux_port_changed(const void *vport, void *dpif);
170 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint32_t port_no);
172 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
174 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
175 const struct ofpbuf *);
177 static struct dpif_linux *
178 dpif_linux_cast(const struct dpif *dpif)
180 dpif_assert_class(dpif, &dpif_linux_class);
181 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
185 dpif_linux_enumerate(struct sset *all_dps)
191 error = dpif_linux_init();
196 dpif_linux_dp_dump_start(&dump);
197 while (nl_dump_next(&dump, &msg)) {
198 struct dpif_linux_dp dp;
200 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
201 sset_add(all_dps, dp.name);
204 return nl_dump_done(&dump);
208 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
209 bool create, struct dpif **dpifp)
211 struct dpif_linux_dp dp_request, dp;
216 error = dpif_linux_init();
221 /* Create or look up datapath. */
222 dpif_linux_dp_init(&dp_request);
224 dp_request.cmd = OVS_DP_CMD_NEW;
226 dp_request.upcall_pid = &upcall_pid;
228 dp_request.cmd = OVS_DP_CMD_GET;
230 dp_request.name = name;
231 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
236 open_dpif(&dp, dpifp);
242 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
244 struct dpif_linux *dpif;
246 dpif = xzalloc(sizeof *dpif);
247 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
251 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
252 dp->dp_ifindex, dp->dp_ifindex);
254 dpif->dp_ifindex = dp->dp_ifindex;
255 sset_init(&dpif->changed_ports);
256 *dpifp = &dpif->dpif;
260 destroy_channels(struct dpif_linux *dpif)
264 if (dpif->epoll_fd < 0) {
268 for (i = 0; i < dpif->uc_array_size; i++ ) {
269 struct dpif_linux_vport vport_request;
270 struct dpif_channel *ch = &dpif->channels[i];
271 uint32_t upcall_pid = 0;
277 /* Turn off upcalls. */
278 dpif_linux_vport_init(&vport_request);
279 vport_request.cmd = OVS_VPORT_CMD_SET;
280 vport_request.dp_ifindex = dpif->dp_ifindex;
281 vport_request.port_no = i;
282 vport_request.upcall_pid = &upcall_pid;
283 dpif_linux_vport_transact(&vport_request, NULL, NULL);
285 nl_sock_destroy(ch->sock);
288 free(dpif->channels);
289 dpif->channels = NULL;
290 dpif->uc_array_size = 0;
292 free(dpif->epoll_events);
293 dpif->epoll_events = NULL;
294 dpif->n_events = dpif->event_offset = 0;
296 close(dpif->epoll_fd);
301 add_channel(struct dpif_linux *dpif, uint32_t port_no, struct nl_sock *sock)
303 struct epoll_event event;
305 if (dpif->epoll_fd < 0) {
309 /* We assume that the datapath densely chooses port numbers, which
310 * can therefore be used as an index into an array of channels. */
311 if (port_no >= dpif->uc_array_size) {
312 int new_size = port_no + 1;
315 if (new_size > 65535) {
316 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
317 dpif_name(&dpif->dpif), port_no);
321 dpif->channels = xrealloc(dpif->channels,
322 new_size * sizeof *dpif->channels);
323 for (i = dpif->uc_array_size; i < new_size; i++) {
324 dpif->channels[i].sock = NULL;
327 dpif->epoll_events = xrealloc(dpif->epoll_events,
328 new_size * sizeof *dpif->epoll_events);
329 dpif->uc_array_size = new_size;
332 memset(&event, 0, sizeof event);
333 event.events = EPOLLIN;
334 event.data.u32 = port_no;
335 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
340 dpif->channels[port_no].sock = sock;
341 dpif->channels[port_no].last_poll = LLONG_MIN;
347 del_channel(struct dpif_linux *dpif, uint32_t port_no)
349 struct dpif_channel *ch;
351 if (dpif->epoll_fd < 0 || port_no >= dpif->uc_array_size) {
355 ch = &dpif->channels[port_no];
360 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
362 nl_sock_destroy(ch->sock);
367 dpif_linux_close(struct dpif *dpif_)
369 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
371 nln_notifier_destroy(dpif->port_notifier);
372 destroy_channels(dpif);
373 sset_destroy(&dpif->changed_ports);
378 dpif_linux_destroy(struct dpif *dpif_)
380 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
381 struct dpif_linux_dp dp;
383 dpif_linux_dp_init(&dp);
384 dp.cmd = OVS_DP_CMD_DEL;
385 dp.dp_ifindex = dpif->dp_ifindex;
386 return dpif_linux_dp_transact(&dp, NULL, NULL);
390 dpif_linux_run(struct dpif *dpif_ OVS_UNUSED)
398 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
406 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
408 struct dpif_linux_dp dp;
412 error = dpif_linux_dp_get(dpif_, &dp, &buf);
414 stats->n_hit = dp.stats.n_hit;
415 stats->n_missed = dp.stats.n_missed;
416 stats->n_lost = dp.stats.n_lost;
417 stats->n_flows = dp.stats.n_flows;
424 get_vport_type(const struct dpif_linux_vport *vport)
426 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
428 switch (vport->type) {
429 case OVS_VPORT_TYPE_NETDEV:
432 case OVS_VPORT_TYPE_INTERNAL:
435 case OVS_VPORT_TYPE_GRE:
438 case OVS_VPORT_TYPE_GRE64:
441 case OVS_VPORT_TYPE_VXLAN:
444 case OVS_VPORT_TYPE_LISP:
447 case OVS_VPORT_TYPE_UNSPEC:
448 case __OVS_VPORT_TYPE_MAX:
452 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
453 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
457 static enum ovs_vport_type
458 netdev_to_ovs_vport_type(const struct netdev *netdev)
460 const char *type = netdev_get_type(netdev);
462 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
463 return OVS_VPORT_TYPE_NETDEV;
464 } else if (!strcmp(type, "internal")) {
465 return OVS_VPORT_TYPE_INTERNAL;
466 } else if (strstr(type, "gre64")) {
467 return OVS_VPORT_TYPE_GRE64;
468 } else if (strstr(type, "gre")) {
469 return OVS_VPORT_TYPE_GRE;
470 } else if (!strcmp(type, "vxlan")) {
471 return OVS_VPORT_TYPE_VXLAN;
472 } else if (!strcmp(type, "lisp")) {
473 return OVS_VPORT_TYPE_LISP;
475 return OVS_VPORT_TYPE_UNSPEC;
480 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
483 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
484 const struct netdev_tunnel_config *tnl_cfg;
485 const char *name = netdev_vport_get_dpif_port(netdev);
486 const char *type = netdev_get_type(netdev);
487 struct dpif_linux_vport request, reply;
488 struct nl_sock *sock = NULL;
491 uint64_t options_stub[64 / 8];
492 struct ofpbuf options;
495 if (dpif->epoll_fd >= 0) {
496 error = nl_sock_create(NETLINK_GENERIC, &sock);
502 dpif_linux_vport_init(&request);
503 request.cmd = OVS_VPORT_CMD_NEW;
504 request.dp_ifindex = dpif->dp_ifindex;
505 request.type = netdev_to_ovs_vport_type(netdev);
506 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
507 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
508 "unsupported type `%s'",
509 dpif_name(dpif_), name, type);
510 nl_sock_destroy(sock);
515 if (request.type == OVS_VPORT_TYPE_NETDEV) {
516 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
519 tnl_cfg = netdev_get_tunnel_config(netdev);
520 if (tnl_cfg && tnl_cfg->dst_port != 0) {
521 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
522 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
523 ntohs(tnl_cfg->dst_port));
524 request.options = options.data;
525 request.options_len = options.size;
528 request.port_no = *port_nop;
529 upcall_pid = sock ? nl_sock_pid(sock) : 0;
530 request.upcall_pid = &upcall_pid;
532 error = dpif_linux_vport_transact(&request, &reply, &buf);
534 *port_nop = reply.port_no;
535 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
536 dpif_name(dpif_), reply.port_no, upcall_pid);
538 if (error == EBUSY && *port_nop != UINT32_MAX) {
539 VLOG_INFO("%s: requested port %"PRIu32" is in use",
540 dpif_name(dpif_), *port_nop);
542 nl_sock_destroy(sock);
549 error = add_channel(dpif, *port_nop, sock);
551 VLOG_INFO("%s: could not add channel for port %s",
552 dpif_name(dpif_), name);
554 /* Delete the port. */
555 dpif_linux_vport_init(&request);
556 request.cmd = OVS_VPORT_CMD_DEL;
557 request.dp_ifindex = dpif->dp_ifindex;
558 request.port_no = *port_nop;
559 dpif_linux_vport_transact(&request, NULL, NULL);
561 nl_sock_destroy(sock);
570 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
572 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
573 struct dpif_linux_vport vport;
576 dpif_linux_vport_init(&vport);
577 vport.cmd = OVS_VPORT_CMD_DEL;
578 vport.dp_ifindex = dpif->dp_ifindex;
579 vport.port_no = port_no;
580 error = dpif_linux_vport_transact(&vport, NULL, NULL);
582 del_channel(dpif, port_no);
588 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
589 const char *port_name, struct dpif_port *dpif_port)
591 struct dpif_linux_vport request;
592 struct dpif_linux_vport reply;
596 dpif_linux_vport_init(&request);
597 request.cmd = OVS_VPORT_CMD_GET;
598 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
599 request.port_no = port_no;
600 request.name = port_name;
602 error = dpif_linux_vport_transact(&request, &reply, &buf);
604 if (reply.dp_ifindex != request.dp_ifindex) {
605 /* A query by name reported that 'port_name' is in some datapath
606 * other than 'dpif', but the caller wants to know about 'dpif'. */
608 } else if (dpif_port) {
609 dpif_port->name = xstrdup(reply.name);
610 dpif_port->type = xstrdup(get_vport_type(&reply));
611 dpif_port->port_no = reply.port_no;
619 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
620 struct dpif_port *dpif_port)
622 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
626 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
627 struct dpif_port *dpif_port)
629 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
633 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
639 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
641 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
643 if (dpif->epoll_fd < 0) {
646 /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
647 * channel, since it is not heavily loaded. */
648 int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
649 return nl_sock_pid(dpif->channels[idx].sock);
654 dpif_linux_flow_flush(struct dpif *dpif_)
656 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
657 struct dpif_linux_flow flow;
659 dpif_linux_flow_init(&flow);
660 flow.cmd = OVS_FLOW_CMD_DEL;
661 flow.dp_ifindex = dpif->dp_ifindex;
662 return dpif_linux_flow_transact(&flow, NULL, NULL);
665 struct dpif_linux_port_state {
670 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
672 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
673 struct dpif_linux_port_state *state;
674 struct dpif_linux_vport request;
677 *statep = state = xmalloc(sizeof *state);
679 dpif_linux_vport_init(&request);
680 request.cmd = OVS_DP_CMD_GET;
681 request.dp_ifindex = dpif->dp_ifindex;
683 buf = ofpbuf_new(1024);
684 dpif_linux_vport_to_ofpbuf(&request, buf);
685 nl_dump_start(&state->dump, genl_sock, buf);
692 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
693 struct dpif_port *dpif_port)
695 struct dpif_linux_port_state *state = state_;
696 struct dpif_linux_vport vport;
700 if (!nl_dump_next(&state->dump, &buf)) {
704 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
709 dpif_port->name = CONST_CAST(char *, vport.name);
710 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
711 dpif_port->port_no = vport.port_no;
716 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
718 struct dpif_linux_port_state *state = state_;
719 int error = nl_dump_done(&state->dump);
726 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
728 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
730 if (dpif->change_error) {
731 dpif->change_error = false;
732 sset_clear(&dpif->changed_ports);
734 } else if (!sset_is_empty(&dpif->changed_ports)) {
735 *devnamep = sset_pop(&dpif->changed_ports);
743 dpif_linux_port_poll_wait(const struct dpif *dpif_)
745 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
746 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
747 poll_immediate_wake();
752 dpif_linux_flow_get__(const struct dpif *dpif_,
753 const struct nlattr *key, size_t key_len,
754 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
756 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
757 struct dpif_linux_flow request;
759 dpif_linux_flow_init(&request);
760 request.cmd = OVS_FLOW_CMD_GET;
761 request.dp_ifindex = dpif->dp_ifindex;
763 request.key_len = key_len;
764 return dpif_linux_flow_transact(&request, reply, bufp);
768 dpif_linux_flow_get(const struct dpif *dpif_,
769 const struct nlattr *key, size_t key_len,
770 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
772 struct dpif_linux_flow reply;
776 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
779 dpif_linux_flow_get_stats(&reply, stats);
782 buf->data = CONST_CAST(struct nlattr *, reply.actions);
783 buf->size = reply.actions_len;
793 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
794 struct dpif_linux_flow *request)
796 static struct nlattr dummy_action;
798 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
800 dpif_linux_flow_init(request);
801 request->cmd = (put->flags & DPIF_FP_CREATE
802 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
803 request->dp_ifindex = dpif->dp_ifindex;
804 request->key = put->key;
805 request->key_len = put->key_len;
806 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
807 request->actions = put->actions ? put->actions : &dummy_action;
808 request->actions_len = put->actions_len;
809 if (put->flags & DPIF_FP_ZERO_STATS) {
810 request->clear = true;
812 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
816 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
818 struct dpif_linux_flow request, reply;
822 dpif_linux_init_flow_put(dpif_, put, &request);
823 error = dpif_linux_flow_transact(&request,
824 put->stats ? &reply : NULL,
825 put->stats ? &buf : NULL);
826 if (!error && put->stats) {
827 dpif_linux_flow_get_stats(&reply, put->stats);
834 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
835 struct dpif_linux_flow *request)
837 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
839 dpif_linux_flow_init(request);
840 request->cmd = OVS_FLOW_CMD_DEL;
841 request->dp_ifindex = dpif->dp_ifindex;
842 request->key = del->key;
843 request->key_len = del->key_len;
847 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
849 struct dpif_linux_flow request, reply;
853 dpif_linux_init_flow_del(dpif_, del, &request);
854 error = dpif_linux_flow_transact(&request,
855 del->stats ? &reply : NULL,
856 del->stats ? &buf : NULL);
857 if (!error && del->stats) {
858 dpif_linux_flow_get_stats(&reply, del->stats);
864 struct dpif_linux_flow_state {
866 struct dpif_linux_flow flow;
867 struct dpif_flow_stats stats;
872 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
874 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
875 struct dpif_linux_flow_state *state;
876 struct dpif_linux_flow request;
879 *statep = state = xmalloc(sizeof *state);
881 dpif_linux_flow_init(&request);
882 request.cmd = OVS_DP_CMD_GET;
883 request.dp_ifindex = dpif->dp_ifindex;
885 buf = ofpbuf_new(1024);
886 dpif_linux_flow_to_ofpbuf(&request, buf);
887 nl_dump_start(&state->dump, genl_sock, buf);
896 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
897 const struct nlattr **key, size_t *key_len,
898 const struct nlattr **actions, size_t *actions_len,
899 const struct dpif_flow_stats **stats)
901 struct dpif_linux_flow_state *state = state_;
906 ofpbuf_delete(state->buf);
909 if (!nl_dump_next(&state->dump, &buf)) {
913 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
918 if (actions && !state->flow.actions) {
919 error = dpif_linux_flow_get__(dpif_, state->flow.key,
921 &state->flow, &state->buf);
922 if (error == ENOENT) {
923 VLOG_DBG("dumped flow disappeared on get");
925 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
931 *actions = state->flow.actions;
932 *actions_len = state->flow.actions_len;
935 *key = state->flow.key;
936 *key_len = state->flow.key_len;
939 dpif_linux_flow_get_stats(&state->flow, &state->stats);
940 *stats = &state->stats;
946 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
948 struct dpif_linux_flow_state *state = state_;
949 int error = nl_dump_done(&state->dump);
950 ofpbuf_delete(state->buf);
956 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
959 struct ovs_header *k_exec;
961 ofpbuf_prealloc_tailroom(buf, (64
962 + d_exec->packet->size
964 + d_exec->actions_len));
966 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
967 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
969 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
970 k_exec->dp_ifindex = dp_ifindex;
972 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
973 d_exec->packet->data, d_exec->packet->size);
974 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
975 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
976 d_exec->actions, d_exec->actions_len);
980 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
982 uint64_t request_stub[1024 / 8];
983 struct ofpbuf request;
986 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
987 dpif_linux_encode_execute(dp_ifindex, execute, &request);
988 error = nl_sock_transact(genl_sock, &request, NULL);
989 ofpbuf_uninit(&request);
995 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
997 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
999 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1005 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1007 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1010 struct nl_transaction txn;
1012 struct ofpbuf request;
1013 uint64_t request_stub[1024 / 8];
1015 struct ofpbuf reply;
1016 uint64_t reply_stub[1024 / 8];
1019 struct nl_transaction *txnsp[MAX_OPS];
1022 ovs_assert(n_ops <= MAX_OPS);
1023 for (i = 0; i < n_ops; i++) {
1024 struct op_auxdata *aux = &auxes[i];
1025 struct dpif_op *op = ops[i];
1026 struct dpif_flow_put *put;
1027 struct dpif_flow_del *del;
1028 struct dpif_execute *execute;
1029 struct dpif_linux_flow flow;
1031 ofpbuf_use_stub(&aux->request,
1032 aux->request_stub, sizeof aux->request_stub);
1033 aux->txn.request = &aux->request;
1035 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1036 aux->txn.reply = NULL;
1039 case DPIF_OP_FLOW_PUT:
1040 put = &op->u.flow_put;
1041 dpif_linux_init_flow_put(dpif_, put, &flow);
1043 flow.nlmsg_flags |= NLM_F_ECHO;
1044 aux->txn.reply = &aux->reply;
1046 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1049 case DPIF_OP_FLOW_DEL:
1050 del = &op->u.flow_del;
1051 dpif_linux_init_flow_del(dpif_, del, &flow);
1053 flow.nlmsg_flags |= NLM_F_ECHO;
1054 aux->txn.reply = &aux->reply;
1056 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1059 case DPIF_OP_EXECUTE:
1060 execute = &op->u.execute;
1061 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1070 for (i = 0; i < n_ops; i++) {
1071 txnsp[i] = &auxes[i].txn;
1073 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1075 for (i = 0; i < n_ops; i++) {
1076 struct op_auxdata *aux = &auxes[i];
1077 struct nl_transaction *txn = &auxes[i].txn;
1078 struct dpif_op *op = ops[i];
1079 struct dpif_flow_put *put;
1080 struct dpif_flow_del *del;
1082 op->error = txn->error;
1085 case DPIF_OP_FLOW_PUT:
1086 put = &op->u.flow_put;
1089 struct dpif_linux_flow reply;
1091 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1094 dpif_linux_flow_get_stats(&reply, put->stats);
1099 memset(put->stats, 0, sizeof *put->stats);
1104 case DPIF_OP_FLOW_DEL:
1105 del = &op->u.flow_del;
1108 struct dpif_linux_flow reply;
1110 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1113 dpif_linux_flow_get_stats(&reply, del->stats);
1118 memset(del->stats, 0, sizeof *del->stats);
1123 case DPIF_OP_EXECUTE:
1130 ofpbuf_uninit(&aux->request);
1131 ofpbuf_uninit(&aux->reply);
1136 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1139 size_t chunk = MIN(n_ops, MAX_OPS);
1140 dpif_linux_operate__(dpif, ops, chunk);
1147 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1149 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1151 if ((dpif->epoll_fd >= 0) == enable) {
1156 destroy_channels(dpif);
1158 struct dpif_port_dump port_dump;
1159 struct dpif_port port;
1161 dpif->epoll_fd = epoll_create(10);
1162 if (dpif->epoll_fd < 0) {
1166 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1167 struct dpif_linux_vport vport_request;
1168 struct nl_sock *sock;
1169 uint32_t upcall_pid;
1172 error = nl_sock_create(NETLINK_GENERIC, &sock);
1177 upcall_pid = nl_sock_pid(sock);
1179 dpif_linux_vport_init(&vport_request);
1180 vport_request.cmd = OVS_VPORT_CMD_SET;
1181 vport_request.dp_ifindex = dpif->dp_ifindex;
1182 vport_request.port_no = port.port_no;
1183 vport_request.upcall_pid = &upcall_pid;
1184 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1186 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1187 dpif_name(&dpif->dpif), vport_request.port_no,
1190 VLOG_WARN_RL(&error_rl,
1191 "%s: failed to set upcall pid on port: %s",
1192 dpif_name(&dpif->dpif), strerror(error));
1193 nl_sock_destroy(sock);
1195 if (error == ENODEV || error == ENOENT) {
1196 /* This device isn't there, but keep trying the others. */
1203 error = add_channel(dpif, port.port_no, sock);
1205 VLOG_INFO("%s: could not add channel for port %s",
1206 dpif_name(dpif_), port.name);
1207 nl_sock_destroy(sock);
1217 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1218 uint32_t queue_id, uint32_t *priority)
1220 if (queue_id < 0xf000) {
1221 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1229 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1232 static const struct nl_policy ovs_packet_policy[] = {
1233 /* Always present. */
1234 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1235 .min_len = ETH_HEADER_LEN },
1236 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1238 /* OVS_PACKET_CMD_ACTION only. */
1239 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1242 struct ovs_header *ovs_header;
1243 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1244 struct nlmsghdr *nlmsg;
1245 struct genlmsghdr *genl;
1249 ofpbuf_use_const(&b, buf->data, buf->size);
1251 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1252 genl = ofpbuf_try_pull(&b, sizeof *genl);
1253 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1254 if (!nlmsg || !genl || !ovs_header
1255 || nlmsg->nlmsg_type != ovs_packet_family
1256 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1257 ARRAY_SIZE(ovs_packet_policy))) {
1261 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1262 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1268 memset(upcall, 0, sizeof *upcall);
1269 upcall->type = type;
1270 upcall->packet = buf;
1271 upcall->packet->data = CONST_CAST(struct nlattr *,
1272 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1273 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1274 upcall->key = CONST_CAST(struct nlattr *,
1275 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1276 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1277 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1278 *dp_ifindex = ovs_header->dp_ifindex;
1284 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1287 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1290 if (dpif->epoll_fd < 0) {
1294 if (dpif->event_offset >= dpif->n_events) {
1297 dpif->event_offset = dpif->n_events = 0;
1300 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1301 dpif->uc_array_size, 0);
1302 } while (retval < 0 && errno == EINTR);
1304 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1305 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1306 } else if (retval > 0) {
1307 dpif->n_events = retval;
1311 while (dpif->event_offset < dpif->n_events) {
1312 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1313 struct dpif_channel *ch = &dpif->channels[idx];
1315 dpif->event_offset++;
1321 if (++read_tries > 50) {
1325 error = nl_sock_recv(ch->sock, buf, false);
1326 if (error == ENOBUFS) {
1327 /* ENOBUFS typically means that we've received so many
1328 * packets that the buffer overflowed. Try again
1329 * immediately because there's almost certainly a packet
1330 * waiting for us. */
1331 report_loss(dpif_, ch);
1335 ch->last_poll = time_msec();
1337 if (error == EAGAIN) {
1343 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1344 if (!error && dp_ifindex == dpif->dp_ifindex) {
1356 dpif_linux_recv_wait(struct dpif *dpif_)
1358 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1360 if (dpif->epoll_fd < 0) {
1364 poll_fd_wait(dpif->epoll_fd, POLLIN);
1368 dpif_linux_recv_purge(struct dpif *dpif_)
1370 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1371 struct dpif_channel *ch;
1373 if (dpif->epoll_fd < 0) {
1377 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1379 nl_sock_drain(ch->sock);
1384 const struct dpif_class dpif_linux_class = {
1386 dpif_linux_enumerate,
1393 dpif_linux_get_stats,
1394 dpif_linux_port_add,
1395 dpif_linux_port_del,
1396 dpif_linux_port_query_by_number,
1397 dpif_linux_port_query_by_name,
1398 dpif_linux_get_max_ports,
1399 dpif_linux_port_get_pid,
1400 dpif_linux_port_dump_start,
1401 dpif_linux_port_dump_next,
1402 dpif_linux_port_dump_done,
1403 dpif_linux_port_poll,
1404 dpif_linux_port_poll_wait,
1405 dpif_linux_flow_get,
1406 dpif_linux_flow_put,
1407 dpif_linux_flow_del,
1408 dpif_linux_flow_flush,
1409 dpif_linux_flow_dump_start,
1410 dpif_linux_flow_dump_next,
1411 dpif_linux_flow_dump_done,
1414 dpif_linux_recv_set,
1415 dpif_linux_queue_to_priority,
1417 dpif_linux_recv_wait,
1418 dpif_linux_recv_purge,
1422 dpif_linux_init(void)
1424 static int error = -1;
1427 unsigned int ovs_vport_mcgroup;
1429 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1430 &ovs_datapath_family);
1432 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1433 "The Open vSwitch kernel module is probably not loaded.",
1434 OVS_DATAPATH_FAMILY);
1437 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1440 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1443 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1444 &ovs_packet_family);
1447 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1450 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1452 OVS_VPORT_MCGROUP_FALLBACK_ID);
1455 static struct dpif_linux_vport vport;
1456 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1457 dpif_linux_nln_parse, &vport);
1465 dpif_linux_is_internal_device(const char *name)
1467 struct dpif_linux_vport reply;
1471 error = dpif_linux_vport_get(name, &reply, &buf);
1474 } else if (error != ENODEV && error != ENOENT) {
1475 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1476 name, strerror(error));
1479 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1483 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1485 struct dpif_linux_vport *vport = vport_;
1486 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1490 dpif_linux_port_changed(const void *vport_, void *dpif_)
1492 const struct dpif_linux_vport *vport = vport_;
1493 struct dpif_linux *dpif = dpif_;
1496 if (vport->dp_ifindex == dpif->dp_ifindex
1497 && (vport->cmd == OVS_VPORT_CMD_NEW
1498 || vport->cmd == OVS_VPORT_CMD_DEL
1499 || vport->cmd == OVS_VPORT_CMD_SET)) {
1500 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1501 dpif->dpif.full_name, vport->name, vport->cmd);
1502 sset_add(&dpif->changed_ports, vport->name);
1505 dpif->change_error = true;
1509 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1510 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1511 * positive errno value.
1513 * 'vport' will contain pointers into 'buf', so the caller should not free
1514 * 'buf' while 'vport' is still in use. */
1516 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1517 const struct ofpbuf *buf)
1519 static const struct nl_policy ovs_vport_policy[] = {
1520 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1521 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1522 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1523 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1524 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1526 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1529 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1530 struct ovs_header *ovs_header;
1531 struct nlmsghdr *nlmsg;
1532 struct genlmsghdr *genl;
1535 dpif_linux_vport_init(vport);
1537 ofpbuf_use_const(&b, buf->data, buf->size);
1538 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1539 genl = ofpbuf_try_pull(&b, sizeof *genl);
1540 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1541 if (!nlmsg || !genl || !ovs_header
1542 || nlmsg->nlmsg_type != ovs_vport_family
1543 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1544 ARRAY_SIZE(ovs_vport_policy))) {
1548 vport->cmd = genl->cmd;
1549 vport->dp_ifindex = ovs_header->dp_ifindex;
1550 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1551 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1552 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1553 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1554 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1556 if (a[OVS_VPORT_ATTR_STATS]) {
1557 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1559 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1560 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1561 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1566 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1567 * followed by Netlink attributes corresponding to 'vport'. */
1569 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1572 struct ovs_header *ovs_header;
1574 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1575 vport->cmd, OVS_VPORT_VERSION);
1577 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1578 ovs_header->dp_ifindex = vport->dp_ifindex;
1580 if (vport->port_no != UINT32_MAX) {
1581 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1584 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1585 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1589 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1592 if (vport->upcall_pid) {
1593 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1597 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1598 vport->stats, sizeof *vport->stats);
1601 if (vport->options) {
1602 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1603 vport->options, vport->options_len);
1607 /* Clears 'vport' to "empty" values. */
1609 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1611 memset(vport, 0, sizeof *vport);
1612 vport->port_no = UINT32_MAX;
1615 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1616 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1617 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1618 * result of the command is expected to be an ovs_vport also, which is decoded
1619 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1620 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1622 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1623 struct dpif_linux_vport *reply,
1624 struct ofpbuf **bufp)
1626 struct ofpbuf *request_buf;
1629 ovs_assert((reply != NULL) == (bufp != NULL));
1631 error = dpif_linux_init();
1635 dpif_linux_vport_init(reply);
1640 request_buf = ofpbuf_new(1024);
1641 dpif_linux_vport_to_ofpbuf(request, request_buf);
1642 error = nl_sock_transact(genl_sock, request_buf, bufp);
1643 ofpbuf_delete(request_buf);
1647 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1650 dpif_linux_vport_init(reply);
1651 ofpbuf_delete(*bufp);
1658 /* Obtains information about the kernel vport named 'name' and stores it into
1659 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1660 * longer needed ('reply' will contain pointers into '*bufp'). */
1662 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1663 struct ofpbuf **bufp)
1665 struct dpif_linux_vport request;
1667 dpif_linux_vport_init(&request);
1668 request.cmd = OVS_VPORT_CMD_GET;
1669 request.name = name;
1671 return dpif_linux_vport_transact(&request, reply, bufp);
1674 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1675 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1676 * positive errno value.
1678 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1679 * while 'dp' is still in use. */
1681 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1683 static const struct nl_policy ovs_datapath_policy[] = {
1684 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1685 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1689 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1690 struct ovs_header *ovs_header;
1691 struct nlmsghdr *nlmsg;
1692 struct genlmsghdr *genl;
1695 dpif_linux_dp_init(dp);
1697 ofpbuf_use_const(&b, buf->data, buf->size);
1698 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1699 genl = ofpbuf_try_pull(&b, sizeof *genl);
1700 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1701 if (!nlmsg || !genl || !ovs_header
1702 || nlmsg->nlmsg_type != ovs_datapath_family
1703 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1704 ARRAY_SIZE(ovs_datapath_policy))) {
1708 dp->cmd = genl->cmd;
1709 dp->dp_ifindex = ovs_header->dp_ifindex;
1710 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1711 if (a[OVS_DP_ATTR_STATS]) {
1712 /* Can't use structure assignment because Netlink doesn't ensure
1713 * sufficient alignment for 64-bit members. */
1714 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1721 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1723 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1725 struct ovs_header *ovs_header;
1727 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1728 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1729 OVS_DATAPATH_VERSION);
1731 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1732 ovs_header->dp_ifindex = dp->dp_ifindex;
1735 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1738 if (dp->upcall_pid) {
1739 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1742 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1745 /* Clears 'dp' to "empty" values. */
1747 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1749 memset(dp, 0, sizeof *dp);
1753 dpif_linux_dp_dump_start(struct nl_dump *dump)
1755 struct dpif_linux_dp request;
1758 dpif_linux_dp_init(&request);
1759 request.cmd = OVS_DP_CMD_GET;
1761 buf = ofpbuf_new(1024);
1762 dpif_linux_dp_to_ofpbuf(&request, buf);
1763 nl_dump_start(dump, genl_sock, buf);
1767 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1768 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1769 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1770 * result of the command is expected to be of the same form, which is decoded
1771 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1772 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1774 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1775 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1777 struct ofpbuf *request_buf;
1780 ovs_assert((reply != NULL) == (bufp != NULL));
1782 request_buf = ofpbuf_new(1024);
1783 dpif_linux_dp_to_ofpbuf(request, request_buf);
1784 error = nl_sock_transact(genl_sock, request_buf, bufp);
1785 ofpbuf_delete(request_buf);
1789 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1792 dpif_linux_dp_init(reply);
1793 ofpbuf_delete(*bufp);
1800 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1801 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1802 * will contain pointers into '*bufp'). */
1804 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1805 struct ofpbuf **bufp)
1807 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1808 struct dpif_linux_dp request;
1810 dpif_linux_dp_init(&request);
1811 request.cmd = OVS_DP_CMD_GET;
1812 request.dp_ifindex = dpif->dp_ifindex;
1814 return dpif_linux_dp_transact(&request, reply, bufp);
1817 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1818 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1819 * positive errno value.
1821 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1822 * while 'flow' is still in use. */
1824 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1825 const struct ofpbuf *buf)
1827 static const struct nl_policy ovs_flow_policy[] = {
1828 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1829 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1830 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1832 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1833 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1834 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1837 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1838 struct ovs_header *ovs_header;
1839 struct nlmsghdr *nlmsg;
1840 struct genlmsghdr *genl;
1843 dpif_linux_flow_init(flow);
1845 ofpbuf_use_const(&b, buf->data, buf->size);
1846 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1847 genl = ofpbuf_try_pull(&b, sizeof *genl);
1848 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1849 if (!nlmsg || !genl || !ovs_header
1850 || nlmsg->nlmsg_type != ovs_flow_family
1851 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1852 ARRAY_SIZE(ovs_flow_policy))) {
1856 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1857 flow->dp_ifindex = ovs_header->dp_ifindex;
1858 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1859 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1860 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1861 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1862 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1864 if (a[OVS_FLOW_ATTR_STATS]) {
1865 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1867 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1868 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1870 if (a[OVS_FLOW_ATTR_USED]) {
1871 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1876 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1877 * followed by Netlink attributes corresponding to 'flow'. */
1879 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1882 struct ovs_header *ovs_header;
1884 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1885 NLM_F_REQUEST | flow->nlmsg_flags,
1886 flow->cmd, OVS_FLOW_VERSION);
1888 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1889 ovs_header->dp_ifindex = flow->dp_ifindex;
1891 if (flow->key_len) {
1892 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1895 if (flow->actions || flow->actions_len) {
1896 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1897 flow->actions, flow->actions_len);
1900 /* We never need to send these to the kernel. */
1901 ovs_assert(!flow->stats);
1902 ovs_assert(!flow->tcp_flags);
1903 ovs_assert(!flow->used);
1906 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1910 /* Clears 'flow' to "empty" values. */
1912 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1914 memset(flow, 0, sizeof *flow);
1917 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1918 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1919 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1920 * result of the command is expected to be a flow also, which is decoded and
1921 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1922 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1924 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1925 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1927 struct ofpbuf *request_buf;
1930 ovs_assert((reply != NULL) == (bufp != NULL));
1933 request->nlmsg_flags |= NLM_F_ECHO;
1936 request_buf = ofpbuf_new(1024);
1937 dpif_linux_flow_to_ofpbuf(request, request_buf);
1938 error = nl_sock_transact(genl_sock, request_buf, bufp);
1939 ofpbuf_delete(request_buf);
1943 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1946 dpif_linux_flow_init(reply);
1947 ofpbuf_delete(*bufp);
1955 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1956 struct dpif_flow_stats *stats)
1959 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1960 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1962 stats->n_packets = 0;
1965 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1966 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1969 /* Logs information about a packet that was recently lost in 'ch' (in
1972 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1974 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1975 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1978 if (VLOG_DROP_WARN(&rl)) {
1983 if (ch->last_poll != LLONG_MIN) {
1984 ds_put_format(&s, " (last polled %lld ms ago)",
1985 time_msec() - ch->last_poll);
1988 VLOG_WARN("%s: lost packet on channel %td%s",
1989 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));