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 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
488 const char *name = netdev_vport_get_dpif_port(netdev,
489 namebuf, sizeof namebuf);
490 const char *type = netdev_get_type(netdev);
491 struct dpif_linux_vport request, reply;
492 struct nl_sock *sock = NULL;
495 uint64_t options_stub[64 / 8];
496 struct ofpbuf options;
499 if (dpif->epoll_fd >= 0) {
500 error = nl_sock_create(NETLINK_GENERIC, &sock);
506 dpif_linux_vport_init(&request);
507 request.cmd = OVS_VPORT_CMD_NEW;
508 request.dp_ifindex = dpif->dp_ifindex;
509 request.type = netdev_to_ovs_vport_type(netdev);
510 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
511 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
512 "unsupported type `%s'",
513 dpif_name(dpif_), name, type);
514 nl_sock_destroy(sock);
519 if (request.type == OVS_VPORT_TYPE_NETDEV) {
520 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
523 tnl_cfg = netdev_get_tunnel_config(netdev);
524 if (tnl_cfg && tnl_cfg->dst_port != 0) {
525 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
526 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
527 ntohs(tnl_cfg->dst_port));
528 request.options = options.data;
529 request.options_len = options.size;
532 request.port_no = *port_nop;
533 upcall_pid = sock ? nl_sock_pid(sock) : 0;
534 request.upcall_pid = &upcall_pid;
536 error = dpif_linux_vport_transact(&request, &reply, &buf);
538 *port_nop = reply.port_no;
539 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
540 dpif_name(dpif_), reply.port_no, upcall_pid);
542 if (error == EBUSY && *port_nop != UINT32_MAX) {
543 VLOG_INFO("%s: requested port %"PRIu32" is in use",
544 dpif_name(dpif_), *port_nop);
546 nl_sock_destroy(sock);
553 error = add_channel(dpif, *port_nop, sock);
555 VLOG_INFO("%s: could not add channel for port %s",
556 dpif_name(dpif_), name);
558 /* Delete the port. */
559 dpif_linux_vport_init(&request);
560 request.cmd = OVS_VPORT_CMD_DEL;
561 request.dp_ifindex = dpif->dp_ifindex;
562 request.port_no = *port_nop;
563 dpif_linux_vport_transact(&request, NULL, NULL);
565 nl_sock_destroy(sock);
574 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
576 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
577 struct dpif_linux_vport vport;
580 dpif_linux_vport_init(&vport);
581 vport.cmd = OVS_VPORT_CMD_DEL;
582 vport.dp_ifindex = dpif->dp_ifindex;
583 vport.port_no = port_no;
584 error = dpif_linux_vport_transact(&vport, NULL, NULL);
586 del_channel(dpif, port_no);
592 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
593 const char *port_name, struct dpif_port *dpif_port)
595 struct dpif_linux_vport request;
596 struct dpif_linux_vport reply;
600 dpif_linux_vport_init(&request);
601 request.cmd = OVS_VPORT_CMD_GET;
602 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
603 request.port_no = port_no;
604 request.name = port_name;
606 error = dpif_linux_vport_transact(&request, &reply, &buf);
608 if (reply.dp_ifindex != request.dp_ifindex) {
609 /* A query by name reported that 'port_name' is in some datapath
610 * other than 'dpif', but the caller wants to know about 'dpif'. */
612 } else if (dpif_port) {
613 dpif_port->name = xstrdup(reply.name);
614 dpif_port->type = xstrdup(get_vport_type(&reply));
615 dpif_port->port_no = reply.port_no;
623 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
624 struct dpif_port *dpif_port)
626 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
630 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
631 struct dpif_port *dpif_port)
633 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
637 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
643 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
645 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
647 if (dpif->epoll_fd < 0) {
650 /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
651 * channel, since it is not heavily loaded. */
652 int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
653 return nl_sock_pid(dpif->channels[idx].sock);
658 dpif_linux_flow_flush(struct dpif *dpif_)
660 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
661 struct dpif_linux_flow flow;
663 dpif_linux_flow_init(&flow);
664 flow.cmd = OVS_FLOW_CMD_DEL;
665 flow.dp_ifindex = dpif->dp_ifindex;
666 return dpif_linux_flow_transact(&flow, NULL, NULL);
669 struct dpif_linux_port_state {
674 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
676 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
677 struct dpif_linux_port_state *state;
678 struct dpif_linux_vport request;
681 *statep = state = xmalloc(sizeof *state);
683 dpif_linux_vport_init(&request);
684 request.cmd = OVS_DP_CMD_GET;
685 request.dp_ifindex = dpif->dp_ifindex;
687 buf = ofpbuf_new(1024);
688 dpif_linux_vport_to_ofpbuf(&request, buf);
689 nl_dump_start(&state->dump, genl_sock, buf);
696 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
697 struct dpif_port *dpif_port)
699 struct dpif_linux_port_state *state = state_;
700 struct dpif_linux_vport vport;
704 if (!nl_dump_next(&state->dump, &buf)) {
708 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
713 dpif_port->name = CONST_CAST(char *, vport.name);
714 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
715 dpif_port->port_no = vport.port_no;
720 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
722 struct dpif_linux_port_state *state = state_;
723 int error = nl_dump_done(&state->dump);
730 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
732 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
734 if (dpif->change_error) {
735 dpif->change_error = false;
736 sset_clear(&dpif->changed_ports);
738 } else if (!sset_is_empty(&dpif->changed_ports)) {
739 *devnamep = sset_pop(&dpif->changed_ports);
747 dpif_linux_port_poll_wait(const struct dpif *dpif_)
749 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
750 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
751 poll_immediate_wake();
756 dpif_linux_flow_get__(const struct dpif *dpif_,
757 const struct nlattr *key, size_t key_len,
758 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
760 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
761 struct dpif_linux_flow request;
763 dpif_linux_flow_init(&request);
764 request.cmd = OVS_FLOW_CMD_GET;
765 request.dp_ifindex = dpif->dp_ifindex;
767 request.key_len = key_len;
768 return dpif_linux_flow_transact(&request, reply, bufp);
772 dpif_linux_flow_get(const struct dpif *dpif_,
773 const struct nlattr *key, size_t key_len,
774 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
776 struct dpif_linux_flow reply;
780 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
783 dpif_linux_flow_get_stats(&reply, stats);
786 buf->data = CONST_CAST(struct nlattr *, reply.actions);
787 buf->size = reply.actions_len;
797 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
798 struct dpif_linux_flow *request)
800 static const struct nlattr dummy_action;
802 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
804 dpif_linux_flow_init(request);
805 request->cmd = (put->flags & DPIF_FP_CREATE
806 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
807 request->dp_ifindex = dpif->dp_ifindex;
808 request->key = put->key;
809 request->key_len = put->key_len;
810 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
811 request->actions = (put->actions
813 : CONST_CAST(struct nlattr *, &dummy_action));
814 request->actions_len = put->actions_len;
815 if (put->flags & DPIF_FP_ZERO_STATS) {
816 request->clear = true;
818 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
822 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
824 struct dpif_linux_flow request, reply;
828 dpif_linux_init_flow_put(dpif_, put, &request);
829 error = dpif_linux_flow_transact(&request,
830 put->stats ? &reply : NULL,
831 put->stats ? &buf : NULL);
832 if (!error && put->stats) {
833 dpif_linux_flow_get_stats(&reply, put->stats);
840 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
841 struct dpif_linux_flow *request)
843 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
845 dpif_linux_flow_init(request);
846 request->cmd = OVS_FLOW_CMD_DEL;
847 request->dp_ifindex = dpif->dp_ifindex;
848 request->key = del->key;
849 request->key_len = del->key_len;
853 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
855 struct dpif_linux_flow request, reply;
859 dpif_linux_init_flow_del(dpif_, del, &request);
860 error = dpif_linux_flow_transact(&request,
861 del->stats ? &reply : NULL,
862 del->stats ? &buf : NULL);
863 if (!error && del->stats) {
864 dpif_linux_flow_get_stats(&reply, del->stats);
870 struct dpif_linux_flow_state {
872 struct dpif_linux_flow flow;
873 struct dpif_flow_stats stats;
878 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
880 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
881 struct dpif_linux_flow_state *state;
882 struct dpif_linux_flow request;
885 *statep = state = xmalloc(sizeof *state);
887 dpif_linux_flow_init(&request);
888 request.cmd = OVS_DP_CMD_GET;
889 request.dp_ifindex = dpif->dp_ifindex;
891 buf = ofpbuf_new(1024);
892 dpif_linux_flow_to_ofpbuf(&request, buf);
893 nl_dump_start(&state->dump, genl_sock, buf);
902 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
903 const struct nlattr **key, size_t *key_len,
904 const struct nlattr **actions, size_t *actions_len,
905 const struct dpif_flow_stats **stats)
907 struct dpif_linux_flow_state *state = state_;
912 ofpbuf_delete(state->buf);
915 if (!nl_dump_next(&state->dump, &buf)) {
919 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
924 if (actions && !state->flow.actions) {
925 error = dpif_linux_flow_get__(dpif_, state->flow.key,
927 &state->flow, &state->buf);
928 if (error == ENOENT) {
929 VLOG_DBG("dumped flow disappeared on get");
931 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
937 *actions = state->flow.actions;
938 *actions_len = state->flow.actions_len;
941 *key = state->flow.key;
942 *key_len = state->flow.key_len;
945 dpif_linux_flow_get_stats(&state->flow, &state->stats);
946 *stats = &state->stats;
952 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
954 struct dpif_linux_flow_state *state = state_;
955 int error = nl_dump_done(&state->dump);
956 ofpbuf_delete(state->buf);
962 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
965 struct ovs_header *k_exec;
967 ofpbuf_prealloc_tailroom(buf, (64
968 + d_exec->packet->size
970 + d_exec->actions_len));
972 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
973 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
975 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
976 k_exec->dp_ifindex = dp_ifindex;
978 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
979 d_exec->packet->data, d_exec->packet->size);
980 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
981 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
982 d_exec->actions, d_exec->actions_len);
986 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
988 uint64_t request_stub[1024 / 8];
989 struct ofpbuf request;
992 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
993 dpif_linux_encode_execute(dp_ifindex, execute, &request);
994 error = nl_sock_transact(genl_sock, &request, NULL);
995 ofpbuf_uninit(&request);
1001 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1003 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1005 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1011 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1013 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1016 struct nl_transaction txn;
1018 struct ofpbuf request;
1019 uint64_t request_stub[1024 / 8];
1021 struct ofpbuf reply;
1022 uint64_t reply_stub[1024 / 8];
1025 struct nl_transaction *txnsp[MAX_OPS];
1028 ovs_assert(n_ops <= MAX_OPS);
1029 for (i = 0; i < n_ops; i++) {
1030 struct op_auxdata *aux = &auxes[i];
1031 struct dpif_op *op = ops[i];
1032 struct dpif_flow_put *put;
1033 struct dpif_flow_del *del;
1034 struct dpif_execute *execute;
1035 struct dpif_linux_flow flow;
1037 ofpbuf_use_stub(&aux->request,
1038 aux->request_stub, sizeof aux->request_stub);
1039 aux->txn.request = &aux->request;
1041 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1042 aux->txn.reply = NULL;
1045 case DPIF_OP_FLOW_PUT:
1046 put = &op->u.flow_put;
1047 dpif_linux_init_flow_put(dpif_, put, &flow);
1049 flow.nlmsg_flags |= NLM_F_ECHO;
1050 aux->txn.reply = &aux->reply;
1052 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1055 case DPIF_OP_FLOW_DEL:
1056 del = &op->u.flow_del;
1057 dpif_linux_init_flow_del(dpif_, del, &flow);
1059 flow.nlmsg_flags |= NLM_F_ECHO;
1060 aux->txn.reply = &aux->reply;
1062 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1065 case DPIF_OP_EXECUTE:
1066 execute = &op->u.execute;
1067 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1076 for (i = 0; i < n_ops; i++) {
1077 txnsp[i] = &auxes[i].txn;
1079 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1081 for (i = 0; i < n_ops; i++) {
1082 struct op_auxdata *aux = &auxes[i];
1083 struct nl_transaction *txn = &auxes[i].txn;
1084 struct dpif_op *op = ops[i];
1085 struct dpif_flow_put *put;
1086 struct dpif_flow_del *del;
1088 op->error = txn->error;
1091 case DPIF_OP_FLOW_PUT:
1092 put = &op->u.flow_put;
1095 struct dpif_linux_flow reply;
1097 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1100 dpif_linux_flow_get_stats(&reply, put->stats);
1105 memset(put->stats, 0, sizeof *put->stats);
1110 case DPIF_OP_FLOW_DEL:
1111 del = &op->u.flow_del;
1114 struct dpif_linux_flow reply;
1116 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1119 dpif_linux_flow_get_stats(&reply, del->stats);
1124 memset(del->stats, 0, sizeof *del->stats);
1129 case DPIF_OP_EXECUTE:
1136 ofpbuf_uninit(&aux->request);
1137 ofpbuf_uninit(&aux->reply);
1142 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1145 size_t chunk = MIN(n_ops, MAX_OPS);
1146 dpif_linux_operate__(dpif, ops, chunk);
1153 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1155 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1157 if ((dpif->epoll_fd >= 0) == enable) {
1162 destroy_channels(dpif);
1164 struct dpif_port_dump port_dump;
1165 struct dpif_port port;
1167 dpif->epoll_fd = epoll_create(10);
1168 if (dpif->epoll_fd < 0) {
1172 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1173 struct dpif_linux_vport vport_request;
1174 struct nl_sock *sock;
1175 uint32_t upcall_pid;
1178 error = nl_sock_create(NETLINK_GENERIC, &sock);
1183 upcall_pid = nl_sock_pid(sock);
1185 dpif_linux_vport_init(&vport_request);
1186 vport_request.cmd = OVS_VPORT_CMD_SET;
1187 vport_request.dp_ifindex = dpif->dp_ifindex;
1188 vport_request.port_no = port.port_no;
1189 vport_request.upcall_pid = &upcall_pid;
1190 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1192 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1193 dpif_name(&dpif->dpif), vport_request.port_no,
1196 VLOG_WARN_RL(&error_rl,
1197 "%s: failed to set upcall pid on port: %s",
1198 dpif_name(&dpif->dpif), strerror(error));
1199 nl_sock_destroy(sock);
1201 if (error == ENODEV || error == ENOENT) {
1202 /* This device isn't there, but keep trying the others. */
1209 error = add_channel(dpif, port.port_no, sock);
1211 VLOG_INFO("%s: could not add channel for port %s",
1212 dpif_name(dpif_), port.name);
1213 nl_sock_destroy(sock);
1223 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1224 uint32_t queue_id, uint32_t *priority)
1226 if (queue_id < 0xf000) {
1227 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1235 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1238 static const struct nl_policy ovs_packet_policy[] = {
1239 /* Always present. */
1240 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1241 .min_len = ETH_HEADER_LEN },
1242 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1244 /* OVS_PACKET_CMD_ACTION only. */
1245 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1248 struct ovs_header *ovs_header;
1249 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1250 struct nlmsghdr *nlmsg;
1251 struct genlmsghdr *genl;
1255 ofpbuf_use_const(&b, buf->data, buf->size);
1257 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1258 genl = ofpbuf_try_pull(&b, sizeof *genl);
1259 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1260 if (!nlmsg || !genl || !ovs_header
1261 || nlmsg->nlmsg_type != ovs_packet_family
1262 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1263 ARRAY_SIZE(ovs_packet_policy))) {
1267 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1268 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1274 memset(upcall, 0, sizeof *upcall);
1275 upcall->type = type;
1276 upcall->packet = buf;
1277 upcall->packet->data = CONST_CAST(struct nlattr *,
1278 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1279 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1280 upcall->key = CONST_CAST(struct nlattr *,
1281 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1282 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1283 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1284 *dp_ifindex = ovs_header->dp_ifindex;
1290 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1293 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1296 if (dpif->epoll_fd < 0) {
1300 if (dpif->event_offset >= dpif->n_events) {
1303 dpif->event_offset = dpif->n_events = 0;
1306 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1307 dpif->uc_array_size, 0);
1308 } while (retval < 0 && errno == EINTR);
1310 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1311 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1312 } else if (retval > 0) {
1313 dpif->n_events = retval;
1317 while (dpif->event_offset < dpif->n_events) {
1318 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1319 struct dpif_channel *ch = &dpif->channels[idx];
1321 dpif->event_offset++;
1327 if (++read_tries > 50) {
1331 error = nl_sock_recv(ch->sock, buf, false);
1332 if (error == ENOBUFS) {
1333 /* ENOBUFS typically means that we've received so many
1334 * packets that the buffer overflowed. Try again
1335 * immediately because there's almost certainly a packet
1336 * waiting for us. */
1337 report_loss(dpif_, ch);
1341 ch->last_poll = time_msec();
1343 if (error == EAGAIN) {
1349 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1350 if (!error && dp_ifindex == dpif->dp_ifindex) {
1362 dpif_linux_recv_wait(struct dpif *dpif_)
1364 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1366 if (dpif->epoll_fd < 0) {
1370 poll_fd_wait(dpif->epoll_fd, POLLIN);
1374 dpif_linux_recv_purge(struct dpif *dpif_)
1376 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1377 struct dpif_channel *ch;
1379 if (dpif->epoll_fd < 0) {
1383 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1385 nl_sock_drain(ch->sock);
1390 const struct dpif_class dpif_linux_class = {
1392 dpif_linux_enumerate,
1399 dpif_linux_get_stats,
1400 dpif_linux_port_add,
1401 dpif_linux_port_del,
1402 dpif_linux_port_query_by_number,
1403 dpif_linux_port_query_by_name,
1404 dpif_linux_get_max_ports,
1405 dpif_linux_port_get_pid,
1406 dpif_linux_port_dump_start,
1407 dpif_linux_port_dump_next,
1408 dpif_linux_port_dump_done,
1409 dpif_linux_port_poll,
1410 dpif_linux_port_poll_wait,
1411 dpif_linux_flow_get,
1412 dpif_linux_flow_put,
1413 dpif_linux_flow_del,
1414 dpif_linux_flow_flush,
1415 dpif_linux_flow_dump_start,
1416 dpif_linux_flow_dump_next,
1417 dpif_linux_flow_dump_done,
1420 dpif_linux_recv_set,
1421 dpif_linux_queue_to_priority,
1423 dpif_linux_recv_wait,
1424 dpif_linux_recv_purge,
1428 dpif_linux_init(void)
1430 static int error = -1;
1433 unsigned int ovs_vport_mcgroup;
1435 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1436 &ovs_datapath_family);
1438 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1439 "The Open vSwitch kernel module is probably not loaded.",
1440 OVS_DATAPATH_FAMILY);
1443 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1446 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1449 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1450 &ovs_packet_family);
1453 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1456 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1458 OVS_VPORT_MCGROUP_FALLBACK_ID);
1461 static struct dpif_linux_vport vport;
1462 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1463 dpif_linux_nln_parse, &vport);
1471 dpif_linux_is_internal_device(const char *name)
1473 struct dpif_linux_vport reply;
1477 error = dpif_linux_vport_get(name, &reply, &buf);
1480 } else if (error != ENODEV && error != ENOENT) {
1481 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1482 name, strerror(error));
1485 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1489 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1491 struct dpif_linux_vport *vport = vport_;
1492 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1496 dpif_linux_port_changed(const void *vport_, void *dpif_)
1498 const struct dpif_linux_vport *vport = vport_;
1499 struct dpif_linux *dpif = dpif_;
1502 if (vport->dp_ifindex == dpif->dp_ifindex
1503 && (vport->cmd == OVS_VPORT_CMD_NEW
1504 || vport->cmd == OVS_VPORT_CMD_DEL
1505 || vport->cmd == OVS_VPORT_CMD_SET)) {
1506 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1507 dpif->dpif.full_name, vport->name, vport->cmd);
1508 sset_add(&dpif->changed_ports, vport->name);
1511 dpif->change_error = true;
1515 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1516 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1517 * positive errno value.
1519 * 'vport' will contain pointers into 'buf', so the caller should not free
1520 * 'buf' while 'vport' is still in use. */
1522 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1523 const struct ofpbuf *buf)
1525 static const struct nl_policy ovs_vport_policy[] = {
1526 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1527 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1528 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1529 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1530 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1532 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1535 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1536 struct ovs_header *ovs_header;
1537 struct nlmsghdr *nlmsg;
1538 struct genlmsghdr *genl;
1541 dpif_linux_vport_init(vport);
1543 ofpbuf_use_const(&b, buf->data, buf->size);
1544 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1545 genl = ofpbuf_try_pull(&b, sizeof *genl);
1546 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1547 if (!nlmsg || !genl || !ovs_header
1548 || nlmsg->nlmsg_type != ovs_vport_family
1549 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1550 ARRAY_SIZE(ovs_vport_policy))) {
1554 vport->cmd = genl->cmd;
1555 vport->dp_ifindex = ovs_header->dp_ifindex;
1556 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1557 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1558 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1559 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1560 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1562 if (a[OVS_VPORT_ATTR_STATS]) {
1563 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1565 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1566 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1567 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1572 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1573 * followed by Netlink attributes corresponding to 'vport'. */
1575 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1578 struct ovs_header *ovs_header;
1580 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1581 vport->cmd, OVS_VPORT_VERSION);
1583 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1584 ovs_header->dp_ifindex = vport->dp_ifindex;
1586 if (vport->port_no != UINT32_MAX) {
1587 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1590 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1591 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1595 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1598 if (vport->upcall_pid) {
1599 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1603 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1604 vport->stats, sizeof *vport->stats);
1607 if (vport->options) {
1608 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1609 vport->options, vport->options_len);
1613 /* Clears 'vport' to "empty" values. */
1615 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1617 memset(vport, 0, sizeof *vport);
1618 vport->port_no = UINT32_MAX;
1621 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1622 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1623 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1624 * result of the command is expected to be an ovs_vport also, which is decoded
1625 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1626 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1628 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1629 struct dpif_linux_vport *reply,
1630 struct ofpbuf **bufp)
1632 struct ofpbuf *request_buf;
1635 ovs_assert((reply != NULL) == (bufp != NULL));
1637 error = dpif_linux_init();
1641 dpif_linux_vport_init(reply);
1646 request_buf = ofpbuf_new(1024);
1647 dpif_linux_vport_to_ofpbuf(request, request_buf);
1648 error = nl_sock_transact(genl_sock, request_buf, bufp);
1649 ofpbuf_delete(request_buf);
1653 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1656 dpif_linux_vport_init(reply);
1657 ofpbuf_delete(*bufp);
1664 /* Obtains information about the kernel vport named 'name' and stores it into
1665 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1666 * longer needed ('reply' will contain pointers into '*bufp'). */
1668 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1669 struct ofpbuf **bufp)
1671 struct dpif_linux_vport request;
1673 dpif_linux_vport_init(&request);
1674 request.cmd = OVS_VPORT_CMD_GET;
1675 request.name = name;
1677 return dpif_linux_vport_transact(&request, reply, bufp);
1680 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1681 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1682 * positive errno value.
1684 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1685 * while 'dp' is still in use. */
1687 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1689 static const struct nl_policy ovs_datapath_policy[] = {
1690 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1691 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1695 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1696 struct ovs_header *ovs_header;
1697 struct nlmsghdr *nlmsg;
1698 struct genlmsghdr *genl;
1701 dpif_linux_dp_init(dp);
1703 ofpbuf_use_const(&b, buf->data, buf->size);
1704 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1705 genl = ofpbuf_try_pull(&b, sizeof *genl);
1706 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1707 if (!nlmsg || !genl || !ovs_header
1708 || nlmsg->nlmsg_type != ovs_datapath_family
1709 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1710 ARRAY_SIZE(ovs_datapath_policy))) {
1714 dp->cmd = genl->cmd;
1715 dp->dp_ifindex = ovs_header->dp_ifindex;
1716 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1717 if (a[OVS_DP_ATTR_STATS]) {
1718 /* Can't use structure assignment because Netlink doesn't ensure
1719 * sufficient alignment for 64-bit members. */
1720 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1727 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1729 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1731 struct ovs_header *ovs_header;
1733 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1734 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1735 OVS_DATAPATH_VERSION);
1737 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1738 ovs_header->dp_ifindex = dp->dp_ifindex;
1741 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1744 if (dp->upcall_pid) {
1745 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1748 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1751 /* Clears 'dp' to "empty" values. */
1753 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1755 memset(dp, 0, sizeof *dp);
1759 dpif_linux_dp_dump_start(struct nl_dump *dump)
1761 struct dpif_linux_dp request;
1764 dpif_linux_dp_init(&request);
1765 request.cmd = OVS_DP_CMD_GET;
1767 buf = ofpbuf_new(1024);
1768 dpif_linux_dp_to_ofpbuf(&request, buf);
1769 nl_dump_start(dump, genl_sock, buf);
1773 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1774 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1775 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1776 * result of the command is expected to be of the same form, which is decoded
1777 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1778 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1780 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1781 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1783 struct ofpbuf *request_buf;
1786 ovs_assert((reply != NULL) == (bufp != NULL));
1788 request_buf = ofpbuf_new(1024);
1789 dpif_linux_dp_to_ofpbuf(request, request_buf);
1790 error = nl_sock_transact(genl_sock, request_buf, bufp);
1791 ofpbuf_delete(request_buf);
1795 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1798 dpif_linux_dp_init(reply);
1799 ofpbuf_delete(*bufp);
1806 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1807 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1808 * will contain pointers into '*bufp'). */
1810 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1811 struct ofpbuf **bufp)
1813 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1814 struct dpif_linux_dp request;
1816 dpif_linux_dp_init(&request);
1817 request.cmd = OVS_DP_CMD_GET;
1818 request.dp_ifindex = dpif->dp_ifindex;
1820 return dpif_linux_dp_transact(&request, reply, bufp);
1823 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1824 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1825 * positive errno value.
1827 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1828 * while 'flow' is still in use. */
1830 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1831 const struct ofpbuf *buf)
1833 static const struct nl_policy ovs_flow_policy[] = {
1834 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1835 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1836 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1838 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1839 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1840 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1843 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1844 struct ovs_header *ovs_header;
1845 struct nlmsghdr *nlmsg;
1846 struct genlmsghdr *genl;
1849 dpif_linux_flow_init(flow);
1851 ofpbuf_use_const(&b, buf->data, buf->size);
1852 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1853 genl = ofpbuf_try_pull(&b, sizeof *genl);
1854 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1855 if (!nlmsg || !genl || !ovs_header
1856 || nlmsg->nlmsg_type != ovs_flow_family
1857 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1858 ARRAY_SIZE(ovs_flow_policy))) {
1862 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1863 flow->dp_ifindex = ovs_header->dp_ifindex;
1864 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1865 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1866 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1867 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1868 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1870 if (a[OVS_FLOW_ATTR_STATS]) {
1871 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1873 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1874 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1876 if (a[OVS_FLOW_ATTR_USED]) {
1877 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1882 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1883 * followed by Netlink attributes corresponding to 'flow'. */
1885 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1888 struct ovs_header *ovs_header;
1890 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1891 NLM_F_REQUEST | flow->nlmsg_flags,
1892 flow->cmd, OVS_FLOW_VERSION);
1894 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1895 ovs_header->dp_ifindex = flow->dp_ifindex;
1897 if (flow->key_len) {
1898 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1901 if (flow->actions || flow->actions_len) {
1902 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1903 flow->actions, flow->actions_len);
1906 /* We never need to send these to the kernel. */
1907 ovs_assert(!flow->stats);
1908 ovs_assert(!flow->tcp_flags);
1909 ovs_assert(!flow->used);
1912 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1916 /* Clears 'flow' to "empty" values. */
1918 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1920 memset(flow, 0, sizeof *flow);
1923 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1924 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1925 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1926 * result of the command is expected to be a flow also, which is decoded and
1927 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1928 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1930 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1931 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1933 struct ofpbuf *request_buf;
1936 ovs_assert((reply != NULL) == (bufp != NULL));
1939 request->nlmsg_flags |= NLM_F_ECHO;
1942 request_buf = ofpbuf_new(1024);
1943 dpif_linux_flow_to_ofpbuf(request, request_buf);
1944 error = nl_sock_transact(genl_sock, request_buf, bufp);
1945 ofpbuf_delete(request_buf);
1949 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1952 dpif_linux_flow_init(reply);
1953 ofpbuf_delete(*bufp);
1961 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1962 struct dpif_flow_stats *stats)
1965 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1966 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1968 stats->n_packets = 0;
1971 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1972 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1975 /* Logs information about a packet that was recently lost in 'ch' (in
1978 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1980 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1981 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1984 if (VLOG_DROP_WARN(&rl)) {
1989 if (ch->last_poll != LLONG_MIN) {
1990 ds_put_format(&s, " (last polled %lld ms ago)",
1991 time_msec() - ch->last_poll);
1994 VLOG_WARN("%s: lost packet on channel %td%s",
1995 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));