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 nl_sock_destroy(dpif->channels[port_no].sock);
341 dpif->channels[port_no].sock = sock;
342 dpif->channels[port_no].last_poll = LLONG_MIN;
348 del_channel(struct dpif_linux *dpif, uint32_t port_no)
350 struct dpif_channel *ch;
352 if (dpif->epoll_fd < 0 || port_no >= dpif->uc_array_size) {
356 ch = &dpif->channels[port_no];
361 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
362 dpif->event_offset = dpif->n_events = 0;
364 nl_sock_destroy(ch->sock);
369 dpif_linux_close(struct dpif *dpif_)
371 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
373 nln_notifier_destroy(dpif->port_notifier);
374 destroy_channels(dpif);
375 sset_destroy(&dpif->changed_ports);
380 dpif_linux_destroy(struct dpif *dpif_)
382 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
383 struct dpif_linux_dp dp;
385 dpif_linux_dp_init(&dp);
386 dp.cmd = OVS_DP_CMD_DEL;
387 dp.dp_ifindex = dpif->dp_ifindex;
388 return dpif_linux_dp_transact(&dp, NULL, NULL);
392 dpif_linux_run(struct dpif *dpif_ OVS_UNUSED)
400 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
408 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
410 struct dpif_linux_dp dp;
414 error = dpif_linux_dp_get(dpif_, &dp, &buf);
416 stats->n_hit = dp.stats.n_hit;
417 stats->n_missed = dp.stats.n_missed;
418 stats->n_lost = dp.stats.n_lost;
419 stats->n_flows = dp.stats.n_flows;
426 get_vport_type(const struct dpif_linux_vport *vport)
428 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
430 switch (vport->type) {
431 case OVS_VPORT_TYPE_NETDEV:
434 case OVS_VPORT_TYPE_INTERNAL:
437 case OVS_VPORT_TYPE_GRE:
440 case OVS_VPORT_TYPE_GRE64:
443 case OVS_VPORT_TYPE_VXLAN:
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;
472 return OVS_VPORT_TYPE_UNSPEC;
477 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
480 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
481 const struct netdev_tunnel_config *tnl_cfg;
482 const char *name = netdev_vport_get_dpif_port(netdev);
483 const char *type = netdev_get_type(netdev);
484 struct dpif_linux_vport request, reply;
485 struct nl_sock *sock = NULL;
488 uint64_t options_stub[64 / 8];
489 struct ofpbuf options;
492 if (dpif->epoll_fd >= 0) {
493 error = nl_sock_create(NETLINK_GENERIC, &sock);
499 dpif_linux_vport_init(&request);
500 request.cmd = OVS_VPORT_CMD_NEW;
501 request.dp_ifindex = dpif->dp_ifindex;
502 request.type = netdev_to_ovs_vport_type(netdev);
503 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
504 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
505 "unsupported type `%s'",
506 dpif_name(dpif_), name, type);
507 nl_sock_destroy(sock);
512 if (request.type == OVS_VPORT_TYPE_NETDEV) {
513 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
516 tnl_cfg = netdev_get_tunnel_config(netdev);
517 if (tnl_cfg && tnl_cfg->dst_port != 0) {
518 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
519 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
520 ntohs(tnl_cfg->dst_port));
521 request.options = options.data;
522 request.options_len = options.size;
525 request.port_no = *port_nop;
526 upcall_pid = sock ? nl_sock_pid(sock) : 0;
527 request.upcall_pid = &upcall_pid;
529 error = dpif_linux_vport_transact(&request, &reply, &buf);
531 *port_nop = reply.port_no;
532 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
533 dpif_name(dpif_), reply.port_no, upcall_pid);
535 if (error == EBUSY && *port_nop != UINT32_MAX) {
536 VLOG_INFO("%s: requested port %"PRIu32" is in use",
537 dpif_name(dpif_), *port_nop);
539 nl_sock_destroy(sock);
546 error = add_channel(dpif, *port_nop, sock);
548 VLOG_INFO("%s: could not add channel for port %s",
549 dpif_name(dpif_), name);
551 /* Delete the port. */
552 dpif_linux_vport_init(&request);
553 request.cmd = OVS_VPORT_CMD_DEL;
554 request.dp_ifindex = dpif->dp_ifindex;
555 request.port_no = *port_nop;
556 dpif_linux_vport_transact(&request, NULL, NULL);
558 nl_sock_destroy(sock);
567 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
569 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
570 struct dpif_linux_vport vport;
573 dpif_linux_vport_init(&vport);
574 vport.cmd = OVS_VPORT_CMD_DEL;
575 vport.dp_ifindex = dpif->dp_ifindex;
576 vport.port_no = port_no;
577 error = dpif_linux_vport_transact(&vport, NULL, NULL);
579 del_channel(dpif, port_no);
585 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
586 const char *port_name, struct dpif_port *dpif_port)
588 struct dpif_linux_vport request;
589 struct dpif_linux_vport reply;
593 dpif_linux_vport_init(&request);
594 request.cmd = OVS_VPORT_CMD_GET;
595 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
596 request.port_no = port_no;
597 request.name = port_name;
599 error = dpif_linux_vport_transact(&request, &reply, &buf);
601 if (reply.dp_ifindex != request.dp_ifindex) {
602 /* A query by name reported that 'port_name' is in some datapath
603 * other than 'dpif', but the caller wants to know about 'dpif'. */
605 } else if (dpif_port) {
606 dpif_port->name = xstrdup(reply.name);
607 dpif_port->type = xstrdup(get_vport_type(&reply));
608 dpif_port->port_no = reply.port_no;
616 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
617 struct dpif_port *dpif_port)
619 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
623 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
624 struct dpif_port *dpif_port)
626 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
630 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
636 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
638 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
640 if (dpif->epoll_fd < 0) {
643 /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
644 * channel, since it is not heavily loaded. */
645 int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
646 return nl_sock_pid(dpif->channels[idx].sock);
651 dpif_linux_flow_flush(struct dpif *dpif_)
653 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
654 struct dpif_linux_flow flow;
656 dpif_linux_flow_init(&flow);
657 flow.cmd = OVS_FLOW_CMD_DEL;
658 flow.dp_ifindex = dpif->dp_ifindex;
659 return dpif_linux_flow_transact(&flow, NULL, NULL);
662 struct dpif_linux_port_state {
667 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
669 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
670 struct dpif_linux_port_state *state;
671 struct dpif_linux_vport request;
674 *statep = state = xmalloc(sizeof *state);
676 dpif_linux_vport_init(&request);
677 request.cmd = OVS_DP_CMD_GET;
678 request.dp_ifindex = dpif->dp_ifindex;
680 buf = ofpbuf_new(1024);
681 dpif_linux_vport_to_ofpbuf(&request, buf);
682 nl_dump_start(&state->dump, genl_sock, buf);
689 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
690 struct dpif_port *dpif_port)
692 struct dpif_linux_port_state *state = state_;
693 struct dpif_linux_vport vport;
697 if (!nl_dump_next(&state->dump, &buf)) {
701 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
706 dpif_port->name = CONST_CAST(char *, vport.name);
707 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
708 dpif_port->port_no = vport.port_no;
713 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
715 struct dpif_linux_port_state *state = state_;
716 int error = nl_dump_done(&state->dump);
723 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
725 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
727 if (dpif->change_error) {
728 dpif->change_error = false;
729 sset_clear(&dpif->changed_ports);
731 } else if (!sset_is_empty(&dpif->changed_ports)) {
732 *devnamep = sset_pop(&dpif->changed_ports);
740 dpif_linux_port_poll_wait(const struct dpif *dpif_)
742 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
743 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
744 poll_immediate_wake();
749 dpif_linux_flow_get__(const struct dpif *dpif_,
750 const struct nlattr *key, size_t key_len,
751 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
753 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
754 struct dpif_linux_flow request;
756 dpif_linux_flow_init(&request);
757 request.cmd = OVS_FLOW_CMD_GET;
758 request.dp_ifindex = dpif->dp_ifindex;
760 request.key_len = key_len;
761 return dpif_linux_flow_transact(&request, reply, bufp);
765 dpif_linux_flow_get(const struct dpif *dpif_,
766 const struct nlattr *key, size_t key_len,
767 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
769 struct dpif_linux_flow reply;
773 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
776 dpif_linux_flow_get_stats(&reply, stats);
779 buf->data = CONST_CAST(struct nlattr *, reply.actions);
780 buf->size = reply.actions_len;
790 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
791 struct dpif_linux_flow *request)
793 static struct nlattr dummy_action;
795 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
797 dpif_linux_flow_init(request);
798 request->cmd = (put->flags & DPIF_FP_CREATE
799 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
800 request->dp_ifindex = dpif->dp_ifindex;
801 request->key = put->key;
802 request->key_len = put->key_len;
803 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
804 request->actions = put->actions ? put->actions : &dummy_action;
805 request->actions_len = put->actions_len;
806 if (put->flags & DPIF_FP_ZERO_STATS) {
807 request->clear = true;
809 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
813 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
815 struct dpif_linux_flow request, reply;
819 dpif_linux_init_flow_put(dpif_, put, &request);
820 error = dpif_linux_flow_transact(&request,
821 put->stats ? &reply : NULL,
822 put->stats ? &buf : NULL);
823 if (!error && put->stats) {
824 dpif_linux_flow_get_stats(&reply, put->stats);
831 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
832 struct dpif_linux_flow *request)
834 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
836 dpif_linux_flow_init(request);
837 request->cmd = OVS_FLOW_CMD_DEL;
838 request->dp_ifindex = dpif->dp_ifindex;
839 request->key = del->key;
840 request->key_len = del->key_len;
844 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
846 struct dpif_linux_flow request, reply;
850 dpif_linux_init_flow_del(dpif_, del, &request);
851 error = dpif_linux_flow_transact(&request,
852 del->stats ? &reply : NULL,
853 del->stats ? &buf : NULL);
854 if (!error && del->stats) {
855 dpif_linux_flow_get_stats(&reply, del->stats);
861 struct dpif_linux_flow_state {
863 struct dpif_linux_flow flow;
864 struct dpif_flow_stats stats;
869 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
871 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
872 struct dpif_linux_flow_state *state;
873 struct dpif_linux_flow request;
876 *statep = state = xmalloc(sizeof *state);
878 dpif_linux_flow_init(&request);
879 request.cmd = OVS_DP_CMD_GET;
880 request.dp_ifindex = dpif->dp_ifindex;
882 buf = ofpbuf_new(1024);
883 dpif_linux_flow_to_ofpbuf(&request, buf);
884 nl_dump_start(&state->dump, genl_sock, buf);
893 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
894 const struct nlattr **key, size_t *key_len,
895 const struct nlattr **actions, size_t *actions_len,
896 const struct dpif_flow_stats **stats)
898 struct dpif_linux_flow_state *state = state_;
903 ofpbuf_delete(state->buf);
906 if (!nl_dump_next(&state->dump, &buf)) {
910 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
915 if (actions && !state->flow.actions) {
916 error = dpif_linux_flow_get__(dpif_, state->flow.key,
918 &state->flow, &state->buf);
919 if (error == ENOENT) {
920 VLOG_DBG("dumped flow disappeared on get");
922 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
928 *actions = state->flow.actions;
929 *actions_len = state->flow.actions_len;
932 *key = state->flow.key;
933 *key_len = state->flow.key_len;
936 dpif_linux_flow_get_stats(&state->flow, &state->stats);
937 *stats = &state->stats;
943 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
945 struct dpif_linux_flow_state *state = state_;
946 int error = nl_dump_done(&state->dump);
947 ofpbuf_delete(state->buf);
953 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
956 struct ovs_header *k_exec;
958 ofpbuf_prealloc_tailroom(buf, (64
959 + d_exec->packet->size
961 + d_exec->actions_len));
963 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
964 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
966 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
967 k_exec->dp_ifindex = dp_ifindex;
969 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
970 d_exec->packet->data, d_exec->packet->size);
971 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
972 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
973 d_exec->actions, d_exec->actions_len);
977 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
979 uint64_t request_stub[1024 / 8];
980 struct ofpbuf request;
983 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
984 dpif_linux_encode_execute(dp_ifindex, execute, &request);
985 error = nl_sock_transact(genl_sock, &request, NULL);
986 ofpbuf_uninit(&request);
992 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
994 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
996 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1002 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1004 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1007 struct nl_transaction txn;
1009 struct ofpbuf request;
1010 uint64_t request_stub[1024 / 8];
1012 struct ofpbuf reply;
1013 uint64_t reply_stub[1024 / 8];
1016 struct nl_transaction *txnsp[MAX_OPS];
1019 ovs_assert(n_ops <= MAX_OPS);
1020 for (i = 0; i < n_ops; i++) {
1021 struct op_auxdata *aux = &auxes[i];
1022 struct dpif_op *op = ops[i];
1023 struct dpif_flow_put *put;
1024 struct dpif_flow_del *del;
1025 struct dpif_execute *execute;
1026 struct dpif_linux_flow flow;
1028 ofpbuf_use_stub(&aux->request,
1029 aux->request_stub, sizeof aux->request_stub);
1030 aux->txn.request = &aux->request;
1032 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1033 aux->txn.reply = NULL;
1036 case DPIF_OP_FLOW_PUT:
1037 put = &op->u.flow_put;
1038 dpif_linux_init_flow_put(dpif_, put, &flow);
1040 flow.nlmsg_flags |= NLM_F_ECHO;
1041 aux->txn.reply = &aux->reply;
1043 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1046 case DPIF_OP_FLOW_DEL:
1047 del = &op->u.flow_del;
1048 dpif_linux_init_flow_del(dpif_, del, &flow);
1050 flow.nlmsg_flags |= NLM_F_ECHO;
1051 aux->txn.reply = &aux->reply;
1053 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1056 case DPIF_OP_EXECUTE:
1057 execute = &op->u.execute;
1058 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1067 for (i = 0; i < n_ops; i++) {
1068 txnsp[i] = &auxes[i].txn;
1070 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1072 for (i = 0; i < n_ops; i++) {
1073 struct op_auxdata *aux = &auxes[i];
1074 struct nl_transaction *txn = &auxes[i].txn;
1075 struct dpif_op *op = ops[i];
1076 struct dpif_flow_put *put;
1077 struct dpif_flow_del *del;
1079 op->error = txn->error;
1082 case DPIF_OP_FLOW_PUT:
1083 put = &op->u.flow_put;
1086 struct dpif_linux_flow reply;
1088 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1091 dpif_linux_flow_get_stats(&reply, put->stats);
1096 memset(put->stats, 0, sizeof *put->stats);
1101 case DPIF_OP_FLOW_DEL:
1102 del = &op->u.flow_del;
1105 struct dpif_linux_flow reply;
1107 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1110 dpif_linux_flow_get_stats(&reply, del->stats);
1115 memset(del->stats, 0, sizeof *del->stats);
1120 case DPIF_OP_EXECUTE:
1127 ofpbuf_uninit(&aux->request);
1128 ofpbuf_uninit(&aux->reply);
1133 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1136 size_t chunk = MIN(n_ops, MAX_OPS);
1137 dpif_linux_operate__(dpif, ops, chunk);
1144 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1146 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1148 if ((dpif->epoll_fd >= 0) == enable) {
1153 destroy_channels(dpif);
1155 struct dpif_port_dump port_dump;
1156 struct dpif_port port;
1158 dpif->epoll_fd = epoll_create(10);
1159 if (dpif->epoll_fd < 0) {
1163 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1164 struct dpif_linux_vport vport_request;
1165 struct nl_sock *sock;
1166 uint32_t upcall_pid;
1169 error = nl_sock_create(NETLINK_GENERIC, &sock);
1174 upcall_pid = nl_sock_pid(sock);
1176 dpif_linux_vport_init(&vport_request);
1177 vport_request.cmd = OVS_VPORT_CMD_SET;
1178 vport_request.dp_ifindex = dpif->dp_ifindex;
1179 vport_request.port_no = port.port_no;
1180 vport_request.upcall_pid = &upcall_pid;
1181 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1183 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1184 dpif_name(&dpif->dpif), vport_request.port_no,
1187 VLOG_WARN_RL(&error_rl,
1188 "%s: failed to set upcall pid on port: %s",
1189 dpif_name(&dpif->dpif), strerror(error));
1190 nl_sock_destroy(sock);
1192 if (error == ENODEV || error == ENOENT) {
1193 /* This device isn't there, but keep trying the others. */
1200 error = add_channel(dpif, port.port_no, sock);
1202 VLOG_INFO("%s: could not add channel for port %s",
1203 dpif_name(dpif_), port.name);
1204 nl_sock_destroy(sock);
1214 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1215 uint32_t queue_id, uint32_t *priority)
1217 if (queue_id < 0xf000) {
1218 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1226 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1229 static const struct nl_policy ovs_packet_policy[] = {
1230 /* Always present. */
1231 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1232 .min_len = ETH_HEADER_LEN },
1233 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1235 /* OVS_PACKET_CMD_ACTION only. */
1236 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1239 struct ovs_header *ovs_header;
1240 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1241 struct nlmsghdr *nlmsg;
1242 struct genlmsghdr *genl;
1246 ofpbuf_use_const(&b, buf->data, buf->size);
1248 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1249 genl = ofpbuf_try_pull(&b, sizeof *genl);
1250 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1251 if (!nlmsg || !genl || !ovs_header
1252 || nlmsg->nlmsg_type != ovs_packet_family
1253 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1254 ARRAY_SIZE(ovs_packet_policy))) {
1258 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1259 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1265 memset(upcall, 0, sizeof *upcall);
1266 upcall->type = type;
1267 upcall->packet = buf;
1268 upcall->packet->data = CONST_CAST(struct nlattr *,
1269 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1270 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1271 upcall->key = CONST_CAST(struct nlattr *,
1272 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1273 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1274 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1275 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1277 *dp_ifindex = ovs_header->dp_ifindex;
1283 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1286 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1289 if (dpif->epoll_fd < 0) {
1293 if (dpif->event_offset >= dpif->n_events) {
1296 dpif->event_offset = dpif->n_events = 0;
1299 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1300 dpif->uc_array_size, 0);
1301 } while (retval < 0 && errno == EINTR);
1303 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1304 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1305 } else if (retval > 0) {
1306 dpif->n_events = retval;
1310 while (dpif->event_offset < dpif->n_events) {
1311 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1312 struct dpif_channel *ch = &dpif->channels[idx];
1314 dpif->event_offset++;
1320 if (++read_tries > 50) {
1324 error = nl_sock_recv(ch->sock, buf, false);
1325 if (error == ENOBUFS) {
1326 /* ENOBUFS typically means that we've received so many
1327 * packets that the buffer overflowed. Try again
1328 * immediately because there's almost certainly a packet
1329 * waiting for us. */
1330 report_loss(dpif_, ch);
1334 ch->last_poll = time_msec();
1336 if (error == EAGAIN) {
1342 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1343 if (!error && dp_ifindex == dpif->dp_ifindex) {
1355 dpif_linux_recv_wait(struct dpif *dpif_)
1357 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1359 if (dpif->epoll_fd < 0) {
1363 poll_fd_wait(dpif->epoll_fd, POLLIN);
1367 dpif_linux_recv_purge(struct dpif *dpif_)
1369 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1370 struct dpif_channel *ch;
1372 if (dpif->epoll_fd < 0) {
1376 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1378 nl_sock_drain(ch->sock);
1383 const struct dpif_class dpif_linux_class = {
1385 dpif_linux_enumerate,
1392 dpif_linux_get_stats,
1393 dpif_linux_port_add,
1394 dpif_linux_port_del,
1395 dpif_linux_port_query_by_number,
1396 dpif_linux_port_query_by_name,
1397 dpif_linux_get_max_ports,
1398 dpif_linux_port_get_pid,
1399 dpif_linux_port_dump_start,
1400 dpif_linux_port_dump_next,
1401 dpif_linux_port_dump_done,
1402 dpif_linux_port_poll,
1403 dpif_linux_port_poll_wait,
1404 dpif_linux_flow_get,
1405 dpif_linux_flow_put,
1406 dpif_linux_flow_del,
1407 dpif_linux_flow_flush,
1408 dpif_linux_flow_dump_start,
1409 dpif_linux_flow_dump_next,
1410 dpif_linux_flow_dump_done,
1413 dpif_linux_recv_set,
1414 dpif_linux_queue_to_priority,
1416 dpif_linux_recv_wait,
1417 dpif_linux_recv_purge,
1421 dpif_linux_init(void)
1423 static int error = -1;
1426 unsigned int ovs_vport_mcgroup;
1428 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1429 &ovs_datapath_family);
1431 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1432 "The Open vSwitch kernel module is probably not loaded.",
1433 OVS_DATAPATH_FAMILY);
1436 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1439 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1442 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1443 &ovs_packet_family);
1446 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1449 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1451 OVS_VPORT_MCGROUP_FALLBACK_ID);
1454 static struct dpif_linux_vport vport;
1455 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1456 dpif_linux_nln_parse, &vport);
1464 dpif_linux_is_internal_device(const char *name)
1466 struct dpif_linux_vport reply;
1470 error = dpif_linux_vport_get(name, &reply, &buf);
1473 } else if (error != ENODEV && error != ENOENT) {
1474 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1475 name, strerror(error));
1478 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1482 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1484 struct dpif_linux_vport *vport = vport_;
1485 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1489 dpif_linux_port_changed(const void *vport_, void *dpif_)
1491 const struct dpif_linux_vport *vport = vport_;
1492 struct dpif_linux *dpif = dpif_;
1495 if (vport->dp_ifindex == dpif->dp_ifindex
1496 && (vport->cmd == OVS_VPORT_CMD_NEW
1497 || vport->cmd == OVS_VPORT_CMD_DEL
1498 || vport->cmd == OVS_VPORT_CMD_SET)) {
1499 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1500 dpif->dpif.full_name, vport->name, vport->cmd);
1501 sset_add(&dpif->changed_ports, vport->name);
1504 dpif->change_error = true;
1508 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1509 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1510 * positive errno value.
1512 * 'vport' will contain pointers into 'buf', so the caller should not free
1513 * 'buf' while 'vport' is still in use. */
1515 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1516 const struct ofpbuf *buf)
1518 static const struct nl_policy ovs_vport_policy[] = {
1519 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1520 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1521 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1522 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1523 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1525 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1528 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1529 struct ovs_header *ovs_header;
1530 struct nlmsghdr *nlmsg;
1531 struct genlmsghdr *genl;
1534 dpif_linux_vport_init(vport);
1536 ofpbuf_use_const(&b, buf->data, buf->size);
1537 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1538 genl = ofpbuf_try_pull(&b, sizeof *genl);
1539 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1540 if (!nlmsg || !genl || !ovs_header
1541 || nlmsg->nlmsg_type != ovs_vport_family
1542 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1543 ARRAY_SIZE(ovs_vport_policy))) {
1547 vport->cmd = genl->cmd;
1548 vport->dp_ifindex = ovs_header->dp_ifindex;
1549 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1550 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1551 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1552 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1553 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1555 if (a[OVS_VPORT_ATTR_STATS]) {
1556 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1558 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1559 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1560 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1565 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1566 * followed by Netlink attributes corresponding to 'vport'. */
1568 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1571 struct ovs_header *ovs_header;
1573 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1574 vport->cmd, OVS_VPORT_VERSION);
1576 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1577 ovs_header->dp_ifindex = vport->dp_ifindex;
1579 if (vport->port_no != UINT32_MAX) {
1580 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1583 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1584 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1588 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1591 if (vport->upcall_pid) {
1592 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1596 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1597 vport->stats, sizeof *vport->stats);
1600 if (vport->options) {
1601 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1602 vport->options, vport->options_len);
1606 /* Clears 'vport' to "empty" values. */
1608 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1610 memset(vport, 0, sizeof *vport);
1611 vport->port_no = UINT32_MAX;
1614 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1615 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1616 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1617 * result of the command is expected to be an ovs_vport also, which is decoded
1618 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1619 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1621 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1622 struct dpif_linux_vport *reply,
1623 struct ofpbuf **bufp)
1625 struct ofpbuf *request_buf;
1628 ovs_assert((reply != NULL) == (bufp != NULL));
1630 error = dpif_linux_init();
1634 dpif_linux_vport_init(reply);
1639 request_buf = ofpbuf_new(1024);
1640 dpif_linux_vport_to_ofpbuf(request, request_buf);
1641 error = nl_sock_transact(genl_sock, request_buf, bufp);
1642 ofpbuf_delete(request_buf);
1646 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1649 dpif_linux_vport_init(reply);
1650 ofpbuf_delete(*bufp);
1657 /* Obtains information about the kernel vport named 'name' and stores it into
1658 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1659 * longer needed ('reply' will contain pointers into '*bufp'). */
1661 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1662 struct ofpbuf **bufp)
1664 struct dpif_linux_vport request;
1666 dpif_linux_vport_init(&request);
1667 request.cmd = OVS_VPORT_CMD_GET;
1668 request.name = name;
1670 return dpif_linux_vport_transact(&request, reply, bufp);
1673 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1674 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1675 * positive errno value.
1677 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1678 * while 'dp' is still in use. */
1680 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1682 static const struct nl_policy ovs_datapath_policy[] = {
1683 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1684 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1688 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1689 struct ovs_header *ovs_header;
1690 struct nlmsghdr *nlmsg;
1691 struct genlmsghdr *genl;
1694 dpif_linux_dp_init(dp);
1696 ofpbuf_use_const(&b, buf->data, buf->size);
1697 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1698 genl = ofpbuf_try_pull(&b, sizeof *genl);
1699 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1700 if (!nlmsg || !genl || !ovs_header
1701 || nlmsg->nlmsg_type != ovs_datapath_family
1702 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1703 ARRAY_SIZE(ovs_datapath_policy))) {
1707 dp->cmd = genl->cmd;
1708 dp->dp_ifindex = ovs_header->dp_ifindex;
1709 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1710 if (a[OVS_DP_ATTR_STATS]) {
1711 /* Can't use structure assignment because Netlink doesn't ensure
1712 * sufficient alignment for 64-bit members. */
1713 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1720 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1722 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1724 struct ovs_header *ovs_header;
1726 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1727 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1728 OVS_DATAPATH_VERSION);
1730 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1731 ovs_header->dp_ifindex = dp->dp_ifindex;
1734 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1737 if (dp->upcall_pid) {
1738 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1741 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1744 /* Clears 'dp' to "empty" values. */
1746 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1748 memset(dp, 0, sizeof *dp);
1752 dpif_linux_dp_dump_start(struct nl_dump *dump)
1754 struct dpif_linux_dp request;
1757 dpif_linux_dp_init(&request);
1758 request.cmd = OVS_DP_CMD_GET;
1760 buf = ofpbuf_new(1024);
1761 dpif_linux_dp_to_ofpbuf(&request, buf);
1762 nl_dump_start(dump, genl_sock, buf);
1766 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1767 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1768 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1769 * result of the command is expected to be of the same form, which is decoded
1770 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1771 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1773 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1774 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1776 struct ofpbuf *request_buf;
1779 ovs_assert((reply != NULL) == (bufp != NULL));
1781 request_buf = ofpbuf_new(1024);
1782 dpif_linux_dp_to_ofpbuf(request, request_buf);
1783 error = nl_sock_transact(genl_sock, request_buf, bufp);
1784 ofpbuf_delete(request_buf);
1788 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1791 dpif_linux_dp_init(reply);
1792 ofpbuf_delete(*bufp);
1799 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1800 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1801 * will contain pointers into '*bufp'). */
1803 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1804 struct ofpbuf **bufp)
1806 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1807 struct dpif_linux_dp request;
1809 dpif_linux_dp_init(&request);
1810 request.cmd = OVS_DP_CMD_GET;
1811 request.dp_ifindex = dpif->dp_ifindex;
1813 return dpif_linux_dp_transact(&request, reply, bufp);
1816 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1817 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1818 * positive errno value.
1820 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1821 * while 'flow' is still in use. */
1823 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1824 const struct ofpbuf *buf)
1826 static const struct nl_policy ovs_flow_policy[] = {
1827 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1828 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1829 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1831 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1832 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1833 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1836 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1837 struct ovs_header *ovs_header;
1838 struct nlmsghdr *nlmsg;
1839 struct genlmsghdr *genl;
1842 dpif_linux_flow_init(flow);
1844 ofpbuf_use_const(&b, buf->data, buf->size);
1845 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1846 genl = ofpbuf_try_pull(&b, sizeof *genl);
1847 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1848 if (!nlmsg || !genl || !ovs_header
1849 || nlmsg->nlmsg_type != ovs_flow_family
1850 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1851 ARRAY_SIZE(ovs_flow_policy))) {
1855 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1856 flow->dp_ifindex = ovs_header->dp_ifindex;
1857 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1858 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1859 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1860 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1861 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1863 if (a[OVS_FLOW_ATTR_STATS]) {
1864 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1866 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1867 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1869 if (a[OVS_FLOW_ATTR_USED]) {
1870 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1875 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1876 * followed by Netlink attributes corresponding to 'flow'. */
1878 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1881 struct ovs_header *ovs_header;
1883 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1884 NLM_F_REQUEST | flow->nlmsg_flags,
1885 flow->cmd, OVS_FLOW_VERSION);
1887 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1888 ovs_header->dp_ifindex = flow->dp_ifindex;
1890 if (flow->key_len) {
1891 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1894 if (flow->actions || flow->actions_len) {
1895 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1896 flow->actions, flow->actions_len);
1899 /* We never need to send these to the kernel. */
1900 ovs_assert(!flow->stats);
1901 ovs_assert(!flow->tcp_flags);
1902 ovs_assert(!flow->used);
1905 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1909 /* Clears 'flow' to "empty" values. */
1911 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1913 memset(flow, 0, sizeof *flow);
1916 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1917 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1918 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1919 * result of the command is expected to be a flow also, which is decoded and
1920 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1921 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1923 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1924 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1926 struct ofpbuf *request_buf;
1929 ovs_assert((reply != NULL) == (bufp != NULL));
1932 request->nlmsg_flags |= NLM_F_ECHO;
1935 request_buf = ofpbuf_new(1024);
1936 dpif_linux_flow_to_ofpbuf(request, request_buf);
1937 error = nl_sock_transact(genl_sock, request_buf, bufp);
1938 ofpbuf_delete(request_buf);
1942 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1945 dpif_linux_flow_init(reply);
1946 ofpbuf_delete(*bufp);
1954 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1955 struct dpif_flow_stats *stats)
1958 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1959 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1961 stats->n_packets = 0;
1964 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1965 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1968 /* Logs information about a packet that was recently lost in 'ch' (in
1971 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1973 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1974 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1977 if (VLOG_DROP_WARN(&rl)) {
1982 if (ch->last_poll != LLONG_MIN) {
1983 ds_put_format(&s, " (last polled %lld ms ago)",
1984 time_msec() - ch->last_poll);
1987 VLOG_WARN("%s: lost packet on channel %td%s",
1988 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));