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 nl_sock *port_notifier; /* vport multicast group subscriber. */
154 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
156 /* Generic Netlink family numbers for OVS.
158 * Initialized by dpif_linux_init(). */
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 multicast groups for OVS.
166 * Initialized by dpif_linux_init(). */
167 static unsigned int ovs_vport_mcgroup;
169 static int dpif_linux_init(void);
170 static int open_dpif(const struct dpif_linux_dp *, struct 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 error = 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 = NULL;
252 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
253 dp->dp_ifindex, dp->dp_ifindex);
255 dpif->dp_ifindex = dp->dp_ifindex;
256 *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 nl_sock_destroy(dpif->port_notifier);
378 destroy_channels(dpif);
383 dpif_linux_destroy(struct dpif *dpif_)
385 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
386 struct dpif_linux_dp dp;
388 dpif_linux_dp_init(&dp);
389 dp.cmd = OVS_DP_CMD_DEL;
390 dp.dp_ifindex = dpif->dp_ifindex;
391 return dpif_linux_dp_transact(&dp, NULL, NULL);
395 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
397 struct dpif_linux_dp dp;
401 error = dpif_linux_dp_get(dpif_, &dp, &buf);
403 stats->n_hit = dp.stats.n_hit;
404 stats->n_missed = dp.stats.n_missed;
405 stats->n_lost = dp.stats.n_lost;
406 stats->n_flows = dp.stats.n_flows;
413 get_vport_type(const struct dpif_linux_vport *vport)
415 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
417 switch (vport->type) {
418 case OVS_VPORT_TYPE_NETDEV:
421 case OVS_VPORT_TYPE_INTERNAL:
424 case OVS_VPORT_TYPE_GRE:
427 case OVS_VPORT_TYPE_GRE64:
430 case OVS_VPORT_TYPE_VXLAN:
433 case OVS_VPORT_TYPE_LISP:
436 case OVS_VPORT_TYPE_UNSPEC:
437 case __OVS_VPORT_TYPE_MAX:
441 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
442 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
446 static enum ovs_vport_type
447 netdev_to_ovs_vport_type(const struct netdev *netdev)
449 const char *type = netdev_get_type(netdev);
451 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
452 return OVS_VPORT_TYPE_NETDEV;
453 } else if (!strcmp(type, "internal")) {
454 return OVS_VPORT_TYPE_INTERNAL;
455 } else if (strstr(type, "gre64")) {
456 return OVS_VPORT_TYPE_GRE64;
457 } else if (strstr(type, "gre")) {
458 return OVS_VPORT_TYPE_GRE;
459 } else if (!strcmp(type, "vxlan")) {
460 return OVS_VPORT_TYPE_VXLAN;
461 } else if (!strcmp(type, "lisp")) {
462 return OVS_VPORT_TYPE_LISP;
464 return OVS_VPORT_TYPE_UNSPEC;
469 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
470 odp_port_t *port_nop)
472 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
473 const struct netdev_tunnel_config *tnl_cfg;
474 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
475 const char *name = netdev_vport_get_dpif_port(netdev,
476 namebuf, sizeof namebuf);
477 const char *type = netdev_get_type(netdev);
478 struct dpif_linux_vport request, reply;
479 struct nl_sock *sock = NULL;
482 uint64_t options_stub[64 / 8];
483 struct ofpbuf options;
486 if (dpif->epoll_fd >= 0) {
487 error = nl_sock_create(NETLINK_GENERIC, &sock);
493 dpif_linux_vport_init(&request);
494 request.cmd = OVS_VPORT_CMD_NEW;
495 request.dp_ifindex = dpif->dp_ifindex;
496 request.type = netdev_to_ovs_vport_type(netdev);
497 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
498 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
499 "unsupported type `%s'",
500 dpif_name(dpif_), name, type);
501 nl_sock_destroy(sock);
506 if (request.type == OVS_VPORT_TYPE_NETDEV) {
507 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
510 tnl_cfg = netdev_get_tunnel_config(netdev);
511 if (tnl_cfg && tnl_cfg->dst_port != 0) {
512 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
513 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
514 ntohs(tnl_cfg->dst_port));
515 request.options = options.data;
516 request.options_len = options.size;
519 request.port_no = *port_nop;
520 upcall_pid = sock ? nl_sock_pid(sock) : 0;
521 request.upcall_pid = &upcall_pid;
523 error = dpif_linux_vport_transact(&request, &reply, &buf);
525 *port_nop = reply.port_no;
526 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
527 dpif_name(dpif_), reply.port_no, upcall_pid);
529 if (error == EBUSY && *port_nop != ODPP_NONE) {
530 VLOG_INFO("%s: requested port %"PRIu32" is in use",
531 dpif_name(dpif_), *port_nop);
533 nl_sock_destroy(sock);
540 error = add_channel(dpif, *port_nop, sock);
542 VLOG_INFO("%s: could not add channel for port %s",
543 dpif_name(dpif_), name);
545 /* Delete the port. */
546 dpif_linux_vport_init(&request);
547 request.cmd = OVS_VPORT_CMD_DEL;
548 request.dp_ifindex = dpif->dp_ifindex;
549 request.port_no = *port_nop;
550 dpif_linux_vport_transact(&request, NULL, NULL);
552 nl_sock_destroy(sock);
561 dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
563 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
564 struct dpif_linux_vport vport;
567 dpif_linux_vport_init(&vport);
568 vport.cmd = OVS_VPORT_CMD_DEL;
569 vport.dp_ifindex = dpif->dp_ifindex;
570 vport.port_no = port_no;
571 error = dpif_linux_vport_transact(&vport, NULL, NULL);
573 del_channel(dpif, port_no);
579 dpif_linux_port_query__(const struct dpif *dpif, odp_port_t port_no,
580 const char *port_name, struct dpif_port *dpif_port)
582 struct dpif_linux_vport request;
583 struct dpif_linux_vport reply;
587 dpif_linux_vport_init(&request);
588 request.cmd = OVS_VPORT_CMD_GET;
589 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
590 request.port_no = port_no;
591 request.name = port_name;
593 error = dpif_linux_vport_transact(&request, &reply, &buf);
595 if (reply.dp_ifindex != request.dp_ifindex) {
596 /* A query by name reported that 'port_name' is in some datapath
597 * other than 'dpif', but the caller wants to know about 'dpif'. */
599 } else if (dpif_port) {
600 dpif_port->name = xstrdup(reply.name);
601 dpif_port->type = xstrdup(get_vport_type(&reply));
602 dpif_port->port_no = reply.port_no;
610 dpif_linux_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
611 struct dpif_port *dpif_port)
613 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
617 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
618 struct dpif_port *dpif_port)
620 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
624 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
626 return u32_to_odp(MAX_PORTS);
630 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no)
632 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
633 uint32_t port_idx = odp_to_u32(port_no);
635 if (dpif->epoll_fd < 0) {
638 /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
639 * channel, since it is not heavily loaded. */
640 uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
641 return nl_sock_pid(dpif->channels[idx].sock);
646 dpif_linux_flow_flush(struct dpif *dpif_)
648 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
649 struct dpif_linux_flow flow;
651 dpif_linux_flow_init(&flow);
652 flow.cmd = OVS_FLOW_CMD_DEL;
653 flow.dp_ifindex = dpif->dp_ifindex;
654 return dpif_linux_flow_transact(&flow, NULL, NULL);
657 struct dpif_linux_port_state {
662 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
664 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
665 struct dpif_linux_port_state *state;
666 struct dpif_linux_vport request;
669 *statep = state = xmalloc(sizeof *state);
671 dpif_linux_vport_init(&request);
672 request.cmd = OVS_DP_CMD_GET;
673 request.dp_ifindex = dpif->dp_ifindex;
675 buf = ofpbuf_new(1024);
676 dpif_linux_vport_to_ofpbuf(&request, buf);
677 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
684 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
685 struct dpif_port *dpif_port)
687 struct dpif_linux_port_state *state = state_;
688 struct dpif_linux_vport vport;
692 if (!nl_dump_next(&state->dump, &buf)) {
696 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
701 dpif_port->name = CONST_CAST(char *, vport.name);
702 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
703 dpif_port->port_no = vport.port_no;
708 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
710 struct dpif_linux_port_state *state = state_;
711 int error = nl_dump_done(&state->dump);
718 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
720 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
722 /* Lazily create the Netlink socket to listen for notifications. */
723 if (!dpif->port_notifier) {
724 struct nl_sock *sock;
727 error = nl_sock_create(NETLINK_GENERIC, &sock);
732 error = nl_sock_join_mcgroup(sock, ovs_vport_mcgroup);
734 nl_sock_destroy(sock);
737 dpif->port_notifier = sock;
739 /* We have no idea of the current state so report that everything
745 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
746 uint64_t buf_stub[4096 / 8];
750 ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub);
751 error = nl_sock_recv(dpif->port_notifier, &buf, false);
753 struct dpif_linux_vport vport;
755 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
757 if (vport.dp_ifindex == dpif->dp_ifindex
758 && (vport.cmd == OVS_VPORT_CMD_NEW
759 || vport.cmd == OVS_VPORT_CMD_DEL
760 || vport.cmd == OVS_VPORT_CMD_SET)) {
761 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
762 dpif->dpif.full_name, vport.name, vport.cmd);
763 *devnamep = xstrdup(vport.name);
769 } else if (error == EAGAIN) {
773 VLOG_WARN_RL(&rl, "error reading or parsing netlink (%s)",
774 ovs_strerror(error));
775 nl_sock_drain(dpif->port_notifier);
781 dpif_linux_port_poll_wait(const struct dpif *dpif_)
783 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
785 if (dpif->port_notifier) {
786 nl_sock_wait(dpif->port_notifier, POLLIN);
788 poll_immediate_wake();
793 dpif_linux_flow_get__(const struct dpif *dpif_,
794 const struct nlattr *key, size_t key_len,
795 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
797 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
798 struct dpif_linux_flow request;
800 dpif_linux_flow_init(&request);
801 request.cmd = OVS_FLOW_CMD_GET;
802 request.dp_ifindex = dpif->dp_ifindex;
804 request.key_len = key_len;
805 return dpif_linux_flow_transact(&request, reply, bufp);
809 dpif_linux_flow_get(const struct dpif *dpif_,
810 const struct nlattr *key, size_t key_len,
811 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
813 struct dpif_linux_flow reply;
817 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
820 dpif_linux_flow_get_stats(&reply, stats);
823 buf->data = CONST_CAST(struct nlattr *, reply.actions);
824 buf->size = reply.actions_len;
834 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
835 struct dpif_linux_flow *request)
837 static const struct nlattr dummy_action;
839 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
841 dpif_linux_flow_init(request);
842 request->cmd = (put->flags & DPIF_FP_CREATE
843 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
844 request->dp_ifindex = dpif->dp_ifindex;
845 request->key = put->key;
846 request->key_len = put->key_len;
847 request->mask = put->mask;
848 request->mask_len = put->mask_len;
849 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
850 request->actions = (put->actions
852 : CONST_CAST(struct nlattr *, &dummy_action));
853 request->actions_len = put->actions_len;
854 if (put->flags & DPIF_FP_ZERO_STATS) {
855 request->clear = true;
857 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
861 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
863 struct dpif_linux_flow request, reply;
867 dpif_linux_init_flow_put(dpif_, put, &request);
868 error = dpif_linux_flow_transact(&request,
869 put->stats ? &reply : NULL,
870 put->stats ? &buf : NULL);
871 if (!error && put->stats) {
872 dpif_linux_flow_get_stats(&reply, put->stats);
879 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
880 struct dpif_linux_flow *request)
882 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
884 dpif_linux_flow_init(request);
885 request->cmd = OVS_FLOW_CMD_DEL;
886 request->dp_ifindex = dpif->dp_ifindex;
887 request->key = del->key;
888 request->key_len = del->key_len;
892 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
894 struct dpif_linux_flow request, reply;
898 dpif_linux_init_flow_del(dpif_, del, &request);
899 error = dpif_linux_flow_transact(&request,
900 del->stats ? &reply : NULL,
901 del->stats ? &buf : NULL);
902 if (!error && del->stats) {
903 dpif_linux_flow_get_stats(&reply, del->stats);
909 struct dpif_linux_flow_state {
911 struct dpif_linux_flow flow;
912 struct dpif_flow_stats stats;
917 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
919 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
920 struct dpif_linux_flow_state *state;
921 struct dpif_linux_flow request;
924 *statep = state = xmalloc(sizeof *state);
926 dpif_linux_flow_init(&request);
927 request.cmd = OVS_DP_CMD_GET;
928 request.dp_ifindex = dpif->dp_ifindex;
930 buf = ofpbuf_new(1024);
931 dpif_linux_flow_to_ofpbuf(&request, buf);
932 nl_dump_start(&state->dump, NETLINK_GENERIC, buf);
941 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
942 const struct nlattr **key, size_t *key_len,
943 const struct nlattr **mask, size_t *mask_len,
944 const struct nlattr **actions, size_t *actions_len,
945 const struct dpif_flow_stats **stats)
947 struct dpif_linux_flow_state *state = state_;
952 ofpbuf_delete(state->buf);
955 if (!nl_dump_next(&state->dump, &buf)) {
959 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
964 if (actions && !state->flow.actions) {
965 error = dpif_linux_flow_get__(dpif_, state->flow.key,
967 &state->flow, &state->buf);
968 if (error == ENOENT) {
969 VLOG_DBG("dumped flow disappeared on get");
971 VLOG_WARN("error fetching dumped flow: %s",
972 ovs_strerror(error));
978 *actions = state->flow.actions;
979 *actions_len = state->flow.actions_len;
982 *key = state->flow.key;
983 *key_len = state->flow.key_len;
986 *mask = state->flow.mask;
987 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
990 dpif_linux_flow_get_stats(&state->flow, &state->stats);
991 *stats = &state->stats;
997 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
999 struct dpif_linux_flow_state *state = state_;
1000 int error = nl_dump_done(&state->dump);
1001 ofpbuf_delete(state->buf);
1007 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
1010 struct ovs_header *k_exec;
1012 ofpbuf_prealloc_tailroom(buf, (64
1013 + d_exec->packet->size
1015 + d_exec->actions_len));
1017 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
1018 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
1020 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
1021 k_exec->dp_ifindex = dp_ifindex;
1023 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
1024 d_exec->packet->data, d_exec->packet->size);
1025 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
1026 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
1027 d_exec->actions, d_exec->actions_len);
1031 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
1033 uint64_t request_stub[1024 / 8];
1034 struct ofpbuf request;
1037 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1038 dpif_linux_encode_execute(dp_ifindex, execute, &request);
1039 error = nl_transact(NETLINK_GENERIC, &request, NULL);
1040 ofpbuf_uninit(&request);
1046 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1048 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1050 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1056 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1058 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1061 struct nl_transaction txn;
1063 struct ofpbuf request;
1064 uint64_t request_stub[1024 / 8];
1066 struct ofpbuf reply;
1067 uint64_t reply_stub[1024 / 8];
1070 struct nl_transaction *txnsp[MAX_OPS];
1073 ovs_assert(n_ops <= MAX_OPS);
1074 for (i = 0; i < n_ops; i++) {
1075 struct op_auxdata *aux = &auxes[i];
1076 struct dpif_op *op = ops[i];
1077 struct dpif_flow_put *put;
1078 struct dpif_flow_del *del;
1079 struct dpif_execute *execute;
1080 struct dpif_linux_flow flow;
1082 ofpbuf_use_stub(&aux->request,
1083 aux->request_stub, sizeof aux->request_stub);
1084 aux->txn.request = &aux->request;
1086 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1087 aux->txn.reply = NULL;
1090 case DPIF_OP_FLOW_PUT:
1091 put = &op->u.flow_put;
1092 dpif_linux_init_flow_put(dpif_, put, &flow);
1094 flow.nlmsg_flags |= NLM_F_ECHO;
1095 aux->txn.reply = &aux->reply;
1097 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1100 case DPIF_OP_FLOW_DEL:
1101 del = &op->u.flow_del;
1102 dpif_linux_init_flow_del(dpif_, del, &flow);
1104 flow.nlmsg_flags |= NLM_F_ECHO;
1105 aux->txn.reply = &aux->reply;
1107 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1110 case DPIF_OP_EXECUTE:
1111 execute = &op->u.execute;
1112 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1121 for (i = 0; i < n_ops; i++) {
1122 txnsp[i] = &auxes[i].txn;
1124 nl_transact_multiple(NETLINK_GENERIC, txnsp, n_ops);
1126 for (i = 0; i < n_ops; i++) {
1127 struct op_auxdata *aux = &auxes[i];
1128 struct nl_transaction *txn = &auxes[i].txn;
1129 struct dpif_op *op = ops[i];
1130 struct dpif_flow_put *put;
1131 struct dpif_flow_del *del;
1133 op->error = txn->error;
1136 case DPIF_OP_FLOW_PUT:
1137 put = &op->u.flow_put;
1140 struct dpif_linux_flow reply;
1142 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1145 dpif_linux_flow_get_stats(&reply, put->stats);
1150 memset(put->stats, 0, sizeof *put->stats);
1155 case DPIF_OP_FLOW_DEL:
1156 del = &op->u.flow_del;
1159 struct dpif_linux_flow reply;
1161 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1164 dpif_linux_flow_get_stats(&reply, del->stats);
1169 memset(del->stats, 0, sizeof *del->stats);
1174 case DPIF_OP_EXECUTE:
1181 ofpbuf_uninit(&aux->request);
1182 ofpbuf_uninit(&aux->reply);
1187 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1190 size_t chunk = MIN(n_ops, MAX_OPS);
1191 dpif_linux_operate__(dpif, ops, chunk);
1198 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1200 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1202 if ((dpif->epoll_fd >= 0) == enable) {
1207 destroy_channels(dpif);
1209 struct dpif_port_dump port_dump;
1210 struct dpif_port port;
1212 dpif->epoll_fd = epoll_create(10);
1213 if (dpif->epoll_fd < 0) {
1217 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1218 struct dpif_linux_vport vport_request;
1219 struct nl_sock *sock;
1220 uint32_t upcall_pid;
1223 error = nl_sock_create(NETLINK_GENERIC, &sock);
1228 upcall_pid = nl_sock_pid(sock);
1230 dpif_linux_vport_init(&vport_request);
1231 vport_request.cmd = OVS_VPORT_CMD_SET;
1232 vport_request.dp_ifindex = dpif->dp_ifindex;
1233 vport_request.port_no = port.port_no;
1234 vport_request.upcall_pid = &upcall_pid;
1235 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1237 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1238 dpif_name(&dpif->dpif), vport_request.port_no,
1241 VLOG_WARN_RL(&error_rl,
1242 "%s: failed to set upcall pid on port: %s",
1243 dpif_name(&dpif->dpif), ovs_strerror(error));
1244 nl_sock_destroy(sock);
1246 if (error == ENODEV || error == ENOENT) {
1247 /* This device isn't there, but keep trying the others. */
1254 error = add_channel(dpif, port.port_no, sock);
1256 VLOG_INFO("%s: could not add channel for port %s",
1257 dpif_name(dpif_), port.name);
1258 nl_sock_destroy(sock);
1268 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1269 uint32_t queue_id, uint32_t *priority)
1271 if (queue_id < 0xf000) {
1272 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1280 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1283 static const struct nl_policy ovs_packet_policy[] = {
1284 /* Always present. */
1285 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1286 .min_len = ETH_HEADER_LEN },
1287 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1289 /* OVS_PACKET_CMD_ACTION only. */
1290 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1293 struct ovs_header *ovs_header;
1294 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1295 struct nlmsghdr *nlmsg;
1296 struct genlmsghdr *genl;
1300 ofpbuf_use_const(&b, buf->data, buf->size);
1302 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1303 genl = ofpbuf_try_pull(&b, sizeof *genl);
1304 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1305 if (!nlmsg || !genl || !ovs_header
1306 || nlmsg->nlmsg_type != ovs_packet_family
1307 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1308 ARRAY_SIZE(ovs_packet_policy))) {
1312 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1313 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1319 memset(upcall, 0, sizeof *upcall);
1320 upcall->type = type;
1321 upcall->packet = buf;
1322 upcall->packet->data = CONST_CAST(struct nlattr *,
1323 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1324 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1325 upcall->key = CONST_CAST(struct nlattr *,
1326 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1327 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1328 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1329 *dp_ifindex = ovs_header->dp_ifindex;
1335 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1338 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1341 if (dpif->epoll_fd < 0) {
1345 if (dpif->event_offset >= dpif->n_events) {
1348 dpif->event_offset = dpif->n_events = 0;
1351 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1352 dpif->uc_array_size, 0);
1353 } while (retval < 0 && errno == EINTR);
1355 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1356 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", ovs_strerror(errno));
1357 } else if (retval > 0) {
1358 dpif->n_events = retval;
1362 while (dpif->event_offset < dpif->n_events) {
1363 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1364 struct dpif_channel *ch = &dpif->channels[idx];
1366 dpif->event_offset++;
1372 if (++read_tries > 50) {
1376 error = nl_sock_recv(ch->sock, buf, false);
1377 if (error == ENOBUFS) {
1378 /* ENOBUFS typically means that we've received so many
1379 * packets that the buffer overflowed. Try again
1380 * immediately because there's almost certainly a packet
1381 * waiting for us. */
1382 report_loss(dpif_, ch);
1386 ch->last_poll = time_msec();
1388 if (error == EAGAIN) {
1394 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1395 if (!error && dp_ifindex == dpif->dp_ifindex) {
1407 dpif_linux_recv_wait(struct dpif *dpif_)
1409 const struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1411 if (dpif->epoll_fd < 0) {
1415 poll_fd_wait(dpif->epoll_fd, POLLIN);
1419 dpif_linux_recv_purge(struct dpif *dpif_)
1421 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1422 struct dpif_channel *ch;
1424 if (dpif->epoll_fd < 0) {
1428 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1430 nl_sock_drain(ch->sock);
1435 const struct dpif_class dpif_linux_class = {
1437 dpif_linux_enumerate,
1444 dpif_linux_get_stats,
1445 dpif_linux_port_add,
1446 dpif_linux_port_del,
1447 dpif_linux_port_query_by_number,
1448 dpif_linux_port_query_by_name,
1449 dpif_linux_get_max_ports,
1450 dpif_linux_port_get_pid,
1451 dpif_linux_port_dump_start,
1452 dpif_linux_port_dump_next,
1453 dpif_linux_port_dump_done,
1454 dpif_linux_port_poll,
1455 dpif_linux_port_poll_wait,
1456 dpif_linux_flow_get,
1457 dpif_linux_flow_put,
1458 dpif_linux_flow_del,
1459 dpif_linux_flow_flush,
1460 dpif_linux_flow_dump_start,
1461 dpif_linux_flow_dump_next,
1462 dpif_linux_flow_dump_done,
1465 dpif_linux_recv_set,
1466 dpif_linux_queue_to_priority,
1468 dpif_linux_recv_wait,
1469 dpif_linux_recv_purge,
1473 dpif_linux_init(void)
1475 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1478 if (ovsthread_once_start(&once)) {
1479 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1480 &ovs_datapath_family);
1482 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1483 "The Open vSwitch kernel module is probably not loaded.",
1484 OVS_DATAPATH_FAMILY);
1487 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1490 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1493 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1494 &ovs_packet_family);
1497 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1499 OVS_VPORT_MCGROUP_FALLBACK_ID);
1502 ovsthread_once_done(&once);
1509 dpif_linux_is_internal_device(const char *name)
1511 struct dpif_linux_vport reply;
1515 error = dpif_linux_vport_get(name, &reply, &buf);
1518 } else if (error != ENODEV && error != ENOENT) {
1519 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1520 name, ovs_strerror(error));
1523 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1526 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1527 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1528 * positive errno value.
1530 * 'vport' will contain pointers into 'buf', so the caller should not free
1531 * 'buf' while 'vport' is still in use. */
1533 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1534 const struct ofpbuf *buf)
1536 static const struct nl_policy ovs_vport_policy[] = {
1537 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1538 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1539 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1540 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1541 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1543 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1546 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1547 struct ovs_header *ovs_header;
1548 struct nlmsghdr *nlmsg;
1549 struct genlmsghdr *genl;
1552 dpif_linux_vport_init(vport);
1554 ofpbuf_use_const(&b, buf->data, buf->size);
1555 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1556 genl = ofpbuf_try_pull(&b, sizeof *genl);
1557 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1558 if (!nlmsg || !genl || !ovs_header
1559 || nlmsg->nlmsg_type != ovs_vport_family
1560 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1561 ARRAY_SIZE(ovs_vport_policy))) {
1565 vport->cmd = genl->cmd;
1566 vport->dp_ifindex = ovs_header->dp_ifindex;
1567 vport->port_no = nl_attr_get_odp_port(a[OVS_VPORT_ATTR_PORT_NO]);
1568 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1569 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1570 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1571 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1573 if (a[OVS_VPORT_ATTR_STATS]) {
1574 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1576 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1577 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1578 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1583 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1584 * followed by Netlink attributes corresponding to 'vport'. */
1586 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1589 struct ovs_header *ovs_header;
1591 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1592 vport->cmd, OVS_VPORT_VERSION);
1594 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1595 ovs_header->dp_ifindex = vport->dp_ifindex;
1597 if (vport->port_no != ODPP_NONE) {
1598 nl_msg_put_odp_port(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1601 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1602 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1606 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1609 if (vport->upcall_pid) {
1610 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1614 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1615 vport->stats, sizeof *vport->stats);
1618 if (vport->options) {
1619 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1620 vport->options, vport->options_len);
1624 /* Clears 'vport' to "empty" values. */
1626 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1628 memset(vport, 0, sizeof *vport);
1629 vport->port_no = ODPP_NONE;
1632 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1633 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1634 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1635 * result of the command is expected to be an ovs_vport also, which is decoded
1636 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1637 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1639 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1640 struct dpif_linux_vport *reply,
1641 struct ofpbuf **bufp)
1643 struct ofpbuf *request_buf;
1646 ovs_assert((reply != NULL) == (bufp != NULL));
1648 error = dpif_linux_init();
1652 dpif_linux_vport_init(reply);
1657 request_buf = ofpbuf_new(1024);
1658 dpif_linux_vport_to_ofpbuf(request, request_buf);
1659 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1660 ofpbuf_delete(request_buf);
1664 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1667 dpif_linux_vport_init(reply);
1668 ofpbuf_delete(*bufp);
1675 /* Obtains information about the kernel vport named 'name' and stores it into
1676 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1677 * longer needed ('reply' will contain pointers into '*bufp'). */
1679 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1680 struct ofpbuf **bufp)
1682 struct dpif_linux_vport request;
1684 dpif_linux_vport_init(&request);
1685 request.cmd = OVS_VPORT_CMD_GET;
1686 request.name = name;
1688 return dpif_linux_vport_transact(&request, reply, bufp);
1691 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1692 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1693 * positive errno value.
1695 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1696 * while 'dp' is still in use. */
1698 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1700 static const struct nl_policy ovs_datapath_policy[] = {
1701 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1702 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1706 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1707 struct ovs_header *ovs_header;
1708 struct nlmsghdr *nlmsg;
1709 struct genlmsghdr *genl;
1712 dpif_linux_dp_init(dp);
1714 ofpbuf_use_const(&b, buf->data, buf->size);
1715 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1716 genl = ofpbuf_try_pull(&b, sizeof *genl);
1717 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1718 if (!nlmsg || !genl || !ovs_header
1719 || nlmsg->nlmsg_type != ovs_datapath_family
1720 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1721 ARRAY_SIZE(ovs_datapath_policy))) {
1725 dp->cmd = genl->cmd;
1726 dp->dp_ifindex = ovs_header->dp_ifindex;
1727 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1728 if (a[OVS_DP_ATTR_STATS]) {
1729 /* Can't use structure assignment because Netlink doesn't ensure
1730 * sufficient alignment for 64-bit members. */
1731 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1738 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1740 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1742 struct ovs_header *ovs_header;
1744 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1745 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1746 OVS_DATAPATH_VERSION);
1748 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1749 ovs_header->dp_ifindex = dp->dp_ifindex;
1752 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1755 if (dp->upcall_pid) {
1756 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1759 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1762 /* Clears 'dp' to "empty" values. */
1764 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1766 memset(dp, 0, sizeof *dp);
1770 dpif_linux_dp_dump_start(struct nl_dump *dump)
1772 struct dpif_linux_dp request;
1775 dpif_linux_dp_init(&request);
1776 request.cmd = OVS_DP_CMD_GET;
1778 buf = ofpbuf_new(1024);
1779 dpif_linux_dp_to_ofpbuf(&request, buf);
1780 nl_dump_start(dump, NETLINK_GENERIC, buf);
1784 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1785 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1786 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1787 * result of the command is expected to be of the same form, which is decoded
1788 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1789 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1791 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1792 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1794 struct ofpbuf *request_buf;
1797 ovs_assert((reply != NULL) == (bufp != NULL));
1799 request_buf = ofpbuf_new(1024);
1800 dpif_linux_dp_to_ofpbuf(request, request_buf);
1801 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1802 ofpbuf_delete(request_buf);
1806 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1809 dpif_linux_dp_init(reply);
1810 ofpbuf_delete(*bufp);
1817 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1818 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1819 * will contain pointers into '*bufp'). */
1821 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1822 struct ofpbuf **bufp)
1824 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1825 struct dpif_linux_dp request;
1827 dpif_linux_dp_init(&request);
1828 request.cmd = OVS_DP_CMD_GET;
1829 request.dp_ifindex = dpif->dp_ifindex;
1831 return dpif_linux_dp_transact(&request, reply, bufp);
1834 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1835 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1836 * positive errno value.
1838 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1839 * while 'flow' is still in use. */
1841 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1842 const struct ofpbuf *buf)
1844 static const struct nl_policy ovs_flow_policy[] = {
1845 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1846 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
1847 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1848 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1850 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1851 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1852 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1855 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1856 struct ovs_header *ovs_header;
1857 struct nlmsghdr *nlmsg;
1858 struct genlmsghdr *genl;
1861 dpif_linux_flow_init(flow);
1863 ofpbuf_use_const(&b, buf->data, buf->size);
1864 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1865 genl = ofpbuf_try_pull(&b, sizeof *genl);
1866 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1867 if (!nlmsg || !genl || !ovs_header
1868 || nlmsg->nlmsg_type != ovs_flow_family
1869 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1870 ARRAY_SIZE(ovs_flow_policy))) {
1874 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1875 flow->dp_ifindex = ovs_header->dp_ifindex;
1876 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1877 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1879 if (a[OVS_FLOW_ATTR_MASK]) {
1880 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
1881 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
1883 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1884 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1885 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1887 if (a[OVS_FLOW_ATTR_STATS]) {
1888 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1890 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1891 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1893 if (a[OVS_FLOW_ATTR_USED]) {
1894 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1899 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1900 * followed by Netlink attributes corresponding to 'flow'. */
1902 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1905 struct ovs_header *ovs_header;
1907 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1908 NLM_F_REQUEST | flow->nlmsg_flags,
1909 flow->cmd, OVS_FLOW_VERSION);
1911 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1912 ovs_header->dp_ifindex = flow->dp_ifindex;
1914 if (flow->key_len) {
1915 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1918 if (flow->mask_len) {
1919 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
1922 if (flow->actions || flow->actions_len) {
1923 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1924 flow->actions, flow->actions_len);
1927 /* We never need to send these to the kernel. */
1928 ovs_assert(!flow->stats);
1929 ovs_assert(!flow->tcp_flags);
1930 ovs_assert(!flow->used);
1933 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1937 /* Clears 'flow' to "empty" values. */
1939 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1941 memset(flow, 0, sizeof *flow);
1944 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1945 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1946 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1947 * result of the command is expected to be a flow also, which is decoded and
1948 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1949 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1951 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1952 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1954 struct ofpbuf *request_buf;
1957 ovs_assert((reply != NULL) == (bufp != NULL));
1960 request->nlmsg_flags |= NLM_F_ECHO;
1963 request_buf = ofpbuf_new(1024);
1964 dpif_linux_flow_to_ofpbuf(request, request_buf);
1965 error = nl_transact(NETLINK_GENERIC, request_buf, bufp);
1966 ofpbuf_delete(request_buf);
1970 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1973 dpif_linux_flow_init(reply);
1974 ofpbuf_delete(*bufp);
1982 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1983 struct dpif_flow_stats *stats)
1986 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1987 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1989 stats->n_packets = 0;
1992 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1993 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1996 /* Logs information about a packet that was recently lost in 'ch' (in
1999 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
2001 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
2002 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
2005 if (VLOG_DROP_WARN(&rl)) {
2010 if (ch->last_poll != LLONG_MIN) {
2011 ds_put_format(&s, " (last polled %lld ms ago)",
2012 time_msec() - ch->last_poll);
2015 VLOG_WARN("%s: lost packet on channel %td%s",
2016 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));