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;
202 /* Port number allocation. */
203 uint16_t alloc_port_no;
206 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
208 /* Generic Netlink family numbers for OVS. */
209 static int ovs_datapath_family;
210 static int ovs_vport_family;
211 static int ovs_flow_family;
212 static int ovs_packet_family;
214 /* Generic Netlink socket. */
215 static struct nl_sock *genl_sock;
216 static struct nln *nln = NULL;
218 static int dpif_linux_init(void);
219 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
220 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
221 static void dpif_linux_port_changed(const void *vport, void *dpif);
222 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint16_t port_no);
224 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
226 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
227 const struct ofpbuf *);
229 static struct dpif_linux *
230 dpif_linux_cast(const struct dpif *dpif)
232 dpif_assert_class(dpif, &dpif_linux_class);
233 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
237 dpif_linux_enumerate(struct sset *all_dps)
243 error = dpif_linux_init();
248 dpif_linux_dp_dump_start(&dump);
249 while (nl_dump_next(&dump, &msg)) {
250 struct dpif_linux_dp dp;
252 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
253 sset_add(all_dps, dp.name);
256 return nl_dump_done(&dump);
260 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
261 bool create, struct dpif **dpifp)
263 struct dpif_linux_dp dp_request, dp;
268 error = dpif_linux_init();
273 /* Create or look up datapath. */
274 dpif_linux_dp_init(&dp_request);
276 dp_request.cmd = OVS_DP_CMD_NEW;
278 dp_request.upcall_pid = &upcall_pid;
280 dp_request.cmd = OVS_DP_CMD_GET;
282 dp_request.name = name;
283 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
288 open_dpif(&dp, dpifp);
294 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
296 struct dpif_linux *dpif;
298 dpif = xzalloc(sizeof *dpif);
299 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
303 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
304 dp->dp_ifindex, dp->dp_ifindex);
306 dpif->next_scale = LLONG_MAX;
308 dpif->dp_ifindex = dp->dp_ifindex;
309 sset_init(&dpif->changed_ports);
310 *dpifp = &dpif->dpif;
314 destroy_channels(struct dpif_linux *dpif)
316 struct dpif_channel *ch;
318 if (dpif->epoll_fd >= 0) {
319 close(dpif->epoll_fd);
322 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
323 nl_sock_destroy(ch->sock);
326 dpif->next_scale = LLONG_MAX;
330 dpif_linux_close(struct dpif *dpif_)
332 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
334 nln_notifier_destroy(dpif->port_notifier);
335 destroy_channels(dpif);
336 sset_destroy(&dpif->changed_ports);
341 dpif_linux_destroy(struct dpif *dpif_)
343 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
344 struct dpif_linux_dp dp;
346 dpif_linux_dp_init(&dp);
347 dp.cmd = OVS_DP_CMD_DEL;
348 dp.dp_ifindex = dpif->dp_ifindex;
349 return dpif_linux_dp_transact(&dp, NULL, NULL);
353 dpif_linux_run(struct dpif *dpif_)
355 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
357 if (time_msec() >= dpif->next_scale) {
358 dpif->next_scale = time_msec() + SCALE_INTERVAL;
359 scale_sketches(dpif_);
368 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
376 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
378 struct dpif_linux_dp dp;
382 error = dpif_linux_dp_get(dpif_, &dp, &buf);
384 stats->n_hit = dp.stats.n_hit;
385 stats->n_missed = dp.stats.n_missed;
386 stats->n_lost = dp.stats.n_lost;
387 stats->n_flows = dp.stats.n_flows;
394 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
397 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
398 const char *name = netdev_get_name(netdev);
399 const char *type = netdev_get_type(netdev);
400 struct dpif_linux_vport request, reply;
401 const struct ofpbuf *options;
403 int error, i = 0, max_ports = MAX_PORTS;
405 dpif_linux_vport_init(&request);
406 request.cmd = OVS_VPORT_CMD_NEW;
407 request.dp_ifindex = dpif->dp_ifindex;
408 request.type = netdev_vport_get_vport_type(netdev);
409 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
410 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
411 "unsupported type `%s'",
412 dpif_name(dpif_), name, type);
417 options = netdev_vport_get_options(netdev);
418 if (options && options->size) {
419 request.options = options->data;
420 request.options_len = options->size;
423 if (request.type == OVS_VPORT_TYPE_NETDEV) {
424 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
427 /* Unless a specific port was requested, loop until we find a port
428 * that isn't used. */
432 request.port_no = *port_nop != UINT16_MAX ? *port_nop
433 : ++dpif->alloc_port_no;
434 upcall_pid = dpif_linux_port_get_pid(dpif_, request.port_no);
435 request.upcall_pid = &upcall_pid;
436 error = dpif_linux_vport_transact(&request, &reply, &buf);
439 *port_nop = reply.port_no;
440 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
441 dpif_name(dpif_), request.port_no, upcall_pid);
442 } else if (error == EFBIG) {
443 /* Older datapath has lower limit. */
444 max_ports = dpif->alloc_port_no;
445 dpif->alloc_port_no = 0;
446 } else if (error == EBUSY && *port_nop != UINT16_MAX) {
447 VLOG_INFO("%s: requested port %"PRIu16" is in use",
448 dpif_name(dpif_), *port_nop);
452 } while ((*port_nop == UINT16_MAX) && (i++ < max_ports)
453 && (error == EBUSY || error == EFBIG));
459 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
461 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
462 struct dpif_linux_vport vport;
465 dpif_linux_vport_init(&vport);
466 vport.cmd = OVS_VPORT_CMD_DEL;
467 vport.dp_ifindex = dpif->dp_ifindex;
468 vport.port_no = port_no;
469 error = dpif_linux_vport_transact(&vport, NULL, NULL);
475 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
476 const char *port_name, struct dpif_port *dpif_port)
478 struct dpif_linux_vport request;
479 struct dpif_linux_vport reply;
483 dpif_linux_vport_init(&request);
484 request.cmd = OVS_VPORT_CMD_GET;
485 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
486 request.port_no = port_no;
487 request.name = port_name;
489 error = dpif_linux_vport_transact(&request, &reply, &buf);
491 if (reply.dp_ifindex != request.dp_ifindex) {
492 /* A query by name reported that 'port_name' is in some datapath
493 * other than 'dpif', but the caller wants to know about 'dpif'. */
496 dpif_port->name = xstrdup(reply.name);
497 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
498 dpif_port->port_no = reply.port_no;
506 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
507 struct dpif_port *dpif_port)
509 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
513 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
514 struct dpif_port *dpif_port)
516 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
520 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
526 dpif_linux_port_get_pid(const struct dpif *dpif_, uint16_t port_no)
528 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
530 if (dpif->epoll_fd < 0) {
535 idx = (port_no != UINT16_MAX
536 ? 1 + (port_no & (N_CHANNELS - 2))
538 return nl_sock_pid(dpif->channels[idx].sock);
543 dpif_linux_flow_flush(struct dpif *dpif_)
545 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
546 struct dpif_linux_flow flow;
548 dpif_linux_flow_init(&flow);
549 flow.cmd = OVS_FLOW_CMD_DEL;
550 flow.dp_ifindex = dpif->dp_ifindex;
551 return dpif_linux_flow_transact(&flow, NULL, NULL);
554 struct dpif_linux_port_state {
559 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
561 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
562 struct dpif_linux_port_state *state;
563 struct dpif_linux_vport request;
566 *statep = state = xmalloc(sizeof *state);
568 dpif_linux_vport_init(&request);
569 request.cmd = OVS_DP_CMD_GET;
570 request.dp_ifindex = dpif->dp_ifindex;
572 buf = ofpbuf_new(1024);
573 dpif_linux_vport_to_ofpbuf(&request, buf);
574 nl_dump_start(&state->dump, genl_sock, buf);
581 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
582 struct dpif_port *dpif_port)
584 struct dpif_linux_port_state *state = state_;
585 struct dpif_linux_vport vport;
589 if (!nl_dump_next(&state->dump, &buf)) {
593 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
598 dpif_port->name = CONST_CAST(char *, vport.name);
599 dpif_port->type = CONST_CAST(char *, netdev_vport_get_netdev_type(&vport));
600 dpif_port->port_no = vport.port_no;
605 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
607 struct dpif_linux_port_state *state = state_;
608 int error = nl_dump_done(&state->dump);
615 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
617 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
619 if (dpif->change_error) {
620 dpif->change_error = false;
621 sset_clear(&dpif->changed_ports);
623 } else if (!sset_is_empty(&dpif->changed_ports)) {
624 *devnamep = sset_pop(&dpif->changed_ports);
632 dpif_linux_port_poll_wait(const struct dpif *dpif_)
634 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
635 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
636 poll_immediate_wake();
641 dpif_linux_flow_get__(const struct dpif *dpif_,
642 const struct nlattr *key, size_t key_len,
643 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
645 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
646 struct dpif_linux_flow request;
648 dpif_linux_flow_init(&request);
649 request.cmd = OVS_FLOW_CMD_GET;
650 request.dp_ifindex = dpif->dp_ifindex;
652 request.key_len = key_len;
653 return dpif_linux_flow_transact(&request, reply, bufp);
657 dpif_linux_flow_get(const struct dpif *dpif_,
658 const struct nlattr *key, size_t key_len,
659 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
661 struct dpif_linux_flow reply;
665 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
668 dpif_linux_flow_get_stats(&reply, stats);
671 buf->data = CONST_CAST(struct nlattr *, reply.actions);
672 buf->size = reply.actions_len;
682 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
683 struct dpif_linux_flow *request)
685 static struct nlattr dummy_action;
687 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
689 dpif_linux_flow_init(request);
690 request->cmd = (put->flags & DPIF_FP_CREATE
691 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
692 request->dp_ifindex = dpif->dp_ifindex;
693 request->key = put->key;
694 request->key_len = put->key_len;
695 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
696 request->actions = put->actions ? put->actions : &dummy_action;
697 request->actions_len = put->actions_len;
698 if (put->flags & DPIF_FP_ZERO_STATS) {
699 request->clear = true;
701 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
705 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
707 struct dpif_linux_flow request, reply;
711 dpif_linux_init_flow_put(dpif_, put, &request);
712 error = dpif_linux_flow_transact(&request,
713 put->stats ? &reply : NULL,
714 put->stats ? &buf : NULL);
715 if (!error && put->stats) {
716 dpif_linux_flow_get_stats(&reply, put->stats);
723 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
724 struct dpif_linux_flow *request)
726 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
728 dpif_linux_flow_init(request);
729 request->cmd = OVS_FLOW_CMD_DEL;
730 request->dp_ifindex = dpif->dp_ifindex;
731 request->key = del->key;
732 request->key_len = del->key_len;
736 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
738 struct dpif_linux_flow request, reply;
742 dpif_linux_init_flow_del(dpif_, del, &request);
743 error = dpif_linux_flow_transact(&request,
744 del->stats ? &reply : NULL,
745 del->stats ? &buf : NULL);
746 if (!error && del->stats) {
747 dpif_linux_flow_get_stats(&reply, del->stats);
753 struct dpif_linux_flow_state {
755 struct dpif_linux_flow flow;
756 struct dpif_flow_stats stats;
761 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
763 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
764 struct dpif_linux_flow_state *state;
765 struct dpif_linux_flow request;
768 *statep = state = xmalloc(sizeof *state);
770 dpif_linux_flow_init(&request);
771 request.cmd = OVS_DP_CMD_GET;
772 request.dp_ifindex = dpif->dp_ifindex;
774 buf = ofpbuf_new(1024);
775 dpif_linux_flow_to_ofpbuf(&request, buf);
776 nl_dump_start(&state->dump, genl_sock, buf);
785 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
786 const struct nlattr **key, size_t *key_len,
787 const struct nlattr **actions, size_t *actions_len,
788 const struct dpif_flow_stats **stats)
790 struct dpif_linux_flow_state *state = state_;
795 ofpbuf_delete(state->buf);
798 if (!nl_dump_next(&state->dump, &buf)) {
802 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
807 if (actions && !state->flow.actions) {
808 error = dpif_linux_flow_get__(dpif_, state->flow.key,
810 &state->flow, &state->buf);
811 if (error == ENOENT) {
812 VLOG_DBG("dumped flow disappeared on get");
814 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
820 *actions = state->flow.actions;
821 *actions_len = state->flow.actions_len;
824 *key = state->flow.key;
825 *key_len = state->flow.key_len;
828 dpif_linux_flow_get_stats(&state->flow, &state->stats);
829 *stats = &state->stats;
835 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
837 struct dpif_linux_flow_state *state = state_;
838 int error = nl_dump_done(&state->dump);
839 ofpbuf_delete(state->buf);
845 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
848 struct ovs_header *k_exec;
850 ofpbuf_prealloc_tailroom(buf, (64
851 + d_exec->packet->size
853 + d_exec->actions_len));
855 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
856 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
858 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
859 k_exec->dp_ifindex = dp_ifindex;
861 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
862 d_exec->packet->data, d_exec->packet->size);
863 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
864 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
865 d_exec->actions, d_exec->actions_len);
869 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
871 uint64_t request_stub[1024 / 8];
872 struct ofpbuf request;
875 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
876 dpif_linux_encode_execute(dp_ifindex, execute, &request);
877 error = nl_sock_transact(genl_sock, &request, NULL);
878 ofpbuf_uninit(&request);
884 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
886 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
888 return dpif_linux_execute__(dpif->dp_ifindex, execute);
894 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
896 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
899 struct nl_transaction txn;
901 struct ofpbuf request;
902 uint64_t request_stub[1024 / 8];
905 uint64_t reply_stub[1024 / 8];
908 struct nl_transaction *txnsp[MAX_OPS];
911 assert(n_ops <= MAX_OPS);
912 for (i = 0; i < n_ops; i++) {
913 struct op_auxdata *aux = &auxes[i];
914 struct dpif_op *op = ops[i];
915 struct dpif_flow_put *put;
916 struct dpif_flow_del *del;
917 struct dpif_execute *execute;
918 struct dpif_linux_flow flow;
920 ofpbuf_use_stub(&aux->request,
921 aux->request_stub, sizeof aux->request_stub);
922 aux->txn.request = &aux->request;
924 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
925 aux->txn.reply = NULL;
928 case DPIF_OP_FLOW_PUT:
929 put = &op->u.flow_put;
930 dpif_linux_init_flow_put(dpif_, put, &flow);
932 flow.nlmsg_flags |= NLM_F_ECHO;
933 aux->txn.reply = &aux->reply;
935 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
938 case DPIF_OP_FLOW_DEL:
939 del = &op->u.flow_del;
940 dpif_linux_init_flow_del(dpif_, del, &flow);
942 flow.nlmsg_flags |= NLM_F_ECHO;
943 aux->txn.reply = &aux->reply;
945 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
948 case DPIF_OP_EXECUTE:
949 execute = &op->u.execute;
950 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
959 for (i = 0; i < n_ops; i++) {
960 txnsp[i] = &auxes[i].txn;
962 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
964 for (i = 0; i < n_ops; i++) {
965 struct op_auxdata *aux = &auxes[i];
966 struct nl_transaction *txn = &auxes[i].txn;
967 struct dpif_op *op = ops[i];
968 struct dpif_flow_put *put;
969 struct dpif_flow_del *del;
971 op->error = txn->error;
974 case DPIF_OP_FLOW_PUT:
975 put = &op->u.flow_put;
978 struct dpif_linux_flow reply;
980 op->error = dpif_linux_flow_from_ofpbuf(&reply,
983 dpif_linux_flow_get_stats(&reply, put->stats);
988 memset(put->stats, 0, sizeof *put->stats);
993 case DPIF_OP_FLOW_DEL:
994 del = &op->u.flow_del;
997 struct dpif_linux_flow reply;
999 op->error = dpif_linux_flow_from_ofpbuf(&reply,
1002 dpif_linux_flow_get_stats(&reply, del->stats);
1007 memset(del->stats, 0, sizeof *del->stats);
1012 case DPIF_OP_EXECUTE:
1019 ofpbuf_uninit(&aux->request);
1020 ofpbuf_uninit(&aux->reply);
1025 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1028 size_t chunk = MIN(n_ops, MAX_OPS);
1029 dpif_linux_operate__(dpif, ops, chunk);
1036 set_upcall_pids(struct dpif *dpif_)
1038 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1039 struct dpif_port_dump port_dump;
1040 struct dpif_port port;
1043 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1044 uint32_t upcall_pid = dpif_linux_port_get_pid(dpif_, port.port_no);
1045 struct dpif_linux_vport vport_request;
1047 dpif_linux_vport_init(&vport_request);
1048 vport_request.cmd = OVS_VPORT_CMD_SET;
1049 vport_request.dp_ifindex = dpif->dp_ifindex;
1050 vport_request.port_no = port.port_no;
1051 vport_request.upcall_pid = &upcall_pid;
1052 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1054 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1055 dpif_name(&dpif->dpif), vport_request.port_no,
1058 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
1059 dpif_name(&dpif->dpif), strerror(error));
1065 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1067 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1069 if ((dpif->epoll_fd >= 0) == enable) {
1074 destroy_channels(dpif);
1076 struct dpif_channel *ch;
1079 dpif->epoll_fd = epoll_create(N_CHANNELS);
1080 if (dpif->epoll_fd < 0) {
1084 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
1085 int indx = ch - dpif->channels;
1086 struct epoll_event event;
1088 error = nl_sock_create(NETLINK_GENERIC, &ch->sock);
1090 destroy_channels(dpif);
1094 memset(&event, 0, sizeof event);
1095 event.events = EPOLLIN;
1096 event.data.u32 = indx;
1097 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(ch->sock),
1100 destroy_channels(dpif);
1104 memset(ch->sketches, 0, sizeof ch->sketches);
1105 ch->last_poll = LLONG_MIN;
1108 dpif->ready_mask = 0;
1109 dpif->next_scale = time_msec() + SCALE_INTERVAL;
1112 set_upcall_pids(dpif_);
1118 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1119 uint32_t queue_id, uint32_t *priority)
1121 if (queue_id < 0xf000) {
1122 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1130 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1133 static const struct nl_policy ovs_packet_policy[] = {
1134 /* Always present. */
1135 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1136 .min_len = ETH_HEADER_LEN },
1137 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1139 /* OVS_PACKET_CMD_ACTION only. */
1140 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1143 struct ovs_header *ovs_header;
1144 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1145 struct nlmsghdr *nlmsg;
1146 struct genlmsghdr *genl;
1150 ofpbuf_use_const(&b, buf->data, buf->size);
1152 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1153 genl = ofpbuf_try_pull(&b, sizeof *genl);
1154 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1155 if (!nlmsg || !genl || !ovs_header
1156 || nlmsg->nlmsg_type != ovs_packet_family
1157 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1158 ARRAY_SIZE(ovs_packet_policy))) {
1162 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1163 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1169 memset(upcall, 0, sizeof *upcall);
1170 upcall->type = type;
1171 upcall->packet = buf;
1172 upcall->packet->data = CONST_CAST(struct nlattr *,
1173 nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1174 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1175 upcall->key = CONST_CAST(struct nlattr *,
1176 nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1177 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1178 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1179 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1181 *dp_ifindex = ovs_header->dp_ifindex;
1187 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1190 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1193 if (dpif->epoll_fd < 0) {
1197 if (!dpif->ready_mask) {
1198 struct epoll_event events[N_CHANNELS];
1203 retval = epoll_wait(dpif->epoll_fd, events, N_CHANNELS, 0);
1204 } while (retval < 0 && errno == EINTR);
1206 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1207 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1210 for (i = 0; i < retval; i++) {
1211 dpif->ready_mask |= 1u << events[i].data.u32;
1215 while (dpif->ready_mask) {
1216 int indx = ffs(dpif->ready_mask) - 1;
1217 struct dpif_channel *ch = &dpif->channels[indx];
1219 dpif->ready_mask &= ~(1u << indx);
1225 if (++read_tries > 50) {
1229 error = nl_sock_recv(ch->sock, buf, false);
1230 if (error == ENOBUFS) {
1231 /* ENOBUFS typically means that we've received so many
1232 * packets that the buffer overflowed. Try again
1233 * immediately because there's almost certainly a packet
1234 * waiting for us. */
1235 report_loss(dpif_, ch);
1239 ch->last_poll = time_msec();
1241 if (error == EAGAIN) {
1247 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1248 if (!error && dp_ifindex == dpif->dp_ifindex) {
1249 const struct nlattr *in_port;
1251 in_port = nl_attr_find__(upcall->key, upcall->key_len,
1252 OVS_KEY_ATTR_IN_PORT);
1254 update_sketch(ch, nl_attr_get_u32(in_port));
1268 dpif_linux_recv_wait(struct dpif *dpif_)
1270 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1272 if (dpif->epoll_fd < 0) {
1276 poll_fd_wait(dpif->epoll_fd, POLLIN);
1280 dpif_linux_recv_purge(struct dpif *dpif_)
1282 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1283 struct dpif_channel *ch;
1285 if (dpif->epoll_fd < 0) {
1289 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
1290 nl_sock_drain(ch->sock);
1294 const struct dpif_class dpif_linux_class = {
1296 dpif_linux_enumerate,
1302 dpif_linux_get_stats,
1303 dpif_linux_port_add,
1304 dpif_linux_port_del,
1305 dpif_linux_port_query_by_number,
1306 dpif_linux_port_query_by_name,
1307 dpif_linux_get_max_ports,
1308 dpif_linux_port_get_pid,
1309 dpif_linux_port_dump_start,
1310 dpif_linux_port_dump_next,
1311 dpif_linux_port_dump_done,
1312 dpif_linux_port_poll,
1313 dpif_linux_port_poll_wait,
1314 dpif_linux_flow_get,
1315 dpif_linux_flow_put,
1316 dpif_linux_flow_del,
1317 dpif_linux_flow_flush,
1318 dpif_linux_flow_dump_start,
1319 dpif_linux_flow_dump_next,
1320 dpif_linux_flow_dump_done,
1323 dpif_linux_recv_set,
1324 dpif_linux_queue_to_priority,
1326 dpif_linux_recv_wait,
1327 dpif_linux_recv_purge,
1331 dpif_linux_init(void)
1333 static int error = -1;
1336 unsigned int ovs_vport_mcgroup;
1338 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1339 &ovs_datapath_family);
1341 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1342 "The Open vSwitch kernel module is probably not loaded.",
1343 OVS_DATAPATH_FAMILY);
1346 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1349 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1352 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1353 &ovs_packet_family);
1356 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1359 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1361 OVS_VPORT_MCGROUP_FALLBACK_ID);
1364 static struct dpif_linux_vport vport;
1365 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1366 dpif_linux_nln_parse, &vport);
1374 dpif_linux_is_internal_device(const char *name)
1376 struct dpif_linux_vport reply;
1380 error = dpif_linux_vport_get(name, &reply, &buf);
1383 } else if (error != ENODEV && error != ENOENT) {
1384 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1385 name, strerror(error));
1388 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1392 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1393 const void *data, size_t size)
1395 struct ofpbuf actions, key, packet;
1396 struct odputil_keybuf keybuf;
1397 struct dpif_execute execute;
1401 ofpbuf_use_const(&packet, data, size);
1402 flow_extract(&packet, 0, htonll(0), 0, &flow);
1404 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1405 odp_flow_key_from_flow(&key, &flow);
1407 ofpbuf_use_stack(&actions, &action, sizeof action);
1408 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1410 execute.key = key.data;
1411 execute.key_len = key.size;
1412 execute.actions = actions.data;
1413 execute.actions_len = actions.size;
1414 execute.packet = &packet;
1415 return dpif_linux_execute__(dp_ifindex, &execute);
1419 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1421 struct dpif_linux_vport *vport = vport_;
1422 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1426 dpif_linux_port_changed(const void *vport_, void *dpif_)
1428 const struct dpif_linux_vport *vport = vport_;
1429 struct dpif_linux *dpif = dpif_;
1432 if (vport->dp_ifindex == dpif->dp_ifindex
1433 && (vport->cmd == OVS_VPORT_CMD_NEW
1434 || vport->cmd == OVS_VPORT_CMD_DEL
1435 || vport->cmd == OVS_VPORT_CMD_SET)) {
1436 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1437 dpif->dpif.full_name, vport->name, vport->cmd);
1438 sset_add(&dpif->changed_ports, vport->name);
1441 dpif->change_error = true;
1445 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1446 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1447 * positive errno value.
1449 * 'vport' will contain pointers into 'buf', so the caller should not free
1450 * 'buf' while 'vport' is still in use. */
1452 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1453 const struct ofpbuf *buf)
1455 static const struct nl_policy ovs_vport_policy[] = {
1456 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1457 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1458 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1459 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1460 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1462 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1463 .min_len = ETH_ADDR_LEN,
1464 .max_len = ETH_ADDR_LEN,
1466 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1469 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1470 struct ovs_header *ovs_header;
1471 struct nlmsghdr *nlmsg;
1472 struct genlmsghdr *genl;
1475 dpif_linux_vport_init(vport);
1477 ofpbuf_use_const(&b, buf->data, buf->size);
1478 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1479 genl = ofpbuf_try_pull(&b, sizeof *genl);
1480 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1481 if (!nlmsg || !genl || !ovs_header
1482 || nlmsg->nlmsg_type != ovs_vport_family
1483 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1484 ARRAY_SIZE(ovs_vport_policy))) {
1488 vport->cmd = genl->cmd;
1489 vport->dp_ifindex = ovs_header->dp_ifindex;
1490 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1491 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1492 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1493 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1494 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1496 if (a[OVS_VPORT_ATTR_STATS]) {
1497 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1499 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1500 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1502 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1503 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1504 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1509 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1510 * followed by Netlink attributes corresponding to 'vport'. */
1512 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1515 struct ovs_header *ovs_header;
1517 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1518 vport->cmd, OVS_VPORT_VERSION);
1520 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1521 ovs_header->dp_ifindex = vport->dp_ifindex;
1523 if (vport->port_no != UINT32_MAX) {
1524 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1527 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1528 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1532 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1535 if (vport->upcall_pid) {
1536 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1540 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1541 vport->stats, sizeof *vport->stats);
1544 if (vport->address) {
1545 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1546 vport->address, ETH_ADDR_LEN);
1549 if (vport->options) {
1550 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1551 vport->options, vport->options_len);
1555 /* Clears 'vport' to "empty" values. */
1557 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1559 memset(vport, 0, sizeof *vport);
1560 vport->port_no = UINT32_MAX;
1563 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1564 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1565 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1566 * result of the command is expected to be an ovs_vport also, which is decoded
1567 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1568 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1570 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1571 struct dpif_linux_vport *reply,
1572 struct ofpbuf **bufp)
1574 struct ofpbuf *request_buf;
1577 assert((reply != NULL) == (bufp != NULL));
1579 error = dpif_linux_init();
1583 dpif_linux_vport_init(reply);
1588 request_buf = ofpbuf_new(1024);
1589 dpif_linux_vport_to_ofpbuf(request, request_buf);
1590 error = nl_sock_transact(genl_sock, request_buf, bufp);
1591 ofpbuf_delete(request_buf);
1595 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1598 dpif_linux_vport_init(reply);
1599 ofpbuf_delete(*bufp);
1606 /* Obtains information about the kernel vport named 'name' and stores it into
1607 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1608 * longer needed ('reply' will contain pointers into '*bufp'). */
1610 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1611 struct ofpbuf **bufp)
1613 struct dpif_linux_vport request;
1615 dpif_linux_vport_init(&request);
1616 request.cmd = OVS_VPORT_CMD_GET;
1617 request.name = name;
1619 return dpif_linux_vport_transact(&request, reply, bufp);
1622 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1623 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1624 * positive errno value.
1626 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1627 * while 'dp' is still in use. */
1629 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1631 static const struct nl_policy ovs_datapath_policy[] = {
1632 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1633 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1637 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1638 struct ovs_header *ovs_header;
1639 struct nlmsghdr *nlmsg;
1640 struct genlmsghdr *genl;
1643 dpif_linux_dp_init(dp);
1645 ofpbuf_use_const(&b, buf->data, buf->size);
1646 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1647 genl = ofpbuf_try_pull(&b, sizeof *genl);
1648 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1649 if (!nlmsg || !genl || !ovs_header
1650 || nlmsg->nlmsg_type != ovs_datapath_family
1651 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1652 ARRAY_SIZE(ovs_datapath_policy))) {
1656 dp->cmd = genl->cmd;
1657 dp->dp_ifindex = ovs_header->dp_ifindex;
1658 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1659 if (a[OVS_DP_ATTR_STATS]) {
1660 /* Can't use structure assignment because Netlink doesn't ensure
1661 * sufficient alignment for 64-bit members. */
1662 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1669 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1671 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1673 struct ovs_header *ovs_header;
1675 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1676 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1677 OVS_DATAPATH_VERSION);
1679 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1680 ovs_header->dp_ifindex = dp->dp_ifindex;
1683 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1686 if (dp->upcall_pid) {
1687 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1690 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1693 /* Clears 'dp' to "empty" values. */
1695 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1697 memset(dp, 0, sizeof *dp);
1701 dpif_linux_dp_dump_start(struct nl_dump *dump)
1703 struct dpif_linux_dp request;
1706 dpif_linux_dp_init(&request);
1707 request.cmd = OVS_DP_CMD_GET;
1709 buf = ofpbuf_new(1024);
1710 dpif_linux_dp_to_ofpbuf(&request, buf);
1711 nl_dump_start(dump, genl_sock, buf);
1715 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1716 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1717 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1718 * result of the command is expected to be of the same form, which is decoded
1719 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1720 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1722 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1723 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1725 struct ofpbuf *request_buf;
1728 assert((reply != NULL) == (bufp != NULL));
1730 request_buf = ofpbuf_new(1024);
1731 dpif_linux_dp_to_ofpbuf(request, request_buf);
1732 error = nl_sock_transact(genl_sock, request_buf, bufp);
1733 ofpbuf_delete(request_buf);
1737 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1740 dpif_linux_dp_init(reply);
1741 ofpbuf_delete(*bufp);
1748 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1749 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1750 * will contain pointers into '*bufp'). */
1752 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1753 struct ofpbuf **bufp)
1755 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1756 struct dpif_linux_dp request;
1758 dpif_linux_dp_init(&request);
1759 request.cmd = OVS_DP_CMD_GET;
1760 request.dp_ifindex = dpif->dp_ifindex;
1762 return dpif_linux_dp_transact(&request, reply, bufp);
1765 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1766 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1767 * positive errno value.
1769 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1770 * while 'flow' is still in use. */
1772 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1773 const struct ofpbuf *buf)
1775 static const struct nl_policy ovs_flow_policy[] = {
1776 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1777 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1778 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1780 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1781 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1782 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1785 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1786 struct ovs_header *ovs_header;
1787 struct nlmsghdr *nlmsg;
1788 struct genlmsghdr *genl;
1791 dpif_linux_flow_init(flow);
1793 ofpbuf_use_const(&b, buf->data, buf->size);
1794 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1795 genl = ofpbuf_try_pull(&b, sizeof *genl);
1796 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1797 if (!nlmsg || !genl || !ovs_header
1798 || nlmsg->nlmsg_type != ovs_flow_family
1799 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1800 ARRAY_SIZE(ovs_flow_policy))) {
1804 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1805 flow->dp_ifindex = ovs_header->dp_ifindex;
1806 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1807 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1808 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1809 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1810 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1812 if (a[OVS_FLOW_ATTR_STATS]) {
1813 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1815 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1816 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1818 if (a[OVS_FLOW_ATTR_USED]) {
1819 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1824 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1825 * followed by Netlink attributes corresponding to 'flow'. */
1827 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1830 struct ovs_header *ovs_header;
1832 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1833 NLM_F_REQUEST | flow->nlmsg_flags,
1834 flow->cmd, OVS_FLOW_VERSION);
1836 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1837 ovs_header->dp_ifindex = flow->dp_ifindex;
1839 if (flow->key_len) {
1840 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1843 if (flow->actions || flow->actions_len) {
1844 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1845 flow->actions, flow->actions_len);
1848 /* We never need to send these to the kernel. */
1849 assert(!flow->stats);
1850 assert(!flow->tcp_flags);
1851 assert(!flow->used);
1854 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1858 /* Clears 'flow' to "empty" values. */
1860 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1862 memset(flow, 0, sizeof *flow);
1865 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1866 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1867 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1868 * result of the command is expected to be a flow also, which is decoded and
1869 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1870 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1872 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1873 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1875 struct ofpbuf *request_buf;
1878 assert((reply != NULL) == (bufp != NULL));
1881 request->nlmsg_flags |= NLM_F_ECHO;
1884 request_buf = ofpbuf_new(1024);
1885 dpif_linux_flow_to_ofpbuf(request, request_buf);
1886 error = nl_sock_transact(genl_sock, request_buf, bufp);
1887 ofpbuf_delete(request_buf);
1891 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1894 dpif_linux_flow_init(reply);
1895 ofpbuf_delete(*bufp);
1903 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1904 struct dpif_flow_stats *stats)
1907 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1908 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1910 stats->n_packets = 0;
1913 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1914 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1917 /* Metwally "space-saving" algorithm implementation. */
1919 /* Updates 'ch' to record that a packet was received on 'port_no'. */
1921 update_sketch(struct dpif_channel *ch, uint32_t port_no)
1923 struct dpif_sketch *sk;
1925 /* Find an existing counting element for 'port_no' or, if none, replace the
1926 * counting element with the fewest hits by 'port_no'. */
1927 for (sk = ch->sketches; ; sk++) {
1928 if (port_no == sk->port_no) {
1930 } else if (sk == &ch->sketches[N_SKETCHES - 1]) {
1931 sk->port_no = port_no;
1932 sk->error = sk->hits;
1937 /* Increment the hit count, then re-sort the counting elements (usually
1938 * nothing needs to be done). */
1940 while (sk > ch->sketches && sk[-1].hits > sk->hits) {
1941 struct dpif_sketch tmp = sk[-1];
1948 /* Divide the counts of all the the counting elements in 'dpif' by 2. See the
1949 * comment on SCALE_INTERVAL. */
1951 scale_sketches(struct dpif *dpif_)
1953 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1954 struct dpif_channel *ch;
1956 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
1957 struct dpif_sketch *sk;
1959 for (sk = ch->sketches; sk < &ch->sketches[N_SKETCHES]; sk++) {
1966 /* Logs information about a packet that was recently lost in 'ch' (in
1969 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1971 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1972 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1973 struct dpif_sketch *sk;
1976 if (VLOG_DROP_ERR(&rl)) {
1981 if (ch->last_poll != LLONG_MIN) {
1982 ds_put_format(&s, " (last polled %lld ms ago)",
1983 time_msec() - ch->last_poll);
1985 ds_put_cstr(&s, ", most frequent sources are");
1986 for (sk = ch->sketches; sk < &ch->sketches[N_SKETCHES]; sk++) {
1988 struct dpif_port port;
1990 ds_put_format(&s, " %"PRIu32, sk->port_no);
1991 if (!dpif_port_query_by_number(dpif_, sk->port_no, &port)) {
1992 ds_put_format(&s, "(%s)", port.name);
1993 dpif_port_destroy(&port);
1996 ds_put_format(&s, ": %u to %u,",
1997 sk->hits - sk->error, sk->hits);
1999 ds_put_format(&s, ": %u,", sk->hits);
2005 VLOG_ERR("%s: lost packet on channel %td%s",
2006 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));