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 enum { N_CHANNELS = 17 };
66 BUILD_ASSERT_DECL(IS_POW2(N_CHANNELS - 1));
67 BUILD_ASSERT_DECL(N_CHANNELS > 1);
68 BUILD_ASSERT_DECL(N_CHANNELS <= 32); /* We use a 32-bit word as a mask. */
70 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
71 * missing if we have old headers. */
72 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
74 struct dpif_linux_dp {
75 /* Generic Netlink header. */
78 /* struct ovs_header. */
82 const char *name; /* OVS_DP_ATTR_NAME. */
83 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
84 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
87 static void dpif_linux_dp_init(struct dpif_linux_dp *);
88 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
89 const struct ofpbuf *);
90 static void dpif_linux_dp_dump_start(struct nl_dump *);
91 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
92 struct dpif_linux_dp *reply,
93 struct ofpbuf **bufp);
94 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
95 struct ofpbuf **bufp);
97 struct dpif_linux_flow {
98 /* Generic Netlink header. */
101 /* struct ovs_header. */
102 unsigned int nlmsg_flags;
107 * The 'stats' member points to 64-bit data that might only be aligned on
108 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
111 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
112 * the Netlink version of the command, even if actions_len is zero. */
113 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
115 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
117 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
118 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
119 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
120 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
123 static void dpif_linux_flow_init(struct dpif_linux_flow *);
124 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
125 const struct ofpbuf *);
126 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
128 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
129 struct dpif_linux_flow *reply,
130 struct ofpbuf **bufp);
131 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
132 struct dpif_flow_stats *);
134 /* Packet drop monitoring.
136 * When kernel-to-user Netlink buffers overflow, the kernel notifies us that
137 * one or more packets were dropped, but it doesn't tell us anything about
138 * those packets. However, the administrator really wants to know. So we do
139 * the next best thing, and keep track of the top sources of packets received
140 * on each kernel-to-user channel, since the top sources are those that will
141 * cause the buffers to overflow.
143 * We use a variation on the "Space-Saving" algorithm in Metwally et al.,
144 * "Efficient Computation of Frequent and Top-k Elements in Data Streams", ACM
145 * Transactions on Database Systems 31:3 (2006). This algorithm yields
146 * perfectly accurate results when the data stream's unique values (in this
147 * case, port numbers) fit into our data structure, and degrades gracefully
148 * even for challenging distributions (e.g. Zipf).
150 * Our implementation is very simple, without any of the special flourishes
151 * described in the paper. It avoids the need to use a hash for lookup by
152 * keeping the constant factor (N_SKETCHES) very small. The error calculations
153 * in the paper make it sound like the results should still be satisfactory.
155 * "space-saving" and "Metwally" seem like awkward names for data structures,
156 * so we call this a "sketch" even though technically that's a different sort
157 * of summary structure.
160 /* One of N_SKETCHES counting elements per channel in the Metwally
161 * "space-saving" algorithm. */
162 enum { N_SKETCHES = 8 }; /* Number of elements per channel. */
164 uint32_t port_no; /* Port number. */
165 unsigned int hits; /* Number of hits. */
166 unsigned int error; /* Upper bound on error in 'hits'. */
169 /* One of N_CHANNELS channels per dpif between the kernel and userspace. */
170 struct dpif_channel {
171 struct nl_sock *sock; /* Netlink socket. */
172 struct dpif_sketch sketches[N_SKETCHES]; /* From max to min 'hits'. */
173 long long int last_poll; /* Last time this channel was polled. */
176 static void update_sketch(struct dpif_channel *, uint32_t port_no);
177 static void scale_sketches(struct dpif *);
178 static void report_loss(struct dpif *, struct dpif_channel *);
180 /* Interval, in milliseconds, at which to scale down the sketch values by a
181 * factor of 2. The Metwally algorithm doesn't do this, which makes sense in
182 * the context it assumes, but in our situation we ought to weight recent data
183 * more heavily than old data, so in my opinion this is reasonable. */
184 #define SCALE_INTERVAL (60 * 1000)
186 /* Datapath interface for the openvswitch Linux kernel module. */
191 /* Upcall messages. */
192 struct dpif_channel channels[N_CHANNELS];
193 uint32_t ready_mask; /* 1-bit for each sock with unread messages. */
194 int epoll_fd; /* epoll fd that includes channel socks. */
195 long long int next_scale; /* Next time to scale down the sketches. */
197 /* Change notification. */
198 struct sset changed_ports; /* Ports that have changed. */
199 struct nln_notifier *port_notifier;
203 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
205 /* Generic Netlink family numbers for OVS. */
206 static int ovs_datapath_family;
207 static int ovs_vport_family;
208 static int ovs_flow_family;
209 static int ovs_packet_family;
211 /* Generic Netlink socket. */
212 static struct nl_sock *genl_sock;
213 static struct nln *nln = NULL;
215 static int dpif_linux_init(void);
216 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
217 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
218 static void dpif_linux_port_changed(const void *vport, void *dpif);
219 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint32_t port_no);
221 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
223 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
224 const struct ofpbuf *);
226 static struct dpif_linux *
227 dpif_linux_cast(const struct dpif *dpif)
229 dpif_assert_class(dpif, &dpif_linux_class);
230 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
234 dpif_linux_enumerate(struct sset *all_dps)
240 error = dpif_linux_init();
245 dpif_linux_dp_dump_start(&dump);
246 while (nl_dump_next(&dump, &msg)) {
247 struct dpif_linux_dp dp;
249 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
250 sset_add(all_dps, dp.name);
253 return nl_dump_done(&dump);
257 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
258 bool create, struct dpif **dpifp)
260 struct dpif_linux_dp dp_request, dp;
265 error = dpif_linux_init();
270 /* Create or look up datapath. */
271 dpif_linux_dp_init(&dp_request);
273 dp_request.cmd = OVS_DP_CMD_NEW;
275 dp_request.upcall_pid = &upcall_pid;
277 dp_request.cmd = OVS_DP_CMD_GET;
279 dp_request.name = name;
280 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
285 open_dpif(&dp, dpifp);
291 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
293 struct dpif_linux *dpif;
295 dpif = xzalloc(sizeof *dpif);
296 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
300 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
301 dp->dp_ifindex, dp->dp_ifindex);
303 dpif->next_scale = LLONG_MAX;
305 dpif->dp_ifindex = dp->dp_ifindex;
306 sset_init(&dpif->changed_ports);
307 *dpifp = &dpif->dpif;
311 destroy_channels(struct dpif_linux *dpif)
313 struct dpif_channel *ch;
315 if (dpif->epoll_fd >= 0) {
316 close(dpif->epoll_fd);
319 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
320 nl_sock_destroy(ch->sock);
323 dpif->next_scale = LLONG_MAX;
327 dpif_linux_close(struct dpif *dpif_)
329 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
331 nln_notifier_destroy(dpif->port_notifier);
332 destroy_channels(dpif);
333 sset_destroy(&dpif->changed_ports);
338 dpif_linux_destroy(struct dpif *dpif_)
340 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
341 struct dpif_linux_dp dp;
343 dpif_linux_dp_init(&dp);
344 dp.cmd = OVS_DP_CMD_DEL;
345 dp.dp_ifindex = dpif->dp_ifindex;
346 return dpif_linux_dp_transact(&dp, NULL, NULL);
350 dpif_linux_run(struct dpif *dpif_)
352 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
354 if (time_msec() >= dpif->next_scale) {
355 dpif->next_scale = time_msec() + SCALE_INTERVAL;
356 scale_sketches(dpif_);
365 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
373 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
375 struct dpif_linux_dp dp;
379 error = dpif_linux_dp_get(dpif_, &dp, &buf);
381 stats->n_hit = dp.stats.n_hit;
382 stats->n_missed = dp.stats.n_missed;
383 stats->n_lost = dp.stats.n_lost;
384 stats->n_flows = dp.stats.n_flows;
391 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
394 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
395 const char *name = netdev_get_name(netdev);
396 const char *type = netdev_get_type(netdev);
397 struct dpif_linux_vport request, reply;
398 const struct ofpbuf *options;
403 dpif_linux_vport_init(&request);
404 request.cmd = OVS_VPORT_CMD_NEW;
405 request.dp_ifindex = dpif->dp_ifindex;
406 request.type = netdev_vport_get_vport_type(netdev);
407 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
408 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
409 "unsupported type `%s'",
410 dpif_name(dpif_), name, type);
415 options = netdev_vport_get_options(netdev);
416 if (options && options->size) {
417 request.options = options->data;
418 request.options_len = options->size;
421 if (request.type == OVS_VPORT_TYPE_NETDEV) {
422 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
425 request.port_no = *port_nop;
426 upcall_pid = dpif_linux_port_get_pid(dpif_, request.port_no);
427 request.upcall_pid = &upcall_pid;
429 error = dpif_linux_vport_transact(&request, &reply, &buf);
432 *port_nop = reply.port_no;
433 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
434 dpif_name(dpif_), request.port_no, upcall_pid);
435 } else if (error == EBUSY && *port_nop != UINT32_MAX) {
436 VLOG_INFO("%s: requested port %"PRIu32" is in use",
437 dpif_name(dpif_), *port_nop);
446 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
448 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
449 struct dpif_linux_vport vport;
452 dpif_linux_vport_init(&vport);
453 vport.cmd = OVS_VPORT_CMD_DEL;
454 vport.dp_ifindex = dpif->dp_ifindex;
455 vport.port_no = port_no;
456 error = dpif_linux_vport_transact(&vport, NULL, NULL);
462 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
463 const char *port_name, struct dpif_port *dpif_port)
465 struct dpif_linux_vport request;
466 struct dpif_linux_vport reply;
470 dpif_linux_vport_init(&request);
471 request.cmd = OVS_VPORT_CMD_GET;
472 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
473 request.port_no = port_no;
474 request.name = port_name;
476 error = dpif_linux_vport_transact(&request, &reply, &buf);
478 if (reply.dp_ifindex != request.dp_ifindex) {
479 /* A query by name reported that 'port_name' is in some datapath
480 * other than 'dpif', but the caller wants to know about 'dpif'. */
482 } else if (dpif_port) {
483 dpif_port->name = xstrdup(reply.name);
484 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
485 dpif_port->port_no = reply.port_no;
493 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
494 struct dpif_port *dpif_port)
496 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
500 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
501 struct dpif_port *dpif_port)
503 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
507 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
513 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
515 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
517 if (dpif->epoll_fd < 0) {
522 idx = (port_no != UINT32_MAX
523 ? 1 + (port_no & (N_CHANNELS - 2))
525 return nl_sock_pid(dpif->channels[idx].sock);
530 dpif_linux_flow_flush(struct dpif *dpif_)
532 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
533 struct dpif_linux_flow flow;
535 dpif_linux_flow_init(&flow);
536 flow.cmd = OVS_FLOW_CMD_DEL;
537 flow.dp_ifindex = dpif->dp_ifindex;
538 return dpif_linux_flow_transact(&flow, NULL, NULL);
541 struct dpif_linux_port_state {
546 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
548 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
549 struct dpif_linux_port_state *state;
550 struct dpif_linux_vport request;
553 *statep = state = xmalloc(sizeof *state);
555 dpif_linux_vport_init(&request);
556 request.cmd = OVS_DP_CMD_GET;
557 request.dp_ifindex = dpif->dp_ifindex;
559 buf = ofpbuf_new(1024);
560 dpif_linux_vport_to_ofpbuf(&request, buf);
561 nl_dump_start(&state->dump, genl_sock, buf);
568 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
569 struct dpif_port *dpif_port)
571 struct dpif_linux_port_state *state = state_;
572 struct dpif_linux_vport vport;
576 if (!nl_dump_next(&state->dump, &buf)) {
580 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
585 dpif_port->name = CONST_CAST(char *, vport.name);
586 dpif_port->type = CONST_CAST(char *, netdev_vport_get_netdev_type(&vport));
587 dpif_port->port_no = vport.port_no;
592 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
594 struct dpif_linux_port_state *state = state_;
595 int error = nl_dump_done(&state->dump);
602 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
604 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
606 if (dpif->change_error) {
607 dpif->change_error = false;
608 sset_clear(&dpif->changed_ports);
610 } else if (!sset_is_empty(&dpif->changed_ports)) {
611 *devnamep = sset_pop(&dpif->changed_ports);
619 dpif_linux_port_poll_wait(const struct dpif *dpif_)
621 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
622 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
623 poll_immediate_wake();
628 dpif_linux_flow_get__(const struct dpif *dpif_,
629 const struct nlattr *key, size_t key_len,
630 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
632 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
633 struct dpif_linux_flow request;
635 dpif_linux_flow_init(&request);
636 request.cmd = OVS_FLOW_CMD_GET;
637 request.dp_ifindex = dpif->dp_ifindex;
639 request.key_len = key_len;
640 return dpif_linux_flow_transact(&request, reply, bufp);
644 dpif_linux_flow_get(const struct dpif *dpif_,
645 const struct nlattr *key, size_t key_len,
646 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
648 struct dpif_linux_flow reply;
652 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
655 dpif_linux_flow_get_stats(&reply, stats);
658 buf->data = CONST_CAST(struct nlattr *, reply.actions);
659 buf->size = reply.actions_len;
669 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
670 struct dpif_linux_flow *request)
672 static struct nlattr dummy_action;
674 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
676 dpif_linux_flow_init(request);
677 request->cmd = (put->flags & DPIF_FP_CREATE
678 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
679 request->dp_ifindex = dpif->dp_ifindex;
680 request->key = put->key;
681 request->key_len = put->key_len;
682 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
683 request->actions = put->actions ? put->actions : &dummy_action;
684 request->actions_len = put->actions_len;
685 if (put->flags & DPIF_FP_ZERO_STATS) {
686 request->clear = true;
688 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
692 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
694 struct dpif_linux_flow request, reply;
698 dpif_linux_init_flow_put(dpif_, put, &request);
699 error = dpif_linux_flow_transact(&request,
700 put->stats ? &reply : NULL,
701 put->stats ? &buf : NULL);
702 if (!error && put->stats) {
703 dpif_linux_flow_get_stats(&reply, put->stats);
710 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
711 struct dpif_linux_flow *request)
713 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
715 dpif_linux_flow_init(request);
716 request->cmd = OVS_FLOW_CMD_DEL;
717 request->dp_ifindex = dpif->dp_ifindex;
718 request->key = del->key;
719 request->key_len = del->key_len;
723 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
725 struct dpif_linux_flow request, reply;
729 dpif_linux_init_flow_del(dpif_, del, &request);
730 error = dpif_linux_flow_transact(&request,
731 del->stats ? &reply : NULL,
732 del->stats ? &buf : NULL);
733 if (!error && del->stats) {
734 dpif_linux_flow_get_stats(&reply, del->stats);
740 struct dpif_linux_flow_state {
742 struct dpif_linux_flow flow;
743 struct dpif_flow_stats stats;
748 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
750 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
751 struct dpif_linux_flow_state *state;
752 struct dpif_linux_flow request;
755 *statep = state = xmalloc(sizeof *state);
757 dpif_linux_flow_init(&request);
758 request.cmd = OVS_DP_CMD_GET;
759 request.dp_ifindex = dpif->dp_ifindex;
761 buf = ofpbuf_new(1024);
762 dpif_linux_flow_to_ofpbuf(&request, buf);
763 nl_dump_start(&state->dump, genl_sock, buf);
772 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
773 const struct nlattr **key, size_t *key_len,
774 const struct nlattr **actions, size_t *actions_len,
775 const struct dpif_flow_stats **stats)
777 struct dpif_linux_flow_state *state = state_;
782 ofpbuf_delete(state->buf);
785 if (!nl_dump_next(&state->dump, &buf)) {
789 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
794 if (actions && !state->flow.actions) {
795 error = dpif_linux_flow_get__(dpif_, state->flow.key,
797 &state->flow, &state->buf);
798 if (error == ENOENT) {
799 VLOG_DBG("dumped flow disappeared on get");
801 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
807 *actions = state->flow.actions;
808 *actions_len = state->flow.actions_len;
811 *key = state->flow.key;
812 *key_len = state->flow.key_len;
815 dpif_linux_flow_get_stats(&state->flow, &state->stats);
816 *stats = &state->stats;
822 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
824 struct dpif_linux_flow_state *state = state_;
825 int error = nl_dump_done(&state->dump);
826 ofpbuf_delete(state->buf);
832 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
835 struct ovs_header *k_exec;
837 ofpbuf_prealloc_tailroom(buf, (64
838 + d_exec->packet->size
840 + d_exec->actions_len));
842 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
843 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
845 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
846 k_exec->dp_ifindex = dp_ifindex;
848 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
849 d_exec->packet->data, d_exec->packet->size);
850 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
851 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
852 d_exec->actions, d_exec->actions_len);
856 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
858 uint64_t request_stub[1024 / 8];
859 struct ofpbuf request;
862 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
863 dpif_linux_encode_execute(dp_ifindex, execute, &request);
864 error = nl_sock_transact(genl_sock, &request, NULL);
865 ofpbuf_uninit(&request);
871 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
873 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
875 return dpif_linux_execute__(dpif->dp_ifindex, execute);
881 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
883 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
886 struct nl_transaction txn;
888 struct ofpbuf request;
889 uint64_t request_stub[1024 / 8];
892 uint64_t reply_stub[1024 / 8];
895 struct nl_transaction *txnsp[MAX_OPS];
898 assert(n_ops <= MAX_OPS);
899 for (i = 0; i < n_ops; i++) {
900 struct op_auxdata *aux = &auxes[i];
901 struct dpif_op *op = ops[i];
902 struct dpif_flow_put *put;
903 struct dpif_flow_del *del;
904 struct dpif_execute *execute;
905 struct dpif_linux_flow flow;
907 ofpbuf_use_stub(&aux->request,
908 aux->request_stub, sizeof aux->request_stub);
909 aux->txn.request = &aux->request;
911 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
912 aux->txn.reply = NULL;
915 case DPIF_OP_FLOW_PUT:
916 put = &op->u.flow_put;
917 dpif_linux_init_flow_put(dpif_, put, &flow);
919 flow.nlmsg_flags |= NLM_F_ECHO;
920 aux->txn.reply = &aux->reply;
922 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
925 case DPIF_OP_FLOW_DEL:
926 del = &op->u.flow_del;
927 dpif_linux_init_flow_del(dpif_, del, &flow);
929 flow.nlmsg_flags |= NLM_F_ECHO;
930 aux->txn.reply = &aux->reply;
932 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
935 case DPIF_OP_EXECUTE:
936 execute = &op->u.execute;
937 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
946 for (i = 0; i < n_ops; i++) {
947 txnsp[i] = &auxes[i].txn;
949 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
951 for (i = 0; i < n_ops; i++) {
952 struct op_auxdata *aux = &auxes[i];
953 struct nl_transaction *txn = &auxes[i].txn;
954 struct dpif_op *op = ops[i];
955 struct dpif_flow_put *put;
956 struct dpif_flow_del *del;
958 op->error = txn->error;
961 case DPIF_OP_FLOW_PUT:
962 put = &op->u.flow_put;
965 struct dpif_linux_flow reply;
967 op->error = dpif_linux_flow_from_ofpbuf(&reply,
970 dpif_linux_flow_get_stats(&reply, put->stats);
975 memset(put->stats, 0, sizeof *put->stats);
980 case DPIF_OP_FLOW_DEL:
981 del = &op->u.flow_del;
984 struct dpif_linux_flow reply;
986 op->error = dpif_linux_flow_from_ofpbuf(&reply,
989 dpif_linux_flow_get_stats(&reply, del->stats);
994 memset(del->stats, 0, sizeof *del->stats);
999 case DPIF_OP_EXECUTE:
1006 ofpbuf_uninit(&aux->request);
1007 ofpbuf_uninit(&aux->reply);
1012 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1015 size_t chunk = MIN(n_ops, MAX_OPS);
1016 dpif_linux_operate__(dpif, ops, chunk);
1023 set_upcall_pids(struct dpif *dpif_)
1025 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1026 struct dpif_port_dump port_dump;
1027 struct dpif_port port;
1030 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1031 uint32_t upcall_pid = dpif_linux_port_get_pid(dpif_, port.port_no);
1032 struct dpif_linux_vport vport_request;
1034 dpif_linux_vport_init(&vport_request);
1035 vport_request.cmd = OVS_VPORT_CMD_SET;
1036 vport_request.dp_ifindex = dpif->dp_ifindex;
1037 vport_request.port_no = port.port_no;
1038 vport_request.upcall_pid = &upcall_pid;
1039 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1041 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1042 dpif_name(&dpif->dpif), vport_request.port_no,
1045 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
1046 dpif_name(&dpif->dpif), strerror(error));
1052 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1054 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1056 if ((dpif->epoll_fd >= 0) == enable) {
1061 destroy_channels(dpif);
1063 struct dpif_channel *ch;
1066 dpif->epoll_fd = epoll_create(N_CHANNELS);
1067 if (dpif->epoll_fd < 0) {
1071 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
1072 int indx = ch - dpif->channels;
1073 struct epoll_event event;
1075 error = nl_sock_create(NETLINK_GENERIC, &ch->sock);
1077 destroy_channels(dpif);
1081 memset(&event, 0, sizeof event);
1082 event.events = EPOLLIN;
1083 event.data.u32 = indx;
1084 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(ch->sock),
1087 destroy_channels(dpif);
1091 memset(ch->sketches, 0, sizeof ch->sketches);
1092 ch->last_poll = LLONG_MIN;
1095 dpif->ready_mask = 0;
1096 dpif->next_scale = time_msec() + SCALE_INTERVAL;
1099 set_upcall_pids(dpif_);
1105 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1106 uint32_t queue_id, uint32_t *priority)
1108 if (queue_id < 0xf000) {
1109 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1117 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1120 static const struct nl_policy ovs_packet_policy[] = {
1121 /* Always present. */
1122 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1123 .min_len = ETH_HEADER_LEN },
1124 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1126 /* OVS_PACKET_CMD_ACTION only. */
1127 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1130 struct ovs_header *ovs_header;
1131 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1132 struct nlmsghdr *nlmsg;
1133 struct genlmsghdr *genl;
1137 ofpbuf_use_const(&b, buf->data, buf->size);
1139 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1140 genl = ofpbuf_try_pull(&b, sizeof *genl);
1141 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1142 if (!nlmsg || !genl || !ovs_header
1143 || nlmsg->nlmsg_type != ovs_packet_family
1144 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1145 ARRAY_SIZE(ovs_packet_policy))) {
1149 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1150 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1156 memset(upcall, 0, sizeof *upcall);
1157 upcall->type = type;
1158 upcall->packet = buf;
1159 upcall->packet->data = CONST_CAST(struct nlattr *,
1160 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1161 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1162 upcall->key = CONST_CAST(struct nlattr *,
1163 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1164 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1165 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1166 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1168 *dp_ifindex = ovs_header->dp_ifindex;
1174 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1177 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1180 if (dpif->epoll_fd < 0) {
1184 if (!dpif->ready_mask) {
1185 struct epoll_event events[N_CHANNELS];
1190 retval = epoll_wait(dpif->epoll_fd, events, N_CHANNELS, 0);
1191 } while (retval < 0 && errno == EINTR);
1193 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1194 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1197 for (i = 0; i < retval; i++) {
1198 dpif->ready_mask |= 1u << events[i].data.u32;
1202 while (dpif->ready_mask) {
1203 int indx = ffs(dpif->ready_mask) - 1;
1204 struct dpif_channel *ch = &dpif->channels[indx];
1206 dpif->ready_mask &= ~(1u << indx);
1212 if (++read_tries > 50) {
1216 error = nl_sock_recv(ch->sock, buf, false);
1217 if (error == ENOBUFS) {
1218 /* ENOBUFS typically means that we've received so many
1219 * packets that the buffer overflowed. Try again
1220 * immediately because there's almost certainly a packet
1221 * waiting for us. */
1222 report_loss(dpif_, ch);
1226 ch->last_poll = time_msec();
1228 if (error == EAGAIN) {
1234 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1235 if (!error && dp_ifindex == dpif->dp_ifindex) {
1236 const struct nlattr *in_port;
1238 in_port = nl_attr_find__(upcall->key, upcall->key_len,
1239 OVS_KEY_ATTR_IN_PORT);
1241 update_sketch(ch, nl_attr_get_u32(in_port));
1255 dpif_linux_recv_wait(struct dpif *dpif_)
1257 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1259 if (dpif->epoll_fd < 0) {
1263 poll_fd_wait(dpif->epoll_fd, POLLIN);
1267 dpif_linux_recv_purge(struct dpif *dpif_)
1269 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1270 struct dpif_channel *ch;
1272 if (dpif->epoll_fd < 0) {
1276 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
1277 nl_sock_drain(ch->sock);
1281 const struct dpif_class dpif_linux_class = {
1283 dpif_linux_enumerate,
1290 dpif_linux_get_stats,
1291 dpif_linux_port_add,
1292 dpif_linux_port_del,
1293 dpif_linux_port_query_by_number,
1294 dpif_linux_port_query_by_name,
1295 dpif_linux_get_max_ports,
1296 dpif_linux_port_get_pid,
1297 dpif_linux_port_dump_start,
1298 dpif_linux_port_dump_next,
1299 dpif_linux_port_dump_done,
1300 dpif_linux_port_poll,
1301 dpif_linux_port_poll_wait,
1302 dpif_linux_flow_get,
1303 dpif_linux_flow_put,
1304 dpif_linux_flow_del,
1305 dpif_linux_flow_flush,
1306 dpif_linux_flow_dump_start,
1307 dpif_linux_flow_dump_next,
1308 dpif_linux_flow_dump_done,
1311 dpif_linux_recv_set,
1312 dpif_linux_queue_to_priority,
1314 dpif_linux_recv_wait,
1315 dpif_linux_recv_purge,
1319 dpif_linux_init(void)
1321 static int error = -1;
1324 unsigned int ovs_vport_mcgroup;
1326 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1327 &ovs_datapath_family);
1329 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1330 "The Open vSwitch kernel module is probably not loaded.",
1331 OVS_DATAPATH_FAMILY);
1334 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1337 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1340 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1341 &ovs_packet_family);
1344 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1347 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1349 OVS_VPORT_MCGROUP_FALLBACK_ID);
1352 static struct dpif_linux_vport vport;
1353 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1354 dpif_linux_nln_parse, &vport);
1362 dpif_linux_is_internal_device(const char *name)
1364 struct dpif_linux_vport reply;
1368 error = dpif_linux_vport_get(name, &reply, &buf);
1371 } else if (error != ENODEV && error != ENOENT) {
1372 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1373 name, strerror(error));
1376 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1380 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1381 const void *data, size_t size)
1383 struct ofpbuf actions, key, packet;
1384 struct odputil_keybuf keybuf;
1385 struct dpif_execute execute;
1389 ofpbuf_use_const(&packet, data, size);
1390 flow_extract(&packet, 0, NULL, 0, &flow);
1392 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1393 odp_flow_key_from_flow(&key, &flow, OVSP_NONE);
1395 ofpbuf_use_stack(&actions, &action, sizeof action);
1396 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1398 execute.key = key.data;
1399 execute.key_len = key.size;
1400 execute.actions = actions.data;
1401 execute.actions_len = actions.size;
1402 execute.packet = &packet;
1403 return dpif_linux_execute__(dp_ifindex, &execute);
1407 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1409 struct dpif_linux_vport *vport = vport_;
1410 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1414 dpif_linux_port_changed(const void *vport_, void *dpif_)
1416 const struct dpif_linux_vport *vport = vport_;
1417 struct dpif_linux *dpif = dpif_;
1420 if (vport->dp_ifindex == dpif->dp_ifindex
1421 && (vport->cmd == OVS_VPORT_CMD_NEW
1422 || vport->cmd == OVS_VPORT_CMD_DEL
1423 || vport->cmd == OVS_VPORT_CMD_SET)) {
1424 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1425 dpif->dpif.full_name, vport->name, vport->cmd);
1426 sset_add(&dpif->changed_ports, vport->name);
1429 dpif->change_error = true;
1433 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1434 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1435 * positive errno value.
1437 * 'vport' will contain pointers into 'buf', so the caller should not free
1438 * 'buf' while 'vport' is still in use. */
1440 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1441 const struct ofpbuf *buf)
1443 static const struct nl_policy ovs_vport_policy[] = {
1444 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1445 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1446 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1447 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1448 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1450 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1451 .min_len = ETH_ADDR_LEN,
1452 .max_len = ETH_ADDR_LEN,
1454 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1457 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1458 struct ovs_header *ovs_header;
1459 struct nlmsghdr *nlmsg;
1460 struct genlmsghdr *genl;
1463 dpif_linux_vport_init(vport);
1465 ofpbuf_use_const(&b, buf->data, buf->size);
1466 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1467 genl = ofpbuf_try_pull(&b, sizeof *genl);
1468 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1469 if (!nlmsg || !genl || !ovs_header
1470 || nlmsg->nlmsg_type != ovs_vport_family
1471 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1472 ARRAY_SIZE(ovs_vport_policy))) {
1476 vport->cmd = genl->cmd;
1477 vport->dp_ifindex = ovs_header->dp_ifindex;
1478 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1479 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1480 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1481 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1482 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1484 if (a[OVS_VPORT_ATTR_STATS]) {
1485 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1487 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1488 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1490 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1491 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1492 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1497 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1498 * followed by Netlink attributes corresponding to 'vport'. */
1500 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1503 struct ovs_header *ovs_header;
1505 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1506 vport->cmd, OVS_VPORT_VERSION);
1508 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1509 ovs_header->dp_ifindex = vport->dp_ifindex;
1511 if (vport->port_no != UINT32_MAX) {
1512 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1515 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1516 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1520 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1523 if (vport->upcall_pid) {
1524 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1528 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1529 vport->stats, sizeof *vport->stats);
1532 if (vport->address) {
1533 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1534 vport->address, ETH_ADDR_LEN);
1537 if (vport->options) {
1538 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1539 vport->options, vport->options_len);
1543 /* Clears 'vport' to "empty" values. */
1545 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1547 memset(vport, 0, sizeof *vport);
1548 vport->port_no = UINT32_MAX;
1551 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1552 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1553 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1554 * result of the command is expected to be an ovs_vport also, which is decoded
1555 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1556 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1558 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1559 struct dpif_linux_vport *reply,
1560 struct ofpbuf **bufp)
1562 struct ofpbuf *request_buf;
1565 assert((reply != NULL) == (bufp != NULL));
1567 error = dpif_linux_init();
1571 dpif_linux_vport_init(reply);
1576 request_buf = ofpbuf_new(1024);
1577 dpif_linux_vport_to_ofpbuf(request, request_buf);
1578 error = nl_sock_transact(genl_sock, request_buf, bufp);
1579 ofpbuf_delete(request_buf);
1583 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1586 dpif_linux_vport_init(reply);
1587 ofpbuf_delete(*bufp);
1594 /* Obtains information about the kernel vport named 'name' and stores it into
1595 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1596 * longer needed ('reply' will contain pointers into '*bufp'). */
1598 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1599 struct ofpbuf **bufp)
1601 struct dpif_linux_vport request;
1603 dpif_linux_vport_init(&request);
1604 request.cmd = OVS_VPORT_CMD_GET;
1605 request.name = name;
1607 return dpif_linux_vport_transact(&request, reply, bufp);
1610 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1611 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1612 * positive errno value.
1614 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1615 * while 'dp' is still in use. */
1617 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1619 static const struct nl_policy ovs_datapath_policy[] = {
1620 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1621 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1625 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1626 struct ovs_header *ovs_header;
1627 struct nlmsghdr *nlmsg;
1628 struct genlmsghdr *genl;
1631 dpif_linux_dp_init(dp);
1633 ofpbuf_use_const(&b, buf->data, buf->size);
1634 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1635 genl = ofpbuf_try_pull(&b, sizeof *genl);
1636 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1637 if (!nlmsg || !genl || !ovs_header
1638 || nlmsg->nlmsg_type != ovs_datapath_family
1639 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1640 ARRAY_SIZE(ovs_datapath_policy))) {
1644 dp->cmd = genl->cmd;
1645 dp->dp_ifindex = ovs_header->dp_ifindex;
1646 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1647 if (a[OVS_DP_ATTR_STATS]) {
1648 /* Can't use structure assignment because Netlink doesn't ensure
1649 * sufficient alignment for 64-bit members. */
1650 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1657 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1659 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1661 struct ovs_header *ovs_header;
1663 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1664 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1665 OVS_DATAPATH_VERSION);
1667 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1668 ovs_header->dp_ifindex = dp->dp_ifindex;
1671 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1674 if (dp->upcall_pid) {
1675 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1678 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1681 /* Clears 'dp' to "empty" values. */
1683 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1685 memset(dp, 0, sizeof *dp);
1689 dpif_linux_dp_dump_start(struct nl_dump *dump)
1691 struct dpif_linux_dp request;
1694 dpif_linux_dp_init(&request);
1695 request.cmd = OVS_DP_CMD_GET;
1697 buf = ofpbuf_new(1024);
1698 dpif_linux_dp_to_ofpbuf(&request, buf);
1699 nl_dump_start(dump, genl_sock, buf);
1703 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1704 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1705 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1706 * result of the command is expected to be of the same form, which is decoded
1707 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1708 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1710 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1711 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1713 struct ofpbuf *request_buf;
1716 assert((reply != NULL) == (bufp != NULL));
1718 request_buf = ofpbuf_new(1024);
1719 dpif_linux_dp_to_ofpbuf(request, request_buf);
1720 error = nl_sock_transact(genl_sock, request_buf, bufp);
1721 ofpbuf_delete(request_buf);
1725 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1728 dpif_linux_dp_init(reply);
1729 ofpbuf_delete(*bufp);
1736 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1737 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1738 * will contain pointers into '*bufp'). */
1740 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1741 struct ofpbuf **bufp)
1743 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1744 struct dpif_linux_dp request;
1746 dpif_linux_dp_init(&request);
1747 request.cmd = OVS_DP_CMD_GET;
1748 request.dp_ifindex = dpif->dp_ifindex;
1750 return dpif_linux_dp_transact(&request, reply, bufp);
1753 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1754 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1755 * positive errno value.
1757 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1758 * while 'flow' is still in use. */
1760 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1761 const struct ofpbuf *buf)
1763 static const struct nl_policy ovs_flow_policy[] = {
1764 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1765 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1766 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1768 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1769 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1770 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1773 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1774 struct ovs_header *ovs_header;
1775 struct nlmsghdr *nlmsg;
1776 struct genlmsghdr *genl;
1779 dpif_linux_flow_init(flow);
1781 ofpbuf_use_const(&b, buf->data, buf->size);
1782 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1783 genl = ofpbuf_try_pull(&b, sizeof *genl);
1784 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1785 if (!nlmsg || !genl || !ovs_header
1786 || nlmsg->nlmsg_type != ovs_flow_family
1787 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1788 ARRAY_SIZE(ovs_flow_policy))) {
1792 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1793 flow->dp_ifindex = ovs_header->dp_ifindex;
1794 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1795 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1796 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1797 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1798 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1800 if (a[OVS_FLOW_ATTR_STATS]) {
1801 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1803 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1804 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1806 if (a[OVS_FLOW_ATTR_USED]) {
1807 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1812 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1813 * followed by Netlink attributes corresponding to 'flow'. */
1815 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1818 struct ovs_header *ovs_header;
1820 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1821 NLM_F_REQUEST | flow->nlmsg_flags,
1822 flow->cmd, OVS_FLOW_VERSION);
1824 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1825 ovs_header->dp_ifindex = flow->dp_ifindex;
1827 if (flow->key_len) {
1828 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1831 if (flow->actions || flow->actions_len) {
1832 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1833 flow->actions, flow->actions_len);
1836 /* We never need to send these to the kernel. */
1837 assert(!flow->stats);
1838 assert(!flow->tcp_flags);
1839 assert(!flow->used);
1842 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1846 /* Clears 'flow' to "empty" values. */
1848 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1850 memset(flow, 0, sizeof *flow);
1853 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1854 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1855 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1856 * result of the command is expected to be a flow also, which is decoded and
1857 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1858 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1860 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1861 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1863 struct ofpbuf *request_buf;
1866 assert((reply != NULL) == (bufp != NULL));
1869 request->nlmsg_flags |= NLM_F_ECHO;
1872 request_buf = ofpbuf_new(1024);
1873 dpif_linux_flow_to_ofpbuf(request, request_buf);
1874 error = nl_sock_transact(genl_sock, request_buf, bufp);
1875 ofpbuf_delete(request_buf);
1879 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1882 dpif_linux_flow_init(reply);
1883 ofpbuf_delete(*bufp);
1891 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1892 struct dpif_flow_stats *stats)
1895 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1896 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1898 stats->n_packets = 0;
1901 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1902 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1905 /* Metwally "space-saving" algorithm implementation. */
1907 /* Updates 'ch' to record that a packet was received on 'port_no'. */
1909 update_sketch(struct dpif_channel *ch, uint32_t port_no)
1911 struct dpif_sketch *sk;
1913 /* Find an existing counting element for 'port_no' or, if none, replace the
1914 * counting element with the fewest hits by 'port_no'. */
1915 for (sk = ch->sketches; ; sk++) {
1916 if (port_no == sk->port_no) {
1918 } else if (sk == &ch->sketches[N_SKETCHES - 1]) {
1919 sk->port_no = port_no;
1920 sk->error = sk->hits;
1925 /* Increment the hit count, then re-sort the counting elements (usually
1926 * nothing needs to be done). */
1928 while (sk > ch->sketches && sk[-1].hits > sk->hits) {
1929 struct dpif_sketch tmp = sk[-1];
1936 /* Divide the counts of all the the counting elements in 'dpif' by 2. See the
1937 * comment on SCALE_INTERVAL. */
1939 scale_sketches(struct dpif *dpif_)
1941 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1942 struct dpif_channel *ch;
1944 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
1945 struct dpif_sketch *sk;
1947 for (sk = ch->sketches; sk < &ch->sketches[N_SKETCHES]; sk++) {
1954 /* Logs information about a packet that was recently lost in 'ch' (in
1957 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1959 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1960 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1961 struct dpif_sketch *sk;
1964 if (VLOG_DROP_ERR(&rl)) {
1969 if (ch->last_poll != LLONG_MIN) {
1970 ds_put_format(&s, " (last polled %lld ms ago)",
1971 time_msec() - ch->last_poll);
1973 ds_put_cstr(&s, ", most frequent sources are");
1974 for (sk = ch->sketches; sk < &ch->sketches[N_SKETCHES]; sk++) {
1976 struct dpif_port port;
1978 ds_put_format(&s, " %"PRIu32, sk->port_no);
1979 if (!dpif_port_query_by_number(dpif_, sk->port_no, &port)) {
1980 ds_put_format(&s, "(%s)", port.name);
1981 dpif_port_destroy(&port);
1984 ds_put_format(&s, ": %u to %u,",
1985 sk->hits - sk->error, sk->hits);
1987 ds_put_format(&s, ": %u,", sk->hits);
1993 VLOG_WARN("%s: lost packet on channel %td%s",
1994 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));