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 nln *nln = NULL;
167 static int dpif_linux_init(void);
168 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
169 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
170 static void dpif_linux_port_changed(const void *vport, void *dpif);
171 static uint32_t dpif_linux_port_get_pid(const struct dpif *,
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 = u32_to_odp(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, odp_port_t port_no, struct nl_sock *sock)
305 struct epoll_event event;
306 uint32_t port_idx = odp_to_u32(port_no);
308 if (dpif->epoll_fd < 0) {
312 /* We assume that the datapath densely chooses port numbers, which
313 * can therefore be used as an index into an array of channels. */
314 if (port_idx >= dpif->uc_array_size) {
315 uint32_t new_size = port_idx + 1;
318 if (new_size > MAX_PORTS) {
319 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
320 dpif_name(&dpif->dpif), port_no);
324 dpif->channels = xrealloc(dpif->channels,
325 new_size * sizeof *dpif->channels);
326 for (i = dpif->uc_array_size; i < new_size; i++) {
327 dpif->channels[i].sock = NULL;
330 dpif->epoll_events = xrealloc(dpif->epoll_events,
331 new_size * sizeof *dpif->epoll_events);
332 dpif->uc_array_size = new_size;
335 memset(&event, 0, sizeof event);
336 event.events = EPOLLIN;
337 event.data.u32 = port_idx;
338 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
343 nl_sock_destroy(dpif->channels[port_idx].sock);
344 dpif->channels[port_idx].sock = sock;
345 dpif->channels[port_idx].last_poll = LLONG_MIN;
351 del_channel(struct dpif_linux *dpif, odp_port_t port_no)
353 struct dpif_channel *ch;
354 uint32_t port_idx = odp_to_u32(port_no);
356 if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
360 ch = &dpif->channels[port_idx];
365 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
366 dpif->event_offset = dpif->n_events = 0;
368 nl_sock_destroy(ch->sock);
373 dpif_linux_close(struct dpif *dpif_)
375 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
377 nln_notifier_destroy(dpif->port_notifier);
378 destroy_channels(dpif);
379 sset_destroy(&dpif->changed_ports);
384 dpif_linux_destroy(struct dpif *dpif_)
386 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
387 struct dpif_linux_dp dp;
389 dpif_linux_dp_init(&dp);
390 dp.cmd = OVS_DP_CMD_DEL;
391 dp.dp_ifindex = dpif->dp_ifindex;
392 return dpif_linux_dp_transact(&dp, NULL, NULL);
396 dpif_linux_run(struct dpif *dpif_ OVS_UNUSED)
404 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
412 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
414 struct dpif_linux_dp dp;
418 error = dpif_linux_dp_get(dpif_, &dp, &buf);
420 stats->n_hit = dp.stats.n_hit;
421 stats->n_missed = dp.stats.n_missed;
422 stats->n_lost = dp.stats.n_lost;
423 stats->n_flows = dp.stats.n_flows;
430 get_vport_type(const struct dpif_linux_vport *vport)
432 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
434 switch (vport->type) {
435 case OVS_VPORT_TYPE_NETDEV:
438 case OVS_VPORT_TYPE_INTERNAL:
441 case OVS_VPORT_TYPE_GRE:
444 case OVS_VPORT_TYPE_GRE64:
447 case OVS_VPORT_TYPE_VXLAN:
450 case OVS_VPORT_TYPE_LISP:
453 case OVS_VPORT_TYPE_UNSPEC:
454 case __OVS_VPORT_TYPE_MAX:
458 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
459 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
463 static enum ovs_vport_type
464 netdev_to_ovs_vport_type(const struct netdev *netdev)
466 const char *type = netdev_get_type(netdev);
468 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
469 return OVS_VPORT_TYPE_NETDEV;
470 } else if (!strcmp(type, "internal")) {
471 return OVS_VPORT_TYPE_INTERNAL;
472 } else if (strstr(type, "gre64")) {
473 return OVS_VPORT_TYPE_GRE64;
474 } else if (strstr(type, "gre")) {
475 return OVS_VPORT_TYPE_GRE;
476 } else if (!strcmp(type, "vxlan")) {
477 return OVS_VPORT_TYPE_VXLAN;
478 } else if (!strcmp(type, "lisp")) {
479 return OVS_VPORT_TYPE_LISP;
481 return OVS_VPORT_TYPE_UNSPEC;
486 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
487 odp_port_t *port_nop)
489 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
490 const struct netdev_tunnel_config *tnl_cfg;
491 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
492 const char *name = netdev_vport_get_dpif_port(netdev,
493 namebuf, sizeof namebuf);
494 const char *type = netdev_get_type(netdev);
495 struct dpif_linux_vport request, reply;
496 struct nl_sock *sock = NULL;
499 uint64_t options_stub[64 / 8];
500 struct ofpbuf options;
503 if (dpif->epoll_fd >= 0) {
504 error = nl_sock_create(NETLINK_GENERIC, &sock);
510 dpif_linux_vport_init(&request);
511 request.cmd = OVS_VPORT_CMD_NEW;
512 request.dp_ifindex = dpif->dp_ifindex;
513 request.type = netdev_to_ovs_vport_type(netdev);
514 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
515 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
516 "unsupported type `%s'",
517 dpif_name(dpif_), name, type);
518 nl_sock_destroy(sock);
523 if (request.type == OVS_VPORT_TYPE_NETDEV) {
524 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
527 tnl_cfg = netdev_get_tunnel_config(netdev);
528 if (tnl_cfg && tnl_cfg->dst_port != 0) {
529 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
530 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
531 ntohs(tnl_cfg->dst_port));
532 request.options = options.data;
533 request.options_len = options.size;
536 request.port_no = *port_nop;
537 upcall_pid = sock ? nl_sock_pid(sock) : 0;
538 request.upcall_pid = &upcall_pid;
540 error = dpif_linux_vport_transact(&request, &reply, &buf);
542 *port_nop = reply.port_no;
543 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
544 dpif_name(dpif_), reply.port_no, upcall_pid);
546 if (error == EBUSY && *port_nop != ODPP_NONE) {
547 VLOG_INFO("%s: requested port %"PRIu32" is in use",
548 dpif_name(dpif_), *port_nop);
550 nl_sock_destroy(sock);
557 error = add_channel(dpif, *port_nop, sock);
559 VLOG_INFO("%s: could not add channel for port %s",
560 dpif_name(dpif_), name);
562 /* Delete the port. */
563 dpif_linux_vport_init(&request);
564 request.cmd = OVS_VPORT_CMD_DEL;
565 request.dp_ifindex = dpif->dp_ifindex;
566 request.port_no = *port_nop;
567 dpif_linux_vport_transact(&request, NULL, NULL);
569 nl_sock_destroy(sock);
578 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
580 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
581 struct dpif_linux_vport vport;
584 dpif_linux_vport_init(&vport);
585 vport.cmd = OVS_VPORT_CMD_DEL;
586 vport.dp_ifindex = dpif->dp_ifindex;
587 vport.port_no = port_no;
588 error = dpif_linux_vport_transact(&vport, NULL, NULL);
590 del_channel(dpif, port_no);
596 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
597 const char *port_name, struct dpif_port *dpif_port)
599 struct dpif_linux_vport request;
600 struct dpif_linux_vport reply;
604 dpif_linux_vport_init(&request);
605 request.cmd = OVS_VPORT_CMD_GET;
606 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
607 request.port_no = port_no;
608 request.name = port_name;
610 error = dpif_linux_vport_transact(&request, &reply, &buf);
612 if (reply.dp_ifindex != request.dp_ifindex) {
613 /* A query by name reported that 'port_name' is in some datapath
614 * other than 'dpif', but the caller wants to know about 'dpif'. */
616 } else if (dpif_port) {
617 dpif_port->name = xstrdup(reply.name);
618 dpif_port->type = xstrdup(get_vport_type(&reply));
619 dpif_port->port_no = reply.port_no;
627 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
628 struct dpif_port *dpif_port)
630 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
634 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
635 struct dpif_port *dpif_port)
637 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
641 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
643 return u32_to_odp(MAX_PORTS);
647 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
649 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
650 uint32_t port_idx = odp_to_u32(port_no);
652 if (dpif->epoll_fd < 0) {
655 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
656 * channel, since it is not heavily loaded. */
657 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
658 return nl_sock_pid(dpif->channels[idx].sock);
663 dpif_linux_flow_flush(struct dpif *dpif_)
665 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
666 struct dpif_linux_flow flow;
668 dpif_linux_flow_init(&flow);
669 flow.cmd = OVS_FLOW_CMD_DEL;
670 flow.dp_ifindex = dpif->dp_ifindex;
671 return dpif_linux_flow_transact(&flow, NULL, NULL);
674 struct dpif_linux_port_state {
679 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
681 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
682 struct dpif_linux_port_state *state;
683 struct dpif_linux_vport request;
686 *statep = state = xmalloc(sizeof *state);
688 dpif_linux_vport_init(&request);
689 request.cmd = OVS_DP_CMD_GET;
690 request.dp_ifindex = dpif->dp_ifindex;
692 buf = ofpbuf_new(1024);
693 dpif_linux_vport_to_ofpbuf(&request, buf);
694 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
701 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
702 struct dpif_port *dpif_port)
704 struct dpif_linux_port_state *state = state_;
705 struct dpif_linux_vport vport;
709 if (!nl_dump_next(&state->dump, &buf)) {
713 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
718 dpif_port->name = CONST_CAST(char *, vport.name);
719 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
720 dpif_port->port_no = vport.port_no;
725 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
727 struct dpif_linux_port_state *state = state_;
728 int error = nl_dump_done(&state->dump);
735 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
737 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
739 if (dpif->change_error) {
740 dpif->change_error = false;
741 sset_clear(&dpif->changed_ports);
743 } else if (!sset_is_empty(&dpif->changed_ports)) {
744 *devnamep = sset_pop(&dpif->changed_ports);
752 dpif_linux_port_poll_wait(const struct dpif *dpif_)
754 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
755 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
756 poll_immediate_wake();
761 dpif_linux_flow_get__(const struct dpif *dpif_,
762 const struct nlattr *key, size_t key_len,
763 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
765 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
766 struct dpif_linux_flow request;
768 dpif_linux_flow_init(&request);
769 request.cmd = OVS_FLOW_CMD_GET;
770 request.dp_ifindex = dpif->dp_ifindex;
772 request.key_len = key_len;
773 return dpif_linux_flow_transact(&request, reply, bufp);
777 dpif_linux_flow_get(const struct dpif *dpif_,
778 const struct nlattr *key, size_t key_len,
779 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
781 struct dpif_linux_flow reply;
785 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
788 dpif_linux_flow_get_stats(&reply, stats);
791 buf->data = CONST_CAST(struct nlattr *, reply.actions);
792 buf->size = reply.actions_len;
802 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
803 struct dpif_linux_flow *request)
805 static const struct nlattr dummy_action;
807 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
809 dpif_linux_flow_init(request);
810 request->cmd = (put->flags & DPIF_FP_CREATE
811 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
812 request->dp_ifindex = dpif->dp_ifindex;
813 request->key = put->key;
814 request->key_len = put->key_len;
815 request->mask = put->mask;
816 request->mask_len = put->mask_len;
817 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
818 request->actions = (put->actions
820 : CONST_CAST(struct nlattr *, &dummy_action));
821 request->actions_len = put->actions_len;
822 if (put->flags & DPIF_FP_ZERO_STATS) {
823 request->clear = true;
825 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
829 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
831 struct dpif_linux_flow request, reply;
835 dpif_linux_init_flow_put(dpif_, put, &request);
836 error = dpif_linux_flow_transact(&request,
837 put->stats ? &reply : NULL,
838 put->stats ? &buf : NULL);
839 if (!error && put->stats) {
840 dpif_linux_flow_get_stats(&reply, put->stats);
847 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
848 struct dpif_linux_flow *request)
850 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
852 dpif_linux_flow_init(request);
853 request->cmd = OVS_FLOW_CMD_DEL;
854 request->dp_ifindex = dpif->dp_ifindex;
855 request->key = del->key;
856 request->key_len = del->key_len;
860 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
862 struct dpif_linux_flow request, reply;
866 dpif_linux_init_flow_del(dpif_, del, &request);
867 error = dpif_linux_flow_transact(&request,
868 del->stats ? &reply : NULL,
869 del->stats ? &buf : NULL);
870 if (!error && del->stats) {
871 dpif_linux_flow_get_stats(&reply, del->stats);
877 struct dpif_linux_flow_state {
879 struct dpif_linux_flow flow;
880 struct dpif_flow_stats stats;
885 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
887 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
888 struct dpif_linux_flow_state *state;
889 struct dpif_linux_flow request;
892 *statep = state = xmalloc(sizeof *state);
894 dpif_linux_flow_init(&request);
895 request.cmd = OVS_DP_CMD_GET;
896 request.dp_ifindex = dpif->dp_ifindex;
898 buf = ofpbuf_new(1024);
899 dpif_linux_flow_to_ofpbuf(&request, buf);
900 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
909 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
910 const struct nlattr **key, size_t *key_len,
911 const struct nlattr **mask, size_t *mask_len,
912 const struct nlattr **actions, size_t *actions_len,
913 const struct dpif_flow_stats **stats)
915 struct dpif_linux_flow_state *state = state_;
920 ofpbuf_delete(state->buf);
923 if (!nl_dump_next(&state->dump, &buf)) {
927 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
932 if (actions && !state->flow.actions) {
933 error = dpif_linux_flow_get__(dpif_, state->flow.key,
935 &state->flow, &state->buf);
936 if (error == ENOENT) {
937 VLOG_DBG("dumped flow disappeared on get");
939 VLOG_WARN("error fetching dumped flow: %s",
940 ovs_strerror(error));
946 *actions = state->flow.actions;
947 *actions_len = state->flow.actions_len;
950 *key = state->flow.key;
951 *key_len = state->flow.key_len;
954 *mask = state->flow.mask;
955 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
958 dpif_linux_flow_get_stats(&state->flow, &state->stats);
959 *stats = &state->stats;
965 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
967 struct dpif_linux_flow_state *state = state_;
968 int error = nl_dump_done(&state->dump);
969 ofpbuf_delete(state->buf);
975 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
978 struct ovs_header *k_exec;
980 ofpbuf_prealloc_tailroom(buf, (64
981 + d_exec->packet->size
983 + d_exec->actions_len));
985 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
986 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
988 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
989 k_exec->dp_ifindex = dp_ifindex;
991 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
992 d_exec->packet->data, d_exec->packet->size);
993 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
994 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
995 d_exec->actions, d_exec->actions_len);
999 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1001 uint64_t request_stub[1024 / 8];
1002 struct ofpbuf request;
1005 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1006 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1007 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1008 ofpbuf_uninit(&request);
1014 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1016 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1018 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1024 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1026 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1029 struct nl_transaction txn;
1031 struct ofpbuf request;
1032 uint64_t request_stub[1024 / 8];
1034 struct ofpbuf reply;
1035 uint64_t reply_stub[1024 / 8];
1038 struct nl_transaction *txnsp[MAX_OPS];
1041 ovs_assert(n_ops <= MAX_OPS);
1042 for (i = 0; i < n_ops; i++) {
1043 struct op_auxdata *aux = &auxes[i];
1044 struct dpif_op *op = ops[i];
1045 struct dpif_flow_put *put;
1046 struct dpif_flow_del *del;
1047 struct dpif_execute *execute;
1048 struct dpif_linux_flow flow;
1050 ofpbuf_use_stub(&aux->request,
1051 aux->request_stub, sizeof aux->request_stub);
1052 aux->txn.request = &aux->request;
1054 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1055 aux->txn.reply = NULL;
1058 case DPIF_OP_FLOW_PUT:
1059 put = &op->u.flow_put;
1060 dpif_linux_init_flow_put(dpif_, put, &flow);
1062 flow.nlmsg_flags |= NLM_F_ECHO;
1063 aux->txn.reply = &aux->reply;
1065 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1068 case DPIF_OP_FLOW_DEL:
1069 del = &op->u.flow_del;
1070 dpif_linux_init_flow_del(dpif_, del, &flow);
1072 flow.nlmsg_flags |= NLM_F_ECHO;
1073 aux->txn.reply = &aux->reply;
1075 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1078 case DPIF_OP_EXECUTE:
1079 execute = &op->u.execute;
1080 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1089 for (i = 0; i < n_ops; i++) {
1090 txnsp[i] = &auxes[i].txn;
1092 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1094 for (i = 0; i < n_ops; i++) {
1095 struct op_auxdata *aux = &auxes[i];
1096 struct nl_transaction *txn = &auxes[i].txn;
1097 struct dpif_op *op = ops[i];
1098 struct dpif_flow_put *put;
1099 struct dpif_flow_del *del;
1101 op->error = txn->error;
1104 case DPIF_OP_FLOW_PUT:
1105 put = &op->u.flow_put;
1108 struct dpif_linux_flow reply;
1110 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1113 dpif_linux_flow_get_stats(&reply, put->stats);
1118 memset(put->stats, 0, sizeof *put->stats);
1123 case DPIF_OP_FLOW_DEL:
1124 del = &op->u.flow_del;
1127 struct dpif_linux_flow reply;
1129 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1132 dpif_linux_flow_get_stats(&reply, del->stats);
1137 memset(del->stats, 0, sizeof *del->stats);
1142 case DPIF_OP_EXECUTE:
1149 ofpbuf_uninit(&aux->request);
1150 ofpbuf_uninit(&aux->reply);
1155 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1158 size_t chunk = MIN(n_ops, MAX_OPS);
1159 dpif_linux_operate__(dpif, ops, chunk);
1166 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1168 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1170 if ((dpif->epoll_fd >= 0) == enable) {
1175 destroy_channels(dpif);
1177 struct dpif_port_dump port_dump;
1178 struct dpif_port port;
1180 dpif->epoll_fd = epoll_create(10);
1181 if (dpif->epoll_fd < 0) {
1185 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1186 struct dpif_linux_vport vport_request;
1187 struct nl_sock *sock;
1188 uint32_t upcall_pid;
1191 error = nl_sock_create(NETLINK_GENERIC, &sock);
1196 upcall_pid = nl_sock_pid(sock);
1198 dpif_linux_vport_init(&vport_request);
1199 vport_request.cmd = OVS_VPORT_CMD_SET;
1200 vport_request.dp_ifindex = dpif->dp_ifindex;
1201 vport_request.port_no = port.port_no;
1202 vport_request.upcall_pid = &upcall_pid;
1203 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1205 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1206 dpif_name(&dpif->dpif), vport_request.port_no,
1209 VLOG_WARN_RL(&error_rl,
1210 "%s: failed to set upcall pid on port: %s",
1211 dpif_name(&dpif->dpif), ovs_strerror(error));
1212 nl_sock_destroy(sock);
1214 if (error == ENODEV || error == ENOENT) {
1215 /* This device isn't there, but keep trying the others. */
1222 error = add_channel(dpif, port.port_no, sock);
1224 VLOG_INFO("%s: could not add channel for port %s",
1225 dpif_name(dpif_), port.name);
1226 nl_sock_destroy(sock);
1236 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1237 uint32_t queue_id, uint32_t *priority)
1239 if (queue_id < 0xf000) {
1240 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1248 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1251 static const struct nl_policy ovs_packet_policy[] = {
1252 /* Always present. */
1253 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1254 .min_len = ETH_HEADER_LEN },
1255 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1257 /* OVS_PACKET_CMD_ACTION only. */
1258 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1261 struct ovs_header *ovs_header;
1262 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1263 struct nlmsghdr *nlmsg;
1264 struct genlmsghdr *genl;
1268 ofpbuf_use_const(&b, buf->data, buf->size);
1270 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1271 genl = ofpbuf_try_pull(&b, sizeof *genl);
1272 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1273 if (!nlmsg || !genl || !ovs_header
1274 || nlmsg->nlmsg_type != ovs_packet_family
1275 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1276 ARRAY_SIZE(ovs_packet_policy))) {
1280 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1281 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1287 memset(upcall, 0, sizeof *upcall);
1288 upcall->type = type;
1289 upcall->packet = buf;
1290 upcall->packet->data = CONST_CAST(struct nlattr *,
1291 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1292 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1293 upcall->key = CONST_CAST(struct nlattr *,
1294 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1295 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1296 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1297 *dp_ifindex = ovs_header->dp_ifindex;
1303 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1306 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1309 if (dpif->epoll_fd < 0) {
1313 if (dpif->event_offset >= dpif->n_events) {
1316 dpif->event_offset = dpif->n_events = 0;
1319 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1320 dpif->uc_array_size, 0);
1321 } while (retval < 0 && errno == EINTR);
1323 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1324 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1325 } else if (retval > 0) {
1326 dpif->n_events = retval;
1330 while (dpif->event_offset < dpif->n_events) {
1331 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1332 struct dpif_channel *ch = &dpif->channels[idx];
1334 dpif->event_offset++;
1340 if (++read_tries > 50) {
1344 error = nl_sock_recv(ch->sock, buf, false);
1345 if (error == ENOBUFS) {
1346 /* ENOBUFS typically means that we've received so many
1347 * packets that the buffer overflowed. Try again
1348 * immediately because there's almost certainly a packet
1349 * waiting for us. */
1350 report_loss(dpif_, ch);
1354 ch->last_poll = time_msec();
1356 if (error == EAGAIN) {
1362 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1363 if (!error && dp_ifindex == dpif->dp_ifindex) {
1375 dpif_linux_recv_wait(struct dpif *dpif_)
1377 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1379 if (dpif->epoll_fd < 0) {
1383 poll_fd_wait(dpif->epoll_fd, POLLIN);
1387 dpif_linux_recv_purge(struct dpif *dpif_)
1389 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1390 struct dpif_channel *ch;
1392 if (dpif->epoll_fd < 0) {
1396 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1398 nl_sock_drain(ch->sock);
1403 const struct dpif_class dpif_linux_class = {
1405 dpif_linux_enumerate,
1412 dpif_linux_get_stats,
1413 dpif_linux_port_add,
1414 dpif_linux_port_del,
1415 dpif_linux_port_query_by_number,
1416 dpif_linux_port_query_by_name,
1417 dpif_linux_get_max_ports,
1418 dpif_linux_port_get_pid,
1419 dpif_linux_port_dump_start,
1420 dpif_linux_port_dump_next,
1421 dpif_linux_port_dump_done,
1422 dpif_linux_port_poll,
1423 dpif_linux_port_poll_wait,
1424 dpif_linux_flow_get,
1425 dpif_linux_flow_put,
1426 dpif_linux_flow_del,
1427 dpif_linux_flow_flush,
1428 dpif_linux_flow_dump_start,
1429 dpif_linux_flow_dump_next,
1430 dpif_linux_flow_dump_done,
1433 dpif_linux_recv_set,
1434 dpif_linux_queue_to_priority,
1436 dpif_linux_recv_wait,
1437 dpif_linux_recv_purge,
1441 dpif_linux_init(void)
1443 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1446 if (ovsthread_once_start(&once)) {
1447 unsigned int ovs_vport_mcgroup;
1449 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1450 &ovs_datapath_family);
1452 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1453 "The Open vSwitch kernel module is probably not loaded.",
1454 OVS_DATAPATH_FAMILY);
1457 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1460 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1463 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1464 &ovs_packet_family);
1467 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1469 OVS_VPORT_MCGROUP_FALLBACK_ID);
1472 static struct dpif_linux_vport vport;
1473 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1474 dpif_linux_nln_parse, &vport);
1477 ovsthread_once_done(&once);
1484 dpif_linux_is_internal_device(const char *name)
1486 struct dpif_linux_vport reply;
1490 error = dpif_linux_vport_get(name, &reply, &buf);
1493 } else if (error != ENODEV && error != ENOENT) {
1494 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1495 name, ovs_strerror(error));
1498 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1502 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1504 struct dpif_linux_vport *vport = vport_;
1505 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1509 dpif_linux_port_changed(const void *vport_, void *dpif_)
1511 const struct dpif_linux_vport *vport = vport_;
1512 struct dpif_linux *dpif = dpif_;
1515 if (vport->dp_ifindex == dpif->dp_ifindex
1516 && (vport->cmd == OVS_VPORT_CMD_NEW
1517 || vport->cmd == OVS_VPORT_CMD_DEL
1518 || vport->cmd == OVS_VPORT_CMD_SET)) {
1519 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1520 dpif->dpif.full_name, vport->name, vport->cmd);
1521 sset_add(&dpif->changed_ports, vport->name);
1524 dpif->change_error = true;
1528 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1529 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1530 * positive errno value.
1532 * 'vport' will contain pointers into 'buf', so the caller should not free
1533 * 'buf' while 'vport' is still in use. */
1535 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1536 const struct ofpbuf *buf)
1538 static const struct nl_policy ovs_vport_policy[] = {
1539 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1540 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1541 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1542 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1543 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1545 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1548 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1549 struct ovs_header *ovs_header;
1550 struct nlmsghdr *nlmsg;
1551 struct genlmsghdr *genl;
1554 dpif_linux_vport_init(vport);
1556 ofpbuf_use_const(&b, buf->data, buf->size);
1557 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1558 genl = ofpbuf_try_pull(&b, sizeof *genl);
1559 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1560 if (!nlmsg || !genl || !ovs_header
1561 || nlmsg->nlmsg_type != ovs_vport_family
1562 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1563 ARRAY_SIZE(ovs_vport_policy))) {
1567 vport->cmd = genl->cmd;
1568 vport->dp_ifindex = ovs_header->dp_ifindex;
1569 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1570 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1571 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1572 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1573 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1575 if (a[OVS_VPORT_ATTR_STATS]) {
1576 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1578 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1579 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1580 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1585 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1586 * followed by Netlink attributes corresponding to 'vport'. */
1588 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1591 struct ovs_header *ovs_header;
1593 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1594 vport->cmd, OVS_VPORT_VERSION);
1596 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1597 ovs_header->dp_ifindex = vport->dp_ifindex;
1599 if (vport->port_no != ODPP_NONE) {
1600 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1603 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1604 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1608 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1611 if (vport->upcall_pid) {
1612 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1616 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1617 vport->stats, sizeof *vport->stats);
1620 if (vport->options) {
1621 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1622 vport->options, vport->options_len);
1626 /* Clears 'vport' to "empty" values. */
1628 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1630 memset(vport, 0, sizeof *vport);
1631 vport->port_no = ODPP_NONE;
1634 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1635 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1636 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1637 * result of the command is expected to be an ovs_vport also, which is decoded
1638 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1639 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1641 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1642 struct dpif_linux_vport *reply,
1643 struct ofpbuf **bufp)
1645 struct ofpbuf *request_buf;
1648 ovs_assert((reply != NULL) == (bufp != NULL));
1650 error = dpif_linux_init();
1654 dpif_linux_vport_init(reply);
1659 request_buf = ofpbuf_new(1024);
1660 dpif_linux_vport_to_ofpbuf(request, request_buf);
1661 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1662 ofpbuf_delete(request_buf);
1666 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1669 dpif_linux_vport_init(reply);
1670 ofpbuf_delete(*bufp);
1677 /* Obtains information about the kernel vport named 'name' and stores it into
1678 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1679 * longer needed ('reply' will contain pointers into '*bufp'). */
1681 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1682 struct ofpbuf **bufp)
1684 struct dpif_linux_vport request;
1686 dpif_linux_vport_init(&request);
1687 request.cmd = OVS_VPORT_CMD_GET;
1688 request.name = name;
1690 return dpif_linux_vport_transact(&request, reply, bufp);
1693 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1694 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1695 * positive errno value.
1697 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1698 * while 'dp' is still in use. */
1700 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1702 static const struct nl_policy ovs_datapath_policy[] = {
1703 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1704 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1708 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1709 struct ovs_header *ovs_header;
1710 struct nlmsghdr *nlmsg;
1711 struct genlmsghdr *genl;
1714 dpif_linux_dp_init(dp);
1716 ofpbuf_use_const(&b, buf->data, buf->size);
1717 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1718 genl = ofpbuf_try_pull(&b, sizeof *genl);
1719 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1720 if (!nlmsg || !genl || !ovs_header
1721 || nlmsg->nlmsg_type != ovs_datapath_family
1722 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1723 ARRAY_SIZE(ovs_datapath_policy))) {
1727 dp->cmd = genl->cmd;
1728 dp->dp_ifindex = ovs_header->dp_ifindex;
1729 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1730 if (a[OVS_DP_ATTR_STATS]) {
1731 /* Can't use structure assignment because Netlink doesn't ensure
1732 * sufficient alignment for 64-bit members. */
1733 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1740 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1742 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1744 struct ovs_header *ovs_header;
1746 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1747 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1748 OVS_DATAPATH_VERSION);
1750 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1751 ovs_header->dp_ifindex = dp->dp_ifindex;
1754 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1757 if (dp->upcall_pid) {
1758 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1761 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1764 /* Clears 'dp' to "empty" values. */
1766 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1768 memset(dp, 0, sizeof *dp);
1772 dpif_linux_dp_dump_start(struct nl_dump *dump)
1774 struct dpif_linux_dp request;
1777 dpif_linux_dp_init(&request);
1778 request.cmd = OVS_DP_CMD_GET;
1780 buf = ofpbuf_new(1024);
1781 dpif_linux_dp_to_ofpbuf(&request, buf);
1782 nl_dump_start(dump, NETLINK_GENERIC, buf);
1786 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1787 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1788 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1789 * result of the command is expected to be of the same form, which is decoded
1790 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1791 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1793 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1794 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1796 struct ofpbuf *request_buf;
1799 ovs_assert((reply != NULL) == (bufp != NULL));
1801 request_buf = ofpbuf_new(1024);
1802 dpif_linux_dp_to_ofpbuf(request, request_buf);
1803 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1804 ofpbuf_delete(request_buf);
1808 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1811 dpif_linux_dp_init(reply);
1812 ofpbuf_delete(*bufp);
1819 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1820 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1821 * will contain pointers into '*bufp'). */
1823 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1824 struct ofpbuf **bufp)
1826 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1827 struct dpif_linux_dp request;
1829 dpif_linux_dp_init(&request);
1830 request.cmd = OVS_DP_CMD_GET;
1831 request.dp_ifindex = dpif->dp_ifindex;
1833 return dpif_linux_dp_transact(&request, reply, bufp);
1836 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1837 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1838 * positive errno value.
1840 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1841 * while 'flow' is still in use. */
1843 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1844 const struct ofpbuf *buf)
1846 static const struct nl_policy ovs_flow_policy[] = {
1847 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1848 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
1849 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1850 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1852 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1853 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1854 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1857 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1858 struct ovs_header *ovs_header;
1859 struct nlmsghdr *nlmsg;
1860 struct genlmsghdr *genl;
1863 dpif_linux_flow_init(flow);
1865 ofpbuf_use_const(&b, buf->data, buf->size);
1866 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1867 genl = ofpbuf_try_pull(&b, sizeof *genl);
1868 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1869 if (!nlmsg || !genl || !ovs_header
1870 || nlmsg->nlmsg_type != ovs_flow_family
1871 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1872 ARRAY_SIZE(ovs_flow_policy))) {
1876 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1877 flow->dp_ifindex = ovs_header->dp_ifindex;
1878 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1879 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1881 if (a[OVS_FLOW_ATTR_MASK]) {
1882 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
1883 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
1885 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1886 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1887 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1889 if (a[OVS_FLOW_ATTR_STATS]) {
1890 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1892 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1893 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1895 if (a[OVS_FLOW_ATTR_USED]) {
1896 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1901 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1902 * followed by Netlink attributes corresponding to 'flow'. */
1904 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1907 struct ovs_header *ovs_header;
1909 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1910 NLM_F_REQUEST | flow->nlmsg_flags,
1911 flow->cmd, OVS_FLOW_VERSION);
1913 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1914 ovs_header->dp_ifindex = flow->dp_ifindex;
1916 if (flow->key_len) {
1917 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1920 if (flow->mask_len) {
1921 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
1924 if (flow->actions || flow->actions_len) {
1925 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1926 flow->actions, flow->actions_len);
1929 /* We never need to send these to the kernel. */
1930 ovs_assert(!flow->stats);
1931 ovs_assert(!flow->tcp_flags);
1932 ovs_assert(!flow->used);
1935 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1939 /* Clears 'flow' to "empty" values. */
1941 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1943 memset(flow, 0, sizeof *flow);
1946 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1947 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1948 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1949 * result of the command is expected to be a flow also, which is decoded and
1950 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1951 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1953 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1954 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1956 struct ofpbuf *request_buf;
1959 ovs_assert((reply != NULL) == (bufp != NULL));
1962 request->nlmsg_flags |= NLM_F_ECHO;
1965 request_buf = ofpbuf_new(1024);
1966 dpif_linux_flow_to_ofpbuf(request, request_buf);
1967 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1968 ofpbuf_delete(request_buf);
1972 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1975 dpif_linux_flow_init(reply);
1976 ofpbuf_delete(*bufp);
1984 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1985 struct dpif_flow_stats *stats)
1988 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1989 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1991 stats->n_packets = 0;
1994 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1995 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1998 /* Logs information about a packet that was recently lost in 'ch' (in
2001 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2003 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2004 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2007 if (VLOG_DROP_WARN(&rl)) {
2012 if (ch->last_poll != LLONG_MIN) {
2013 ds_put_format(&s, " (last polled %lld ms ago)",
2014 time_msec() - ch->last_poll);
2017 VLOG_WARN("%s: lost packet on channel %td%s",
2018 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));