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"
27 #include <linux/types.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
34 #include <sys/epoll.h>
39 #include "dpif-provider.h"
40 #include "dynamic-string.h"
43 #include "netdev-linux.h"
44 #include "netdev-vport.h"
45 #include "netlink-notifier.h"
46 #include "netlink-socket.h"
50 #include "openvswitch/datapath-compat.h"
51 #include "openvswitch/tunnel.h"
53 #include "poll-loop.h"
58 #include "unaligned.h"
62 VLOG_DEFINE_THIS_MODULE(dpif_linux);
63 enum { MAX_PORTS = USHRT_MAX };
65 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
66 * missing if we have old headers. */
67 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
69 struct dpif_linux_dp {
70 /* Generic Netlink header. */
73 /* struct ovs_header. */
77 const char *name; /* OVS_DP_ATTR_NAME. */
78 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
79 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
82 static void dpif_linux_dp_init(struct dpif_linux_dp *);
83 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
84 const struct ofpbuf *);
85 static void dpif_linux_dp_dump_start(struct nl_dump *);
86 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
87 struct dpif_linux_dp *reply,
88 struct ofpbuf **bufp);
89 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
90 struct ofpbuf **bufp);
92 struct dpif_linux_flow {
93 /* Generic Netlink header. */
96 /* struct ovs_header. */
97 unsigned int nlmsg_flags;
102 * The 'stats' member points to 64-bit data that might only be aligned on
103 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
106 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
107 * the Netlink version of the command, even if actions_len is zero. */
108 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
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 dpif->channels[port_no].sock = sock;
343 dpif->channels[port_no].last_poll = LLONG_MIN;
349 del_channel(struct dpif_linux *dpif, uint32_t port_no)
351 struct dpif_channel *ch;
353 if (dpif->epoll_fd < 0 || port_no >= dpif->uc_array_size) {
357 ch = &dpif->channels[port_no];
362 epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
364 nl_sock_destroy(ch->sock);
369 dpif_linux_close(struct dpif *dpif_)
371 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
373 nln_notifier_destroy(dpif->port_notifier);
374 destroy_channels(dpif);
375 sset_destroy(&dpif->changed_ports);
380 dpif_linux_destroy(struct dpif *dpif_)
382 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
383 struct dpif_linux_dp dp;
385 dpif_linux_dp_init(&dp);
386 dp.cmd = OVS_DP_CMD_DEL;
387 dp.dp_ifindex = dpif->dp_ifindex;
388 return dpif_linux_dp_transact(&dp, NULL, NULL);
392 dpif_linux_run(struct dpif *dpif_ OVS_UNUSED)
400 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
408 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
410 struct dpif_linux_dp dp;
414 error = dpif_linux_dp_get(dpif_, &dp, &buf);
416 stats->n_hit = dp.stats.n_hit;
417 stats->n_missed = dp.stats.n_missed;
418 stats->n_lost = dp.stats.n_lost;
419 stats->n_flows = dp.stats.n_flows;
426 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
429 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
430 const char *name = netdev_get_name(netdev);
431 const char *type = netdev_get_type(netdev);
432 struct dpif_linux_vport request, reply;
433 const struct ofpbuf *options;
434 struct nl_sock *sock = NULL;
439 if (dpif->epoll_fd >= 0) {
440 error = nl_sock_create(NETLINK_GENERIC, &sock);
446 dpif_linux_vport_init(&request);
447 request.cmd = OVS_VPORT_CMD_NEW;
448 request.dp_ifindex = dpif->dp_ifindex;
449 request.type = netdev_vport_get_vport_type(netdev);
450 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
451 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
452 "unsupported type `%s'",
453 dpif_name(dpif_), name, type);
454 nl_sock_destroy(sock);
459 options = netdev_vport_get_options(netdev);
460 if (options && options->size) {
461 request.options = options->data;
462 request.options_len = options->size;
465 if (request.type == OVS_VPORT_TYPE_NETDEV) {
466 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
469 request.port_no = *port_nop;
470 upcall_pid = sock ? nl_sock_pid(sock) : 0;
471 request.upcall_pid = &upcall_pid;
473 error = dpif_linux_vport_transact(&request, &reply, &buf);
475 *port_nop = reply.port_no;
476 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
477 dpif_name(dpif_), reply.port_no, upcall_pid);
478 } else if (error == EBUSY && *port_nop != UINT32_MAX) {
479 VLOG_INFO("%s: requested port %"PRIu32" is in use",
480 dpif_name(dpif_), *port_nop);
481 nl_sock_destroy(sock);
488 error = add_channel(dpif, *port_nop, sock);
490 VLOG_INFO("%s: could not add channel for port %s",
491 dpif_name(dpif_), name);
493 /* Delete the port. */
494 dpif_linux_vport_init(&request);
495 request.cmd = OVS_VPORT_CMD_DEL;
496 request.dp_ifindex = dpif->dp_ifindex;
497 request.port_no = *port_nop;
498 dpif_linux_vport_transact(&request, NULL, NULL);
500 nl_sock_destroy(sock);
509 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
511 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
512 struct dpif_linux_vport vport;
515 dpif_linux_vport_init(&vport);
516 vport.cmd = OVS_VPORT_CMD_DEL;
517 vport.dp_ifindex = dpif->dp_ifindex;
518 vport.port_no = port_no;
519 error = dpif_linux_vport_transact(&vport, NULL, NULL);
521 del_channel(dpif, port_no);
527 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
528 const char *port_name, struct dpif_port *dpif_port)
530 struct dpif_linux_vport request;
531 struct dpif_linux_vport reply;
535 dpif_linux_vport_init(&request);
536 request.cmd = OVS_VPORT_CMD_GET;
537 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
538 request.port_no = port_no;
539 request.name = port_name;
541 error = dpif_linux_vport_transact(&request, &reply, &buf);
543 if (reply.dp_ifindex != request.dp_ifindex) {
544 /* A query by name reported that 'port_name' is in some datapath
545 * other than 'dpif', but the caller wants to know about 'dpif'. */
547 } else if (dpif_port) {
548 dpif_port->name = xstrdup(reply.name);
549 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
550 dpif_port->port_no = reply.port_no;
558 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
559 struct dpif_port *dpif_port)
561 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
565 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
566 struct dpif_port *dpif_port)
568 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
572 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
578 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
580 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
582 if (dpif->epoll_fd < 0) {
585 /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
586 * channel, since it is not heavily loaded. */
587 int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
588 return nl_sock_pid(dpif->channels[idx].sock);
593 dpif_linux_flow_flush(struct dpif *dpif_)
595 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
596 struct dpif_linux_flow flow;
598 dpif_linux_flow_init(&flow);
599 flow.cmd = OVS_FLOW_CMD_DEL;
600 flow.dp_ifindex = dpif->dp_ifindex;
601 return dpif_linux_flow_transact(&flow, NULL, NULL);
604 struct dpif_linux_port_state {
609 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
611 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
612 struct dpif_linux_port_state *state;
613 struct dpif_linux_vport request;
616 *statep = state = xmalloc(sizeof *state);
618 dpif_linux_vport_init(&request);
619 request.cmd = OVS_DP_CMD_GET;
620 request.dp_ifindex = dpif->dp_ifindex;
622 buf = ofpbuf_new(1024);
623 dpif_linux_vport_to_ofpbuf(&request, buf);
624 nl_dump_start(&state->dump, genl_sock, buf);
631 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
632 struct dpif_port *dpif_port)
634 struct dpif_linux_port_state *state = state_;
635 struct dpif_linux_vport vport;
639 if (!nl_dump_next(&state->dump, &buf)) {
643 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
648 dpif_port->name = CONST_CAST(char *, vport.name);
649 dpif_port->type = CONST_CAST(char *, netdev_vport_get_netdev_type(&vport));
650 dpif_port->port_no = vport.port_no;
655 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
657 struct dpif_linux_port_state *state = state_;
658 int error = nl_dump_done(&state->dump);
665 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
667 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
669 if (dpif->change_error) {
670 dpif->change_error = false;
671 sset_clear(&dpif->changed_ports);
673 } else if (!sset_is_empty(&dpif->changed_ports)) {
674 *devnamep = sset_pop(&dpif->changed_ports);
682 dpif_linux_port_poll_wait(const struct dpif *dpif_)
684 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
685 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
686 poll_immediate_wake();
691 dpif_linux_flow_get__(const struct dpif *dpif_,
692 const struct nlattr *key, size_t key_len,
693 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
695 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
696 struct dpif_linux_flow request;
698 dpif_linux_flow_init(&request);
699 request.cmd = OVS_FLOW_CMD_GET;
700 request.dp_ifindex = dpif->dp_ifindex;
702 request.key_len = key_len;
703 return dpif_linux_flow_transact(&request, reply, bufp);
707 dpif_linux_flow_get(const struct dpif *dpif_,
708 const struct nlattr *key, size_t key_len,
709 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
711 struct dpif_linux_flow reply;
715 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
718 dpif_linux_flow_get_stats(&reply, stats);
721 buf->data = CONST_CAST(struct nlattr *, reply.actions);
722 buf->size = reply.actions_len;
732 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
733 struct dpif_linux_flow *request)
735 static struct nlattr dummy_action;
737 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
739 dpif_linux_flow_init(request);
740 request->cmd = (put->flags & DPIF_FP_CREATE
741 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
742 request->dp_ifindex = dpif->dp_ifindex;
743 request->key = put->key;
744 request->key_len = put->key_len;
745 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
746 request->actions = put->actions ? put->actions : &dummy_action;
747 request->actions_len = put->actions_len;
748 if (put->flags & DPIF_FP_ZERO_STATS) {
749 request->clear = true;
751 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
755 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
757 struct dpif_linux_flow request, reply;
761 dpif_linux_init_flow_put(dpif_, put, &request);
762 error = dpif_linux_flow_transact(&request,
763 put->stats ? &reply : NULL,
764 put->stats ? &buf : NULL);
765 if (!error && put->stats) {
766 dpif_linux_flow_get_stats(&reply, put->stats);
773 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
774 struct dpif_linux_flow *request)
776 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
778 dpif_linux_flow_init(request);
779 request->cmd = OVS_FLOW_CMD_DEL;
780 request->dp_ifindex = dpif->dp_ifindex;
781 request->key = del->key;
782 request->key_len = del->key_len;
786 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
788 struct dpif_linux_flow request, reply;
792 dpif_linux_init_flow_del(dpif_, del, &request);
793 error = dpif_linux_flow_transact(&request,
794 del->stats ? &reply : NULL,
795 del->stats ? &buf : NULL);
796 if (!error && del->stats) {
797 dpif_linux_flow_get_stats(&reply, del->stats);
803 struct dpif_linux_flow_state {
805 struct dpif_linux_flow flow;
806 struct dpif_flow_stats stats;
811 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
813 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
814 struct dpif_linux_flow_state *state;
815 struct dpif_linux_flow request;
818 *statep = state = xmalloc(sizeof *state);
820 dpif_linux_flow_init(&request);
821 request.cmd = OVS_DP_CMD_GET;
822 request.dp_ifindex = dpif->dp_ifindex;
824 buf = ofpbuf_new(1024);
825 dpif_linux_flow_to_ofpbuf(&request, buf);
826 nl_dump_start(&state->dump, genl_sock, buf);
835 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
836 const struct nlattr **key, size_t *key_len,
837 const struct nlattr **actions, size_t *actions_len,
838 const struct dpif_flow_stats **stats)
840 struct dpif_linux_flow_state *state = state_;
845 ofpbuf_delete(state->buf);
848 if (!nl_dump_next(&state->dump, &buf)) {
852 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
857 if (actions && !state->flow.actions) {
858 error = dpif_linux_flow_get__(dpif_, state->flow.key,
860 &state->flow, &state->buf);
861 if (error == ENOENT) {
862 VLOG_DBG("dumped flow disappeared on get");
864 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
870 *actions = state->flow.actions;
871 *actions_len = state->flow.actions_len;
874 *key = state->flow.key;
875 *key_len = state->flow.key_len;
878 dpif_linux_flow_get_stats(&state->flow, &state->stats);
879 *stats = &state->stats;
885 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
887 struct dpif_linux_flow_state *state = state_;
888 int error = nl_dump_done(&state->dump);
889 ofpbuf_delete(state->buf);
895 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
898 struct ovs_header *k_exec;
900 ofpbuf_prealloc_tailroom(buf, (64
901 + d_exec->packet->size
903 + d_exec->actions_len));
905 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
906 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
908 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
909 k_exec->dp_ifindex = dp_ifindex;
911 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
912 d_exec->packet->data, d_exec->packet->size);
913 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
914 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
915 d_exec->actions, d_exec->actions_len);
919 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
921 uint64_t request_stub[1024 / 8];
922 struct ofpbuf request;
925 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
926 dpif_linux_encode_execute(dp_ifindex, execute, &request);
927 error = nl_sock_transact(genl_sock, &request, NULL);
928 ofpbuf_uninit(&request);
934 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
936 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
938 return dpif_linux_execute__(dpif->dp_ifindex, execute);
944 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
946 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
949 struct nl_transaction txn;
951 struct ofpbuf request;
952 uint64_t request_stub[1024 / 8];
955 uint64_t reply_stub[1024 / 8];
958 struct nl_transaction *txnsp[MAX_OPS];
961 assert(n_ops <= MAX_OPS);
962 for (i = 0; i < n_ops; i++) {
963 struct op_auxdata *aux = &auxes[i];
964 struct dpif_op *op = ops[i];
965 struct dpif_flow_put *put;
966 struct dpif_flow_del *del;
967 struct dpif_execute *execute;
968 struct dpif_linux_flow flow;
970 ofpbuf_use_stub(&aux->request,
971 aux->request_stub, sizeof aux->request_stub);
972 aux->txn.request = &aux->request;
974 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
975 aux->txn.reply = NULL;
978 case DPIF_OP_FLOW_PUT:
979 put = &op->u.flow_put;
980 dpif_linux_init_flow_put(dpif_, put, &flow);
982 flow.nlmsg_flags |= NLM_F_ECHO;
983 aux->txn.reply = &aux->reply;
985 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
988 case DPIF_OP_FLOW_DEL:
989 del = &op->u.flow_del;
990 dpif_linux_init_flow_del(dpif_, del, &flow);
992 flow.nlmsg_flags |= NLM_F_ECHO;
993 aux->txn.reply = &aux->reply;
995 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
998 case DPIF_OP_EXECUTE:
999 execute = &op->u.execute;
1000 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1009 for (i = 0; i < n_ops; i++) {
1010 txnsp[i] = &auxes[i].txn;
1012 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1014 for (i = 0; i < n_ops; i++) {
1015 struct op_auxdata *aux = &auxes[i];
1016 struct nl_transaction *txn = &auxes[i].txn;
1017 struct dpif_op *op = ops[i];
1018 struct dpif_flow_put *put;
1019 struct dpif_flow_del *del;
1021 op->error = txn->error;
1024 case DPIF_OP_FLOW_PUT:
1025 put = &op->u.flow_put;
1028 struct dpif_linux_flow reply;
1030 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1033 dpif_linux_flow_get_stats(&reply, put->stats);
1038 memset(put->stats, 0, sizeof *put->stats);
1043 case DPIF_OP_FLOW_DEL:
1044 del = &op->u.flow_del;
1047 struct dpif_linux_flow reply;
1049 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1052 dpif_linux_flow_get_stats(&reply, del->stats);
1057 memset(del->stats, 0, sizeof *del->stats);
1062 case DPIF_OP_EXECUTE:
1069 ofpbuf_uninit(&aux->request);
1070 ofpbuf_uninit(&aux->reply);
1075 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1078 size_t chunk = MIN(n_ops, MAX_OPS);
1079 dpif_linux_operate__(dpif, ops, chunk);
1086 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1088 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1090 if ((dpif->epoll_fd >= 0) == enable) {
1095 destroy_channels(dpif);
1097 struct dpif_port_dump port_dump;
1098 struct dpif_port port;
1100 dpif->epoll_fd = epoll_create(10);
1101 if (dpif->epoll_fd < 0) {
1105 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1106 struct dpif_linux_vport vport_request;
1107 struct nl_sock *sock;
1108 uint32_t upcall_pid;
1111 error = nl_sock_create(NETLINK_GENERIC, &sock);
1116 upcall_pid = nl_sock_pid(sock);
1118 dpif_linux_vport_init(&vport_request);
1119 vport_request.cmd = OVS_VPORT_CMD_SET;
1120 vport_request.dp_ifindex = dpif->dp_ifindex;
1121 vport_request.port_no = port.port_no;
1122 vport_request.upcall_pid = &upcall_pid;
1123 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1125 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1126 dpif_name(&dpif->dpif), vport_request.port_no,
1129 VLOG_WARN_RL(&error_rl,
1130 "%s: failed to set upcall pid on port: %s",
1131 dpif_name(&dpif->dpif), strerror(error));
1132 nl_sock_destroy(sock);
1134 if (error == ENODEV || error == ENOENT) {
1135 /* This device isn't there, but keep trying the others. */
1142 error = add_channel(dpif, port.port_no, sock);
1144 VLOG_INFO("%s: could not add channel for port %s",
1145 dpif_name(dpif_), port.name);
1146 nl_sock_destroy(sock);
1156 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1157 uint32_t queue_id, uint32_t *priority)
1159 if (queue_id < 0xf000) {
1160 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1168 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1171 static const struct nl_policy ovs_packet_policy[] = {
1172 /* Always present. */
1173 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1174 .min_len = ETH_HEADER_LEN },
1175 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1177 /* OVS_PACKET_CMD_ACTION only. */
1178 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1181 struct ovs_header *ovs_header;
1182 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1183 struct nlmsghdr *nlmsg;
1184 struct genlmsghdr *genl;
1188 ofpbuf_use_const(&b, buf->data, buf->size);
1190 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1191 genl = ofpbuf_try_pull(&b, sizeof *genl);
1192 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1193 if (!nlmsg || !genl || !ovs_header
1194 || nlmsg->nlmsg_type != ovs_packet_family
1195 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1196 ARRAY_SIZE(ovs_packet_policy))) {
1200 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1201 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1207 memset(upcall, 0, sizeof *upcall);
1208 upcall->type = type;
1209 upcall->packet = buf;
1210 upcall->packet->data = CONST_CAST(struct nlattr *,
1211 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1212 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1213 upcall->key = CONST_CAST(struct nlattr *,
1214 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1215 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1216 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1217 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1219 *dp_ifindex = ovs_header->dp_ifindex;
1225 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1228 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1231 if (dpif->epoll_fd < 0) {
1235 if (dpif->event_offset >= dpif->n_events) {
1238 dpif->event_offset = dpif->n_events = 0;
1241 retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1242 dpif->uc_array_size, 0);
1243 } while (retval < 0 && errno == EINTR);
1245 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1246 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1247 } else if (retval > 0) {
1248 dpif->n_events = retval;
1252 while (dpif->event_offset < dpif->n_events) {
1253 int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1254 struct dpif_channel *ch = &dpif->channels[idx];
1256 dpif->event_offset++;
1262 if (++read_tries > 50) {
1266 error = nl_sock_recv(ch->sock, buf, false);
1267 if (error == ENOBUFS) {
1268 /* ENOBUFS typically means that we've received so many
1269 * packets that the buffer overflowed. Try again
1270 * immediately because there's almost certainly a packet
1271 * waiting for us. */
1272 report_loss(dpif_, ch);
1276 ch->last_poll = time_msec();
1278 if (error == EAGAIN) {
1284 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1285 if (!error && dp_ifindex == dpif->dp_ifindex) {
1297 dpif_linux_recv_wait(struct dpif *dpif_)
1299 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1301 if (dpif->epoll_fd < 0) {
1305 poll_fd_wait(dpif->epoll_fd, POLLIN);
1309 dpif_linux_recv_purge(struct dpif *dpif_)
1311 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1312 struct dpif_channel *ch;
1314 if (dpif->epoll_fd < 0) {
1318 for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1320 nl_sock_drain(ch->sock);
1325 const struct dpif_class dpif_linux_class = {
1327 dpif_linux_enumerate,
1334 dpif_linux_get_stats,
1335 dpif_linux_port_add,
1336 dpif_linux_port_del,
1337 dpif_linux_port_query_by_number,
1338 dpif_linux_port_query_by_name,
1339 dpif_linux_get_max_ports,
1340 dpif_linux_port_get_pid,
1341 dpif_linux_port_dump_start,
1342 dpif_linux_port_dump_next,
1343 dpif_linux_port_dump_done,
1344 dpif_linux_port_poll,
1345 dpif_linux_port_poll_wait,
1346 dpif_linux_flow_get,
1347 dpif_linux_flow_put,
1348 dpif_linux_flow_del,
1349 dpif_linux_flow_flush,
1350 dpif_linux_flow_dump_start,
1351 dpif_linux_flow_dump_next,
1352 dpif_linux_flow_dump_done,
1355 dpif_linux_recv_set,
1356 dpif_linux_queue_to_priority,
1358 dpif_linux_recv_wait,
1359 dpif_linux_recv_purge,
1363 dpif_linux_init(void)
1365 static int error = -1;
1368 unsigned int ovs_vport_mcgroup;
1370 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1371 &ovs_datapath_family);
1373 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1374 "The Open vSwitch kernel module is probably not loaded.",
1375 OVS_DATAPATH_FAMILY);
1378 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1381 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1384 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1385 &ovs_packet_family);
1388 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1391 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1393 OVS_VPORT_MCGROUP_FALLBACK_ID);
1396 static struct dpif_linux_vport vport;
1397 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1398 dpif_linux_nln_parse, &vport);
1406 dpif_linux_is_internal_device(const char *name)
1408 struct dpif_linux_vport reply;
1412 error = dpif_linux_vport_get(name, &reply, &buf);
1415 } else if (error != ENODEV && error != ENOENT) {
1416 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1417 name, strerror(error));
1420 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1424 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1426 struct dpif_linux_vport *vport = vport_;
1427 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1431 dpif_linux_port_changed(const void *vport_, void *dpif_)
1433 const struct dpif_linux_vport *vport = vport_;
1434 struct dpif_linux *dpif = dpif_;
1437 if (vport->dp_ifindex == dpif->dp_ifindex
1438 && (vport->cmd == OVS_VPORT_CMD_NEW
1439 || vport->cmd == OVS_VPORT_CMD_DEL
1440 || vport->cmd == OVS_VPORT_CMD_SET)) {
1441 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1442 dpif->dpif.full_name, vport->name, vport->cmd);
1443 sset_add(&dpif->changed_ports, vport->name);
1446 dpif->change_error = true;
1450 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1451 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1452 * positive errno value.
1454 * 'vport' will contain pointers into 'buf', so the caller should not free
1455 * 'buf' while 'vport' is still in use. */
1457 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1458 const struct ofpbuf *buf)
1460 static const struct nl_policy ovs_vport_policy[] = {
1461 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1462 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1463 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1464 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1465 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1467 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1468 .min_len = ETH_ADDR_LEN,
1469 .max_len = ETH_ADDR_LEN,
1471 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1474 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1475 struct ovs_header *ovs_header;
1476 struct nlmsghdr *nlmsg;
1477 struct genlmsghdr *genl;
1480 dpif_linux_vport_init(vport);
1482 ofpbuf_use_const(&b, buf->data, buf->size);
1483 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1484 genl = ofpbuf_try_pull(&b, sizeof *genl);
1485 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1486 if (!nlmsg || !genl || !ovs_header
1487 || nlmsg->nlmsg_type != ovs_vport_family
1488 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1489 ARRAY_SIZE(ovs_vport_policy))) {
1493 vport->cmd = genl->cmd;
1494 vport->dp_ifindex = ovs_header->dp_ifindex;
1495 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1496 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1497 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1498 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1499 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1501 if (a[OVS_VPORT_ATTR_STATS]) {
1502 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1504 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1505 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1507 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1508 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1509 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1514 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1515 * followed by Netlink attributes corresponding to 'vport'. */
1517 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1520 struct ovs_header *ovs_header;
1522 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1523 vport->cmd, OVS_VPORT_VERSION);
1525 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1526 ovs_header->dp_ifindex = vport->dp_ifindex;
1528 if (vport->port_no != UINT32_MAX) {
1529 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1532 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1533 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1537 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1540 if (vport->upcall_pid) {
1541 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1545 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1546 vport->stats, sizeof *vport->stats);
1549 if (vport->address) {
1550 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1551 vport->address, ETH_ADDR_LEN);
1554 if (vport->options) {
1555 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1556 vport->options, vport->options_len);
1560 /* Clears 'vport' to "empty" values. */
1562 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1564 memset(vport, 0, sizeof *vport);
1565 vport->port_no = UINT32_MAX;
1568 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1569 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1570 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1571 * result of the command is expected to be an ovs_vport also, which is decoded
1572 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1573 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1575 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1576 struct dpif_linux_vport *reply,
1577 struct ofpbuf **bufp)
1579 struct ofpbuf *request_buf;
1582 assert((reply != NULL) == (bufp != NULL));
1584 error = dpif_linux_init();
1588 dpif_linux_vport_init(reply);
1593 request_buf = ofpbuf_new(1024);
1594 dpif_linux_vport_to_ofpbuf(request, request_buf);
1595 error = nl_sock_transact(genl_sock, request_buf, bufp);
1596 ofpbuf_delete(request_buf);
1600 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1603 dpif_linux_vport_init(reply);
1604 ofpbuf_delete(*bufp);
1611 /* Obtains information about the kernel vport named 'name' and stores it into
1612 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1613 * longer needed ('reply' will contain pointers into '*bufp'). */
1615 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1616 struct ofpbuf **bufp)
1618 struct dpif_linux_vport request;
1620 dpif_linux_vport_init(&request);
1621 request.cmd = OVS_VPORT_CMD_GET;
1622 request.name = name;
1624 return dpif_linux_vport_transact(&request, reply, bufp);
1627 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1628 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1629 * positive errno value.
1631 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1632 * while 'dp' is still in use. */
1634 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1636 static const struct nl_policy ovs_datapath_policy[] = {
1637 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1638 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1642 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1643 struct ovs_header *ovs_header;
1644 struct nlmsghdr *nlmsg;
1645 struct genlmsghdr *genl;
1648 dpif_linux_dp_init(dp);
1650 ofpbuf_use_const(&b, buf->data, buf->size);
1651 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1652 genl = ofpbuf_try_pull(&b, sizeof *genl);
1653 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1654 if (!nlmsg || !genl || !ovs_header
1655 || nlmsg->nlmsg_type != ovs_datapath_family
1656 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1657 ARRAY_SIZE(ovs_datapath_policy))) {
1661 dp->cmd = genl->cmd;
1662 dp->dp_ifindex = ovs_header->dp_ifindex;
1663 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1664 if (a[OVS_DP_ATTR_STATS]) {
1665 /* Can't use structure assignment because Netlink doesn't ensure
1666 * sufficient alignment for 64-bit members. */
1667 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1674 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1676 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1678 struct ovs_header *ovs_header;
1680 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1681 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1682 OVS_DATAPATH_VERSION);
1684 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1685 ovs_header->dp_ifindex = dp->dp_ifindex;
1688 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1691 if (dp->upcall_pid) {
1692 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1695 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1698 /* Clears 'dp' to "empty" values. */
1700 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1702 memset(dp, 0, sizeof *dp);
1706 dpif_linux_dp_dump_start(struct nl_dump *dump)
1708 struct dpif_linux_dp request;
1711 dpif_linux_dp_init(&request);
1712 request.cmd = OVS_DP_CMD_GET;
1714 buf = ofpbuf_new(1024);
1715 dpif_linux_dp_to_ofpbuf(&request, buf);
1716 nl_dump_start(dump, genl_sock, buf);
1720 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1721 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1722 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1723 * result of the command is expected to be of the same form, which is decoded
1724 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1725 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1727 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1728 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1730 struct ofpbuf *request_buf;
1733 assert((reply != NULL) == (bufp != NULL));
1735 request_buf = ofpbuf_new(1024);
1736 dpif_linux_dp_to_ofpbuf(request, request_buf);
1737 error = nl_sock_transact(genl_sock, request_buf, bufp);
1738 ofpbuf_delete(request_buf);
1742 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1745 dpif_linux_dp_init(reply);
1746 ofpbuf_delete(*bufp);
1753 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1754 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1755 * will contain pointers into '*bufp'). */
1757 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1758 struct ofpbuf **bufp)
1760 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1761 struct dpif_linux_dp request;
1763 dpif_linux_dp_init(&request);
1764 request.cmd = OVS_DP_CMD_GET;
1765 request.dp_ifindex = dpif->dp_ifindex;
1767 return dpif_linux_dp_transact(&request, reply, bufp);
1770 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1771 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1772 * positive errno value.
1774 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1775 * while 'flow' is still in use. */
1777 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1778 const struct ofpbuf *buf)
1780 static const struct nl_policy ovs_flow_policy[] = {
1781 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1782 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1783 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1785 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1786 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1787 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1790 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1791 struct ovs_header *ovs_header;
1792 struct nlmsghdr *nlmsg;
1793 struct genlmsghdr *genl;
1796 dpif_linux_flow_init(flow);
1798 ofpbuf_use_const(&b, buf->data, buf->size);
1799 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1800 genl = ofpbuf_try_pull(&b, sizeof *genl);
1801 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1802 if (!nlmsg || !genl || !ovs_header
1803 || nlmsg->nlmsg_type != ovs_flow_family
1804 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1805 ARRAY_SIZE(ovs_flow_policy))) {
1809 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1810 flow->dp_ifindex = ovs_header->dp_ifindex;
1811 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1812 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1813 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1814 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1815 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1817 if (a[OVS_FLOW_ATTR_STATS]) {
1818 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1820 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1821 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1823 if (a[OVS_FLOW_ATTR_USED]) {
1824 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1829 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1830 * followed by Netlink attributes corresponding to 'flow'. */
1832 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1835 struct ovs_header *ovs_header;
1837 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1838 NLM_F_REQUEST | flow->nlmsg_flags,
1839 flow->cmd, OVS_FLOW_VERSION);
1841 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1842 ovs_header->dp_ifindex = flow->dp_ifindex;
1844 if (flow->key_len) {
1845 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1848 if (flow->actions || flow->actions_len) {
1849 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1850 flow->actions, flow->actions_len);
1853 /* We never need to send these to the kernel. */
1854 assert(!flow->stats);
1855 assert(!flow->tcp_flags);
1856 assert(!flow->used);
1859 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1863 /* Clears 'flow' to "empty" values. */
1865 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1867 memset(flow, 0, sizeof *flow);
1870 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1871 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1872 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1873 * result of the command is expected to be a flow also, which is decoded and
1874 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1875 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1877 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1878 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1880 struct ofpbuf *request_buf;
1883 assert((reply != NULL) == (bufp != NULL));
1886 request->nlmsg_flags |= NLM_F_ECHO;
1889 request_buf = ofpbuf_new(1024);
1890 dpif_linux_flow_to_ofpbuf(request, request_buf);
1891 error = nl_sock_transact(genl_sock, request_buf, bufp);
1892 ofpbuf_delete(request_buf);
1896 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1899 dpif_linux_flow_init(reply);
1900 ofpbuf_delete(*bufp);
1908 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1909 struct dpif_flow_stats *stats)
1912 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1913 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1915 stats->n_packets = 0;
1918 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1919 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1922 /* Logs information about a packet that was recently lost in 'ch' (in
1925 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1927 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1928 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1931 if (VLOG_DROP_ERR(&rl)) {
1936 if (ch->last_poll != LLONG_MIN) {
1937 ds_put_format(&s, " (last polled %lld ms ago)",
1938 time_msec() - ch->last_poll);
1941 VLOG_WARN("%s: lost packet on channel %td%s",
1942 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));