2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "dpif-linux.h"
26 #include <linux/types.h>
27 #include <linux/pkt_sched.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/sockios.h>
33 #include <sys/epoll.h>
38 #include "dpif-provider.h"
39 #include "dynamic-string.h"
42 #include "netdev-linux.h"
43 #include "netdev-vport.h"
44 #include "netlink-notifier.h"
45 #include "netlink-socket.h"
49 #include "openvswitch/datapath-compat.h"
51 #include "poll-loop.h"
56 #include "unaligned.h"
60 VLOG_DEFINE_THIS_MODULE(dpif_linux);
61 enum { MAX_PORTS = USHRT_MAX };
63 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
64 * missing if we have old headers. */
65 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
67 struct dpif_linux_dp {
68 /* Generic Netlink header. */
71 /* struct ovs_header. */
75 const char *name; /* OVS_DP_ATTR_NAME. */
76 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
77 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
80 static void dpif_linux_dp_init(struct dpif_linux_dp *);
81 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
82 const struct ofpbuf *);
83 static void dpif_linux_dp_dump_start(struct nl_dump *);
84 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
85 struct dpif_linux_dp *reply,
86 struct ofpbuf **bufp);
87 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
88 struct ofpbuf **bufp);
90 struct dpif_linux_flow {
91 /* Generic Netlink header. */
94 /* struct ovs_header. */
95 unsigned int nlmsg_flags;
100 * The 'stats' member points to 64-bit data that might only be aligned on
101 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
104 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
105 * the Netlink version of the command, even if actions_len is zero. */
106 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
108 const struct nlattr *mask; /* OVS_FLOW_ATTR_MASK. */
110 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
112 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
113 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
114 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
115 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
118 static void dpif_linux_flow_init(struct dpif_linux_flow *);
119 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
120 const struct ofpbuf *);
121 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
123 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
124 struct dpif_linux_flow *reply,
125 struct ofpbuf **bufp);
126 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
127 struct dpif_flow_stats *);
129 /* One of the dpif channels between the kernel and userspace. */
130 struct dpif_channel {
131 struct nl_sock *sock; /* Netlink socket. */
132 long long int last_poll; /* Last time this channel was polled. */
135 static void report_loss(struct dpif *, struct dpif_channel *);
137 /* Datapath interface for the openvswitch Linux kernel module. */
142 /* Upcall messages. */
143 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
144 struct dpif_channel *channels;
145 struct epoll_event *epoll_events;
146 int epoll_fd; /* epoll fd that includes channel socks. */
147 int n_events; /* Num events returned by epoll_wait(). */
148 int event_offset; /* Offset into 'epoll_events'. */
150 /* Change notification. */
151 struct sset changed_ports; /* Ports that have changed. */
152 struct nln_notifier *port_notifier;
156 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
158 /* Generic Netlink family numbers for OVS. */
159 static int ovs_datapath_family;
160 static int ovs_vport_family;
161 static int ovs_flow_family;
162 static int ovs_packet_family;
164 /* Generic Netlink socket. */
165 static struct nl_sock *genl_sock;
166 static struct nln *nln = NULL;
168 static int dpif_linux_init(void);
169 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
170 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
171 static void dpif_linux_port_changed(const void *vport, void *dpif);
172 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint32_t port_no);
174 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
176 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
177 const struct ofpbuf *);
179 static struct dpif_linux *
180 dpif_linux_cast(const struct dpif *dpif)
182 dpif_assert_class(dpif, &dpif_linux_class);
183 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
187 dpif_linux_enumerate(struct sset *all_dps)
193 error = dpif_linux_init();
198 dpif_linux_dp_dump_start(&dump);
199 while (nl_dump_next(&dump, &msg)) {
200 struct dpif_linux_dp dp;
202 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
203 sset_add(all_dps, dp.name);
206 return nl_dump_done(&dump);
210 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
211 bool create, struct dpif **dpifp)
213 struct dpif_linux_dp dp_request, dp;
218 error = dpif_linux_init();
223 /* Create or look up datapath. */
224 dpif_linux_dp_init(&dp_request);
226 dp_request.cmd = OVS_DP_CMD_NEW;
228 dp_request.upcall_pid = &upcall_pid;
230 dp_request.cmd = OVS_DP_CMD_GET;
232 dp_request.name = name;
233 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
238 open_dpif(&dp, dpifp);
244 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
246 struct dpif_linux *dpif;
248 dpif = xzalloc(sizeof *dpif);
249 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
253 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
254 dp->dp_ifindex, dp->dp_ifindex);
256 dpif->dp_ifindex = dp->dp_ifindex;
257 sset_init(&dpif->changed_ports);
258 *dpifp = &dpif->dpif;
262 destroy_channels(struct dpif_linux *dpif)
266 if (dpif->epoll_fd < 0) {
270 for (i = 0; i < dpif->uc_array_size; i++ ) {
271 struct dpif_linux_vport vport_request;
272 struct dpif_channel *ch = &dpif->channels[i];
273 uint32_t upcall_pid = 0;
279 /* Turn off upcalls. */
280 dpif_linux_vport_init(&vport_request);
281 vport_request.cmd = OVS_VPORT_CMD_SET;
282 vport_request.dp_ifindex = dpif->dp_ifindex;
283 vport_request.port_no = i;
284 vport_request.upcall_pid = &upcall_pid;
285 dpif_linux_vport_transact(&vport_request, NULL, NULL);
287 nl_sock_destroy(ch->sock);
290 free(dpif->channels);
291 dpif->channels = NULL;
292 dpif->uc_array_size = 0;
294 free(dpif->epoll_events);
295 dpif->epoll_events = NULL;
296 dpif->n_events = dpif->event_offset = 0;
298 close(dpif->epoll_fd);
303 add_channel(struct dpif_linux *dpif, uint32_t port_no, struct nl_sock *sock)
305 struct epoll_event event;
307 if (dpif->epoll_fd < 0) {
311 /* We assume that the datapath densely chooses port numbers, which
312 * can therefore be used as an index into an array of channels. */
313 if (port_no >= dpif->uc_array_size) {
314 int new_size = port_no + 1;
317 if (new_size > 65535) {
318 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
319 dpif_name(&dpif->dpif), port_no);
323 dpif->channels = xrealloc(dpif->channels,
324 new_size * sizeof *dpif->channels);
325 for (i = dpif->uc_array_size; i < new_size; i++) {
326 dpif->channels[i].sock = NULL;
329 dpif->epoll_events = xrealloc(dpif->epoll_events,
330 new_size * sizeof *dpif->epoll_events);
331 dpif->uc_array_size = new_size;
334 memset(&event, 0, sizeof event);
335 event.events = EPOLLIN;
336 event.data.u32 = port_no;
337 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
342 nl_sock_destroy(dpif->channels[port_no].sock);
343 dpif->channels[port_no].sock = sock;
344 dpif->channels[port_no].last_poll = LLONG_MIN;
350 del_channel(struct dpif_linux *dpif, uint32_t port_no)
352 struct dpif_channel *ch;
354 if (dpif->epoll_fd < 0 || port_no >= dpif->uc_array_size) {
358 ch = &dpif->channels[port_no];
363 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
364 dpif->event_offset = dpif->n_events = 0;
366 nl_sock_destroy(ch->sock);
371 dpif_linux_close(struct dpif *dpif_)
373 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
375 nln_notifier_destroy(dpif->port_notifier);
376 destroy_channels(dpif);
377 sset_destroy(&dpif->changed_ports);
382 dpif_linux_destroy(struct dpif *dpif_)
384 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
385 struct dpif_linux_dp dp;
387 dpif_linux_dp_init(&dp);
388 dp.cmd = OVS_DP_CMD_DEL;
389 dp.dp_ifindex = dpif->dp_ifindex;
390 return dpif_linux_dp_transact(&dp, NULL, NULL);
394 dpif_linux_run(struct dpif *dpif_ OVS_UNUSED)
402 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
410 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
412 struct dpif_linux_dp dp;
416 error = dpif_linux_dp_get(dpif_, &dp, &buf);
418 stats->n_hit = dp.stats.n_hit;
419 stats->n_missed = dp.stats.n_missed;
420 stats->n_lost = dp.stats.n_lost;
421 stats->n_flows = dp.stats.n_flows;
428 get_vport_type(const struct dpif_linux_vport *vport)
430 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
432 switch (vport->type) {
433 case OVS_VPORT_TYPE_NETDEV:
436 case OVS_VPORT_TYPE_INTERNAL:
439 case OVS_VPORT_TYPE_GRE:
442 case OVS_VPORT_TYPE_GRE64:
445 case OVS_VPORT_TYPE_VXLAN:
448 case OVS_VPORT_TYPE_LISP:
451 case OVS_VPORT_TYPE_UNSPEC:
452 case __OVS_VPORT_TYPE_MAX:
456 VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
457 vport->dp_ifindex, vport->name, (unsigned int) vport->type);
461 static enum ovs_vport_type
462 netdev_to_ovs_vport_type(const struct netdev *netdev)
464 const char *type = netdev_get_type(netdev);
466 if (!strcmp(type, "tap") || !strcmp(type, "system")) {
467 return OVS_VPORT_TYPE_NETDEV;
468 } else if (!strcmp(type, "internal")) {
469 return OVS_VPORT_TYPE_INTERNAL;
470 } else if (strstr(type, "gre64")) {
471 return OVS_VPORT_TYPE_GRE64;
472 } else if (strstr(type, "gre")) {
473 return OVS_VPORT_TYPE_GRE;
474 } else if (!strcmp(type, "vxlan")) {
475 return OVS_VPORT_TYPE_VXLAN;
476 } else if (!strcmp(type, "lisp")) {
477 return OVS_VPORT_TYPE_LISP;
479 return OVS_VPORT_TYPE_UNSPEC;
484 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
487 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
488 const struct netdev_tunnel_config *tnl_cfg;
489 const char *name = netdev_vport_get_dpif_port(netdev);
490 const char *type = netdev_get_type(netdev);
491 struct dpif_linux_vport request, reply;
492 struct nl_sock *sock = NULL;
495 uint64_t options_stub[64 / 8];
496 struct ofpbuf options;
499 if (dpif->epoll_fd >= 0) {
500 error = nl_sock_create(NETLINK_GENERIC, &sock);
506 dpif_linux_vport_init(&request);
507 request.cmd = OVS_VPORT_CMD_NEW;
508 request.dp_ifindex = dpif->dp_ifindex;
509 request.type = netdev_to_ovs_vport_type(netdev);
510 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
511 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
512 "unsupported type `%s'",
513 dpif_name(dpif_), name, type);
514 nl_sock_destroy(sock);
519 if (request.type == OVS_VPORT_TYPE_NETDEV) {
520 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
523 tnl_cfg = netdev_get_tunnel_config(netdev);
524 if (tnl_cfg && tnl_cfg->dst_port != 0) {
525 ofpbuf_use_stack(&options, options_stub, sizeof options_stub);
526 nl_msg_put_u16(&options, OVS_TUNNEL_ATTR_DST_PORT,
527 ntohs(tnl_cfg->dst_port));
528 request.options = options.data;
529 request.options_len = options.size;
532 request.port_no = *port_nop;
533 upcall_pid = sock ? nl_sock_pid(sock) : 0;
534 request.upcall_pid = &upcall_pid;
536 error = dpif_linux_vport_transact(&request, &reply, &buf);
538 *port_nop = reply.port_no;
539 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
540 dpif_name(dpif_), reply.port_no, upcall_pid);
542 if (error == EBUSY && *port_nop != UINT32_MAX) {
543 VLOG_INFO("%s: requested port %"PRIu32" is in use",
544 dpif_name(dpif_), *port_nop);
546 nl_sock_destroy(sock);
553 error = add_channel(dpif, *port_nop, sock);
555 VLOG_INFO("%s: could not add channel for port %s",
556 dpif_name(dpif_), name);
558 /* Delete the port. */
559 dpif_linux_vport_init(&request);
560 request.cmd = OVS_VPORT_CMD_DEL;
561 request.dp_ifindex = dpif->dp_ifindex;
562 request.port_no = *port_nop;
563 dpif_linux_vport_transact(&request, NULL, NULL);
565 nl_sock_destroy(sock);
574 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
576 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
577 struct dpif_linux_vport vport;
580 dpif_linux_vport_init(&vport);
581 vport.cmd = OVS_VPORT_CMD_DEL;
582 vport.dp_ifindex = dpif->dp_ifindex;
583 vport.port_no = port_no;
584 error = dpif_linux_vport_transact(&vport, NULL, NULL);
586 del_channel(dpif, port_no);
592 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
593 const char *port_name, struct dpif_port *dpif_port)
595 struct dpif_linux_vport request;
596 struct dpif_linux_vport reply;
600 dpif_linux_vport_init(&request);
601 request.cmd = OVS_VPORT_CMD_GET;
602 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
603 request.port_no = port_no;
604 request.name = port_name;
606 error = dpif_linux_vport_transact(&request, &reply, &buf);
608 if (reply.dp_ifindex != request.dp_ifindex) {
609 /* A query by name reported that 'port_name' is in some datapath
610 * other than 'dpif', but the caller wants to know about 'dpif'. */
612 } else if (dpif_port) {
613 dpif_port->name = xstrdup(reply.name);
614 dpif_port->type = xstrdup(get_vport_type(&reply));
615 dpif_port->port_no = reply.port_no;
623 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
624 struct dpif_port *dpif_port)
626 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
630 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
631 struct dpif_port *dpif_port)
633 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
637 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
643 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
645 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
647 if (dpif->epoll_fd < 0) {
650 /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
651 * channel, since it is not heavily loaded. */
652 int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
653 return nl_sock_pid(dpif->channels[idx].sock);
658 dpif_linux_flow_flush(struct dpif *dpif_)
660 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
661 struct dpif_linux_flow flow;
663 dpif_linux_flow_init(&flow);
664 flow.cmd = OVS_FLOW_CMD_DEL;
665 flow.dp_ifindex = dpif->dp_ifindex;
666 return dpif_linux_flow_transact(&flow, NULL, NULL);
669 struct dpif_linux_port_state {
674 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
676 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
677 struct dpif_linux_port_state *state;
678 struct dpif_linux_vport request;
681 *statep = state = xmalloc(sizeof *state);
683 dpif_linux_vport_init(&request);
684 request.cmd = OVS_DP_CMD_GET;
685 request.dp_ifindex = dpif->dp_ifindex;
687 buf = ofpbuf_new(1024);
688 dpif_linux_vport_to_ofpbuf(&request, buf);
689 nl_dump_start(&state->dump, genl_sock, buf);
696 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
697 struct dpif_port *dpif_port)
699 struct dpif_linux_port_state *state = state_;
700 struct dpif_linux_vport vport;
704 if (!nl_dump_next(&state->dump, &buf)) {
708 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
713 dpif_port->name = CONST_CAST(char *, vport.name);
714 dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
715 dpif_port->port_no = vport.port_no;
720 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
722 struct dpif_linux_port_state *state = state_;
723 int error = nl_dump_done(&state->dump);
730 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
732 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
734 if (dpif->change_error) {
735 dpif->change_error = false;
736 sset_clear(&dpif->changed_ports);
738 } else if (!sset_is_empty(&dpif->changed_ports)) {
739 *devnamep = sset_pop(&dpif->changed_ports);
747 dpif_linux_port_poll_wait(const struct dpif *dpif_)
749 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
750 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
751 poll_immediate_wake();
756 dpif_linux_flow_get__(const struct dpif *dpif_,
757 const struct nlattr *key, size_t key_len,
758 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
760 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
761 struct dpif_linux_flow request;
763 dpif_linux_flow_init(&request);
764 request.cmd = OVS_FLOW_CMD_GET;
765 request.dp_ifindex = dpif->dp_ifindex;
767 request.key_len = key_len;
768 return dpif_linux_flow_transact(&request, reply, bufp);
772 dpif_linux_flow_get(const struct dpif *dpif_,
773 const struct nlattr *key, size_t key_len,
774 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
776 struct dpif_linux_flow reply;
780 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
783 dpif_linux_flow_get_stats(&reply, stats);
786 buf->data = CONST_CAST(struct nlattr *, reply.actions);
787 buf->size = reply.actions_len;
797 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
798 struct dpif_linux_flow *request)
800 static struct nlattr dummy_action;
802 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
804 dpif_linux_flow_init(request);
805 request->cmd = (put->flags & DPIF_FP_CREATE
806 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
807 request->dp_ifindex = dpif->dp_ifindex;
808 request->key = put->key;
809 request->key_len = put->key_len;
810 request->mask = put->mask;
811 request->mask_len = put->mask_len;
812 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
813 request->actions = put->actions ? put->actions : &dummy_action;
814 request->actions_len = put->actions_len;
815 if (put->flags & DPIF_FP_ZERO_STATS) {
816 request->clear = true;
818 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
822 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
824 struct dpif_linux_flow request, reply;
828 dpif_linux_init_flow_put(dpif_, put, &request);
829 error = dpif_linux_flow_transact(&request,
830 put->stats ? &reply : NULL,
831 put->stats ? &buf : NULL);
832 if (!error && put->stats) {
833 dpif_linux_flow_get_stats(&reply, put->stats);
840 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
841 struct dpif_linux_flow *request)
843 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
845 dpif_linux_flow_init(request);
846 request->cmd = OVS_FLOW_CMD_DEL;
847 request->dp_ifindex = dpif->dp_ifindex;
848 request->key = del->key;
849 request->key_len = del->key_len;
853 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
855 struct dpif_linux_flow request, reply;
859 dpif_linux_init_flow_del(dpif_, del, &request);
860 error = dpif_linux_flow_transact(&request,
861 del->stats ? &reply : NULL,
862 del->stats ? &buf : NULL);
863 if (!error && del->stats) {
864 dpif_linux_flow_get_stats(&reply, del->stats);
870 struct dpif_linux_flow_state {
872 struct dpif_linux_flow flow;
873 struct dpif_flow_stats stats;
878 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
880 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
881 struct dpif_linux_flow_state *state;
882 struct dpif_linux_flow request;
885 *statep = state = xmalloc(sizeof *state);
887 dpif_linux_flow_init(&request);
888 request.cmd = OVS_DP_CMD_GET;
889 request.dp_ifindex = dpif->dp_ifindex;
891 buf = ofpbuf_new(1024);
892 dpif_linux_flow_to_ofpbuf(&request, buf);
893 nl_dump_start(&state->dump, genl_sock, buf);
902 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
903 const struct nlattr **key, size_t *key_len,
904 const struct nlattr **mask, size_t *mask_len,
905 const struct nlattr **actions, size_t *actions_len,
906 const struct dpif_flow_stats **stats)
908 struct dpif_linux_flow_state *state = state_;
913 ofpbuf_delete(state->buf);
916 if (!nl_dump_next(&state->dump, &buf)) {
920 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
925 if (actions && !state->flow.actions) {
926 error = dpif_linux_flow_get__(dpif_, state->flow.key,
928 &state->flow, &state->buf);
929 if (error == ENOENT) {
930 VLOG_DBG("dumped flow disappeared on get");
932 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
938 *actions = state->flow.actions;
939 *actions_len = state->flow.actions_len;
942 *key = state->flow.key;
943 *key_len = state->flow.key_len;
946 *mask = state->flow.mask;
947 *mask_len = state->flow.mask ? state->flow.mask_len : 0;
950 dpif_linux_flow_get_stats(&state->flow, &state->stats);
951 *stats = &state->stats;
957 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
959 struct dpif_linux_flow_state *state = state_;
960 int error = nl_dump_done(&state->dump);
961 ofpbuf_delete(state->buf);
967 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
970 struct ovs_header *k_exec;
972 ofpbuf_prealloc_tailroom(buf, (64
973 + d_exec->packet->size
975 + d_exec->actions_len));
977 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
978 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
980 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
981 k_exec->dp_ifindex = dp_ifindex;
983 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
984 d_exec->packet->data, d_exec->packet->size);
985 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
986 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
987 d_exec->actions, d_exec->actions_len);
991 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
993 uint64_t request_stub[1024 / 8];
994 struct ofpbuf request;
997 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
998 dpif_linux_encode_execute(dp_ifindex, execute, &request);
999 error = nl_sock_transact(genl_sock, &request, NULL);
1000 ofpbuf_uninit(&request);
1006 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
1008 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1010 return dpif_linux_execute__(dpif->dp_ifindex, execute);
1016 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1018 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1021 struct nl_transaction txn;
1023 struct ofpbuf request;
1024 uint64_t request_stub[1024 / 8];
1026 struct ofpbuf reply;
1027 uint64_t reply_stub[1024 / 8];
1030 struct nl_transaction *txnsp[MAX_OPS];
1033 ovs_assert(n_ops <= MAX_OPS);
1034 for (i = 0; i < n_ops; i++) {
1035 struct op_auxdata *aux = &auxes[i];
1036 struct dpif_op *op = ops[i];
1037 struct dpif_flow_put *put;
1038 struct dpif_flow_del *del;
1039 struct dpif_execute *execute;
1040 struct dpif_linux_flow flow;
1042 ofpbuf_use_stub(&aux->request,
1043 aux->request_stub, sizeof aux->request_stub);
1044 aux->txn.request = &aux->request;
1046 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1047 aux->txn.reply = NULL;
1050 case DPIF_OP_FLOW_PUT:
1051 put = &op->u.flow_put;
1052 dpif_linux_init_flow_put(dpif_, put, &flow);
1054 flow.nlmsg_flags |= NLM_F_ECHO;
1055 aux->txn.reply = &aux->reply;
1057 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1060 case DPIF_OP_FLOW_DEL:
1061 del = &op->u.flow_del;
1062 dpif_linux_init_flow_del(dpif_, del, &flow);
1064 flow.nlmsg_flags |= NLM_F_ECHO;
1065 aux->txn.reply = &aux->reply;
1067 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1070 case DPIF_OP_EXECUTE:
1071 execute = &op->u.execute;
1072 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1081 for (i = 0; i < n_ops; i++) {
1082 txnsp[i] = &auxes[i].txn;
1084 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1086 for (i = 0; i < n_ops; i++) {
1087 struct op_auxdata *aux = &auxes[i];
1088 struct nl_transaction *txn = &auxes[i].txn;
1089 struct dpif_op *op = ops[i];
1090 struct dpif_flow_put *put;
1091 struct dpif_flow_del *del;
1093 op->error = txn->error;
1096 case DPIF_OP_FLOW_PUT:
1097 put = &op->u.flow_put;
1100 struct dpif_linux_flow reply;
1102 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1105 dpif_linux_flow_get_stats(&reply, put->stats);
1110 memset(put->stats, 0, sizeof *put->stats);
1115 case DPIF_OP_FLOW_DEL:
1116 del = &op->u.flow_del;
1119 struct dpif_linux_flow reply;
1121 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1124 dpif_linux_flow_get_stats(&reply, del->stats);
1129 memset(del->stats, 0, sizeof *del->stats);
1134 case DPIF_OP_EXECUTE:
1141 ofpbuf_uninit(&aux->request);
1142 ofpbuf_uninit(&aux->reply);
1147 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1150 size_t chunk = MIN(n_ops, MAX_OPS);
1151 dpif_linux_operate__(dpif, ops, chunk);
1158 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1160 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1162 if ((dpif->epoll_fd >= 0) == enable) {
1167 destroy_channels(dpif);
1169 struct dpif_port_dump port_dump;
1170 struct dpif_port port;
1172 dpif->epoll_fd = epoll_create(10);
1173 if (dpif->epoll_fd < 0) {
1177 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1178 struct dpif_linux_vport vport_request;
1179 struct nl_sock *sock;
1180 uint32_t upcall_pid;
1183 error = nl_sock_create(NETLINK_GENERIC, &sock);
1188 upcall_pid = nl_sock_pid(sock);
1190 dpif_linux_vport_init(&vport_request);
1191 vport_request.cmd = OVS_VPORT_CMD_SET;
1192 vport_request.dp_ifindex = dpif->dp_ifindex;
1193 vport_request.port_no = port.port_no;
1194 vport_request.upcall_pid = &upcall_pid;
1195 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1197 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1198 dpif_name(&dpif->dpif), vport_request.port_no,
1201 VLOG_WARN_RL(&error_rl,
1202 "%s: failed to set upcall pid on port: %s",
1203 dpif_name(&dpif->dpif), strerror(error));
1204 nl_sock_destroy(sock);
1206 if (error == ENODEV || error == ENOENT) {
1207 /* This device isn't there, but keep trying the others. */
1214 error = add_channel(dpif, port.port_no, sock);
1216 VLOG_INFO("%s: could not add channel for port %s",
1217 dpif_name(dpif_), port.name);
1218 nl_sock_destroy(sock);
1228 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1229 uint32_t queue_id, uint32_t *priority)
1231 if (queue_id < 0xf000) {
1232 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1240 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1243 static const struct nl_policy ovs_packet_policy[] = {
1244 /* Always present. */
1245 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1246 .min_len = ETH_HEADER_LEN },
1247 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1249 /* OVS_PACKET_CMD_ACTION only. */
1250 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1253 struct ovs_header *ovs_header;
1254 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1255 struct nlmsghdr *nlmsg;
1256 struct genlmsghdr *genl;
1260 ofpbuf_use_const(&b, buf->data, buf->size);
1262 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1263 genl = ofpbuf_try_pull(&b, sizeof *genl);
1264 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1265 if (!nlmsg || !genl || !ovs_header
1266 || nlmsg->nlmsg_type != ovs_packet_family
1267 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1268 ARRAY_SIZE(ovs_packet_policy))) {
1272 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1273 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1279 memset(upcall, 0, sizeof *upcall);
1280 upcall->type = type;
1281 upcall->packet = buf;
1282 upcall->packet->data = CONST_CAST(struct nlattr *,
1283 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1284 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1285 upcall->key = CONST_CAST(struct nlattr *,
1286 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1287 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1288 upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1289 *dp_ifindex = ovs_header->dp_ifindex;
1295 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1298 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1301 if (dpif->epoll_fd < 0) {
1305 if (dpif->event_offset >= dpif->n_events) {
1308 dpif->event_offset = dpif->n_events = 0;
1311 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1312 dpif->uc_array_size, 0);
1313 } while (retval < 0 && errno == EINTR);
1315 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1316 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1317 } else if (retval > 0) {
1318 dpif->n_events = retval;
1322 while (dpif->event_offset < dpif->n_events) {
1323 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1324 struct dpif_channel *ch = &dpif->channels[idx];
1326 dpif->event_offset++;
1332 if (++read_tries > 50) {
1336 error = nl_sock_recv(ch->sock, buf, false);
1337 if (error == ENOBUFS) {
1338 /* ENOBUFS typically means that we've received so many
1339 * packets that the buffer overflowed. Try again
1340 * immediately because there's almost certainly a packet
1341 * waiting for us. */
1342 report_loss(dpif_, ch);
1346 ch->last_poll = time_msec();
1348 if (error == EAGAIN) {
1354 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1355 if (!error && dp_ifindex == dpif->dp_ifindex) {
1367 dpif_linux_recv_wait(struct dpif *dpif_)
1369 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1371 if (dpif->epoll_fd < 0) {
1375 poll_fd_wait(dpif->epoll_fd, POLLIN);
1379 dpif_linux_recv_purge(struct dpif *dpif_)
1381 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1382 struct dpif_channel *ch;
1384 if (dpif->epoll_fd < 0) {
1388 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1390 nl_sock_drain(ch->sock);
1395 const struct dpif_class dpif_linux_class = {
1397 dpif_linux_enumerate,
1404 dpif_linux_get_stats,
1405 dpif_linux_port_add,
1406 dpif_linux_port_del,
1407 dpif_linux_port_query_by_number,
1408 dpif_linux_port_query_by_name,
1409 dpif_linux_get_max_ports,
1410 dpif_linux_port_get_pid,
1411 dpif_linux_port_dump_start,
1412 dpif_linux_port_dump_next,
1413 dpif_linux_port_dump_done,
1414 dpif_linux_port_poll,
1415 dpif_linux_port_poll_wait,
1416 dpif_linux_flow_get,
1417 dpif_linux_flow_put,
1418 dpif_linux_flow_del,
1419 dpif_linux_flow_flush,
1420 dpif_linux_flow_dump_start,
1421 dpif_linux_flow_dump_next,
1422 dpif_linux_flow_dump_done,
1425 dpif_linux_recv_set,
1426 dpif_linux_queue_to_priority,
1428 dpif_linux_recv_wait,
1429 dpif_linux_recv_purge,
1433 dpif_linux_init(void)
1435 static int error = -1;
1438 unsigned int ovs_vport_mcgroup;
1440 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1441 &ovs_datapath_family);
1443 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1444 "The Open vSwitch kernel module is probably not loaded.",
1445 OVS_DATAPATH_FAMILY);
1448 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1451 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1454 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1455 &ovs_packet_family);
1458 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1461 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1463 OVS_VPORT_MCGROUP_FALLBACK_ID);
1466 static struct dpif_linux_vport vport;
1467 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1468 dpif_linux_nln_parse, &vport);
1476 dpif_linux_is_internal_device(const char *name)
1478 struct dpif_linux_vport reply;
1482 error = dpif_linux_vport_get(name, &reply, &buf);
1485 } else if (error != ENODEV && error != ENOENT) {
1486 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1487 name, strerror(error));
1490 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1494 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1496 struct dpif_linux_vport *vport = vport_;
1497 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1501 dpif_linux_port_changed(const void *vport_, void *dpif_)
1503 const struct dpif_linux_vport *vport = vport_;
1504 struct dpif_linux *dpif = dpif_;
1507 if (vport->dp_ifindex == dpif->dp_ifindex
1508 && (vport->cmd == OVS_VPORT_CMD_NEW
1509 || vport->cmd == OVS_VPORT_CMD_DEL
1510 || vport->cmd == OVS_VPORT_CMD_SET)) {
1511 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1512 dpif->dpif.full_name, vport->name, vport->cmd);
1513 sset_add(&dpif->changed_ports, vport->name);
1516 dpif->change_error = true;
1520 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1521 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1522 * positive errno value.
1524 * 'vport' will contain pointers into 'buf', so the caller should not free
1525 * 'buf' while 'vport' is still in use. */
1527 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1528 const struct ofpbuf *buf)
1530 static const struct nl_policy ovs_vport_policy[] = {
1531 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1532 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1533 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1534 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1535 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1537 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1540 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1541 struct ovs_header *ovs_header;
1542 struct nlmsghdr *nlmsg;
1543 struct genlmsghdr *genl;
1546 dpif_linux_vport_init(vport);
1548 ofpbuf_use_const(&b, buf->data, buf->size);
1549 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1550 genl = ofpbuf_try_pull(&b, sizeof *genl);
1551 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1552 if (!nlmsg || !genl || !ovs_header
1553 || nlmsg->nlmsg_type != ovs_vport_family
1554 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1555 ARRAY_SIZE(ovs_vport_policy))) {
1559 vport->cmd = genl->cmd;
1560 vport->dp_ifindex = ovs_header->dp_ifindex;
1561 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1562 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1563 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1564 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1565 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1567 if (a[OVS_VPORT_ATTR_STATS]) {
1568 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1570 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1571 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1572 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1577 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1578 * followed by Netlink attributes corresponding to 'vport'. */
1580 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1583 struct ovs_header *ovs_header;
1585 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1586 vport->cmd, OVS_VPORT_VERSION);
1588 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1589 ovs_header->dp_ifindex = vport->dp_ifindex;
1591 if (vport->port_no != UINT32_MAX) {
1592 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1595 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1596 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1600 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1603 if (vport->upcall_pid) {
1604 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1608 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1609 vport->stats, sizeof *vport->stats);
1612 if (vport->options) {
1613 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1614 vport->options, vport->options_len);
1618 /* Clears 'vport' to "empty" values. */
1620 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1622 memset(vport, 0, sizeof *vport);
1623 vport->port_no = UINT32_MAX;
1626 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1627 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1628 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1629 * result of the command is expected to be an ovs_vport also, which is decoded
1630 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1631 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1633 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1634 struct dpif_linux_vport *reply,
1635 struct ofpbuf **bufp)
1637 struct ofpbuf *request_buf;
1640 ovs_assert((reply != NULL) == (bufp != NULL));
1642 error = dpif_linux_init();
1646 dpif_linux_vport_init(reply);
1651 request_buf = ofpbuf_new(1024);
1652 dpif_linux_vport_to_ofpbuf(request, request_buf);
1653 error = nl_sock_transact(genl_sock, request_buf, bufp);
1654 ofpbuf_delete(request_buf);
1658 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1661 dpif_linux_vport_init(reply);
1662 ofpbuf_delete(*bufp);
1669 /* Obtains information about the kernel vport named 'name' and stores it into
1670 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1671 * longer needed ('reply' will contain pointers into '*bufp'). */
1673 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1674 struct ofpbuf **bufp)
1676 struct dpif_linux_vport request;
1678 dpif_linux_vport_init(&request);
1679 request.cmd = OVS_VPORT_CMD_GET;
1680 request.name = name;
1682 return dpif_linux_vport_transact(&request, reply, bufp);
1685 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1686 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1687 * positive errno value.
1689 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1690 * while 'dp' is still in use. */
1692 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1694 static const struct nl_policy ovs_datapath_policy[] = {
1695 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1696 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1700 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1701 struct ovs_header *ovs_header;
1702 struct nlmsghdr *nlmsg;
1703 struct genlmsghdr *genl;
1706 dpif_linux_dp_init(dp);
1708 ofpbuf_use_const(&b, buf->data, buf->size);
1709 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1710 genl = ofpbuf_try_pull(&b, sizeof *genl);
1711 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1712 if (!nlmsg || !genl || !ovs_header
1713 || nlmsg->nlmsg_type != ovs_datapath_family
1714 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1715 ARRAY_SIZE(ovs_datapath_policy))) {
1719 dp->cmd = genl->cmd;
1720 dp->dp_ifindex = ovs_header->dp_ifindex;
1721 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1722 if (a[OVS_DP_ATTR_STATS]) {
1723 /* Can't use structure assignment because Netlink doesn't ensure
1724 * sufficient alignment for 64-bit members. */
1725 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1732 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1734 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1736 struct ovs_header *ovs_header;
1738 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1739 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1740 OVS_DATAPATH_VERSION);
1742 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1743 ovs_header->dp_ifindex = dp->dp_ifindex;
1746 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1749 if (dp->upcall_pid) {
1750 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1753 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1756 /* Clears 'dp' to "empty" values. */
1758 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1760 memset(dp, 0, sizeof *dp);
1764 dpif_linux_dp_dump_start(struct nl_dump *dump)
1766 struct dpif_linux_dp request;
1769 dpif_linux_dp_init(&request);
1770 request.cmd = OVS_DP_CMD_GET;
1772 buf = ofpbuf_new(1024);
1773 dpif_linux_dp_to_ofpbuf(&request, buf);
1774 nl_dump_start(dump, genl_sock, buf);
1778 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1779 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1780 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1781 * result of the command is expected to be of the same form, which is decoded
1782 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1783 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1785 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1786 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1788 struct ofpbuf *request_buf;
1791 ovs_assert((reply != NULL) == (bufp != NULL));
1793 request_buf = ofpbuf_new(1024);
1794 dpif_linux_dp_to_ofpbuf(request, request_buf);
1795 error = nl_sock_transact(genl_sock, request_buf, bufp);
1796 ofpbuf_delete(request_buf);
1800 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1803 dpif_linux_dp_init(reply);
1804 ofpbuf_delete(*bufp);
1811 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1812 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1813 * will contain pointers into '*bufp'). */
1815 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1816 struct ofpbuf **bufp)
1818 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1819 struct dpif_linux_dp request;
1821 dpif_linux_dp_init(&request);
1822 request.cmd = OVS_DP_CMD_GET;
1823 request.dp_ifindex = dpif->dp_ifindex;
1825 return dpif_linux_dp_transact(&request, reply, bufp);
1828 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1829 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1830 * positive errno value.
1832 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1833 * while 'flow' is still in use. */
1835 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1836 const struct ofpbuf *buf)
1838 static const struct nl_policy ovs_flow_policy[] = {
1839 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1840 [OVS_FLOW_ATTR_MASK] = { .type = NL_A_NESTED, .optional = true },
1841 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1842 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1844 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1845 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1846 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1849 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1850 struct ovs_header *ovs_header;
1851 struct nlmsghdr *nlmsg;
1852 struct genlmsghdr *genl;
1855 dpif_linux_flow_init(flow);
1857 ofpbuf_use_const(&b, buf->data, buf->size);
1858 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1859 genl = ofpbuf_try_pull(&b, sizeof *genl);
1860 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1861 if (!nlmsg || !genl || !ovs_header
1862 || nlmsg->nlmsg_type != ovs_flow_family
1863 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1864 ARRAY_SIZE(ovs_flow_policy))) {
1868 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1869 flow->dp_ifindex = ovs_header->dp_ifindex;
1870 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1871 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1873 if (a[OVS_FLOW_ATTR_MASK]) {
1874 flow->mask = nl_attr_get(a[OVS_FLOW_ATTR_MASK]);
1875 flow->mask_len = nl_attr_get_size(a[OVS_FLOW_ATTR_MASK]);
1877 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1878 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1879 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1881 if (a[OVS_FLOW_ATTR_STATS]) {
1882 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1884 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1885 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1887 if (a[OVS_FLOW_ATTR_USED]) {
1888 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1893 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1894 * followed by Netlink attributes corresponding to 'flow'. */
1896 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1899 struct ovs_header *ovs_header;
1901 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1902 NLM_F_REQUEST | flow->nlmsg_flags,
1903 flow->cmd, OVS_FLOW_VERSION);
1905 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1906 ovs_header->dp_ifindex = flow->dp_ifindex;
1908 if (flow->key_len) {
1909 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1912 if (flow->mask_len) {
1913 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_MASK, flow->mask, flow->mask_len);
1916 if (flow->actions || flow->actions_len) {
1917 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1918 flow->actions, flow->actions_len);
1921 /* We never need to send these to the kernel. */
1922 ovs_assert(!flow->stats);
1923 ovs_assert(!flow->tcp_flags);
1924 ovs_assert(!flow->used);
1927 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1931 /* Clears 'flow' to "empty" values. */
1933 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1935 memset(flow, 0, sizeof *flow);
1938 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1939 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1940 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1941 * result of the command is expected to be a flow also, which is decoded and
1942 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1943 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1945 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1946 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1948 struct ofpbuf *request_buf;
1951 ovs_assert((reply != NULL) == (bufp != NULL));
1954 request->nlmsg_flags |= NLM_F_ECHO;
1957 request_buf = ofpbuf_new(1024);
1958 dpif_linux_flow_to_ofpbuf(request, request_buf);
1959 error = nl_sock_transact(genl_sock, request_buf, bufp);
1960 ofpbuf_delete(request_buf);
1964 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1967 dpif_linux_flow_init(reply);
1968 ofpbuf_delete(*bufp);
1976 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1977 struct dpif_flow_stats *stats)
1980 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1981 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1983 stats->n_packets = 0;
1986 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1987 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1990 /* Logs information about a packet that was recently lost in 'ch' (in
1993 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1995 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1996 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1999 if (VLOG_DROP_WARN(&rl)) {
2004 if (ch->last_poll != LLONG_MIN) {
2005 ds_put_format(&s, " (last polled %lld ms ago)",
2006 time_msec() - ch->last_poll);
2009 VLOG_WARN("%s: lost packet on channel %td%s",
2010 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));