2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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"
50 #include "openvswitch/tunnel.h"
52 #include "poll-loop.h"
57 #include "unaligned.h"
61 VLOG_DEFINE_THIS_MODULE(dpif_linux);
62 enum { MAX_PORTS = USHRT_MAX };
64 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
65 * missing if we have old headers. */
66 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
68 struct dpif_linux_dp {
69 /* Generic Netlink header. */
72 /* struct ovs_header. */
76 const char *name; /* OVS_DP_ATTR_NAME. */
77 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
78 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
81 static void dpif_linux_dp_init(struct dpif_linux_dp *);
82 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
83 const struct ofpbuf *);
84 static void dpif_linux_dp_dump_start(struct nl_dump *);
85 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
86 struct dpif_linux_dp *reply,
87 struct ofpbuf **bufp);
88 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
89 struct ofpbuf **bufp);
91 struct dpif_linux_flow {
92 /* Generic Netlink header. */
95 /* struct ovs_header. */
96 unsigned int nlmsg_flags;
101 * The 'stats' member points to 64-bit data that might only be aligned on
102 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
105 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
106 * the Netlink version of the command, even if actions_len is zero. */
107 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
109 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
111 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
112 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
113 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
114 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
117 static void dpif_linux_flow_init(struct dpif_linux_flow *);
118 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
119 const struct ofpbuf *);
120 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
122 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
123 struct dpif_linux_flow *reply,
124 struct ofpbuf **bufp);
125 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
126 struct dpif_flow_stats *);
128 /* One of the dpif channels between the kernel and userspace. */
129 struct dpif_channel {
130 struct nl_sock *sock; /* Netlink socket. */
131 long long int last_poll; /* Last time this channel was polled. */
134 static void report_loss(struct dpif *, struct dpif_channel *);
136 /* Datapath interface for the openvswitch Linux kernel module. */
141 /* Upcall messages. */
142 int uc_array_size; /* Size of 'channels' and 'epoll_events'. */
143 struct dpif_channel *channels;
144 struct epoll_event *epoll_events;
145 int epoll_fd; /* epoll fd that includes channel socks. */
146 int n_events; /* Num events returned by epoll_wait(). */
147 int event_offset; /* Offset into 'epoll_events'. */
149 /* Change notification. */
150 struct sset changed_ports; /* Ports that have changed. */
151 struct nln_notifier *port_notifier;
155 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
157 /* Generic Netlink family numbers for OVS. */
158 static int ovs_datapath_family;
159 static int ovs_vport_family;
160 static int ovs_flow_family;
161 static int ovs_packet_family;
163 /* Generic Netlink socket. */
164 static struct nl_sock *genl_sock;
165 static struct nln *nln = NULL;
167 static int dpif_linux_init(void);
168 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
169 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
170 static void dpif_linux_port_changed(const void *vport, void *dpif);
171 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint32_t port_no);
173 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
175 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
176 const struct ofpbuf *);
178 static struct dpif_linux *
179 dpif_linux_cast(const struct dpif *dpif)
181 dpif_assert_class(dpif, &dpif_linux_class);
182 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
186 dpif_linux_enumerate(struct sset *all_dps)
192 error = dpif_linux_init();
197 dpif_linux_dp_dump_start(&dump);
198 while (nl_dump_next(&dump, &msg)) {
199 struct dpif_linux_dp dp;
201 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
202 sset_add(all_dps, dp.name);
205 return nl_dump_done(&dump);
209 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
210 bool create, struct dpif **dpifp)
212 struct dpif_linux_dp dp_request, dp;
217 error = dpif_linux_init();
222 /* Create or look up datapath. */
223 dpif_linux_dp_init(&dp_request);
225 dp_request.cmd = OVS_DP_CMD_NEW;
227 dp_request.upcall_pid = &upcall_pid;
229 dp_request.cmd = OVS_DP_CMD_GET;
231 dp_request.name = name;
232 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
237 open_dpif(&dp, dpifp);
243 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
245 struct dpif_linux *dpif;
247 dpif = xzalloc(sizeof *dpif);
248 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
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 sset_init(&dpif->changed_ports);
257 *dpifp = &dpif->dpif;
261 destroy_channels(struct dpif_linux *dpif)
265 if (dpif->epoll_fd < 0) {
269 for (i = 0; i < dpif->uc_array_size; i++ ) {
270 struct dpif_linux_vport vport_request;
271 struct dpif_channel *ch = &dpif->channels[i];
272 uint32_t upcall_pid = 0;
278 /* Turn off upcalls. */
279 dpif_linux_vport_init(&vport_request);
280 vport_request.cmd = OVS_VPORT_CMD_SET;
281 vport_request.dp_ifindex = dpif->dp_ifindex;
282 vport_request.port_no = i;
283 vport_request.upcall_pid = &upcall_pid;
284 dpif_linux_vport_transact(&vport_request, NULL, NULL);
286 nl_sock_destroy(ch->sock);
289 free(dpif->channels);
290 dpif->channels = NULL;
291 dpif->uc_array_size = 0;
293 free(dpif->epoll_events);
294 dpif->epoll_events = NULL;
295 dpif->n_events = dpif->event_offset = 0;
297 close(dpif->epoll_fd);
302 add_channel(struct dpif_linux *dpif, uint32_t port_no, struct nl_sock *sock)
304 struct epoll_event event;
306 if (dpif->epoll_fd < 0) {
310 /* We assume that the datapath densely chooses port numbers, which
311 * can therefore be used as an index into an array of channels. */
312 if (port_no >= dpif->uc_array_size) {
313 int new_size = port_no + 1;
316 if (new_size > 65535) {
317 VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
318 dpif_name(&dpif->dpif), port_no);
322 dpif->channels = xrealloc(dpif->channels,
323 new_size * sizeof *dpif->channels);
324 for (i = dpif->uc_array_size; i < new_size; i++) {
325 dpif->channels[i].sock = NULL;
328 dpif->epoll_events = xrealloc(dpif->epoll_events,
329 new_size * sizeof *dpif->epoll_events);
330 dpif->uc_array_size = new_size;
333 memset(&event, 0, sizeof event);
334 event.events = EPOLLIN;
335 event.data.u32 = port_no;
336 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
341 dpif->channels[port_no].sock = sock;
342 dpif->channels[port_no].last_poll = LLONG_MIN;
348 del_channel(struct dpif_linux *dpif, uint32_t port_no)
350 struct dpif_channel *ch;
352 if (dpif->epoll_fd < 0 || port_no >= dpif->uc_array_size) {
356 ch = &dpif->channels[port_no];
361 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
363 nl_sock_destroy(ch->sock);
368 dpif_linux_close(struct dpif *dpif_)
370 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
372 nln_notifier_destroy(dpif->port_notifier);
373 destroy_channels(dpif);
374 sset_destroy(&dpif->changed_ports);
379 dpif_linux_destroy(struct dpif *dpif_)
381 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
382 struct dpif_linux_dp dp;
384 dpif_linux_dp_init(&dp);
385 dp.cmd = OVS_DP_CMD_DEL;
386 dp.dp_ifindex = dpif->dp_ifindex;
387 return dpif_linux_dp_transact(&dp, NULL, NULL);
391 dpif_linux_run(struct dpif *dpif_ OVS_UNUSED)
399 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
407 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
409 struct dpif_linux_dp dp;
413 error = dpif_linux_dp_get(dpif_, &dp, &buf);
415 stats->n_hit = dp.stats.n_hit;
416 stats->n_missed = dp.stats.n_missed;
417 stats->n_lost = dp.stats.n_lost;
418 stats->n_flows = dp.stats.n_flows;
425 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
428 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
429 const char *name = netdev_get_name(netdev);
430 const char *type = netdev_get_type(netdev);
431 struct dpif_linux_vport request, reply;
432 const struct ofpbuf *options;
433 struct nl_sock *sock = NULL;
438 if (dpif->epoll_fd >= 0) {
439 error = nl_sock_create(NETLINK_GENERIC, &sock);
445 dpif_linux_vport_init(&request);
446 request.cmd = OVS_VPORT_CMD_NEW;
447 request.dp_ifindex = dpif->dp_ifindex;
448 request.type = netdev_vport_get_vport_type(netdev);
449 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
450 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
451 "unsupported type `%s'",
452 dpif_name(dpif_), name, type);
453 nl_sock_destroy(sock);
458 options = netdev_vport_get_options(netdev);
459 if (options && options->size) {
460 request.options = options->data;
461 request.options_len = options->size;
464 if (request.type == OVS_VPORT_TYPE_NETDEV) {
465 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
468 request.port_no = *port_nop;
469 upcall_pid = sock ? nl_sock_pid(sock) : 0;
470 request.upcall_pid = &upcall_pid;
472 error = dpif_linux_vport_transact(&request, &reply, &buf);
474 *port_nop = reply.port_no;
475 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
476 dpif_name(dpif_), reply.port_no, upcall_pid);
477 } else if (error == EBUSY && *port_nop != UINT32_MAX) {
478 VLOG_INFO("%s: requested port %"PRIu32" is in use",
479 dpif_name(dpif_), *port_nop);
480 nl_sock_destroy(sock);
487 error = add_channel(dpif, *port_nop, sock);
489 VLOG_INFO("%s: could not add channel for port %s",
490 dpif_name(dpif_), name);
492 /* Delete the port. */
493 dpif_linux_vport_init(&request);
494 request.cmd = OVS_VPORT_CMD_DEL;
495 request.dp_ifindex = dpif->dp_ifindex;
496 request.port_no = *port_nop;
497 dpif_linux_vport_transact(&request, NULL, NULL);
499 nl_sock_destroy(sock);
508 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
510 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
511 struct dpif_linux_vport vport;
514 dpif_linux_vport_init(&vport);
515 vport.cmd = OVS_VPORT_CMD_DEL;
516 vport.dp_ifindex = dpif->dp_ifindex;
517 vport.port_no = port_no;
518 error = dpif_linux_vport_transact(&vport, NULL, NULL);
520 del_channel(dpif, port_no);
526 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
527 const char *port_name, struct dpif_port *dpif_port)
529 struct dpif_linux_vport request;
530 struct dpif_linux_vport reply;
534 dpif_linux_vport_init(&request);
535 request.cmd = OVS_VPORT_CMD_GET;
536 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
537 request.port_no = port_no;
538 request.name = port_name;
540 error = dpif_linux_vport_transact(&request, &reply, &buf);
542 if (reply.dp_ifindex != request.dp_ifindex) {
543 /* A query by name reported that 'port_name' is in some datapath
544 * other than 'dpif', but the caller wants to know about 'dpif'. */
546 } else if (dpif_port) {
547 dpif_port->name = xstrdup(reply.name);
548 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
549 dpif_port->port_no = reply.port_no;
557 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
558 struct dpif_port *dpif_port)
560 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
564 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
565 struct dpif_port *dpif_port)
567 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
571 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
577 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
579 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
581 if (dpif->epoll_fd < 0) {
584 /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
585 * channel, since it is not heavily loaded. */
586 int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
587 return nl_sock_pid(dpif->channels[idx].sock);
592 dpif_linux_flow_flush(struct dpif *dpif_)
594 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
595 struct dpif_linux_flow flow;
597 dpif_linux_flow_init(&flow);
598 flow.cmd = OVS_FLOW_CMD_DEL;
599 flow.dp_ifindex = dpif->dp_ifindex;
600 return dpif_linux_flow_transact(&flow, NULL, NULL);
603 struct dpif_linux_port_state {
608 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
610 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
611 struct dpif_linux_port_state *state;
612 struct dpif_linux_vport request;
615 *statep = state = xmalloc(sizeof *state);
617 dpif_linux_vport_init(&request);
618 request.cmd = OVS_DP_CMD_GET;
619 request.dp_ifindex = dpif->dp_ifindex;
621 buf = ofpbuf_new(1024);
622 dpif_linux_vport_to_ofpbuf(&request, buf);
623 nl_dump_start(&state->dump, genl_sock, buf);
630 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
631 struct dpif_port *dpif_port)
633 struct dpif_linux_port_state *state = state_;
634 struct dpif_linux_vport vport;
638 if (!nl_dump_next(&state->dump, &buf)) {
642 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
647 dpif_port->name = CONST_CAST(char *, vport.name);
648 dpif_port->type = CONST_CAST(char *, netdev_vport_get_netdev_type(&vport));
649 dpif_port->port_no = vport.port_no;
654 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
656 struct dpif_linux_port_state *state = state_;
657 int error = nl_dump_done(&state->dump);
664 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
666 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
668 if (dpif->change_error) {
669 dpif->change_error = false;
670 sset_clear(&dpif->changed_ports);
672 } else if (!sset_is_empty(&dpif->changed_ports)) {
673 *devnamep = sset_pop(&dpif->changed_ports);
681 dpif_linux_port_poll_wait(const struct dpif *dpif_)
683 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
684 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
685 poll_immediate_wake();
690 dpif_linux_flow_get__(const struct dpif *dpif_,
691 const struct nlattr *key, size_t key_len,
692 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
694 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
695 struct dpif_linux_flow request;
697 dpif_linux_flow_init(&request);
698 request.cmd = OVS_FLOW_CMD_GET;
699 request.dp_ifindex = dpif->dp_ifindex;
701 request.key_len = key_len;
702 return dpif_linux_flow_transact(&request, reply, bufp);
706 dpif_linux_flow_get(const struct dpif *dpif_,
707 const struct nlattr *key, size_t key_len,
708 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
710 struct dpif_linux_flow reply;
714 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
717 dpif_linux_flow_get_stats(&reply, stats);
720 buf->data = CONST_CAST(struct nlattr *, reply.actions);
721 buf->size = reply.actions_len;
731 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
732 struct dpif_linux_flow *request)
734 static struct nlattr dummy_action;
736 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
738 dpif_linux_flow_init(request);
739 request->cmd = (put->flags & DPIF_FP_CREATE
740 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
741 request->dp_ifindex = dpif->dp_ifindex;
742 request->key = put->key;
743 request->key_len = put->key_len;
744 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
745 request->actions = put->actions ? put->actions : &dummy_action;
746 request->actions_len = put->actions_len;
747 if (put->flags & DPIF_FP_ZERO_STATS) {
748 request->clear = true;
750 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
754 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
756 struct dpif_linux_flow request, reply;
760 dpif_linux_init_flow_put(dpif_, put, &request);
761 error = dpif_linux_flow_transact(&request,
762 put->stats ? &reply : NULL,
763 put->stats ? &buf : NULL);
764 if (!error && put->stats) {
765 dpif_linux_flow_get_stats(&reply, put->stats);
772 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
773 struct dpif_linux_flow *request)
775 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
777 dpif_linux_flow_init(request);
778 request->cmd = OVS_FLOW_CMD_DEL;
779 request->dp_ifindex = dpif->dp_ifindex;
780 request->key = del->key;
781 request->key_len = del->key_len;
785 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
787 struct dpif_linux_flow request, reply;
791 dpif_linux_init_flow_del(dpif_, del, &request);
792 error = dpif_linux_flow_transact(&request,
793 del->stats ? &reply : NULL,
794 del->stats ? &buf : NULL);
795 if (!error && del->stats) {
796 dpif_linux_flow_get_stats(&reply, del->stats);
802 struct dpif_linux_flow_state {
804 struct dpif_linux_flow flow;
805 struct dpif_flow_stats stats;
810 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
812 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
813 struct dpif_linux_flow_state *state;
814 struct dpif_linux_flow request;
817 *statep = state = xmalloc(sizeof *state);
819 dpif_linux_flow_init(&request);
820 request.cmd = OVS_DP_CMD_GET;
821 request.dp_ifindex = dpif->dp_ifindex;
823 buf = ofpbuf_new(1024);
824 dpif_linux_flow_to_ofpbuf(&request, buf);
825 nl_dump_start(&state->dump, genl_sock, buf);
834 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
835 const struct nlattr **key, size_t *key_len,
836 const struct nlattr **actions, size_t *actions_len,
837 const struct dpif_flow_stats **stats)
839 struct dpif_linux_flow_state *state = state_;
844 ofpbuf_delete(state->buf);
847 if (!nl_dump_next(&state->dump, &buf)) {
851 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
856 if (actions && !state->flow.actions) {
857 error = dpif_linux_flow_get__(dpif_, state->flow.key,
859 &state->flow, &state->buf);
860 if (error == ENOENT) {
861 VLOG_DBG("dumped flow disappeared on get");
863 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
869 *actions = state->flow.actions;
870 *actions_len = state->flow.actions_len;
873 *key = state->flow.key;
874 *key_len = state->flow.key_len;
877 dpif_linux_flow_get_stats(&state->flow, &state->stats);
878 *stats = &state->stats;
884 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
886 struct dpif_linux_flow_state *state = state_;
887 int error = nl_dump_done(&state->dump);
888 ofpbuf_delete(state->buf);
894 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
897 struct ovs_header *k_exec;
899 ofpbuf_prealloc_tailroom(buf, (64
900 + d_exec->packet->size
902 + d_exec->actions_len));
904 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
905 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
907 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
908 k_exec->dp_ifindex = dp_ifindex;
910 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
911 d_exec->packet->data, d_exec->packet->size);
912 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
913 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
914 d_exec->actions, d_exec->actions_len);
918 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
920 uint64_t request_stub[1024 / 8];
921 struct ofpbuf request;
924 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
925 dpif_linux_encode_execute(dp_ifindex, execute, &request);
926 error = nl_sock_transact(genl_sock, &request, NULL);
927 ofpbuf_uninit(&request);
933 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
935 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
937 return dpif_linux_execute__(dpif->dp_ifindex, execute);
943 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
945 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
948 struct nl_transaction txn;
950 struct ofpbuf request;
951 uint64_t request_stub[1024 / 8];
954 uint64_t reply_stub[1024 / 8];
957 struct nl_transaction *txnsp[MAX_OPS];
960 ovs_assert(n_ops <= MAX_OPS);
961 for (i = 0; i < n_ops; i++) {
962 struct op_auxdata *aux = &auxes[i];
963 struct dpif_op *op = ops[i];
964 struct dpif_flow_put *put;
965 struct dpif_flow_del *del;
966 struct dpif_execute *execute;
967 struct dpif_linux_flow flow;
969 ofpbuf_use_stub(&aux->request,
970 aux->request_stub, sizeof aux->request_stub);
971 aux->txn.request = &aux->request;
973 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
974 aux->txn.reply = NULL;
977 case DPIF_OP_FLOW_PUT:
978 put = &op->u.flow_put;
979 dpif_linux_init_flow_put(dpif_, put, &flow);
981 flow.nlmsg_flags |= NLM_F_ECHO;
982 aux->txn.reply = &aux->reply;
984 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
987 case DPIF_OP_FLOW_DEL:
988 del = &op->u.flow_del;
989 dpif_linux_init_flow_del(dpif_, del, &flow);
991 flow.nlmsg_flags |= NLM_F_ECHO;
992 aux->txn.reply = &aux->reply;
994 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
997 case DPIF_OP_EXECUTE:
998 execute = &op->u.execute;
999 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1008 for (i = 0; i < n_ops; i++) {
1009 txnsp[i] = &auxes[i].txn;
1011 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1013 for (i = 0; i < n_ops; i++) {
1014 struct op_auxdata *aux = &auxes[i];
1015 struct nl_transaction *txn = &auxes[i].txn;
1016 struct dpif_op *op = ops[i];
1017 struct dpif_flow_put *put;
1018 struct dpif_flow_del *del;
1020 op->error = txn->error;
1023 case DPIF_OP_FLOW_PUT:
1024 put = &op->u.flow_put;
1027 struct dpif_linux_flow reply;
1029 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1032 dpif_linux_flow_get_stats(&reply, put->stats);
1037 memset(put->stats, 0, sizeof *put->stats);
1042 case DPIF_OP_FLOW_DEL:
1043 del = &op->u.flow_del;
1046 struct dpif_linux_flow reply;
1048 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1051 dpif_linux_flow_get_stats(&reply, del->stats);
1056 memset(del->stats, 0, sizeof *del->stats);
1061 case DPIF_OP_EXECUTE:
1068 ofpbuf_uninit(&aux->request);
1069 ofpbuf_uninit(&aux->reply);
1074 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1077 size_t chunk = MIN(n_ops, MAX_OPS);
1078 dpif_linux_operate__(dpif, ops, chunk);
1085 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1087 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1089 if ((dpif->epoll_fd >= 0) == enable) {
1094 destroy_channels(dpif);
1096 struct dpif_port_dump port_dump;
1097 struct dpif_port port;
1099 dpif->epoll_fd = epoll_create(10);
1100 if (dpif->epoll_fd < 0) {
1104 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1105 struct dpif_linux_vport vport_request;
1106 struct nl_sock *sock;
1107 uint32_t upcall_pid;
1110 error = nl_sock_create(NETLINK_GENERIC, &sock);
1115 upcall_pid = nl_sock_pid(sock);
1117 dpif_linux_vport_init(&vport_request);
1118 vport_request.cmd = OVS_VPORT_CMD_SET;
1119 vport_request.dp_ifindex = dpif->dp_ifindex;
1120 vport_request.port_no = port.port_no;
1121 vport_request.upcall_pid = &upcall_pid;
1122 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1124 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1125 dpif_name(&dpif->dpif), vport_request.port_no,
1128 VLOG_WARN_RL(&error_rl,
1129 "%s: failed to set upcall pid on port: %s",
1130 dpif_name(&dpif->dpif), strerror(error));
1131 nl_sock_destroy(sock);
1133 if (error == ENODEV || error == ENOENT) {
1134 /* This device isn't there, but keep trying the others. */
1141 error = add_channel(dpif, port.port_no, sock);
1143 VLOG_INFO("%s: could not add channel for port %s",
1144 dpif_name(dpif_), port.name);
1145 nl_sock_destroy(sock);
1155 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1156 uint32_t queue_id, uint32_t *priority)
1158 if (queue_id < 0xf000) {
1159 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1167 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1170 static const struct nl_policy ovs_packet_policy[] = {
1171 /* Always present. */
1172 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1173 .min_len = ETH_HEADER_LEN },
1174 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1176 /* OVS_PACKET_CMD_ACTION only. */
1177 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1180 struct ovs_header *ovs_header;
1181 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1182 struct nlmsghdr *nlmsg;
1183 struct genlmsghdr *genl;
1187 ofpbuf_use_const(&b, buf->data, buf->size);
1189 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1190 genl = ofpbuf_try_pull(&b, sizeof *genl);
1191 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1192 if (!nlmsg || !genl || !ovs_header
1193 || nlmsg->nlmsg_type != ovs_packet_family
1194 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1195 ARRAY_SIZE(ovs_packet_policy))) {
1199 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1200 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1206 memset(upcall, 0, sizeof *upcall);
1207 upcall->type = type;
1208 upcall->packet = buf;
1209 upcall->packet->data = CONST_CAST(struct nlattr *,
1210 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1211 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1212 upcall->key = CONST_CAST(struct nlattr *,
1213 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1214 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1215 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1216 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1218 *dp_ifindex = ovs_header->dp_ifindex;
1224 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1227 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1230 if (dpif->epoll_fd < 0) {
1234 if (dpif->event_offset >= dpif->n_events) {
1237 dpif->event_offset = dpif->n_events = 0;
1240 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1241 dpif->uc_array_size, 0);
1242 } while (retval < 0 && errno == EINTR);
1244 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1245 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1246 } else if (retval > 0) {
1247 dpif->n_events = retval;
1251 while (dpif->event_offset < dpif->n_events) {
1252 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1253 struct dpif_channel *ch = &dpif->channels[idx];
1255 dpif->event_offset++;
1261 if (++read_tries > 50) {
1265 error = nl_sock_recv(ch->sock, buf, false);
1266 if (error == ENOBUFS) {
1267 /* ENOBUFS typically means that we've received so many
1268 * packets that the buffer overflowed. Try again
1269 * immediately because there's almost certainly a packet
1270 * waiting for us. */
1271 report_loss(dpif_, ch);
1275 ch->last_poll = time_msec();
1277 if (error == EAGAIN) {
1283 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1284 if (!error && dp_ifindex == dpif->dp_ifindex) {
1296 dpif_linux_recv_wait(struct dpif *dpif_)
1298 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1300 if (dpif->epoll_fd < 0) {
1304 poll_fd_wait(dpif->epoll_fd, POLLIN);
1308 dpif_linux_recv_purge(struct dpif *dpif_)
1310 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1311 struct dpif_channel *ch;
1313 if (dpif->epoll_fd < 0) {
1317 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1319 nl_sock_drain(ch->sock);
1324 const struct dpif_class dpif_linux_class = {
1326 dpif_linux_enumerate,
1333 dpif_linux_get_stats,
1334 dpif_linux_port_add,
1335 dpif_linux_port_del,
1336 dpif_linux_port_query_by_number,
1337 dpif_linux_port_query_by_name,
1338 dpif_linux_get_max_ports,
1339 dpif_linux_port_get_pid,
1340 dpif_linux_port_dump_start,
1341 dpif_linux_port_dump_next,
1342 dpif_linux_port_dump_done,
1343 dpif_linux_port_poll,
1344 dpif_linux_port_poll_wait,
1345 dpif_linux_flow_get,
1346 dpif_linux_flow_put,
1347 dpif_linux_flow_del,
1348 dpif_linux_flow_flush,
1349 dpif_linux_flow_dump_start,
1350 dpif_linux_flow_dump_next,
1351 dpif_linux_flow_dump_done,
1354 dpif_linux_recv_set,
1355 dpif_linux_queue_to_priority,
1357 dpif_linux_recv_wait,
1358 dpif_linux_recv_purge,
1362 dpif_linux_init(void)
1364 static int error = -1;
1367 unsigned int ovs_vport_mcgroup;
1369 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1370 &ovs_datapath_family);
1372 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1373 "The Open vSwitch kernel module is probably not loaded.",
1374 OVS_DATAPATH_FAMILY);
1377 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1380 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1383 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1384 &ovs_packet_family);
1387 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1390 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1392 OVS_VPORT_MCGROUP_FALLBACK_ID);
1395 static struct dpif_linux_vport vport;
1396 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1397 dpif_linux_nln_parse, &vport);
1405 dpif_linux_is_internal_device(const char *name)
1407 struct dpif_linux_vport reply;
1411 error = dpif_linux_vport_get(name, &reply, &buf);
1414 } else if (error != ENODEV && error != ENOENT) {
1415 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1416 name, strerror(error));
1419 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1423 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1425 struct dpif_linux_vport *vport = vport_;
1426 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1430 dpif_linux_port_changed(const void *vport_, void *dpif_)
1432 const struct dpif_linux_vport *vport = vport_;
1433 struct dpif_linux *dpif = dpif_;
1436 if (vport->dp_ifindex == dpif->dp_ifindex
1437 && (vport->cmd == OVS_VPORT_CMD_NEW
1438 || vport->cmd == OVS_VPORT_CMD_DEL
1439 || vport->cmd == OVS_VPORT_CMD_SET)) {
1440 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1441 dpif->dpif.full_name, vport->name, vport->cmd);
1442 sset_add(&dpif->changed_ports, vport->name);
1445 dpif->change_error = true;
1449 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1450 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1451 * positive errno value.
1453 * 'vport' will contain pointers into 'buf', so the caller should not free
1454 * 'buf' while 'vport' is still in use. */
1456 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1457 const struct ofpbuf *buf)
1459 static const struct nl_policy ovs_vport_policy[] = {
1460 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1461 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1462 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1463 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1464 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1466 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1467 .min_len = ETH_ADDR_LEN,
1468 .max_len = ETH_ADDR_LEN,
1470 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1473 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1474 struct ovs_header *ovs_header;
1475 struct nlmsghdr *nlmsg;
1476 struct genlmsghdr *genl;
1479 dpif_linux_vport_init(vport);
1481 ofpbuf_use_const(&b, buf->data, buf->size);
1482 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1483 genl = ofpbuf_try_pull(&b, sizeof *genl);
1484 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1485 if (!nlmsg || !genl || !ovs_header
1486 || nlmsg->nlmsg_type != ovs_vport_family
1487 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1488 ARRAY_SIZE(ovs_vport_policy))) {
1492 vport->cmd = genl->cmd;
1493 vport->dp_ifindex = ovs_header->dp_ifindex;
1494 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1495 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1496 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1497 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1498 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1500 if (a[OVS_VPORT_ATTR_STATS]) {
1501 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1503 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1504 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1506 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1507 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1508 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1513 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1514 * followed by Netlink attributes corresponding to 'vport'. */
1516 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1519 struct ovs_header *ovs_header;
1521 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1522 vport->cmd, OVS_VPORT_VERSION);
1524 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1525 ovs_header->dp_ifindex = vport->dp_ifindex;
1527 if (vport->port_no != UINT32_MAX) {
1528 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1531 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1532 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1536 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1539 if (vport->upcall_pid) {
1540 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1544 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1545 vport->stats, sizeof *vport->stats);
1548 if (vport->address) {
1549 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1550 vport->address, ETH_ADDR_LEN);
1553 if (vport->options) {
1554 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1555 vport->options, vport->options_len);
1559 /* Clears 'vport' to "empty" values. */
1561 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1563 memset(vport, 0, sizeof *vport);
1564 vport->port_no = UINT32_MAX;
1567 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1568 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1569 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1570 * result of the command is expected to be an ovs_vport also, which is decoded
1571 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1572 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1574 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1575 struct dpif_linux_vport *reply,
1576 struct ofpbuf **bufp)
1578 struct ofpbuf *request_buf;
1581 ovs_assert((reply != NULL) == (bufp != NULL));
1583 error = dpif_linux_init();
1587 dpif_linux_vport_init(reply);
1592 request_buf = ofpbuf_new(1024);
1593 dpif_linux_vport_to_ofpbuf(request, request_buf);
1594 error = nl_sock_transact(genl_sock, request_buf, bufp);
1595 ofpbuf_delete(request_buf);
1599 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1602 dpif_linux_vport_init(reply);
1603 ofpbuf_delete(*bufp);
1610 /* Obtains information about the kernel vport named 'name' and stores it into
1611 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1612 * longer needed ('reply' will contain pointers into '*bufp'). */
1614 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1615 struct ofpbuf **bufp)
1617 struct dpif_linux_vport request;
1619 dpif_linux_vport_init(&request);
1620 request.cmd = OVS_VPORT_CMD_GET;
1621 request.name = name;
1623 return dpif_linux_vport_transact(&request, reply, bufp);
1626 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1627 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1628 * positive errno value.
1630 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1631 * while 'dp' is still in use. */
1633 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1635 static const struct nl_policy ovs_datapath_policy[] = {
1636 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1637 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1641 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1642 struct ovs_header *ovs_header;
1643 struct nlmsghdr *nlmsg;
1644 struct genlmsghdr *genl;
1647 dpif_linux_dp_init(dp);
1649 ofpbuf_use_const(&b, buf->data, buf->size);
1650 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1651 genl = ofpbuf_try_pull(&b, sizeof *genl);
1652 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1653 if (!nlmsg || !genl || !ovs_header
1654 || nlmsg->nlmsg_type != ovs_datapath_family
1655 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1656 ARRAY_SIZE(ovs_datapath_policy))) {
1660 dp->cmd = genl->cmd;
1661 dp->dp_ifindex = ovs_header->dp_ifindex;
1662 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1663 if (a[OVS_DP_ATTR_STATS]) {
1664 /* Can't use structure assignment because Netlink doesn't ensure
1665 * sufficient alignment for 64-bit members. */
1666 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1673 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1675 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1677 struct ovs_header *ovs_header;
1679 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1680 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1681 OVS_DATAPATH_VERSION);
1683 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1684 ovs_header->dp_ifindex = dp->dp_ifindex;
1687 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1690 if (dp->upcall_pid) {
1691 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1694 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1697 /* Clears 'dp' to "empty" values. */
1699 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1701 memset(dp, 0, sizeof *dp);
1705 dpif_linux_dp_dump_start(struct nl_dump *dump)
1707 struct dpif_linux_dp request;
1710 dpif_linux_dp_init(&request);
1711 request.cmd = OVS_DP_CMD_GET;
1713 buf = ofpbuf_new(1024);
1714 dpif_linux_dp_to_ofpbuf(&request, buf);
1715 nl_dump_start(dump, genl_sock, buf);
1719 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1720 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1721 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1722 * result of the command is expected to be of the same form, which is decoded
1723 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1724 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1726 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1727 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1729 struct ofpbuf *request_buf;
1732 ovs_assert((reply != NULL) == (bufp != NULL));
1734 request_buf = ofpbuf_new(1024);
1735 dpif_linux_dp_to_ofpbuf(request, request_buf);
1736 error = nl_sock_transact(genl_sock, request_buf, bufp);
1737 ofpbuf_delete(request_buf);
1741 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1744 dpif_linux_dp_init(reply);
1745 ofpbuf_delete(*bufp);
1752 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1753 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1754 * will contain pointers into '*bufp'). */
1756 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1757 struct ofpbuf **bufp)
1759 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1760 struct dpif_linux_dp request;
1762 dpif_linux_dp_init(&request);
1763 request.cmd = OVS_DP_CMD_GET;
1764 request.dp_ifindex = dpif->dp_ifindex;
1766 return dpif_linux_dp_transact(&request, reply, bufp);
1769 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1770 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1771 * positive errno value.
1773 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1774 * while 'flow' is still in use. */
1776 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1777 const struct ofpbuf *buf)
1779 static const struct nl_policy ovs_flow_policy[] = {
1780 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1781 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1782 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1784 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1785 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1786 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1789 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1790 struct ovs_header *ovs_header;
1791 struct nlmsghdr *nlmsg;
1792 struct genlmsghdr *genl;
1795 dpif_linux_flow_init(flow);
1797 ofpbuf_use_const(&b, buf->data, buf->size);
1798 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1799 genl = ofpbuf_try_pull(&b, sizeof *genl);
1800 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1801 if (!nlmsg || !genl || !ovs_header
1802 || nlmsg->nlmsg_type != ovs_flow_family
1803 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1804 ARRAY_SIZE(ovs_flow_policy))) {
1808 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1809 flow->dp_ifindex = ovs_header->dp_ifindex;
1810 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1811 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1812 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1813 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1814 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1816 if (a[OVS_FLOW_ATTR_STATS]) {
1817 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1819 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1820 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1822 if (a[OVS_FLOW_ATTR_USED]) {
1823 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1828 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1829 * followed by Netlink attributes corresponding to 'flow'. */
1831 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1834 struct ovs_header *ovs_header;
1836 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1837 NLM_F_REQUEST | flow->nlmsg_flags,
1838 flow->cmd, OVS_FLOW_VERSION);
1840 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1841 ovs_header->dp_ifindex = flow->dp_ifindex;
1843 if (flow->key_len) {
1844 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1847 if (flow->actions || flow->actions_len) {
1848 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1849 flow->actions, flow->actions_len);
1852 /* We never need to send these to the kernel. */
1853 ovs_assert(!flow->stats);
1854 ovs_assert(!flow->tcp_flags);
1855 ovs_assert(!flow->used);
1858 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1862 /* Clears 'flow' to "empty" values. */
1864 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1866 memset(flow, 0, sizeof *flow);
1869 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1870 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1871 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1872 * result of the command is expected to be a flow also, which is decoded and
1873 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1874 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1876 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1877 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1879 struct ofpbuf *request_buf;
1882 ovs_assert((reply != NULL) == (bufp != NULL));
1885 request->nlmsg_flags |= NLM_F_ECHO;
1888 request_buf = ofpbuf_new(1024);
1889 dpif_linux_flow_to_ofpbuf(request, request_buf);
1890 error = nl_sock_transact(genl_sock, request_buf, bufp);
1891 ofpbuf_delete(request_buf);
1895 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1898 dpif_linux_flow_init(reply);
1899 ofpbuf_delete(*bufp);
1907 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1908 struct dpif_flow_stats *stats)
1911 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1912 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1914 stats->n_packets = 0;
1917 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1918 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1921 /* Logs information about a packet that was recently lost in 'ch' (in
1924 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1926 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1927 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1930 if (VLOG_DROP_ERR(&rl)) {
1935 if (ch->last_poll != LLONG_MIN) {
1936 ds_put_format(&s, " (last polled %lld ms ago)",
1937 time_msec() - ch->last_poll);
1940 VLOG_WARN("%s: lost packet on channel %td%s",
1941 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));