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 /* Loop until we find a port that isn't used. */
431 request.port_no = ++dpif->alloc_port_no;
432 upcall_pid = dpif_linux_port_get_pid(dpif_, request.port_no);
433 request.upcall_pid = &upcall_pid;
434 error = dpif_linux_vport_transact(&request, &reply, &buf);
437 *port_nop = reply.port_no;
438 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
439 dpif_name(dpif_), request.port_no, upcall_pid);
440 } else if (error == EFBIG) {
441 /* Older datapath has lower limit. */
442 max_ports = dpif->alloc_port_no;
443 dpif->alloc_port_no = 0;
447 } while ((i++ < max_ports)
448 && (error == EBUSY || error == EFBIG));
454 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
456 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
457 struct dpif_linux_vport vport;
460 dpif_linux_vport_init(&vport);
461 vport.cmd = OVS_VPORT_CMD_DEL;
462 vport.dp_ifindex = dpif->dp_ifindex;
463 vport.port_no = port_no;
464 error = dpif_linux_vport_transact(&vport, NULL, NULL);
470 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
471 const char *port_name, struct dpif_port *dpif_port)
473 struct dpif_linux_vport request;
474 struct dpif_linux_vport reply;
478 dpif_linux_vport_init(&request);
479 request.cmd = OVS_VPORT_CMD_GET;
480 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
481 request.port_no = port_no;
482 request.name = port_name;
484 error = dpif_linux_vport_transact(&request, &reply, &buf);
486 if (reply.dp_ifindex != request.dp_ifindex) {
487 /* A query by name reported that 'port_name' is in some datapath
488 * other than 'dpif', but the caller wants to know about 'dpif'. */
491 dpif_port->name = xstrdup(reply.name);
492 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
493 dpif_port->port_no = reply.port_no;
501 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
502 struct dpif_port *dpif_port)
504 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
508 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
509 struct dpif_port *dpif_port)
511 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
515 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
521 dpif_linux_port_get_pid(const struct dpif *dpif_, uint16_t port_no)
523 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
525 if (dpif->epoll_fd < 0) {
530 idx = (port_no != UINT16_MAX
531 ? 1 + (port_no & (N_CHANNELS - 2))
533 return nl_sock_pid(dpif->channels[idx].sock);
538 dpif_linux_flow_flush(struct dpif *dpif_)
540 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
541 struct dpif_linux_flow flow;
543 dpif_linux_flow_init(&flow);
544 flow.cmd = OVS_FLOW_CMD_DEL;
545 flow.dp_ifindex = dpif->dp_ifindex;
546 return dpif_linux_flow_transact(&flow, NULL, NULL);
549 struct dpif_linux_port_state {
554 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
556 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
557 struct dpif_linux_port_state *state;
558 struct dpif_linux_vport request;
561 *statep = state = xmalloc(sizeof *state);
563 dpif_linux_vport_init(&request);
564 request.cmd = OVS_DP_CMD_GET;
565 request.dp_ifindex = dpif->dp_ifindex;
567 buf = ofpbuf_new(1024);
568 dpif_linux_vport_to_ofpbuf(&request, buf);
569 nl_dump_start(&state->dump, genl_sock, buf);
576 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
577 struct dpif_port *dpif_port)
579 struct dpif_linux_port_state *state = state_;
580 struct dpif_linux_vport vport;
584 if (!nl_dump_next(&state->dump, &buf)) {
588 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
593 dpif_port->name = (char *) vport.name;
594 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
595 dpif_port->port_no = vport.port_no;
600 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
602 struct dpif_linux_port_state *state = state_;
603 int error = nl_dump_done(&state->dump);
610 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
612 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
614 if (dpif->change_error) {
615 dpif->change_error = false;
616 sset_clear(&dpif->changed_ports);
618 } else if (!sset_is_empty(&dpif->changed_ports)) {
619 *devnamep = sset_pop(&dpif->changed_ports);
627 dpif_linux_port_poll_wait(const struct dpif *dpif_)
629 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
630 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
631 poll_immediate_wake();
636 dpif_linux_flow_get__(const struct dpif *dpif_,
637 const struct nlattr *key, size_t key_len,
638 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
640 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
641 struct dpif_linux_flow request;
643 dpif_linux_flow_init(&request);
644 request.cmd = OVS_FLOW_CMD_GET;
645 request.dp_ifindex = dpif->dp_ifindex;
647 request.key_len = key_len;
648 return dpif_linux_flow_transact(&request, reply, bufp);
652 dpif_linux_flow_get(const struct dpif *dpif_,
653 const struct nlattr *key, size_t key_len,
654 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
656 struct dpif_linux_flow reply;
660 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
663 dpif_linux_flow_get_stats(&reply, stats);
666 buf->data = (void *) reply.actions;
667 buf->size = reply.actions_len;
677 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
678 struct dpif_linux_flow *request)
680 static struct nlattr dummy_action;
682 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
684 dpif_linux_flow_init(request);
685 request->cmd = (put->flags & DPIF_FP_CREATE
686 ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
687 request->dp_ifindex = dpif->dp_ifindex;
688 request->key = put->key;
689 request->key_len = put->key_len;
690 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
691 request->actions = put->actions ? put->actions : &dummy_action;
692 request->actions_len = put->actions_len;
693 if (put->flags & DPIF_FP_ZERO_STATS) {
694 request->clear = true;
696 request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
700 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
702 struct dpif_linux_flow request, reply;
706 dpif_linux_init_flow_put(dpif_, put, &request);
707 error = dpif_linux_flow_transact(&request,
708 put->stats ? &reply : NULL,
709 put->stats ? &buf : NULL);
710 if (!error && put->stats) {
711 dpif_linux_flow_get_stats(&reply, put->stats);
718 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
719 struct dpif_linux_flow *request)
721 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
723 dpif_linux_flow_init(request);
724 request->cmd = OVS_FLOW_CMD_DEL;
725 request->dp_ifindex = dpif->dp_ifindex;
726 request->key = del->key;
727 request->key_len = del->key_len;
731 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
733 struct dpif_linux_flow request, reply;
737 dpif_linux_init_flow_del(dpif_, del, &request);
738 error = dpif_linux_flow_transact(&request,
739 del->stats ? &reply : NULL,
740 del->stats ? &buf : NULL);
741 if (!error && del->stats) {
742 dpif_linux_flow_get_stats(&reply, del->stats);
748 struct dpif_linux_flow_state {
750 struct dpif_linux_flow flow;
751 struct dpif_flow_stats stats;
756 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
758 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
759 struct dpif_linux_flow_state *state;
760 struct dpif_linux_flow request;
763 *statep = state = xmalloc(sizeof *state);
765 dpif_linux_flow_init(&request);
766 request.cmd = OVS_DP_CMD_GET;
767 request.dp_ifindex = dpif->dp_ifindex;
769 buf = ofpbuf_new(1024);
770 dpif_linux_flow_to_ofpbuf(&request, buf);
771 nl_dump_start(&state->dump, genl_sock, buf);
780 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
781 const struct nlattr **key, size_t *key_len,
782 const struct nlattr **actions, size_t *actions_len,
783 const struct dpif_flow_stats **stats)
785 struct dpif_linux_flow_state *state = state_;
790 ofpbuf_delete(state->buf);
793 if (!nl_dump_next(&state->dump, &buf)) {
797 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
802 if (actions && !state->flow.actions) {
803 error = dpif_linux_flow_get__(dpif_, state->flow.key,
805 &state->flow, &state->buf);
806 if (error == ENOENT) {
807 VLOG_DBG("dumped flow disappeared on get");
809 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
815 *actions = state->flow.actions;
816 *actions_len = state->flow.actions_len;
819 *key = state->flow.key;
820 *key_len = state->flow.key_len;
823 dpif_linux_flow_get_stats(&state->flow, &state->stats);
824 *stats = &state->stats;
830 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
832 struct dpif_linux_flow_state *state = state_;
833 int error = nl_dump_done(&state->dump);
834 ofpbuf_delete(state->buf);
840 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
843 struct ovs_header *k_exec;
845 ofpbuf_prealloc_tailroom(buf, (64
846 + d_exec->packet->size
848 + d_exec->actions_len));
850 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
851 OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
853 k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
854 k_exec->dp_ifindex = dp_ifindex;
856 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
857 d_exec->packet->data, d_exec->packet->size);
858 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
859 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
860 d_exec->actions, d_exec->actions_len);
864 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
866 uint64_t request_stub[1024 / 8];
867 struct ofpbuf request;
870 ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
871 dpif_linux_encode_execute(dp_ifindex, execute, &request);
872 error = nl_sock_transact(genl_sock, &request, NULL);
873 ofpbuf_uninit(&request);
879 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
881 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
883 return dpif_linux_execute__(dpif->dp_ifindex, execute);
889 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
891 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
894 struct nl_transaction txn;
896 struct ofpbuf request;
897 uint64_t request_stub[1024 / 8];
900 uint64_t reply_stub[1024 / 8];
903 struct nl_transaction *txnsp[MAX_OPS];
906 assert(n_ops <= MAX_OPS);
907 for (i = 0; i < n_ops; i++) {
908 struct op_auxdata *aux = &auxes[i];
909 struct dpif_op *op = ops[i];
910 struct dpif_flow_put *put;
911 struct dpif_flow_del *del;
912 struct dpif_execute *execute;
913 struct dpif_linux_flow flow;
915 ofpbuf_use_stub(&aux->request,
916 aux->request_stub, sizeof aux->request_stub);
917 aux->txn.request = &aux->request;
919 ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
920 aux->txn.reply = NULL;
923 case DPIF_OP_FLOW_PUT:
924 put = &op->u.flow_put;
925 dpif_linux_init_flow_put(dpif_, put, &flow);
927 flow.nlmsg_flags |= NLM_F_ECHO;
928 aux->txn.reply = &aux->reply;
930 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
933 case DPIF_OP_FLOW_DEL:
934 del = &op->u.flow_del;
935 dpif_linux_init_flow_del(dpif_, del, &flow);
937 flow.nlmsg_flags |= NLM_F_ECHO;
938 aux->txn.reply = &aux->reply;
940 dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
943 case DPIF_OP_EXECUTE:
944 execute = &op->u.execute;
945 dpif_linux_encode_execute(dpif->dp_ifindex, execute,
954 for (i = 0; i < n_ops; i++) {
955 txnsp[i] = &auxes[i].txn;
957 nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
959 for (i = 0; i < n_ops; i++) {
960 struct op_auxdata *aux = &auxes[i];
961 struct nl_transaction *txn = &auxes[i].txn;
962 struct dpif_op *op = ops[i];
963 struct dpif_flow_put *put;
964 struct dpif_flow_del *del;
966 op->error = txn->error;
969 case DPIF_OP_FLOW_PUT:
970 put = &op->u.flow_put;
971 if (!op->error && put->stats) {
972 struct dpif_linux_flow reply;
974 op->error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
976 dpif_linux_flow_get_stats(&reply, put->stats);
981 case DPIF_OP_FLOW_DEL:
982 del = &op->u.flow_del;
983 if (!op->error && del->stats) {
984 struct dpif_linux_flow reply;
986 op->error = dpif_linux_flow_from_ofpbuf(&reply, txn->reply);
988 dpif_linux_flow_get_stats(&reply, del->stats);
993 case DPIF_OP_EXECUTE:
1000 ofpbuf_uninit(&aux->request);
1001 ofpbuf_uninit(&aux->reply);
1006 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1009 size_t chunk = MIN(n_ops, MAX_OPS);
1010 dpif_linux_operate__(dpif, ops, chunk);
1017 set_upcall_pids(struct dpif *dpif_)
1019 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1020 struct dpif_port_dump port_dump;
1021 struct dpif_port port;
1024 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1025 uint32_t upcall_pid = dpif_linux_port_get_pid(dpif_, port.port_no);
1026 struct dpif_linux_vport vport_request;
1028 dpif_linux_vport_init(&vport_request);
1029 vport_request.cmd = OVS_VPORT_CMD_SET;
1030 vport_request.dp_ifindex = dpif->dp_ifindex;
1031 vport_request.port_no = port.port_no;
1032 vport_request.upcall_pid = &upcall_pid;
1033 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1035 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1036 dpif_name(&dpif->dpif), vport_request.port_no,
1039 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
1040 dpif_name(&dpif->dpif), strerror(error));
1046 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1048 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1050 if ((dpif->epoll_fd >= 0) == enable) {
1055 destroy_channels(dpif);
1057 struct dpif_channel *ch;
1060 dpif->epoll_fd = epoll_create(N_CHANNELS);
1061 if (dpif->epoll_fd < 0) {
1065 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
1066 int indx = ch - dpif->channels;
1067 struct epoll_event event;
1069 error = nl_sock_create(NETLINK_GENERIC, &ch->sock);
1071 destroy_channels(dpif);
1075 memset(&event, 0, sizeof event);
1076 event.events = EPOLLIN;
1077 event.data.u32 = indx;
1078 if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(ch->sock),
1081 destroy_channels(dpif);
1085 memset(ch->sketches, 0, sizeof ch->sketches);
1086 ch->last_poll = LLONG_MIN;
1089 dpif->ready_mask = 0;
1090 dpif->next_scale = time_msec() + SCALE_INTERVAL;
1093 set_upcall_pids(dpif_);
1099 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1100 uint32_t queue_id, uint32_t *priority)
1102 if (queue_id < 0xf000) {
1103 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1111 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1114 static const struct nl_policy ovs_packet_policy[] = {
1115 /* Always present. */
1116 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1117 .min_len = ETH_HEADER_LEN },
1118 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1120 /* OVS_PACKET_CMD_ACTION only. */
1121 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1124 struct ovs_header *ovs_header;
1125 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1126 struct nlmsghdr *nlmsg;
1127 struct genlmsghdr *genl;
1131 ofpbuf_use_const(&b, buf->data, buf->size);
1133 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1134 genl = ofpbuf_try_pull(&b, sizeof *genl);
1135 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1136 if (!nlmsg || !genl || !ovs_header
1137 || nlmsg->nlmsg_type != ovs_packet_family
1138 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1139 ARRAY_SIZE(ovs_packet_policy))) {
1143 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1144 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1150 memset(upcall, 0, sizeof *upcall);
1151 upcall->type = type;
1152 upcall->packet = buf;
1153 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
1154 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1155 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
1156 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1157 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1158 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1160 *dp_ifindex = ovs_header->dp_ifindex;
1166 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1169 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1172 if (dpif->epoll_fd < 0) {
1176 if (!dpif->ready_mask) {
1177 struct epoll_event events[N_CHANNELS];
1182 retval = epoll_wait(dpif->epoll_fd, events, N_CHANNELS, 0);
1183 } while (retval < 0 && errno == EINTR);
1185 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1186 VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1189 for (i = 0; i < retval; i++) {
1190 dpif->ready_mask |= 1u << events[i].data.u32;
1194 while (dpif->ready_mask) {
1195 int indx = ffs(dpif->ready_mask) - 1;
1196 struct dpif_channel *ch = &dpif->channels[indx];
1198 dpif->ready_mask &= ~(1u << indx);
1204 if (++read_tries > 50) {
1208 error = nl_sock_recv(ch->sock, buf, false);
1209 if (error == ENOBUFS) {
1210 /* ENOBUFS typically means that we've received so many
1211 * packets that the buffer overflowed. Try again
1212 * immediately because there's almost certainly a packet
1213 * waiting for us. */
1214 report_loss(dpif_, ch);
1218 ch->last_poll = time_msec();
1220 if (error == EAGAIN) {
1226 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1227 if (!error && dp_ifindex == dpif->dp_ifindex) {
1228 const struct nlattr *in_port;
1230 in_port = nl_attr_find__(upcall->key, upcall->key_len,
1231 OVS_KEY_ATTR_IN_PORT);
1233 update_sketch(ch, nl_attr_get_u32(in_port));
1247 dpif_linux_recv_wait(struct dpif *dpif_)
1249 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1251 if (dpif->epoll_fd < 0) {
1255 poll_fd_wait(dpif->epoll_fd, POLLIN);
1259 dpif_linux_recv_purge(struct dpif *dpif_)
1261 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1262 struct dpif_channel *ch;
1264 if (dpif->epoll_fd < 0) {
1268 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
1269 nl_sock_drain(ch->sock);
1273 const struct dpif_class dpif_linux_class = {
1275 dpif_linux_enumerate,
1281 dpif_linux_get_stats,
1282 dpif_linux_port_add,
1283 dpif_linux_port_del,
1284 dpif_linux_port_query_by_number,
1285 dpif_linux_port_query_by_name,
1286 dpif_linux_get_max_ports,
1287 dpif_linux_port_get_pid,
1288 dpif_linux_port_dump_start,
1289 dpif_linux_port_dump_next,
1290 dpif_linux_port_dump_done,
1291 dpif_linux_port_poll,
1292 dpif_linux_port_poll_wait,
1293 dpif_linux_flow_get,
1294 dpif_linux_flow_put,
1295 dpif_linux_flow_del,
1296 dpif_linux_flow_flush,
1297 dpif_linux_flow_dump_start,
1298 dpif_linux_flow_dump_next,
1299 dpif_linux_flow_dump_done,
1302 dpif_linux_recv_set,
1303 dpif_linux_queue_to_priority,
1305 dpif_linux_recv_wait,
1306 dpif_linux_recv_purge,
1310 dpif_linux_init(void)
1312 static int error = -1;
1315 unsigned int ovs_vport_mcgroup;
1317 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1318 &ovs_datapath_family);
1320 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1321 "The Open vSwitch kernel module is probably not loaded.",
1322 OVS_DATAPATH_FAMILY);
1325 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1328 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1331 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1332 &ovs_packet_family);
1335 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1338 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1340 OVS_VPORT_MCGROUP_FALLBACK_ID);
1343 static struct dpif_linux_vport vport;
1344 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1345 dpif_linux_nln_parse, &vport);
1353 dpif_linux_is_internal_device(const char *name)
1355 struct dpif_linux_vport reply;
1359 error = dpif_linux_vport_get(name, &reply, &buf);
1362 } else if (error != ENODEV && error != ENOENT) {
1363 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1364 name, strerror(error));
1367 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1371 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1372 const void *data, size_t size)
1374 struct ofpbuf actions, key, packet;
1375 struct odputil_keybuf keybuf;
1376 struct dpif_execute execute;
1380 ofpbuf_use_const(&packet, data, size);
1381 flow_extract(&packet, 0, htonll(0), 0, &flow);
1383 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1384 odp_flow_key_from_flow(&key, &flow);
1386 ofpbuf_use_stack(&actions, &action, sizeof action);
1387 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1389 execute.key = key.data;
1390 execute.key_len = key.size;
1391 execute.actions = actions.data;
1392 execute.actions_len = actions.size;
1393 execute.packet = &packet;
1394 return dpif_linux_execute__(dp_ifindex, &execute);
1398 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1400 struct dpif_linux_vport *vport = vport_;
1401 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1405 dpif_linux_port_changed(const void *vport_, void *dpif_)
1407 const struct dpif_linux_vport *vport = vport_;
1408 struct dpif_linux *dpif = dpif_;
1411 if (vport->dp_ifindex == dpif->dp_ifindex
1412 && (vport->cmd == OVS_VPORT_CMD_NEW
1413 || vport->cmd == OVS_VPORT_CMD_DEL
1414 || vport->cmd == OVS_VPORT_CMD_SET)) {
1415 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1416 dpif->dpif.full_name, vport->name, vport->cmd);
1417 sset_add(&dpif->changed_ports, vport->name);
1420 dpif->change_error = true;
1424 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1425 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1426 * positive errno value.
1428 * 'vport' will contain pointers into 'buf', so the caller should not free
1429 * 'buf' while 'vport' is still in use. */
1431 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1432 const struct ofpbuf *buf)
1434 static const struct nl_policy ovs_vport_policy[] = {
1435 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1436 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1437 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1438 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1439 [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1441 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1442 .min_len = ETH_ADDR_LEN,
1443 .max_len = ETH_ADDR_LEN,
1445 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1448 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1449 struct ovs_header *ovs_header;
1450 struct nlmsghdr *nlmsg;
1451 struct genlmsghdr *genl;
1454 dpif_linux_vport_init(vport);
1456 ofpbuf_use_const(&b, buf->data, buf->size);
1457 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1458 genl = ofpbuf_try_pull(&b, sizeof *genl);
1459 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1460 if (!nlmsg || !genl || !ovs_header
1461 || nlmsg->nlmsg_type != ovs_vport_family
1462 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1463 ARRAY_SIZE(ovs_vport_policy))) {
1467 vport->cmd = genl->cmd;
1468 vport->dp_ifindex = ovs_header->dp_ifindex;
1469 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1470 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1471 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1472 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1473 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1475 if (a[OVS_VPORT_ATTR_STATS]) {
1476 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1478 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1479 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1481 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1482 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1483 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1488 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1489 * followed by Netlink attributes corresponding to 'vport'. */
1491 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1494 struct ovs_header *ovs_header;
1496 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1497 vport->cmd, OVS_VPORT_VERSION);
1499 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1500 ovs_header->dp_ifindex = vport->dp_ifindex;
1502 if (vport->port_no != UINT32_MAX) {
1503 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1506 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1507 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1511 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1514 if (vport->upcall_pid) {
1515 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1519 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1520 vport->stats, sizeof *vport->stats);
1523 if (vport->address) {
1524 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1525 vport->address, ETH_ADDR_LEN);
1528 if (vport->options) {
1529 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1530 vport->options, vport->options_len);
1534 /* Clears 'vport' to "empty" values. */
1536 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1538 memset(vport, 0, sizeof *vport);
1539 vport->port_no = UINT32_MAX;
1542 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1543 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1544 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1545 * result of the command is expected to be an ovs_vport also, which is decoded
1546 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1547 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1549 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1550 struct dpif_linux_vport *reply,
1551 struct ofpbuf **bufp)
1553 struct ofpbuf *request_buf;
1556 assert((reply != NULL) == (bufp != NULL));
1558 error = dpif_linux_init();
1562 dpif_linux_vport_init(reply);
1567 request_buf = ofpbuf_new(1024);
1568 dpif_linux_vport_to_ofpbuf(request, request_buf);
1569 error = nl_sock_transact(genl_sock, request_buf, bufp);
1570 ofpbuf_delete(request_buf);
1574 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1577 dpif_linux_vport_init(reply);
1578 ofpbuf_delete(*bufp);
1585 /* Obtains information about the kernel vport named 'name' and stores it into
1586 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1587 * longer needed ('reply' will contain pointers into '*bufp'). */
1589 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1590 struct ofpbuf **bufp)
1592 struct dpif_linux_vport request;
1594 dpif_linux_vport_init(&request);
1595 request.cmd = OVS_VPORT_CMD_GET;
1596 request.name = name;
1598 return dpif_linux_vport_transact(&request, reply, bufp);
1601 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1602 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1603 * positive errno value.
1605 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1606 * while 'dp' is still in use. */
1608 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1610 static const struct nl_policy ovs_datapath_policy[] = {
1611 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1612 [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1616 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1617 struct ovs_header *ovs_header;
1618 struct nlmsghdr *nlmsg;
1619 struct genlmsghdr *genl;
1622 dpif_linux_dp_init(dp);
1624 ofpbuf_use_const(&b, buf->data, buf->size);
1625 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1626 genl = ofpbuf_try_pull(&b, sizeof *genl);
1627 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1628 if (!nlmsg || !genl || !ovs_header
1629 || nlmsg->nlmsg_type != ovs_datapath_family
1630 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1631 ARRAY_SIZE(ovs_datapath_policy))) {
1635 dp->cmd = genl->cmd;
1636 dp->dp_ifindex = ovs_header->dp_ifindex;
1637 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1638 if (a[OVS_DP_ATTR_STATS]) {
1639 /* Can't use structure assignment because Netlink doesn't ensure
1640 * sufficient alignment for 64-bit members. */
1641 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1648 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1650 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1652 struct ovs_header *ovs_header;
1654 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1655 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1656 OVS_DATAPATH_VERSION);
1658 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1659 ovs_header->dp_ifindex = dp->dp_ifindex;
1662 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1665 if (dp->upcall_pid) {
1666 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1669 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1672 /* Clears 'dp' to "empty" values. */
1674 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1676 memset(dp, 0, sizeof *dp);
1680 dpif_linux_dp_dump_start(struct nl_dump *dump)
1682 struct dpif_linux_dp request;
1685 dpif_linux_dp_init(&request);
1686 request.cmd = OVS_DP_CMD_GET;
1688 buf = ofpbuf_new(1024);
1689 dpif_linux_dp_to_ofpbuf(&request, buf);
1690 nl_dump_start(dump, genl_sock, buf);
1694 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1695 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1696 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1697 * result of the command is expected to be of the same form, which is decoded
1698 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1699 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1701 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1702 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1704 struct ofpbuf *request_buf;
1707 assert((reply != NULL) == (bufp != NULL));
1709 request_buf = ofpbuf_new(1024);
1710 dpif_linux_dp_to_ofpbuf(request, request_buf);
1711 error = nl_sock_transact(genl_sock, request_buf, bufp);
1712 ofpbuf_delete(request_buf);
1716 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1719 dpif_linux_dp_init(reply);
1720 ofpbuf_delete(*bufp);
1727 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1728 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1729 * will contain pointers into '*bufp'). */
1731 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1732 struct ofpbuf **bufp)
1734 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1735 struct dpif_linux_dp request;
1737 dpif_linux_dp_init(&request);
1738 request.cmd = OVS_DP_CMD_GET;
1739 request.dp_ifindex = dpif->dp_ifindex;
1741 return dpif_linux_dp_transact(&request, reply, bufp);
1744 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1745 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1746 * positive errno value.
1748 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1749 * while 'flow' is still in use. */
1751 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1752 const struct ofpbuf *buf)
1754 static const struct nl_policy ovs_flow_policy[] = {
1755 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1756 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1757 [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1759 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1760 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1761 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1764 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1765 struct ovs_header *ovs_header;
1766 struct nlmsghdr *nlmsg;
1767 struct genlmsghdr *genl;
1770 dpif_linux_flow_init(flow);
1772 ofpbuf_use_const(&b, buf->data, buf->size);
1773 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1774 genl = ofpbuf_try_pull(&b, sizeof *genl);
1775 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1776 if (!nlmsg || !genl || !ovs_header
1777 || nlmsg->nlmsg_type != ovs_flow_family
1778 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1779 ARRAY_SIZE(ovs_flow_policy))) {
1783 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1784 flow->dp_ifindex = ovs_header->dp_ifindex;
1785 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1786 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1787 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1788 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1789 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1791 if (a[OVS_FLOW_ATTR_STATS]) {
1792 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1794 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1795 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1797 if (a[OVS_FLOW_ATTR_USED]) {
1798 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1803 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1804 * followed by Netlink attributes corresponding to 'flow'. */
1806 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1809 struct ovs_header *ovs_header;
1811 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1812 NLM_F_REQUEST | flow->nlmsg_flags,
1813 flow->cmd, OVS_FLOW_VERSION);
1815 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1816 ovs_header->dp_ifindex = flow->dp_ifindex;
1818 if (flow->key_len) {
1819 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1822 if (flow->actions || flow->actions_len) {
1823 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1824 flow->actions, flow->actions_len);
1827 /* We never need to send these to the kernel. */
1828 assert(!flow->stats);
1829 assert(!flow->tcp_flags);
1830 assert(!flow->used);
1833 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1837 /* Clears 'flow' to "empty" values. */
1839 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1841 memset(flow, 0, sizeof *flow);
1844 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1845 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1846 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1847 * result of the command is expected to be a flow also, which is decoded and
1848 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1849 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1851 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1852 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1854 struct ofpbuf *request_buf;
1857 assert((reply != NULL) == (bufp != NULL));
1860 request->nlmsg_flags |= NLM_F_ECHO;
1863 request_buf = ofpbuf_new(1024);
1864 dpif_linux_flow_to_ofpbuf(request, request_buf);
1865 error = nl_sock_transact(genl_sock, request_buf, bufp);
1866 ofpbuf_delete(request_buf);
1870 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1873 dpif_linux_flow_init(reply);
1874 ofpbuf_delete(*bufp);
1882 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1883 struct dpif_flow_stats *stats)
1886 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1887 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1889 stats->n_packets = 0;
1892 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1893 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1896 /* Metwally "space-saving" algorithm implementation. */
1898 /* Updates 'ch' to record that a packet was received on 'port_no'. */
1900 update_sketch(struct dpif_channel *ch, uint32_t port_no)
1902 struct dpif_sketch *sk;
1904 /* Find an existing counting element for 'port_no' or, if none, replace the
1905 * counting element with the fewest hits by 'port_no'. */
1906 for (sk = ch->sketches; ; sk++) {
1907 if (port_no == sk->port_no) {
1909 } else if (sk == &ch->sketches[N_SKETCHES - 1]) {
1910 sk->port_no = port_no;
1911 sk->error = sk->hits;
1916 /* Increment the hit count, then re-sort the counting elements (usually
1917 * nothing needs to be done). */
1919 while (sk > ch->sketches && sk[-1].hits > sk->hits) {
1920 struct dpif_sketch tmp = sk[-1];
1927 /* Divide the counts of all the the counting elements in 'dpif' by 2. See the
1928 * comment on SCALE_INTERVAL. */
1930 scale_sketches(struct dpif *dpif_)
1932 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1933 struct dpif_channel *ch;
1935 for (ch = dpif->channels; ch < &dpif->channels[N_CHANNELS]; ch++) {
1936 struct dpif_sketch *sk;
1938 for (sk = ch->sketches; sk < &ch->sketches[N_SKETCHES]; sk++) {
1945 /* Logs information about a packet that was recently lost in 'ch' (in
1948 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1950 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1951 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1952 struct dpif_sketch *sk;
1955 if (VLOG_DROP_ERR(&rl)) {
1960 if (ch->last_poll != LLONG_MIN) {
1961 ds_put_format(&s, " (last polled %lld ms ago)",
1962 time_msec() - ch->last_poll);
1964 ds_put_cstr(&s, ", most frequent sources are");
1965 for (sk = ch->sketches; sk < &ch->sketches[N_SKETCHES]; sk++) {
1967 struct dpif_port port;
1969 ds_put_format(&s, " %"PRIu32, sk->port_no);
1970 if (!dpif_port_query_by_number(dpif_, sk->port_no, &port)) {
1971 ds_put_format(&s, "(%s)", port.name);
1972 dpif_port_destroy(&port);
1975 ds_put_format(&s, ": %u to %u,",
1976 sk->hits - sk->error, sk->hits);
1978 ds_put_format(&s, ": %u,", sk->hits);
1984 VLOG_ERR("%s: lost packet on channel %td%s",
1985 dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));