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 *mask; /* OVS_FLOW_ATTR_MASK. */
110 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
112 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
113 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
114 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
115 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
118 static void dpif_linux_flow_init(struct dpif_linux_flow *);
119 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
120 const struct ofpbuf *);
121 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
123 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
124 struct dpif_linux_flow *reply,
125 struct ofpbuf **bufp);
126 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
127 struct dpif_flow_stats *);
129 /* One of the dpif channels between the kernel and userspace. */
130 struct dpif_channel {
131 struct nl_sock *sock; /* Netlink socket. */
132 long long int last_poll; /* Last time this channel was polled. */
135 static void report_loss(struct dpif *, struct dpif_channel *);
137 /* Datapath interface for the openvswitch Linux kernel module. */
142 /* Upcall messages. */
143 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
144 struct dpif_channel *channels;
145 struct epoll_event *epoll_events;
146 int epoll_fd; /* epoll fd that includes channel socks. */
147 int n_events; /* Num events returned by epoll_wait(). */
148 int event_offset; /* Offset into 'epoll_events'. */
150 /* Change notification. */
151 struct sset changed_ports; /* Ports that have changed. */
152 struct nln_notifier *port_notifier;
156 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
158 /* Generic Netlink family numbers for OVS. */
159 static int ovs_datapath_family;
160 static int ovs_vport_family;
161 static int ovs_flow_family;
162 static int ovs_packet_family;
164 /* Generic Netlink socket. */
165 static struct nl_sock *genl_sock;
166 static struct nln *nln = NULL;
168 static int dpif_linux_init(void);
169 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
170 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
171 static void dpif_linux_port_changed(const void *vport, void *dpif);
172 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint32_t port_no);
174 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
176 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
177 const struct ofpbuf *);
179 static struct dpif_linux *
180 dpif_linux_cast(const struct dpif *dpif)
182 dpif_assert_class(dpif, &dpif_linux_class);
183 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
187 dpif_linux_enumerate(struct sset *all_dps)
193 error = dpif_linux_init();
198 dpif_linux_dp_dump_start(&dump);
199 while (nl_dump_next(&dump, &msg)) {
200 struct dpif_linux_dp dp;
202 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
203 sset_add(all_dps, dp.name);
206 return nl_dump_done(&dump);
210 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
211 bool create, struct dpif **dpifp)
213 struct dpif_linux_dp dp_request, dp;
218 error = dpif_linux_init();
223 /* Create or look up datapath. */
224 dpif_linux_dp_init(&dp_request);
226 dp_request.cmd = OVS_DP_CMD_NEW;
228 dp_request.upcall_pid = &upcall_pid;
230 dp_request.cmd = OVS_DP_CMD_GET;
232 dp_request.name = name;
233 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
238 open_dpif(&dp, dpifp);
244 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
246 struct dpif_linux *dpif;
248 dpif = xzalloc(sizeof *dpif);
249 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
253 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
254 dp->dp_ifindex, dp->dp_ifindex);
256 dpif->dp_ifindex = dp->dp_ifindex;
257 sset_init(&dpif->changed_ports);
258 *dpifp = &dpif->dpif;
262 destroy_channels(struct dpif_linux *dpif)
266 if (dpif->epoll_fd < 0) {
270 for (i = 0; i < dpif->uc_array_size; i++ ) {
271 struct dpif_linux_vport vport_request;
272 struct dpif_channel *ch = &dpif->channels[i];
273 uint32_t upcall_pid = 0;
279 /* Turn off upcalls. */
280 dpif_linux_vport_init(&vport_request);
281 vport_request.cmd = OVS_VPORT_CMD_SET;
282 vport_request.dp_ifindex = dpif->dp_ifindex;
283 vport_request.port_no = i;
284 vport_request.upcall_pid = &upcall_pid;
285 dpif_linux_vport_transact(&vport_request, NULL, NULL);
287 nl_sock_destroy(ch->sock);
290 free(dpif->channels);
291 dpif->channels = NULL;
292 dpif->uc_array_size = 0;
294 free(dpif->epoll_events);
295 dpif->epoll_events = NULL;
296 dpif->n_events = dpif->event_offset = 0;
298 close(dpif->epoll_fd);
303 add_channel(struct dpif_linux *dpif, uint32_t port_no, struct nl_sock *sock)
305 struct epoll_event event;
307 if (dpif->epoll_fd < 0) {
311 /* We assume that the datapath densely chooses port numbers, which
312 * can therefore be used as an index into an array of channels. */
313 if (port_no >= dpif->uc_array_size) {
314 int new_size = port_no + 1;
317 if (new_size > MAX_PORTS) {
318 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
319 dpif_name(&dpif->dpif), port_no);
323 dpif->channels = xrealloc(dpif->channels,
324 new_size * sizeof *dpif->channels);
325 for (i = dpif->uc_array_size; i < new_size; i++) {
326 dpif->channels[i].sock = NULL;
329 dpif->epoll_events = xrealloc(dpif->epoll_events,
330 new_size * sizeof *dpif->epoll_events);
331 dpif->uc_array_size = new_size;
334 memset(&event, 0, sizeof event);
335 event.events = EPOLLIN;
336 event.data.u32 = port_no;
337 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
342 nl_sock_destroy(dpif->channels[port_no].sock);
343 dpif->channels[port_no].sock = sock;
344 dpif->channels[port_no].last_poll = LLONG_MIN;
350 del_channel(struct dpif_linux *dpif, uint32_t port_no)
352 struct dpif_channel *ch;
354 if (dpif->epoll_fd < 0 || port_no >= dpif->uc_array_size) {
358 ch = &dpif->channels[port_no];
363 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
364 dpif->event_offset = dpif->n_events = 0;
366 nl_sock_destroy(ch->sock);
371 dpif_linux_close(struct dpif *dpif_)
373 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
375 nln_notifier_destroy(dpif->port_notifier);
376 destroy_channels(dpif);
377 sset_destroy(&dpif->changed_ports);
382 dpif_linux_destroy(struct dpif *dpif_)
384 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
385 struct dpif_linux_dp dp;
387 dpif_linux_dp_init(&dp);
388 dp.cmd = OVS_DP_CMD_DEL;
389 dp.dp_ifindex = dpif->dp_ifindex;
390 return dpif_linux_dp_transact(&dp, NULL, NULL);
394 dpif_linux_run(struct dpif *dpif_ OVS_UNUSED)
402 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
410 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
412 struct dpif_linux_dp dp;
416 error = dpif_linux_dp_get(dpif_, &dp, &buf);
418 stats->n_hit = dp.stats.n_hit;
419 stats->n_missed = dp.stats.n_missed;
420 stats->n_lost = dp.stats.n_lost;
421 stats->n_flows = dp.stats.n_flows;
428 get_vport_type(const struct dpif_linux_vport *vport)
430 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
432 switch (vport->type) {
433 case OVS_VPORT_TYPE_NETDEV:
436 case OVS_VPORT_TYPE_INTERNAL:
439 case OVS_VPORT_TYPE_GRE:
442 case OVS_VPORT_TYPE_GRE64:
445 case OVS_VPORT_TYPE_VXLAN:
448 case OVS_VPORT_TYPE_LISP:
451 case OVS_VPORT_TYPE_UNSPEC:
452 case __OVS_VPORT_TYPE_MAX:
456 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
457 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
461 static enum ovs_vport_type
462 netdev_to_ovs_vport_type(const struct netdev *netdev)
464 const char *type = netdev_get_type(netdev);
466 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
467 return OVS_VPORT_TYPE_NETDEV;
468 } else if (!strcmp(type, "internal")) {
469 return OVS_VPORT_TYPE_INTERNAL;
470 } else if (strstr(type, "gre64")) {
471 return OVS_VPORT_TYPE_GRE64;
472 } else if (strstr(type, "gre")) {
473 return OVS_VPORT_TYPE_GRE;
474 } else if (!strcmp(type, "vxlan")) {
475 return OVS_VPORT_TYPE_VXLAN;
476 } else if (!strcmp(type, "lisp")) {
477 return OVS_VPORT_TYPE_LISP;
479 return OVS_VPORT_TYPE_UNSPEC;
484 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
487 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
488 const struct netdev_tunnel_config *tnl_cfg;
489 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
490 const char *name = netdev_vport_get_dpif_port(netdev,
491 namebuf, sizeof namebuf);
492 const char *type = netdev_get_type(netdev);
493 struct dpif_linux_vport request, reply;
494 struct nl_sock *sock = NULL;
497 uint64_t options_stub[64 / 8];
498 struct ofpbuf options;
501 if (dpif->epoll_fd >= 0) {
502 error = nl_sock_create(NETLINK_GENERIC, &sock);
508 dpif_linux_vport_init(&request);
509 request.cmd = OVS_VPORT_CMD_NEW;
510 request.dp_ifindex = dpif->dp_ifindex;
511 request.type = netdev_to_ovs_vport_type(netdev);
512 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
513 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
514 "unsupported type `%s'",
515 dpif_name(dpif_), name, type);
516 nl_sock_destroy(sock);
521 if (request.type == OVS_VPORT_TYPE_NETDEV) {
522 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
525 tnl_cfg = netdev_get_tunnel_config(netdev);
526 if (tnl_cfg && tnl_cfg->dst_port != 0) {
527 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
528 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
529 ntohs(tnl_cfg->dst_port));
530 request.options = options.data;
531 request.options_len = options.size;
534 request.port_no = *port_nop;
535 upcall_pid = sock ? nl_sock_pid(sock) : 0;
536 request.upcall_pid = &upcall_pid;
538 error = dpif_linux_vport_transact(&request, &reply, &buf);
540 *port_nop = reply.port_no;
541 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
542 dpif_name(dpif_), reply.port_no, upcall_pid);
544 if (error == EBUSY && *port_nop != UINT32_MAX) {
545 VLOG_INFO("%s: requested port %"PRIu32" is in use",
546 dpif_name(dpif_), *port_nop);
548 nl_sock_destroy(sock);
555 error = add_channel(dpif, *port_nop, sock);
557 VLOG_INFO("%s: could not add channel for port %s",
558 dpif_name(dpif_), name);
560 /* Delete the port. */
561 dpif_linux_vport_init(&request);
562 request.cmd = OVS_VPORT_CMD_DEL;
563 request.dp_ifindex = dpif->dp_ifindex;
564 request.port_no = *port_nop;
565 dpif_linux_vport_transact(&request, NULL, NULL);
567 nl_sock_destroy(sock);
576 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
578 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
579 struct dpif_linux_vport vport;
582 dpif_linux_vport_init(&vport);
583 vport.cmd = OVS_VPORT_CMD_DEL;
584 vport.dp_ifindex = dpif->dp_ifindex;
585 vport.port_no = port_no;
586 error = dpif_linux_vport_transact(&vport, NULL, NULL);
588 del_channel(dpif, port_no);
594 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
595 const char *port_name, struct dpif_port *dpif_port)
597 struct dpif_linux_vport request;
598 struct dpif_linux_vport reply;
602 dpif_linux_vport_init(&request);
603 request.cmd = OVS_VPORT_CMD_GET;
604 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
605 request.port_no = port_no;
606 request.name = port_name;
608 error = dpif_linux_vport_transact(&request, &reply, &buf);
610 if (reply.dp_ifindex != request.dp_ifindex) {
611 /* A query by name reported that 'port_name' is in some datapath
612 * other than 'dpif', but the caller wants to know about 'dpif'. */
614 } else if (dpif_port) {
615 dpif_port->name = xstrdup(reply.name);
616 dpif_port->type = xstrdup(get_vport_type(&reply));
617 dpif_port->port_no = reply.port_no;
625 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
626 struct dpif_port *dpif_port)
628 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
632 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
633 struct dpif_port *dpif_port)
635 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
639 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
645 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
647 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
649 if (dpif->epoll_fd < 0) {
652 /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
653 * channel, since it is not heavily loaded. */
654 int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
655 return nl_sock_pid(dpif->channels[idx].sock);
660 dpif_linux_flow_flush(struct dpif *dpif_)
662 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
663 struct dpif_linux_flow flow;
665 dpif_linux_flow_init(&flow);
666 flow.cmd = OVS_FLOW_CMD_DEL;
667 flow.dp_ifindex = dpif->dp_ifindex;
668 return dpif_linux_flow_transact(&flow, NULL, NULL);
671 struct dpif_linux_port_state {
676 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
678 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
679 struct dpif_linux_port_state *state;
680 struct dpif_linux_vport request;
683 *statep = state = xmalloc(sizeof *state);
685 dpif_linux_vport_init(&request);
686 request.cmd = OVS_DP_CMD_GET;
687 request.dp_ifindex = dpif->dp_ifindex;
689 buf = ofpbuf_new(1024);
690 dpif_linux_vport_to_ofpbuf(&request, buf);
691 nl_dump_start(&state->dump, genl_sock, buf);
698 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
699 struct dpif_port *dpif_port)
701 struct dpif_linux_port_state *state = state_;
702 struct dpif_linux_vport vport;
706 if (!nl_dump_next(&state->dump, &buf)) {
710 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
715 dpif_port->name = CONST_CAST(char *, vport.name);
716 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
717 dpif_port->port_no = vport.port_no;
722 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
724 struct dpif_linux_port_state *state = state_;
725 int error = nl_dump_done(&state->dump);
732 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
734 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
736 if (dpif->change_error) {
737 dpif->change_error = false;
738 sset_clear(&dpif->changed_ports);
740 } else if (!sset_is_empty(&dpif->changed_ports)) {
741 *devnamep = sset_pop(&dpif->changed_ports);
749 dpif_linux_port_poll_wait(const struct dpif *dpif_)
751 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
752 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
753 poll_immediate_wake();
758 dpif_linux_flow_get__(const struct dpif *dpif_,
759 const struct nlattr *key, size_t key_len,
760 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
762 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
763 struct dpif_linux_flow request;
765 dpif_linux_flow_init(&request);
766 request.cmd = OVS_FLOW_CMD_GET;
767 request.dp_ifindex = dpif->dp_ifindex;
769 request.key_len = key_len;
770 return dpif_linux_flow_transact(&request, reply, bufp);
774 dpif_linux_flow_get(const struct dpif *dpif_,
775 const struct nlattr *key, size_t key_len,
776 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
778 struct dpif_linux_flow reply;
782 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
785 dpif_linux_flow_get_stats(&reply, stats);
788 buf->data = CONST_CAST(struct nlattr *, reply.actions);
789 buf->size = reply.actions_len;
799 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
800 struct dpif_linux_flow *request)
802 static const struct nlattr dummy_action;
804 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
806 dpif_linux_flow_init(request);
807 request->cmd = (put->flags & DPIF_FP_CREATE
808 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
809 request->dp_ifindex = dpif->dp_ifindex;
810 request->key = put->key;
811 request->key_len = put->key_len;
812 request->mask = put->mask;
813 request->mask_len = put->mask_len;
814 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
815 request->actions = (put->actions
817 : CONST_CAST(struct nlattr *, &dummy_action));
818 request->actions_len = put->actions_len;
819 if (put->flags & DPIF_FP_ZERO_STATS) {
820 request->clear = true;
822 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
826 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
828 struct dpif_linux_flow request, reply;
832 dpif_linux_init_flow_put(dpif_, put, &request);
833 error = dpif_linux_flow_transact(&request,
834 put->stats ? &reply : NULL,
835 put->stats ? &buf : NULL);
836 if (!error && put->stats) {
837 dpif_linux_flow_get_stats(&reply, put->stats);
844 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
845 struct dpif_linux_flow *request)
847 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
849 dpif_linux_flow_init(request);
850 request->cmd = OVS_FLOW_CMD_DEL;
851 request->dp_ifindex = dpif->dp_ifindex;
852 request->key = del->key;
853 request->key_len = del->key_len;
857 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
859 struct dpif_linux_flow request, reply;
863 dpif_linux_init_flow_del(dpif_, del, &request);
864 error = dpif_linux_flow_transact(&request,
865 del->stats ? &reply : NULL,
866 del->stats ? &buf : NULL);
867 if (!error && del->stats) {
868 dpif_linux_flow_get_stats(&reply, del->stats);
874 struct dpif_linux_flow_state {
876 struct dpif_linux_flow flow;
877 struct dpif_flow_stats stats;
882 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
884 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
885 struct dpif_linux_flow_state *state;
886 struct dpif_linux_flow request;
889 *statep = state = xmalloc(sizeof *state);
891 dpif_linux_flow_init(&request);
892 request.cmd = OVS_DP_CMD_GET;
893 request.dp_ifindex = dpif->dp_ifindex;
895 buf = ofpbuf_new(1024);
896 dpif_linux_flow_to_ofpbuf(&request, buf);
897 nl_dump_start(&state->dump, genl_sock, buf);
906 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
907 const struct nlattr **key, size_t *key_len,
908 const struct nlattr **mask, size_t *mask_len,
909 const struct nlattr **actions, size_t *actions_len,
910 const struct dpif_flow_stats **stats)
912 struct dpif_linux_flow_state *state = state_;
917 ofpbuf_delete(state->buf);
920 if (!nl_dump_next(&state->dump, &buf)) {
924 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
929 if (actions && !state->flow.actions) {
930 error = dpif_linux_flow_get__(dpif_, state->flow.key,
932 &state->flow, &state->buf);
933 if (error == ENOENT) {
934 VLOG_DBG("dumped flow disappeared on get");
936 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
942 *actions = state->flow.actions;
943 *actions_len = state->flow.actions_len;
946 *key = state->flow.key;
947 *key_len = state->flow.key_len;
950 *mask = state->flow.mask;
951 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
954 dpif_linux_flow_get_stats(&state->flow, &state->stats);
955 *stats = &state->stats;
961 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
963 struct dpif_linux_flow_state *state = state_;
964 int error = nl_dump_done(&state->dump);
965 ofpbuf_delete(state->buf);
971 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
974 struct ovs_header *k_exec;
976 ofpbuf_prealloc_tailroom(buf, (64
977 + d_exec->packet->size
979 + d_exec->actions_len));
981 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
982 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
984 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
985 k_exec->dp_ifindex = dp_ifindex;
987 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
988 d_exec->packet->data, d_exec->packet->size);
989 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
990 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
991 d_exec->actions, d_exec->actions_len);
995 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
997 uint64_t request_stub[1024 / 8];
998 struct ofpbuf request;
1001 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1002 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1003 error = nl_sock_transact(genl_sock, &request, NULL);
1004 ofpbuf_uninit(&request);
1010 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1012 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1014 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1020 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1022 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1025 struct nl_transaction txn;
1027 struct ofpbuf request;
1028 uint64_t request_stub[1024 / 8];
1030 struct ofpbuf reply;
1031 uint64_t reply_stub[1024 / 8];
1034 struct nl_transaction *txnsp[MAX_OPS];
1037 ovs_assert(n_ops <= MAX_OPS);
1038 for (i = 0; i < n_ops; i++) {
1039 struct op_auxdata *aux = &auxes[i];
1040 struct dpif_op *op = ops[i];
1041 struct dpif_flow_put *put;
1042 struct dpif_flow_del *del;
1043 struct dpif_execute *execute;
1044 struct dpif_linux_flow flow;
1046 ofpbuf_use_stub(&aux->request,
1047 aux->request_stub, sizeof aux->request_stub);
1048 aux->txn.request = &aux->request;
1050 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1051 aux->txn.reply = NULL;
1054 case DPIF_OP_FLOW_PUT:
1055 put = &op->u.flow_put;
1056 dpif_linux_init_flow_put(dpif_, put, &flow);
1058 flow.nlmsg_flags |= NLM_F_ECHO;
1059 aux->txn.reply = &aux->reply;
1061 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1064 case DPIF_OP_FLOW_DEL:
1065 del = &op->u.flow_del;
1066 dpif_linux_init_flow_del(dpif_, del, &flow);
1068 flow.nlmsg_flags |= NLM_F_ECHO;
1069 aux->txn.reply = &aux->reply;
1071 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1074 case DPIF_OP_EXECUTE:
1075 execute = &op->u.execute;
1076 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1085 for (i = 0; i < n_ops; i++) {
1086 txnsp[i] = &auxes[i].txn;
1088 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1090 for (i = 0; i < n_ops; i++) {
1091 struct op_auxdata *aux = &auxes[i];
1092 struct nl_transaction *txn = &auxes[i].txn;
1093 struct dpif_op *op = ops[i];
1094 struct dpif_flow_put *put;
1095 struct dpif_flow_del *del;
1097 op->error = txn->error;
1100 case DPIF_OP_FLOW_PUT:
1101 put = &op->u.flow_put;
1104 struct dpif_linux_flow reply;
1106 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1109 dpif_linux_flow_get_stats(&reply, put->stats);
1114 memset(put->stats, 0, sizeof *put->stats);
1119 case DPIF_OP_FLOW_DEL:
1120 del = &op->u.flow_del;
1123 struct dpif_linux_flow reply;
1125 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1128 dpif_linux_flow_get_stats(&reply, del->stats);
1133 memset(del->stats, 0, sizeof *del->stats);
1138 case DPIF_OP_EXECUTE:
1145 ofpbuf_uninit(&aux->request);
1146 ofpbuf_uninit(&aux->reply);
1151 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1154 size_t chunk = MIN(n_ops, MAX_OPS);
1155 dpif_linux_operate__(dpif, ops, chunk);
1162 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1164 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1166 if ((dpif->epoll_fd >= 0) == enable) {
1171 destroy_channels(dpif);
1173 struct dpif_port_dump port_dump;
1174 struct dpif_port port;
1176 dpif->epoll_fd = epoll_create(10);
1177 if (dpif->epoll_fd < 0) {
1181 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1182 struct dpif_linux_vport vport_request;
1183 struct nl_sock *sock;
1184 uint32_t upcall_pid;
1187 error = nl_sock_create(NETLINK_GENERIC, &sock);
1192 upcall_pid = nl_sock_pid(sock);
1194 dpif_linux_vport_init(&vport_request);
1195 vport_request.cmd = OVS_VPORT_CMD_SET;
1196 vport_request.dp_ifindex = dpif->dp_ifindex;
1197 vport_request.port_no = port.port_no;
1198 vport_request.upcall_pid = &upcall_pid;
1199 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1201 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1202 dpif_name(&dpif->dpif), vport_request.port_no,
1205 VLOG_WARN_RL(&error_rl,
1206 "%s: failed to set upcall pid on port: %s",
1207 dpif_name(&dpif->dpif), strerror(error));
1208 nl_sock_destroy(sock);
1210 if (error == ENODEV || error == ENOENT) {
1211 /* This device isn't there, but keep trying the others. */
1218 error = add_channel(dpif, port.port_no, sock);
1220 VLOG_INFO("%s: could not add channel for port %s",
1221 dpif_name(dpif_), port.name);
1222 nl_sock_destroy(sock);
1232 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1233 uint32_t queue_id, uint32_t *priority)
1235 if (queue_id < 0xf000) {
1236 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1244 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1247 static const struct nl_policy ovs_packet_policy[] = {
1248 /* Always present. */
1249 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1250 .min_len = ETH_HEADER_LEN },
1251 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1253 /* OVS_PACKET_CMD_ACTION only. */
1254 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1257 struct ovs_header *ovs_header;
1258 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1259 struct nlmsghdr *nlmsg;
1260 struct genlmsghdr *genl;
1264 ofpbuf_use_const(&b, buf->data, buf->size);
1266 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1267 genl = ofpbuf_try_pull(&b, sizeof *genl);
1268 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1269 if (!nlmsg || !genl || !ovs_header
1270 || nlmsg->nlmsg_type != ovs_packet_family
1271 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1272 ARRAY_SIZE(ovs_packet_policy))) {
1276 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1277 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1283 memset(upcall, 0, sizeof *upcall);
1284 upcall->type = type;
1285 upcall->packet = buf;
1286 upcall->packet->data = CONST_CAST(struct nlattr *,
1287 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1288 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1289 upcall->key = CONST_CAST(struct nlattr *,
1290 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1291 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1292 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1293 *dp_ifindex = ovs_header->dp_ifindex;
1299 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1302 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1305 if (dpif->epoll_fd < 0) {
1309 if (dpif->event_offset >= dpif->n_events) {
1312 dpif->event_offset = dpif->n_events = 0;
1315 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1316 dpif->uc_array_size, 0);
1317 } while (retval < 0 && errno == EINTR);
1319 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1320 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1321 } else if (retval > 0) {
1322 dpif->n_events = retval;
1326 while (dpif->event_offset < dpif->n_events) {
1327 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1328 struct dpif_channel *ch = &dpif->channels[idx];
1330 dpif->event_offset++;
1336 if (++read_tries > 50) {
1340 error = nl_sock_recv(ch->sock, buf, false);
1341 if (error == ENOBUFS) {
1342 /* ENOBUFS typically means that we've received so many
1343 * packets that the buffer overflowed. Try again
1344 * immediately because there's almost certainly a packet
1345 * waiting for us. */
1346 report_loss(dpif_, ch);
1350 ch->last_poll = time_msec();
1352 if (error == EAGAIN) {
1358 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1359 if (!error && dp_ifindex == dpif->dp_ifindex) {
1371 dpif_linux_recv_wait(struct dpif *dpif_)
1373 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1375 if (dpif->epoll_fd < 0) {
1379 poll_fd_wait(dpif->epoll_fd, POLLIN);
1383 dpif_linux_recv_purge(struct dpif *dpif_)
1385 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1386 struct dpif_channel *ch;
1388 if (dpif->epoll_fd < 0) {
1392 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1394 nl_sock_drain(ch->sock);
1399 const struct dpif_class dpif_linux_class = {
1401 dpif_linux_enumerate,
1408 dpif_linux_get_stats,
1409 dpif_linux_port_add,
1410 dpif_linux_port_del,
1411 dpif_linux_port_query_by_number,
1412 dpif_linux_port_query_by_name,
1413 dpif_linux_get_max_ports,
1414 dpif_linux_port_get_pid,
1415 dpif_linux_port_dump_start,
1416 dpif_linux_port_dump_next,
1417 dpif_linux_port_dump_done,
1418 dpif_linux_port_poll,
1419 dpif_linux_port_poll_wait,
1420 dpif_linux_flow_get,
1421 dpif_linux_flow_put,
1422 dpif_linux_flow_del,
1423 dpif_linux_flow_flush,
1424 dpif_linux_flow_dump_start,
1425 dpif_linux_flow_dump_next,
1426 dpif_linux_flow_dump_done,
1429 dpif_linux_recv_set,
1430 dpif_linux_queue_to_priority,
1432 dpif_linux_recv_wait,
1433 dpif_linux_recv_purge,
1437 dpif_linux_init(void)
1439 static int error = -1;
1442 unsigned int ovs_vport_mcgroup;
1444 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1445 &ovs_datapath_family);
1447 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1448 "The Open vSwitch kernel module is probably not loaded.",
1449 OVS_DATAPATH_FAMILY);
1452 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1455 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1458 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1459 &ovs_packet_family);
1462 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1465 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1467 OVS_VPORT_MCGROUP_FALLBACK_ID);
1470 static struct dpif_linux_vport vport;
1471 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1472 dpif_linux_nln_parse, &vport);
1480 dpif_linux_is_internal_device(const char *name)
1482 struct dpif_linux_vport reply;
1486 error = dpif_linux_vport_get(name, &reply, &buf);
1489 } else if (error != ENODEV && error != ENOENT) {
1490 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1491 name, strerror(error));
1494 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1498 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1500 struct dpif_linux_vport *vport = vport_;
1501 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1505 dpif_linux_port_changed(const void *vport_, void *dpif_)
1507 const struct dpif_linux_vport *vport = vport_;
1508 struct dpif_linux *dpif = dpif_;
1511 if (vport->dp_ifindex == dpif->dp_ifindex
1512 && (vport->cmd == OVS_VPORT_CMD_NEW
1513 || vport->cmd == OVS_VPORT_CMD_DEL
1514 || vport->cmd == OVS_VPORT_CMD_SET)) {
1515 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1516 dpif->dpif.full_name, vport->name, vport->cmd);
1517 sset_add(&dpif->changed_ports, vport->name);
1520 dpif->change_error = true;
1524 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1525 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1526 * positive errno value.
1528 * 'vport' will contain pointers into 'buf', so the caller should not free
1529 * 'buf' while 'vport' is still in use. */
1531 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1532 const struct ofpbuf *buf)
1534 static const struct nl_policy ovs_vport_policy[] = {
1535 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1536 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1537 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1538 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1539 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1541 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1544 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1545 struct ovs_header *ovs_header;
1546 struct nlmsghdr *nlmsg;
1547 struct genlmsghdr *genl;
1550 dpif_linux_vport_init(vport);
1552 ofpbuf_use_const(&b, buf->data, buf->size);
1553 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1554 genl = ofpbuf_try_pull(&b, sizeof *genl);
1555 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1556 if (!nlmsg || !genl || !ovs_header
1557 || nlmsg->nlmsg_type != ovs_vport_family
1558 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1559 ARRAY_SIZE(ovs_vport_policy))) {
1563 vport->cmd = genl->cmd;
1564 vport->dp_ifindex = ovs_header->dp_ifindex;
1565 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1566 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1567 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1568 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1569 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1571 if (a[OVS_VPORT_ATTR_STATS]) {
1572 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1574 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1575 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1576 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1581 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1582 * followed by Netlink attributes corresponding to 'vport'. */
1584 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1587 struct ovs_header *ovs_header;
1589 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1590 vport->cmd, OVS_VPORT_VERSION);
1592 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1593 ovs_header->dp_ifindex = vport->dp_ifindex;
1595 if (vport->port_no != UINT32_MAX) {
1596 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1599 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1600 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1604 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1607 if (vport->upcall_pid) {
1608 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1612 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1613 vport->stats, sizeof *vport->stats);
1616 if (vport->options) {
1617 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1618 vport->options, vport->options_len);
1622 /* Clears 'vport' to "empty" values. */
1624 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1626 memset(vport, 0, sizeof *vport);
1627 vport->port_no = UINT32_MAX;
1630 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1631 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1632 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1633 * result of the command is expected to be an ovs_vport also, which is decoded
1634 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1635 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1637 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1638 struct dpif_linux_vport *reply,
1639 struct ofpbuf **bufp)
1641 struct ofpbuf *request_buf;
1644 ovs_assert((reply != NULL) == (bufp != NULL));
1646 error = dpif_linux_init();
1650 dpif_linux_vport_init(reply);
1655 request_buf = ofpbuf_new(1024);
1656 dpif_linux_vport_to_ofpbuf(request, request_buf);
1657 error = nl_sock_transact(genl_sock, request_buf, bufp);
1658 ofpbuf_delete(request_buf);
1662 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1665 dpif_linux_vport_init(reply);
1666 ofpbuf_delete(*bufp);
1673 /* Obtains information about the kernel vport named 'name' and stores it into
1674 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1675 * longer needed ('reply' will contain pointers into '*bufp'). */
1677 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1678 struct ofpbuf **bufp)
1680 struct dpif_linux_vport request;
1682 dpif_linux_vport_init(&request);
1683 request.cmd = OVS_VPORT_CMD_GET;
1684 request.name = name;
1686 return dpif_linux_vport_transact(&request, reply, bufp);
1689 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1690 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1691 * positive errno value.
1693 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1694 * while 'dp' is still in use. */
1696 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1698 static const struct nl_policy ovs_datapath_policy[] = {
1699 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1700 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1704 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1705 struct ovs_header *ovs_header;
1706 struct nlmsghdr *nlmsg;
1707 struct genlmsghdr *genl;
1710 dpif_linux_dp_init(dp);
1712 ofpbuf_use_const(&b, buf->data, buf->size);
1713 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1714 genl = ofpbuf_try_pull(&b, sizeof *genl);
1715 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1716 if (!nlmsg || !genl || !ovs_header
1717 || nlmsg->nlmsg_type != ovs_datapath_family
1718 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1719 ARRAY_SIZE(ovs_datapath_policy))) {
1723 dp->cmd = genl->cmd;
1724 dp->dp_ifindex = ovs_header->dp_ifindex;
1725 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1726 if (a[OVS_DP_ATTR_STATS]) {
1727 /* Can't use structure assignment because Netlink doesn't ensure
1728 * sufficient alignment for 64-bit members. */
1729 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1736 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1738 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1740 struct ovs_header *ovs_header;
1742 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1743 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1744 OVS_DATAPATH_VERSION);
1746 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1747 ovs_header->dp_ifindex = dp->dp_ifindex;
1750 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1753 if (dp->upcall_pid) {
1754 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1757 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1760 /* Clears 'dp' to "empty" values. */
1762 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1764 memset(dp, 0, sizeof *dp);
1768 dpif_linux_dp_dump_start(struct nl_dump *dump)
1770 struct dpif_linux_dp request;
1773 dpif_linux_dp_init(&request);
1774 request.cmd = OVS_DP_CMD_GET;
1776 buf = ofpbuf_new(1024);
1777 dpif_linux_dp_to_ofpbuf(&request, buf);
1778 nl_dump_start(dump, genl_sock, buf);
1782 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1783 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1784 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1785 * result of the command is expected to be of the same form, which is decoded
1786 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1787 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1789 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1790 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1792 struct ofpbuf *request_buf;
1795 ovs_assert((reply != NULL) == (bufp != NULL));
1797 request_buf = ofpbuf_new(1024);
1798 dpif_linux_dp_to_ofpbuf(request, request_buf);
1799 error = nl_sock_transact(genl_sock, request_buf, bufp);
1800 ofpbuf_delete(request_buf);
1804 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1807 dpif_linux_dp_init(reply);
1808 ofpbuf_delete(*bufp);
1815 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1816 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1817 * will contain pointers into '*bufp'). */
1819 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1820 struct ofpbuf **bufp)
1822 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1823 struct dpif_linux_dp request;
1825 dpif_linux_dp_init(&request);
1826 request.cmd = OVS_DP_CMD_GET;
1827 request.dp_ifindex = dpif->dp_ifindex;
1829 return dpif_linux_dp_transact(&request, reply, bufp);
1832 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1833 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1834 * positive errno value.
1836 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1837 * while 'flow' is still in use. */
1839 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1840 const struct ofpbuf *buf)
1842 static const struct nl_policy ovs_flow_policy[] = {
1843 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1844 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
1845 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1846 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1848 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1849 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1850 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1853 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1854 struct ovs_header *ovs_header;
1855 struct nlmsghdr *nlmsg;
1856 struct genlmsghdr *genl;
1859 dpif_linux_flow_init(flow);
1861 ofpbuf_use_const(&b, buf->data, buf->size);
1862 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1863 genl = ofpbuf_try_pull(&b, sizeof *genl);
1864 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1865 if (!nlmsg || !genl || !ovs_header
1866 || nlmsg->nlmsg_type != ovs_flow_family
1867 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1868 ARRAY_SIZE(ovs_flow_policy))) {
1872 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1873 flow->dp_ifindex = ovs_header->dp_ifindex;
1874 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1875 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1877 if (a[OVS_FLOW_ATTR_MASK]) {
1878 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
1879 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
1881 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1882 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1883 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1885 if (a[OVS_FLOW_ATTR_STATS]) {
1886 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1888 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1889 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1891 if (a[OVS_FLOW_ATTR_USED]) {
1892 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1897 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1898 * followed by Netlink attributes corresponding to 'flow'. */
1900 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1903 struct ovs_header *ovs_header;
1905 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1906 NLM_F_REQUEST | flow->nlmsg_flags,
1907 flow->cmd, OVS_FLOW_VERSION);
1909 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1910 ovs_header->dp_ifindex = flow->dp_ifindex;
1912 if (flow->key_len) {
1913 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1916 if (flow->mask_len) {
1917 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
1920 if (flow->actions || flow->actions_len) {
1921 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1922 flow->actions, flow->actions_len);
1925 /* We never need to send these to the kernel. */
1926 ovs_assert(!flow->stats);
1927 ovs_assert(!flow->tcp_flags);
1928 ovs_assert(!flow->used);
1931 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1935 /* Clears 'flow' to "empty" values. */
1937 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1939 memset(flow, 0, sizeof *flow);
1942 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1943 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1944 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1945 * result of the command is expected to be a flow also, which is decoded and
1946 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1947 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1949 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1950 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1952 struct ofpbuf *request_buf;
1955 ovs_assert((reply != NULL) == (bufp != NULL));
1958 request->nlmsg_flags |= NLM_F_ECHO;
1961 request_buf = ofpbuf_new(1024);
1962 dpif_linux_flow_to_ofpbuf(request, request_buf);
1963 error = nl_sock_transact(genl_sock, request_buf, bufp);
1964 ofpbuf_delete(request_buf);
1968 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1971 dpif_linux_flow_init(reply);
1972 ofpbuf_delete(*bufp);
1980 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1981 struct dpif_flow_stats *stats)
1984 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1985 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1987 stats->n_packets = 0;
1990 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1991 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1994 /* Logs information about a packet that was recently lost in 'ch' (in
1997 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1999 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2000 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2003 if (VLOG_DROP_WARN(&rl)) {
2008 if (ch->last_poll != LLONG_MIN) {
2009 ds_put_format(&s, " (last polled %lld ms ago)",
2010 time_msec() - ch->last_poll);
2013 VLOG_WARN("%s: lost packet on channel %td%s",
2014 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));