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_UNSPEC:
445 case __OVS_VPORT_TYPE_MAX:
449 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
450 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
454 static enum ovs_vport_type
455 netdev_to_ovs_vport_type(const struct netdev *netdev)
457 const char *type = netdev_get_type(netdev);
459 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
460 return OVS_VPORT_TYPE_NETDEV;
461 } else if (!strcmp(type, "internal")) {
462 return OVS_VPORT_TYPE_INTERNAL;
463 } else if (strstr(type, "gre64")) {
464 return OVS_VPORT_TYPE_GRE64;
465 } else if (strstr(type, "gre")) {
466 return OVS_VPORT_TYPE_GRE;
467 } else if (!strcmp(type, "vxlan")) {
468 return OVS_VPORT_TYPE_VXLAN;
470 return OVS_VPORT_TYPE_UNSPEC;
475 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
478 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
479 const struct netdev_tunnel_config *tnl_cfg;
480 const char *name = netdev_vport_get_dpif_port(netdev);
481 const char *type = netdev_get_type(netdev);
482 struct dpif_linux_vport request, reply;
483 struct nl_sock *sock = NULL;
486 uint64_t options_stub[64 / 8];
487 struct ofpbuf options;
490 if (dpif->epoll_fd >= 0) {
491 error = nl_sock_create(NETLINK_GENERIC, &sock);
497 dpif_linux_vport_init(&request);
498 request.cmd = OVS_VPORT_CMD_NEW;
499 request.dp_ifindex = dpif->dp_ifindex;
500 request.type = netdev_to_ovs_vport_type(netdev);
501 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
502 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
503 "unsupported type `%s'",
504 dpif_name(dpif_), name, type);
505 nl_sock_destroy(sock);
510 if (request.type == OVS_VPORT_TYPE_NETDEV) {
511 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
514 tnl_cfg = netdev_get_tunnel_config(netdev);
515 if (tnl_cfg && tnl_cfg->dst_port != 0) {
516 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
517 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
518 ntohs(tnl_cfg->dst_port));
519 request.options = options.data;
520 request.options_len = options.size;
523 request.port_no = *port_nop;
524 upcall_pid = sock ? nl_sock_pid(sock) : 0;
525 request.upcall_pid = &upcall_pid;
527 error = dpif_linux_vport_transact(&request, &reply, &buf);
529 *port_nop = reply.port_no;
530 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
531 dpif_name(dpif_), reply.port_no, upcall_pid);
533 if (error == EBUSY && *port_nop != UINT32_MAX) {
534 VLOG_INFO("%s: requested port %"PRIu32" is in use",
535 dpif_name(dpif_), *port_nop);
537 nl_sock_destroy(sock);
544 error = add_channel(dpif, *port_nop, sock);
546 VLOG_INFO("%s: could not add channel for port %s",
547 dpif_name(dpif_), name);
549 /* Delete the port. */
550 dpif_linux_vport_init(&request);
551 request.cmd = OVS_VPORT_CMD_DEL;
552 request.dp_ifindex = dpif->dp_ifindex;
553 request.port_no = *port_nop;
554 dpif_linux_vport_transact(&request, NULL, NULL);
556 nl_sock_destroy(sock);
565 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
567 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
568 struct dpif_linux_vport vport;
571 dpif_linux_vport_init(&vport);
572 vport.cmd = OVS_VPORT_CMD_DEL;
573 vport.dp_ifindex = dpif->dp_ifindex;
574 vport.port_no = port_no;
575 error = dpif_linux_vport_transact(&vport, NULL, NULL);
577 del_channel(dpif, port_no);
583 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
584 const char *port_name, struct dpif_port *dpif_port)
586 struct dpif_linux_vport request;
587 struct dpif_linux_vport reply;
591 dpif_linux_vport_init(&request);
592 request.cmd = OVS_VPORT_CMD_GET;
593 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
594 request.port_no = port_no;
595 request.name = port_name;
597 error = dpif_linux_vport_transact(&request, &reply, &buf);
599 if (reply.dp_ifindex != request.dp_ifindex) {
600 /* A query by name reported that 'port_name' is in some datapath
601 * other than 'dpif', but the caller wants to know about 'dpif'. */
603 } else if (dpif_port) {
604 dpif_port->name = xstrdup(reply.name);
605 dpif_port->type = xstrdup(get_vport_type(&reply));
606 dpif_port->port_no = reply.port_no;
614 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
615 struct dpif_port *dpif_port)
617 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
621 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
622 struct dpif_port *dpif_port)
624 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
628 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
634 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
636 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
638 if (dpif->epoll_fd < 0) {
641 /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
642 * channel, since it is not heavily loaded. */
643 int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
644 return nl_sock_pid(dpif->channels[idx].sock);
649 dpif_linux_flow_flush(struct dpif *dpif_)
651 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
652 struct dpif_linux_flow flow;
654 dpif_linux_flow_init(&flow);
655 flow.cmd = OVS_FLOW_CMD_DEL;
656 flow.dp_ifindex = dpif->dp_ifindex;
657 return dpif_linux_flow_transact(&flow, NULL, NULL);
660 struct dpif_linux_port_state {
665 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
667 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
668 struct dpif_linux_port_state *state;
669 struct dpif_linux_vport request;
672 *statep = state = xmalloc(sizeof *state);
674 dpif_linux_vport_init(&request);
675 request.cmd = OVS_DP_CMD_GET;
676 request.dp_ifindex = dpif->dp_ifindex;
678 buf = ofpbuf_new(1024);
679 dpif_linux_vport_to_ofpbuf(&request, buf);
680 nl_dump_start(&state->dump, genl_sock, buf);
687 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
688 struct dpif_port *dpif_port)
690 struct dpif_linux_port_state *state = state_;
691 struct dpif_linux_vport vport;
695 if (!nl_dump_next(&state->dump, &buf)) {
699 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
704 dpif_port->name = CONST_CAST(char *, vport.name);
705 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
706 dpif_port->port_no = vport.port_no;
711 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
713 struct dpif_linux_port_state *state = state_;
714 int error = nl_dump_done(&state->dump);
721 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
723 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
725 if (dpif->change_error) {
726 dpif->change_error = false;
727 sset_clear(&dpif->changed_ports);
729 } else if (!sset_is_empty(&dpif->changed_ports)) {
730 *devnamep = sset_pop(&dpif->changed_ports);
738 dpif_linux_port_poll_wait(const struct dpif *dpif_)
740 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
741 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
742 poll_immediate_wake();
747 dpif_linux_flow_get__(const struct dpif *dpif_,
748 const struct nlattr *key, size_t key_len,
749 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
751 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
752 struct dpif_linux_flow request;
754 dpif_linux_flow_init(&request);
755 request.cmd = OVS_FLOW_CMD_GET;
756 request.dp_ifindex = dpif->dp_ifindex;
758 request.key_len = key_len;
759 return dpif_linux_flow_transact(&request, reply, bufp);
763 dpif_linux_flow_get(const struct dpif *dpif_,
764 const struct nlattr *key, size_t key_len,
765 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
767 struct dpif_linux_flow reply;
771 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
774 dpif_linux_flow_get_stats(&reply, stats);
777 buf->data = CONST_CAST(struct nlattr *, reply.actions);
778 buf->size = reply.actions_len;
788 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
789 struct dpif_linux_flow *request)
791 static struct nlattr dummy_action;
793 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
795 dpif_linux_flow_init(request);
796 request->cmd = (put->flags & DPIF_FP_CREATE
797 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
798 request->dp_ifindex = dpif->dp_ifindex;
799 request->key = put->key;
800 request->key_len = put->key_len;
801 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
802 request->actions = put->actions ? put->actions : &dummy_action;
803 request->actions_len = put->actions_len;
804 if (put->flags & DPIF_FP_ZERO_STATS) {
805 request->clear = true;
807 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
811 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
813 struct dpif_linux_flow request, reply;
817 dpif_linux_init_flow_put(dpif_, put, &request);
818 error = dpif_linux_flow_transact(&request,
819 put->stats ? &reply : NULL,
820 put->stats ? &buf : NULL);
821 if (!error && put->stats) {
822 dpif_linux_flow_get_stats(&reply, put->stats);
829 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
830 struct dpif_linux_flow *request)
832 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
834 dpif_linux_flow_init(request);
835 request->cmd = OVS_FLOW_CMD_DEL;
836 request->dp_ifindex = dpif->dp_ifindex;
837 request->key = del->key;
838 request->key_len = del->key_len;
842 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
844 struct dpif_linux_flow request, reply;
848 dpif_linux_init_flow_del(dpif_, del, &request);
849 error = dpif_linux_flow_transact(&request,
850 del->stats ? &reply : NULL,
851 del->stats ? &buf : NULL);
852 if (!error && del->stats) {
853 dpif_linux_flow_get_stats(&reply, del->stats);
859 struct dpif_linux_flow_state {
861 struct dpif_linux_flow flow;
862 struct dpif_flow_stats stats;
867 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
869 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
870 struct dpif_linux_flow_state *state;
871 struct dpif_linux_flow request;
874 *statep = state = xmalloc(sizeof *state);
876 dpif_linux_flow_init(&request);
877 request.cmd = OVS_DP_CMD_GET;
878 request.dp_ifindex = dpif->dp_ifindex;
880 buf = ofpbuf_new(1024);
881 dpif_linux_flow_to_ofpbuf(&request, buf);
882 nl_dump_start(&state->dump, genl_sock, buf);
891 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
892 const struct nlattr **key, size_t *key_len,
893 const struct nlattr **actions, size_t *actions_len,
894 const struct dpif_flow_stats **stats)
896 struct dpif_linux_flow_state *state = state_;
901 ofpbuf_delete(state->buf);
904 if (!nl_dump_next(&state->dump, &buf)) {
908 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
913 if (actions && !state->flow.actions) {
914 error = dpif_linux_flow_get__(dpif_, state->flow.key,
916 &state->flow, &state->buf);
917 if (error == ENOENT) {
918 VLOG_DBG("dumped flow disappeared on get");
920 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
926 *actions = state->flow.actions;
927 *actions_len = state->flow.actions_len;
930 *key = state->flow.key;
931 *key_len = state->flow.key_len;
934 dpif_linux_flow_get_stats(&state->flow, &state->stats);
935 *stats = &state->stats;
941 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
943 struct dpif_linux_flow_state *state = state_;
944 int error = nl_dump_done(&state->dump);
945 ofpbuf_delete(state->buf);
951 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
954 struct ovs_header *k_exec;
956 ofpbuf_prealloc_tailroom(buf, (64
957 + d_exec->packet->size
959 + d_exec->actions_len));
961 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
962 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
964 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
965 k_exec->dp_ifindex = dp_ifindex;
967 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
968 d_exec->packet->data, d_exec->packet->size);
969 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
970 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
971 d_exec->actions, d_exec->actions_len);
975 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
977 uint64_t request_stub[1024 / 8];
978 struct ofpbuf request;
981 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
982 dpif_linux_encode_execute(dp_ifindex, execute, &request);
983 error = nl_sock_transact(genl_sock, &request, NULL);
984 ofpbuf_uninit(&request);
990 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
992 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
994 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1000 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1002 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1005 struct nl_transaction txn;
1007 struct ofpbuf request;
1008 uint64_t request_stub[1024 / 8];
1010 struct ofpbuf reply;
1011 uint64_t reply_stub[1024 / 8];
1014 struct nl_transaction *txnsp[MAX_OPS];
1017 ovs_assert(n_ops <= MAX_OPS);
1018 for (i = 0; i < n_ops; i++) {
1019 struct op_auxdata *aux = &auxes[i];
1020 struct dpif_op *op = ops[i];
1021 struct dpif_flow_put *put;
1022 struct dpif_flow_del *del;
1023 struct dpif_execute *execute;
1024 struct dpif_linux_flow flow;
1026 ofpbuf_use_stub(&aux->request,
1027 aux->request_stub, sizeof aux->request_stub);
1028 aux->txn.request = &aux->request;
1030 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1031 aux->txn.reply = NULL;
1034 case DPIF_OP_FLOW_PUT:
1035 put = &op->u.flow_put;
1036 dpif_linux_init_flow_put(dpif_, put, &flow);
1038 flow.nlmsg_flags |= NLM_F_ECHO;
1039 aux->txn.reply = &aux->reply;
1041 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1044 case DPIF_OP_FLOW_DEL:
1045 del = &op->u.flow_del;
1046 dpif_linux_init_flow_del(dpif_, del, &flow);
1048 flow.nlmsg_flags |= NLM_F_ECHO;
1049 aux->txn.reply = &aux->reply;
1051 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1054 case DPIF_OP_EXECUTE:
1055 execute = &op->u.execute;
1056 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1065 for (i = 0; i < n_ops; i++) {
1066 txnsp[i] = &auxes[i].txn;
1068 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1070 for (i = 0; i < n_ops; i++) {
1071 struct op_auxdata *aux = &auxes[i];
1072 struct nl_transaction *txn = &auxes[i].txn;
1073 struct dpif_op *op = ops[i];
1074 struct dpif_flow_put *put;
1075 struct dpif_flow_del *del;
1077 op->error = txn->error;
1080 case DPIF_OP_FLOW_PUT:
1081 put = &op->u.flow_put;
1084 struct dpif_linux_flow reply;
1086 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1089 dpif_linux_flow_get_stats(&reply, put->stats);
1094 memset(put->stats, 0, sizeof *put->stats);
1099 case DPIF_OP_FLOW_DEL:
1100 del = &op->u.flow_del;
1103 struct dpif_linux_flow reply;
1105 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1108 dpif_linux_flow_get_stats(&reply, del->stats);
1113 memset(del->stats, 0, sizeof *del->stats);
1118 case DPIF_OP_EXECUTE:
1125 ofpbuf_uninit(&aux->request);
1126 ofpbuf_uninit(&aux->reply);
1131 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1134 size_t chunk = MIN(n_ops, MAX_OPS);
1135 dpif_linux_operate__(dpif, ops, chunk);
1142 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1144 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1146 if ((dpif->epoll_fd >= 0) == enable) {
1151 destroy_channels(dpif);
1153 struct dpif_port_dump port_dump;
1154 struct dpif_port port;
1156 dpif->epoll_fd = epoll_create(10);
1157 if (dpif->epoll_fd < 0) {
1161 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1162 struct dpif_linux_vport vport_request;
1163 struct nl_sock *sock;
1164 uint32_t upcall_pid;
1167 error = nl_sock_create(NETLINK_GENERIC, &sock);
1172 upcall_pid = nl_sock_pid(sock);
1174 dpif_linux_vport_init(&vport_request);
1175 vport_request.cmd = OVS_VPORT_CMD_SET;
1176 vport_request.dp_ifindex = dpif->dp_ifindex;
1177 vport_request.port_no = port.port_no;
1178 vport_request.upcall_pid = &upcall_pid;
1179 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1181 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1182 dpif_name(&dpif->dpif), vport_request.port_no,
1185 VLOG_WARN_RL(&error_rl,
1186 "%s: failed to set upcall pid on port: %s",
1187 dpif_name(&dpif->dpif), strerror(error));
1188 nl_sock_destroy(sock);
1190 if (error == ENODEV || error == ENOENT) {
1191 /* This device isn't there, but keep trying the others. */
1198 error = add_channel(dpif, port.port_no, sock);
1200 VLOG_INFO("%s: could not add channel for port %s",
1201 dpif_name(dpif_), port.name);
1202 nl_sock_destroy(sock);
1212 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1213 uint32_t queue_id, uint32_t *priority)
1215 if (queue_id < 0xf000) {
1216 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1224 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1227 static const struct nl_policy ovs_packet_policy[] = {
1228 /* Always present. */
1229 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1230 .min_len = ETH_HEADER_LEN },
1231 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1233 /* OVS_PACKET_CMD_ACTION only. */
1234 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1237 struct ovs_header *ovs_header;
1238 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1239 struct nlmsghdr *nlmsg;
1240 struct genlmsghdr *genl;
1244 ofpbuf_use_const(&b, buf->data, buf->size);
1246 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1247 genl = ofpbuf_try_pull(&b, sizeof *genl);
1248 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1249 if (!nlmsg || !genl || !ovs_header
1250 || nlmsg->nlmsg_type != ovs_packet_family
1251 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1252 ARRAY_SIZE(ovs_packet_policy))) {
1256 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1257 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1263 memset(upcall, 0, sizeof *upcall);
1264 upcall->type = type;
1265 upcall->packet = buf;
1266 upcall->packet->data = CONST_CAST(struct nlattr *,
1267 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1268 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1269 upcall->key = CONST_CAST(struct nlattr *,
1270 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1271 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1272 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1273 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1275 *dp_ifindex = ovs_header->dp_ifindex;
1281 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1284 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1287 if (dpif->epoll_fd < 0) {
1291 if (dpif->event_offset >= dpif->n_events) {
1294 dpif->event_offset = dpif->n_events = 0;
1297 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1298 dpif->uc_array_size, 0);
1299 } while (retval < 0 && errno == EINTR);
1301 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1302 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1303 } else if (retval > 0) {
1304 dpif->n_events = retval;
1308 while (dpif->event_offset < dpif->n_events) {
1309 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1310 struct dpif_channel *ch = &dpif->channels[idx];
1312 dpif->event_offset++;
1318 if (++read_tries > 50) {
1322 error = nl_sock_recv(ch->sock, buf, false);
1323 if (error == ENOBUFS) {
1324 /* ENOBUFS typically means that we've received so many
1325 * packets that the buffer overflowed. Try again
1326 * immediately because there's almost certainly a packet
1327 * waiting for us. */
1328 report_loss(dpif_, ch);
1332 ch->last_poll = time_msec();
1334 if (error == EAGAIN) {
1340 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1341 if (!error && dp_ifindex == dpif->dp_ifindex) {
1353 dpif_linux_recv_wait(struct dpif *dpif_)
1355 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1357 if (dpif->epoll_fd < 0) {
1361 poll_fd_wait(dpif->epoll_fd, POLLIN);
1365 dpif_linux_recv_purge(struct dpif *dpif_)
1367 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1368 struct dpif_channel *ch;
1370 if (dpif->epoll_fd < 0) {
1374 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1376 nl_sock_drain(ch->sock);
1381 const struct dpif_class dpif_linux_class = {
1383 dpif_linux_enumerate,
1390 dpif_linux_get_stats,
1391 dpif_linux_port_add,
1392 dpif_linux_port_del,
1393 dpif_linux_port_query_by_number,
1394 dpif_linux_port_query_by_name,
1395 dpif_linux_get_max_ports,
1396 dpif_linux_port_get_pid,
1397 dpif_linux_port_dump_start,
1398 dpif_linux_port_dump_next,
1399 dpif_linux_port_dump_done,
1400 dpif_linux_port_poll,
1401 dpif_linux_port_poll_wait,
1402 dpif_linux_flow_get,
1403 dpif_linux_flow_put,
1404 dpif_linux_flow_del,
1405 dpif_linux_flow_flush,
1406 dpif_linux_flow_dump_start,
1407 dpif_linux_flow_dump_next,
1408 dpif_linux_flow_dump_done,
1411 dpif_linux_recv_set,
1412 dpif_linux_queue_to_priority,
1414 dpif_linux_recv_wait,
1415 dpif_linux_recv_purge,
1419 dpif_linux_init(void)
1421 static int error = -1;
1424 unsigned int ovs_vport_mcgroup;
1426 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1427 &ovs_datapath_family);
1429 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1430 "The Open vSwitch kernel module is probably not loaded.",
1431 OVS_DATAPATH_FAMILY);
1434 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1437 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1440 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1441 &ovs_packet_family);
1444 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1447 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1449 OVS_VPORT_MCGROUP_FALLBACK_ID);
1452 static struct dpif_linux_vport vport;
1453 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1454 dpif_linux_nln_parse, &vport);
1462 dpif_linux_is_internal_device(const char *name)
1464 struct dpif_linux_vport reply;
1468 error = dpif_linux_vport_get(name, &reply, &buf);
1471 } else if (error != ENODEV && error != ENOENT) {
1472 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1473 name, strerror(error));
1476 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1480 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1482 struct dpif_linux_vport *vport = vport_;
1483 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1487 dpif_linux_port_changed(const void *vport_, void *dpif_)
1489 const struct dpif_linux_vport *vport = vport_;
1490 struct dpif_linux *dpif = dpif_;
1493 if (vport->dp_ifindex == dpif->dp_ifindex
1494 && (vport->cmd == OVS_VPORT_CMD_NEW
1495 || vport->cmd == OVS_VPORT_CMD_DEL
1496 || vport->cmd == OVS_VPORT_CMD_SET)) {
1497 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1498 dpif->dpif.full_name, vport->name, vport->cmd);
1499 sset_add(&dpif->changed_ports, vport->name);
1502 dpif->change_error = true;
1506 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1507 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1508 * positive errno value.
1510 * 'vport' will contain pointers into 'buf', so the caller should not free
1511 * 'buf' while 'vport' is still in use. */
1513 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1514 const struct ofpbuf *buf)
1516 static const struct nl_policy ovs_vport_policy[] = {
1517 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1518 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1519 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1520 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1521 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1523 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1526 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1527 struct ovs_header *ovs_header;
1528 struct nlmsghdr *nlmsg;
1529 struct genlmsghdr *genl;
1532 dpif_linux_vport_init(vport);
1534 ofpbuf_use_const(&b, buf->data, buf->size);
1535 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1536 genl = ofpbuf_try_pull(&b, sizeof *genl);
1537 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1538 if (!nlmsg || !genl || !ovs_header
1539 || nlmsg->nlmsg_type != ovs_vport_family
1540 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1541 ARRAY_SIZE(ovs_vport_policy))) {
1545 vport->cmd = genl->cmd;
1546 vport->dp_ifindex = ovs_header->dp_ifindex;
1547 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1548 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1549 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1550 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1551 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1553 if (a[OVS_VPORT_ATTR_STATS]) {
1554 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1556 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1557 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1558 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1563 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1564 * followed by Netlink attributes corresponding to 'vport'. */
1566 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1569 struct ovs_header *ovs_header;
1571 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1572 vport->cmd, OVS_VPORT_VERSION);
1574 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1575 ovs_header->dp_ifindex = vport->dp_ifindex;
1577 if (vport->port_no != UINT32_MAX) {
1578 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1581 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1582 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1586 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1589 if (vport->upcall_pid) {
1590 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1594 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1595 vport->stats, sizeof *vport->stats);
1598 if (vport->options) {
1599 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1600 vport->options, vport->options_len);
1604 /* Clears 'vport' to "empty" values. */
1606 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1608 memset(vport, 0, sizeof *vport);
1609 vport->port_no = UINT32_MAX;
1612 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1613 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1614 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1615 * result of the command is expected to be an ovs_vport also, which is decoded
1616 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1617 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1619 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1620 struct dpif_linux_vport *reply,
1621 struct ofpbuf **bufp)
1623 struct ofpbuf *request_buf;
1626 ovs_assert((reply != NULL) == (bufp != NULL));
1628 error = dpif_linux_init();
1632 dpif_linux_vport_init(reply);
1637 request_buf = ofpbuf_new(1024);
1638 dpif_linux_vport_to_ofpbuf(request, request_buf);
1639 error = nl_sock_transact(genl_sock, request_buf, bufp);
1640 ofpbuf_delete(request_buf);
1644 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1647 dpif_linux_vport_init(reply);
1648 ofpbuf_delete(*bufp);
1655 /* Obtains information about the kernel vport named 'name' and stores it into
1656 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1657 * longer needed ('reply' will contain pointers into '*bufp'). */
1659 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1660 struct ofpbuf **bufp)
1662 struct dpif_linux_vport request;
1664 dpif_linux_vport_init(&request);
1665 request.cmd = OVS_VPORT_CMD_GET;
1666 request.name = name;
1668 return dpif_linux_vport_transact(&request, reply, bufp);
1671 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1672 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1673 * positive errno value.
1675 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1676 * while 'dp' is still in use. */
1678 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1680 static const struct nl_policy ovs_datapath_policy[] = {
1681 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1682 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1686 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1687 struct ovs_header *ovs_header;
1688 struct nlmsghdr *nlmsg;
1689 struct genlmsghdr *genl;
1692 dpif_linux_dp_init(dp);
1694 ofpbuf_use_const(&b, buf->data, buf->size);
1695 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1696 genl = ofpbuf_try_pull(&b, sizeof *genl);
1697 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1698 if (!nlmsg || !genl || !ovs_header
1699 || nlmsg->nlmsg_type != ovs_datapath_family
1700 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1701 ARRAY_SIZE(ovs_datapath_policy))) {
1705 dp->cmd = genl->cmd;
1706 dp->dp_ifindex = ovs_header->dp_ifindex;
1707 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1708 if (a[OVS_DP_ATTR_STATS]) {
1709 /* Can't use structure assignment because Netlink doesn't ensure
1710 * sufficient alignment for 64-bit members. */
1711 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1718 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1720 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1722 struct ovs_header *ovs_header;
1724 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1725 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1726 OVS_DATAPATH_VERSION);
1728 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1729 ovs_header->dp_ifindex = dp->dp_ifindex;
1732 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1735 if (dp->upcall_pid) {
1736 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1739 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1742 /* Clears 'dp' to "empty" values. */
1744 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1746 memset(dp, 0, sizeof *dp);
1750 dpif_linux_dp_dump_start(struct nl_dump *dump)
1752 struct dpif_linux_dp request;
1755 dpif_linux_dp_init(&request);
1756 request.cmd = OVS_DP_CMD_GET;
1758 buf = ofpbuf_new(1024);
1759 dpif_linux_dp_to_ofpbuf(&request, buf);
1760 nl_dump_start(dump, genl_sock, buf);
1764 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1765 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1766 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1767 * result of the command is expected to be of the same form, which is decoded
1768 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1769 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1771 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1772 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1774 struct ofpbuf *request_buf;
1777 ovs_assert((reply != NULL) == (bufp != NULL));
1779 request_buf = ofpbuf_new(1024);
1780 dpif_linux_dp_to_ofpbuf(request, request_buf);
1781 error = nl_sock_transact(genl_sock, request_buf, bufp);
1782 ofpbuf_delete(request_buf);
1786 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1789 dpif_linux_dp_init(reply);
1790 ofpbuf_delete(*bufp);
1797 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1798 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1799 * will contain pointers into '*bufp'). */
1801 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1802 struct ofpbuf **bufp)
1804 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1805 struct dpif_linux_dp request;
1807 dpif_linux_dp_init(&request);
1808 request.cmd = OVS_DP_CMD_GET;
1809 request.dp_ifindex = dpif->dp_ifindex;
1811 return dpif_linux_dp_transact(&request, reply, bufp);
1814 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1815 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1816 * positive errno value.
1818 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1819 * while 'flow' is still in use. */
1821 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1822 const struct ofpbuf *buf)
1824 static const struct nl_policy ovs_flow_policy[] = {
1825 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1826 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1827 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1829 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1830 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1831 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1834 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1835 struct ovs_header *ovs_header;
1836 struct nlmsghdr *nlmsg;
1837 struct genlmsghdr *genl;
1840 dpif_linux_flow_init(flow);
1842 ofpbuf_use_const(&b, buf->data, buf->size);
1843 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1844 genl = ofpbuf_try_pull(&b, sizeof *genl);
1845 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1846 if (!nlmsg || !genl || !ovs_header
1847 || nlmsg->nlmsg_type != ovs_flow_family
1848 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1849 ARRAY_SIZE(ovs_flow_policy))) {
1853 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1854 flow->dp_ifindex = ovs_header->dp_ifindex;
1855 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1856 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1857 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1858 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1859 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1861 if (a[OVS_FLOW_ATTR_STATS]) {
1862 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1864 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1865 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1867 if (a[OVS_FLOW_ATTR_USED]) {
1868 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1873 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1874 * followed by Netlink attributes corresponding to 'flow'. */
1876 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1879 struct ovs_header *ovs_header;
1881 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1882 NLM_F_REQUEST | flow->nlmsg_flags,
1883 flow->cmd, OVS_FLOW_VERSION);
1885 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1886 ovs_header->dp_ifindex = flow->dp_ifindex;
1888 if (flow->key_len) {
1889 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1892 if (flow->actions || flow->actions_len) {
1893 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1894 flow->actions, flow->actions_len);
1897 /* We never need to send these to the kernel. */
1898 ovs_assert(!flow->stats);
1899 ovs_assert(!flow->tcp_flags);
1900 ovs_assert(!flow->used);
1903 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1907 /* Clears 'flow' to "empty" values. */
1909 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1911 memset(flow, 0, sizeof *flow);
1914 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1915 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1916 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1917 * result of the command is expected to be a flow also, which is decoded and
1918 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1919 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1921 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1922 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1924 struct ofpbuf *request_buf;
1927 ovs_assert((reply != NULL) == (bufp != NULL));
1930 request->nlmsg_flags |= NLM_F_ECHO;
1933 request_buf = ofpbuf_new(1024);
1934 dpif_linux_flow_to_ofpbuf(request, request_buf);
1935 error = nl_sock_transact(genl_sock, request_buf, bufp);
1936 ofpbuf_delete(request_buf);
1940 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1943 dpif_linux_flow_init(reply);
1944 ofpbuf_delete(*bufp);
1952 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1953 struct dpif_flow_stats *stats)
1956 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1957 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1959 stats->n_packets = 0;
1962 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1963 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1966 /* Logs information about a packet that was recently lost in 'ch' (in
1969 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1971 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1972 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1975 if (VLOG_DROP_WARN(&rl)) {
1980 if (ch->last_poll != LLONG_MIN) {
1981 ds_put_format(&s, " (last polled %lld ms ago)",
1982 time_msec() - ch->last_poll);
1985 VLOG_WARN("%s: lost packet on channel %td%s",
1986 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));