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 > MAX_PORTS) {
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_LISP:
449 case OVS_VPORT_TYPE_UNSPEC:
450 case __OVS_VPORT_TYPE_MAX:
454 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
455 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
459 static enum ovs_vport_type
460 netdev_to_ovs_vport_type(const struct netdev *netdev)
462 const char *type = netdev_get_type(netdev);
464 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
465 return OVS_VPORT_TYPE_NETDEV;
466 } else if (!strcmp(type, "internal")) {
467 return OVS_VPORT_TYPE_INTERNAL;
468 } else if (strstr(type, "gre64")) {
469 return OVS_VPORT_TYPE_GRE64;
470 } else if (strstr(type, "gre")) {
471 return OVS_VPORT_TYPE_GRE;
472 } else if (!strcmp(type, "vxlan")) {
473 return OVS_VPORT_TYPE_VXLAN;
474 } else if (!strcmp(type, "lisp")) {
475 return OVS_VPORT_TYPE_LISP;
477 return OVS_VPORT_TYPE_UNSPEC;
482 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
485 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
486 const struct netdev_tunnel_config *tnl_cfg;
487 const char *name = netdev_vport_get_dpif_port(netdev);
488 const char *type = netdev_get_type(netdev);
489 struct dpif_linux_vport request, reply;
490 struct nl_sock *sock = NULL;
493 uint64_t options_stub[64 / 8];
494 struct ofpbuf options;
497 if (dpif->epoll_fd >= 0) {
498 error = nl_sock_create(NETLINK_GENERIC, &sock);
504 dpif_linux_vport_init(&request);
505 request.cmd = OVS_VPORT_CMD_NEW;
506 request.dp_ifindex = dpif->dp_ifindex;
507 request.type = netdev_to_ovs_vport_type(netdev);
508 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
509 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
510 "unsupported type `%s'",
511 dpif_name(dpif_), name, type);
512 nl_sock_destroy(sock);
517 if (request.type == OVS_VPORT_TYPE_NETDEV) {
518 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
521 tnl_cfg = netdev_get_tunnel_config(netdev);
522 if (tnl_cfg && tnl_cfg->dst_port != 0) {
523 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
524 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
525 ntohs(tnl_cfg->dst_port));
526 request.options = options.data;
527 request.options_len = options.size;
530 request.port_no = *port_nop;
531 upcall_pid = sock ? nl_sock_pid(sock) : 0;
532 request.upcall_pid = &upcall_pid;
534 error = dpif_linux_vport_transact(&request, &reply, &buf);
536 *port_nop = reply.port_no;
537 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
538 dpif_name(dpif_), reply.port_no, upcall_pid);
540 if (error == EBUSY && *port_nop != UINT32_MAX) {
541 VLOG_INFO("%s: requested port %"PRIu32" is in use",
542 dpif_name(dpif_), *port_nop);
544 nl_sock_destroy(sock);
551 error = add_channel(dpif, *port_nop, sock);
553 VLOG_INFO("%s: could not add channel for port %s",
554 dpif_name(dpif_), name);
556 /* Delete the port. */
557 dpif_linux_vport_init(&request);
558 request.cmd = OVS_VPORT_CMD_DEL;
559 request.dp_ifindex = dpif->dp_ifindex;
560 request.port_no = *port_nop;
561 dpif_linux_vport_transact(&request, NULL, NULL);
563 nl_sock_destroy(sock);
572 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
574 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
575 struct dpif_linux_vport vport;
578 dpif_linux_vport_init(&vport);
579 vport.cmd = OVS_VPORT_CMD_DEL;
580 vport.dp_ifindex = dpif->dp_ifindex;
581 vport.port_no = port_no;
582 error = dpif_linux_vport_transact(&vport, NULL, NULL);
584 del_channel(dpif, port_no);
590 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
591 const char *port_name, struct dpif_port *dpif_port)
593 struct dpif_linux_vport request;
594 struct dpif_linux_vport reply;
598 dpif_linux_vport_init(&request);
599 request.cmd = OVS_VPORT_CMD_GET;
600 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
601 request.port_no = port_no;
602 request.name = port_name;
604 error = dpif_linux_vport_transact(&request, &reply, &buf);
606 if (reply.dp_ifindex != request.dp_ifindex) {
607 /* A query by name reported that 'port_name' is in some datapath
608 * other than 'dpif', but the caller wants to know about 'dpif'. */
610 } else if (dpif_port) {
611 dpif_port->name = xstrdup(reply.name);
612 dpif_port->type = xstrdup(get_vport_type(&reply));
613 dpif_port->port_no = reply.port_no;
621 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
622 struct dpif_port *dpif_port)
624 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
628 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
629 struct dpif_port *dpif_port)
631 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
635 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
641 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
643 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
645 if (dpif->epoll_fd < 0) {
648 /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
649 * channel, since it is not heavily loaded. */
650 int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
651 return nl_sock_pid(dpif->channels[idx].sock);
656 dpif_linux_flow_flush(struct dpif *dpif_)
658 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
659 struct dpif_linux_flow flow;
661 dpif_linux_flow_init(&flow);
662 flow.cmd = OVS_FLOW_CMD_DEL;
663 flow.dp_ifindex = dpif->dp_ifindex;
664 return dpif_linux_flow_transact(&flow, NULL, NULL);
667 struct dpif_linux_port_state {
672 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
674 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
675 struct dpif_linux_port_state *state;
676 struct dpif_linux_vport request;
679 *statep = state = xmalloc(sizeof *state);
681 dpif_linux_vport_init(&request);
682 request.cmd = OVS_DP_CMD_GET;
683 request.dp_ifindex = dpif->dp_ifindex;
685 buf = ofpbuf_new(1024);
686 dpif_linux_vport_to_ofpbuf(&request, buf);
687 nl_dump_start(&state->dump, genl_sock, buf);
694 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
695 struct dpif_port *dpif_port)
697 struct dpif_linux_port_state *state = state_;
698 struct dpif_linux_vport vport;
702 if (!nl_dump_next(&state->dump, &buf)) {
706 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
711 dpif_port->name = CONST_CAST(char *, vport.name);
712 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
713 dpif_port->port_no = vport.port_no;
718 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
720 struct dpif_linux_port_state *state = state_;
721 int error = nl_dump_done(&state->dump);
728 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
730 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
732 if (dpif->change_error) {
733 dpif->change_error = false;
734 sset_clear(&dpif->changed_ports);
736 } else if (!sset_is_empty(&dpif->changed_ports)) {
737 *devnamep = sset_pop(&dpif->changed_ports);
745 dpif_linux_port_poll_wait(const struct dpif *dpif_)
747 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
748 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
749 poll_immediate_wake();
754 dpif_linux_flow_get__(const struct dpif *dpif_,
755 const struct nlattr *key, size_t key_len,
756 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
758 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
759 struct dpif_linux_flow request;
761 dpif_linux_flow_init(&request);
762 request.cmd = OVS_FLOW_CMD_GET;
763 request.dp_ifindex = dpif->dp_ifindex;
765 request.key_len = key_len;
766 return dpif_linux_flow_transact(&request, reply, bufp);
770 dpif_linux_flow_get(const struct dpif *dpif_,
771 const struct nlattr *key, size_t key_len,
772 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
774 struct dpif_linux_flow reply;
778 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
781 dpif_linux_flow_get_stats(&reply, stats);
784 buf->data = CONST_CAST(struct nlattr *, reply.actions);
785 buf->size = reply.actions_len;
795 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
796 struct dpif_linux_flow *request)
798 static const struct nlattr dummy_action;
800 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
802 dpif_linux_flow_init(request);
803 request->cmd = (put->flags & DPIF_FP_CREATE
804 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
805 request->dp_ifindex = dpif->dp_ifindex;
806 request->key = put->key;
807 request->key_len = put->key_len;
808 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
809 request->actions = (put->actions
811 : CONST_CAST(struct nlattr *, &dummy_action));
812 request->actions_len = put->actions_len;
813 if (put->flags & DPIF_FP_ZERO_STATS) {
814 request->clear = true;
816 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
820 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
822 struct dpif_linux_flow request, reply;
826 dpif_linux_init_flow_put(dpif_, put, &request);
827 error = dpif_linux_flow_transact(&request,
828 put->stats ? &reply : NULL,
829 put->stats ? &buf : NULL);
830 if (!error && put->stats) {
831 dpif_linux_flow_get_stats(&reply, put->stats);
838 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
839 struct dpif_linux_flow *request)
841 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
843 dpif_linux_flow_init(request);
844 request->cmd = OVS_FLOW_CMD_DEL;
845 request->dp_ifindex = dpif->dp_ifindex;
846 request->key = del->key;
847 request->key_len = del->key_len;
851 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
853 struct dpif_linux_flow request, reply;
857 dpif_linux_init_flow_del(dpif_, del, &request);
858 error = dpif_linux_flow_transact(&request,
859 del->stats ? &reply : NULL,
860 del->stats ? &buf : NULL);
861 if (!error && del->stats) {
862 dpif_linux_flow_get_stats(&reply, del->stats);
868 struct dpif_linux_flow_state {
870 struct dpif_linux_flow flow;
871 struct dpif_flow_stats stats;
876 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
878 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
879 struct dpif_linux_flow_state *state;
880 struct dpif_linux_flow request;
883 *statep = state = xmalloc(sizeof *state);
885 dpif_linux_flow_init(&request);
886 request.cmd = OVS_DP_CMD_GET;
887 request.dp_ifindex = dpif->dp_ifindex;
889 buf = ofpbuf_new(1024);
890 dpif_linux_flow_to_ofpbuf(&request, buf);
891 nl_dump_start(&state->dump, genl_sock, buf);
900 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
901 const struct nlattr **key, size_t *key_len,
902 const struct nlattr **actions, size_t *actions_len,
903 const struct dpif_flow_stats **stats)
905 struct dpif_linux_flow_state *state = state_;
910 ofpbuf_delete(state->buf);
913 if (!nl_dump_next(&state->dump, &buf)) {
917 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
922 if (actions && !state->flow.actions) {
923 error = dpif_linux_flow_get__(dpif_, state->flow.key,
925 &state->flow, &state->buf);
926 if (error == ENOENT) {
927 VLOG_DBG("dumped flow disappeared on get");
929 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
935 *actions = state->flow.actions;
936 *actions_len = state->flow.actions_len;
939 *key = state->flow.key;
940 *key_len = state->flow.key_len;
943 dpif_linux_flow_get_stats(&state->flow, &state->stats);
944 *stats = &state->stats;
950 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
952 struct dpif_linux_flow_state *state = state_;
953 int error = nl_dump_done(&state->dump);
954 ofpbuf_delete(state->buf);
960 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
963 struct ovs_header *k_exec;
965 ofpbuf_prealloc_tailroom(buf, (64
966 + d_exec->packet->size
968 + d_exec->actions_len));
970 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
971 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
973 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
974 k_exec->dp_ifindex = dp_ifindex;
976 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
977 d_exec->packet->data, d_exec->packet->size);
978 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
979 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
980 d_exec->actions, d_exec->actions_len);
984 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
986 uint64_t request_stub[1024 / 8];
987 struct ofpbuf request;
990 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
991 dpif_linux_encode_execute(dp_ifindex, execute, &request);
992 error = nl_sock_transact(genl_sock, &request, NULL);
993 ofpbuf_uninit(&request);
999 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1001 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1003 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1009 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1011 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1014 struct nl_transaction txn;
1016 struct ofpbuf request;
1017 uint64_t request_stub[1024 / 8];
1019 struct ofpbuf reply;
1020 uint64_t reply_stub[1024 / 8];
1023 struct nl_transaction *txnsp[MAX_OPS];
1026 ovs_assert(n_ops <= MAX_OPS);
1027 for (i = 0; i < n_ops; i++) {
1028 struct op_auxdata *aux = &auxes[i];
1029 struct dpif_op *op = ops[i];
1030 struct dpif_flow_put *put;
1031 struct dpif_flow_del *del;
1032 struct dpif_execute *execute;
1033 struct dpif_linux_flow flow;
1035 ofpbuf_use_stub(&aux->request,
1036 aux->request_stub, sizeof aux->request_stub);
1037 aux->txn.request = &aux->request;
1039 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1040 aux->txn.reply = NULL;
1043 case DPIF_OP_FLOW_PUT:
1044 put = &op->u.flow_put;
1045 dpif_linux_init_flow_put(dpif_, put, &flow);
1047 flow.nlmsg_flags |= NLM_F_ECHO;
1048 aux->txn.reply = &aux->reply;
1050 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1053 case DPIF_OP_FLOW_DEL:
1054 del = &op->u.flow_del;
1055 dpif_linux_init_flow_del(dpif_, del, &flow);
1057 flow.nlmsg_flags |= NLM_F_ECHO;
1058 aux->txn.reply = &aux->reply;
1060 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1063 case DPIF_OP_EXECUTE:
1064 execute = &op->u.execute;
1065 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1074 for (i = 0; i < n_ops; i++) {
1075 txnsp[i] = &auxes[i].txn;
1077 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1079 for (i = 0; i < n_ops; i++) {
1080 struct op_auxdata *aux = &auxes[i];
1081 struct nl_transaction *txn = &auxes[i].txn;
1082 struct dpif_op *op = ops[i];
1083 struct dpif_flow_put *put;
1084 struct dpif_flow_del *del;
1086 op->error = txn->error;
1089 case DPIF_OP_FLOW_PUT:
1090 put = &op->u.flow_put;
1093 struct dpif_linux_flow reply;
1095 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1098 dpif_linux_flow_get_stats(&reply, put->stats);
1103 memset(put->stats, 0, sizeof *put->stats);
1108 case DPIF_OP_FLOW_DEL:
1109 del = &op->u.flow_del;
1112 struct dpif_linux_flow reply;
1114 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1117 dpif_linux_flow_get_stats(&reply, del->stats);
1122 memset(del->stats, 0, sizeof *del->stats);
1127 case DPIF_OP_EXECUTE:
1134 ofpbuf_uninit(&aux->request);
1135 ofpbuf_uninit(&aux->reply);
1140 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1143 size_t chunk = MIN(n_ops, MAX_OPS);
1144 dpif_linux_operate__(dpif, ops, chunk);
1151 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1153 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1155 if ((dpif->epoll_fd >= 0) == enable) {
1160 destroy_channels(dpif);
1162 struct dpif_port_dump port_dump;
1163 struct dpif_port port;
1165 dpif->epoll_fd = epoll_create(10);
1166 if (dpif->epoll_fd < 0) {
1170 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1171 struct dpif_linux_vport vport_request;
1172 struct nl_sock *sock;
1173 uint32_t upcall_pid;
1176 error = nl_sock_create(NETLINK_GENERIC, &sock);
1181 upcall_pid = nl_sock_pid(sock);
1183 dpif_linux_vport_init(&vport_request);
1184 vport_request.cmd = OVS_VPORT_CMD_SET;
1185 vport_request.dp_ifindex = dpif->dp_ifindex;
1186 vport_request.port_no = port.port_no;
1187 vport_request.upcall_pid = &upcall_pid;
1188 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1190 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1191 dpif_name(&dpif->dpif), vport_request.port_no,
1194 VLOG_WARN_RL(&error_rl,
1195 "%s: failed to set upcall pid on port: %s",
1196 dpif_name(&dpif->dpif), strerror(error));
1197 nl_sock_destroy(sock);
1199 if (error == ENODEV || error == ENOENT) {
1200 /* This device isn't there, but keep trying the others. */
1207 error = add_channel(dpif, port.port_no, sock);
1209 VLOG_INFO("%s: could not add channel for port %s",
1210 dpif_name(dpif_), port.name);
1211 nl_sock_destroy(sock);
1221 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1222 uint32_t queue_id, uint32_t *priority)
1224 if (queue_id < 0xf000) {
1225 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1233 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1236 static const struct nl_policy ovs_packet_policy[] = {
1237 /* Always present. */
1238 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1239 .min_len = ETH_HEADER_LEN },
1240 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1242 /* OVS_PACKET_CMD_ACTION only. */
1243 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1246 struct ovs_header *ovs_header;
1247 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1248 struct nlmsghdr *nlmsg;
1249 struct genlmsghdr *genl;
1253 ofpbuf_use_const(&b, buf->data, buf->size);
1255 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1256 genl = ofpbuf_try_pull(&b, sizeof *genl);
1257 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1258 if (!nlmsg || !genl || !ovs_header
1259 || nlmsg->nlmsg_type != ovs_packet_family
1260 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1261 ARRAY_SIZE(ovs_packet_policy))) {
1265 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1266 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1272 memset(upcall, 0, sizeof *upcall);
1273 upcall->type = type;
1274 upcall->packet = buf;
1275 upcall->packet->data = CONST_CAST(struct nlattr *,
1276 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1277 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1278 upcall->key = CONST_CAST(struct nlattr *,
1279 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1280 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1281 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1282 *dp_ifindex = ovs_header->dp_ifindex;
1288 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1291 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1294 if (dpif->epoll_fd < 0) {
1298 if (dpif->event_offset >= dpif->n_events) {
1301 dpif->event_offset = dpif->n_events = 0;
1304 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1305 dpif->uc_array_size, 0);
1306 } while (retval < 0 && errno == EINTR);
1308 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1309 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1310 } else if (retval > 0) {
1311 dpif->n_events = retval;
1315 while (dpif->event_offset < dpif->n_events) {
1316 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1317 struct dpif_channel *ch = &dpif->channels[idx];
1319 dpif->event_offset++;
1325 if (++read_tries > 50) {
1329 error = nl_sock_recv(ch->sock, buf, false);
1330 if (error == ENOBUFS) {
1331 /* ENOBUFS typically means that we've received so many
1332 * packets that the buffer overflowed. Try again
1333 * immediately because there's almost certainly a packet
1334 * waiting for us. */
1335 report_loss(dpif_, ch);
1339 ch->last_poll = time_msec();
1341 if (error == EAGAIN) {
1347 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1348 if (!error && dp_ifindex == dpif->dp_ifindex) {
1360 dpif_linux_recv_wait(struct dpif *dpif_)
1362 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1364 if (dpif->epoll_fd < 0) {
1368 poll_fd_wait(dpif->epoll_fd, POLLIN);
1372 dpif_linux_recv_purge(struct dpif *dpif_)
1374 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1375 struct dpif_channel *ch;
1377 if (dpif->epoll_fd < 0) {
1381 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1383 nl_sock_drain(ch->sock);
1388 const struct dpif_class dpif_linux_class = {
1390 dpif_linux_enumerate,
1397 dpif_linux_get_stats,
1398 dpif_linux_port_add,
1399 dpif_linux_port_del,
1400 dpif_linux_port_query_by_number,
1401 dpif_linux_port_query_by_name,
1402 dpif_linux_get_max_ports,
1403 dpif_linux_port_get_pid,
1404 dpif_linux_port_dump_start,
1405 dpif_linux_port_dump_next,
1406 dpif_linux_port_dump_done,
1407 dpif_linux_port_poll,
1408 dpif_linux_port_poll_wait,
1409 dpif_linux_flow_get,
1410 dpif_linux_flow_put,
1411 dpif_linux_flow_del,
1412 dpif_linux_flow_flush,
1413 dpif_linux_flow_dump_start,
1414 dpif_linux_flow_dump_next,
1415 dpif_linux_flow_dump_done,
1418 dpif_linux_recv_set,
1419 dpif_linux_queue_to_priority,
1421 dpif_linux_recv_wait,
1422 dpif_linux_recv_purge,
1426 dpif_linux_init(void)
1428 static int error = -1;
1431 unsigned int ovs_vport_mcgroup;
1433 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1434 &ovs_datapath_family);
1436 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1437 "The Open vSwitch kernel module is probably not loaded.",
1438 OVS_DATAPATH_FAMILY);
1441 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1444 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1447 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1448 &ovs_packet_family);
1451 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1454 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1456 OVS_VPORT_MCGROUP_FALLBACK_ID);
1459 static struct dpif_linux_vport vport;
1460 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1461 dpif_linux_nln_parse, &vport);
1469 dpif_linux_is_internal_device(const char *name)
1471 struct dpif_linux_vport reply;
1475 error = dpif_linux_vport_get(name, &reply, &buf);
1478 } else if (error != ENODEV && error != ENOENT) {
1479 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1480 name, strerror(error));
1483 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1487 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1489 struct dpif_linux_vport *vport = vport_;
1490 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1494 dpif_linux_port_changed(const void *vport_, void *dpif_)
1496 const struct dpif_linux_vport *vport = vport_;
1497 struct dpif_linux *dpif = dpif_;
1500 if (vport->dp_ifindex == dpif->dp_ifindex
1501 && (vport->cmd == OVS_VPORT_CMD_NEW
1502 || vport->cmd == OVS_VPORT_CMD_DEL
1503 || vport->cmd == OVS_VPORT_CMD_SET)) {
1504 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1505 dpif->dpif.full_name, vport->name, vport->cmd);
1506 sset_add(&dpif->changed_ports, vport->name);
1509 dpif->change_error = true;
1513 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1514 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1515 * positive errno value.
1517 * 'vport' will contain pointers into 'buf', so the caller should not free
1518 * 'buf' while 'vport' is still in use. */
1520 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1521 const struct ofpbuf *buf)
1523 static const struct nl_policy ovs_vport_policy[] = {
1524 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1525 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1526 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1527 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1528 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1530 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1533 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1534 struct ovs_header *ovs_header;
1535 struct nlmsghdr *nlmsg;
1536 struct genlmsghdr *genl;
1539 dpif_linux_vport_init(vport);
1541 ofpbuf_use_const(&b, buf->data, buf->size);
1542 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1543 genl = ofpbuf_try_pull(&b, sizeof *genl);
1544 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1545 if (!nlmsg || !genl || !ovs_header
1546 || nlmsg->nlmsg_type != ovs_vport_family
1547 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1548 ARRAY_SIZE(ovs_vport_policy))) {
1552 vport->cmd = genl->cmd;
1553 vport->dp_ifindex = ovs_header->dp_ifindex;
1554 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1555 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1556 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1557 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1558 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1560 if (a[OVS_VPORT_ATTR_STATS]) {
1561 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1563 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1564 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1565 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1570 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1571 * followed by Netlink attributes corresponding to 'vport'. */
1573 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1576 struct ovs_header *ovs_header;
1578 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1579 vport->cmd, OVS_VPORT_VERSION);
1581 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1582 ovs_header->dp_ifindex = vport->dp_ifindex;
1584 if (vport->port_no != UINT32_MAX) {
1585 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1588 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1589 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1593 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1596 if (vport->upcall_pid) {
1597 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1601 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1602 vport->stats, sizeof *vport->stats);
1605 if (vport->options) {
1606 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1607 vport->options, vport->options_len);
1611 /* Clears 'vport' to "empty" values. */
1613 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1615 memset(vport, 0, sizeof *vport);
1616 vport->port_no = UINT32_MAX;
1619 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1620 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1621 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1622 * result of the command is expected to be an ovs_vport also, which is decoded
1623 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1624 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1626 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1627 struct dpif_linux_vport *reply,
1628 struct ofpbuf **bufp)
1630 struct ofpbuf *request_buf;
1633 ovs_assert((reply != NULL) == (bufp != NULL));
1635 error = dpif_linux_init();
1639 dpif_linux_vport_init(reply);
1644 request_buf = ofpbuf_new(1024);
1645 dpif_linux_vport_to_ofpbuf(request, request_buf);
1646 error = nl_sock_transact(genl_sock, request_buf, bufp);
1647 ofpbuf_delete(request_buf);
1651 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1654 dpif_linux_vport_init(reply);
1655 ofpbuf_delete(*bufp);
1662 /* Obtains information about the kernel vport named 'name' and stores it into
1663 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1664 * longer needed ('reply' will contain pointers into '*bufp'). */
1666 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1667 struct ofpbuf **bufp)
1669 struct dpif_linux_vport request;
1671 dpif_linux_vport_init(&request);
1672 request.cmd = OVS_VPORT_CMD_GET;
1673 request.name = name;
1675 return dpif_linux_vport_transact(&request, reply, bufp);
1678 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1679 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1680 * positive errno value.
1682 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1683 * while 'dp' is still in use. */
1685 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1687 static const struct nl_policy ovs_datapath_policy[] = {
1688 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1689 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1693 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1694 struct ovs_header *ovs_header;
1695 struct nlmsghdr *nlmsg;
1696 struct genlmsghdr *genl;
1699 dpif_linux_dp_init(dp);
1701 ofpbuf_use_const(&b, buf->data, buf->size);
1702 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1703 genl = ofpbuf_try_pull(&b, sizeof *genl);
1704 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1705 if (!nlmsg || !genl || !ovs_header
1706 || nlmsg->nlmsg_type != ovs_datapath_family
1707 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1708 ARRAY_SIZE(ovs_datapath_policy))) {
1712 dp->cmd = genl->cmd;
1713 dp->dp_ifindex = ovs_header->dp_ifindex;
1714 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1715 if (a[OVS_DP_ATTR_STATS]) {
1716 /* Can't use structure assignment because Netlink doesn't ensure
1717 * sufficient alignment for 64-bit members. */
1718 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1725 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1727 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1729 struct ovs_header *ovs_header;
1731 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1732 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1733 OVS_DATAPATH_VERSION);
1735 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1736 ovs_header->dp_ifindex = dp->dp_ifindex;
1739 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1742 if (dp->upcall_pid) {
1743 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1746 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1749 /* Clears 'dp' to "empty" values. */
1751 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1753 memset(dp, 0, sizeof *dp);
1757 dpif_linux_dp_dump_start(struct nl_dump *dump)
1759 struct dpif_linux_dp request;
1762 dpif_linux_dp_init(&request);
1763 request.cmd = OVS_DP_CMD_GET;
1765 buf = ofpbuf_new(1024);
1766 dpif_linux_dp_to_ofpbuf(&request, buf);
1767 nl_dump_start(dump, genl_sock, buf);
1771 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1772 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1773 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1774 * result of the command is expected to be of the same form, which is decoded
1775 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1776 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1778 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1779 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1781 struct ofpbuf *request_buf;
1784 ovs_assert((reply != NULL) == (bufp != NULL));
1786 request_buf = ofpbuf_new(1024);
1787 dpif_linux_dp_to_ofpbuf(request, request_buf);
1788 error = nl_sock_transact(genl_sock, request_buf, bufp);
1789 ofpbuf_delete(request_buf);
1793 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1796 dpif_linux_dp_init(reply);
1797 ofpbuf_delete(*bufp);
1804 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1805 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1806 * will contain pointers into '*bufp'). */
1808 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1809 struct ofpbuf **bufp)
1811 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1812 struct dpif_linux_dp request;
1814 dpif_linux_dp_init(&request);
1815 request.cmd = OVS_DP_CMD_GET;
1816 request.dp_ifindex = dpif->dp_ifindex;
1818 return dpif_linux_dp_transact(&request, reply, bufp);
1821 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1822 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1823 * positive errno value.
1825 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1826 * while 'flow' is still in use. */
1828 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1829 const struct ofpbuf *buf)
1831 static const struct nl_policy ovs_flow_policy[] = {
1832 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1833 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1834 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1836 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1837 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1838 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1841 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1842 struct ovs_header *ovs_header;
1843 struct nlmsghdr *nlmsg;
1844 struct genlmsghdr *genl;
1847 dpif_linux_flow_init(flow);
1849 ofpbuf_use_const(&b, buf->data, buf->size);
1850 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1851 genl = ofpbuf_try_pull(&b, sizeof *genl);
1852 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1853 if (!nlmsg || !genl || !ovs_header
1854 || nlmsg->nlmsg_type != ovs_flow_family
1855 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1856 ARRAY_SIZE(ovs_flow_policy))) {
1860 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1861 flow->dp_ifindex = ovs_header->dp_ifindex;
1862 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1863 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1864 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1865 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1866 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1868 if (a[OVS_FLOW_ATTR_STATS]) {
1869 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1871 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1872 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1874 if (a[OVS_FLOW_ATTR_USED]) {
1875 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1880 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1881 * followed by Netlink attributes corresponding to 'flow'. */
1883 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1886 struct ovs_header *ovs_header;
1888 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1889 NLM_F_REQUEST | flow->nlmsg_flags,
1890 flow->cmd, OVS_FLOW_VERSION);
1892 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1893 ovs_header->dp_ifindex = flow->dp_ifindex;
1895 if (flow->key_len) {
1896 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1899 if (flow->actions || flow->actions_len) {
1900 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1901 flow->actions, flow->actions_len);
1904 /* We never need to send these to the kernel. */
1905 ovs_assert(!flow->stats);
1906 ovs_assert(!flow->tcp_flags);
1907 ovs_assert(!flow->used);
1910 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1914 /* Clears 'flow' to "empty" values. */
1916 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1918 memset(flow, 0, sizeof *flow);
1921 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1922 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1923 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1924 * result of the command is expected to be a flow also, which is decoded and
1925 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1926 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1928 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1929 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1931 struct ofpbuf *request_buf;
1934 ovs_assert((reply != NULL) == (bufp != NULL));
1937 request->nlmsg_flags |= NLM_F_ECHO;
1940 request_buf = ofpbuf_new(1024);
1941 dpif_linux_flow_to_ofpbuf(request, request_buf);
1942 error = nl_sock_transact(genl_sock, request_buf, bufp);
1943 ofpbuf_delete(request_buf);
1947 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1950 dpif_linux_flow_init(reply);
1951 ofpbuf_delete(*bufp);
1959 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1960 struct dpif_flow_stats *stats)
1963 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1964 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1966 stats->n_packets = 0;
1969 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1970 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1973 /* Logs information about a packet that was recently lost in 'ch' (in
1976 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1978 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1979 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1982 if (VLOG_DROP_WARN(&rl)) {
1987 if (ch->last_poll != LLONG_MIN) {
1988 ds_put_format(&s, " (last polled %lld ms ago)",
1989 time_msec() - ch->last_poll);
1992 VLOG_WARN("%s: lost packet on channel %td%s",
1993 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));