2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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>
36 #include "dpif-provider.h"
37 #include "dynamic-string.h"
40 #include "netdev-linux.h"
41 #include "netdev-vport.h"
42 #include "netlink-notifier.h"
43 #include "netlink-socket.h"
47 #include "openvswitch/datapath-compat.h"
48 #include "openvswitch/tunnel.h"
50 #include "poll-loop.h"
54 #include "unaligned.h"
58 VLOG_DEFINE_THIS_MODULE(dpif_linux);
60 enum { LRU_MAX_PORTS = 1024 };
61 enum { LRU_MASK = LRU_MAX_PORTS - 1};
62 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
64 enum { N_UPCALL_SOCKS = 16 };
65 BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
67 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
68 * missing if we have old headers. */
69 #define ETH_FLAG_LRO (1 << 15) /* LRO is enabled */
71 struct dpif_linux_dp {
72 /* Generic Netlink header. */
75 /* struct ovs_header. */
79 const char *name; /* OVS_DP_ATTR_NAME. */
80 const uint32_t *upcall_pid; /* OVS_DP_UPCALL_PID. */
81 struct ovs_dp_stats stats; /* OVS_DP_ATTR_STATS. */
82 enum ovs_frag_handling ipv4_frags; /* OVS_DP_ATTR_IPV4_FRAGS. */
85 static void dpif_linux_dp_init(struct dpif_linux_dp *);
86 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
87 const struct ofpbuf *);
88 static void dpif_linux_dp_dump_start(struct nl_dump *);
89 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
90 struct dpif_linux_dp *reply,
91 struct ofpbuf **bufp);
92 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
93 struct ofpbuf **bufp);
95 struct dpif_linux_flow {
96 /* Generic Netlink header. */
99 /* struct ovs_header. */
100 unsigned int nlmsg_flags;
105 * The 'stats' member points to 64-bit data that might only be aligned on
106 * 32-bit boundaries, so get_unaligned_u64() should be used to access its
109 * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
110 * the Netlink version of the command, even if actions_len is zero. */
111 const struct nlattr *key; /* OVS_FLOW_ATTR_KEY. */
113 const struct nlattr *actions; /* OVS_FLOW_ATTR_ACTIONS. */
115 const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
116 const uint8_t *tcp_flags; /* OVS_FLOW_ATTR_TCP_FLAGS. */
117 const ovs_32aligned_u64 *used; /* OVS_FLOW_ATTR_USED. */
118 bool clear; /* OVS_FLOW_ATTR_CLEAR. */
121 static void dpif_linux_flow_init(struct dpif_linux_flow *);
122 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
123 const struct ofpbuf *);
124 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
126 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
127 struct dpif_linux_flow *reply,
128 struct ofpbuf **bufp);
129 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
130 struct dpif_flow_stats *);
132 /* Datapath interface for the openvswitch Linux kernel module. */
137 /* Upcall messages. */
138 struct nl_sock *upcall_socks[N_UPCALL_SOCKS];
139 int last_read_upcall;
140 unsigned int listen_mask;
142 /* Change notification. */
143 struct sset changed_ports; /* Ports that have changed. */
144 struct nln_notifier *port_notifier;
147 /* Queue of unused ports. */
148 unsigned long *lru_bitmap;
149 uint16_t lru_ports[LRU_MAX_PORTS];
154 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
156 /* Generic Netlink family numbers for OVS. */
157 static int ovs_datapath_family;
158 static int ovs_vport_family;
159 static int ovs_flow_family;
160 static int ovs_packet_family;
162 /* Generic Netlink socket. */
163 static struct nl_sock *genl_sock;
164 static struct nln *nln = NULL;
166 static int dpif_linux_init(void);
167 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
168 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
169 static void dpif_linux_port_changed(const void *vport, void *dpif);
170 static uint32_t dpif_linux_port_get_pid__(const struct dpif *,
172 enum dpif_upcall_type);
174 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
176 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
177 const struct ofpbuf *);
179 static struct dpif_linux *
180 dpif_linux_cast(const struct dpif *dpif)
182 dpif_assert_class(dpif, &dpif_linux_class);
183 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
187 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
189 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
190 bitmap_set1(dp->lru_bitmap, port);
191 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
196 dpif_linux_pop_port(struct dpif_linux *dp)
200 if (dp->lru_head == dp->lru_tail) {
204 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
205 bitmap_set0(dp->lru_bitmap, port);
210 dpif_linux_enumerate(struct sset *all_dps)
216 error = dpif_linux_init();
221 dpif_linux_dp_dump_start(&dump);
222 while (nl_dump_next(&dump, &msg)) {
223 struct dpif_linux_dp dp;
225 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
226 sset_add(all_dps, dp.name);
229 return nl_dump_done(&dump);
233 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
234 bool create, struct dpif **dpifp)
236 struct dpif_linux_dp dp_request, dp;
240 error = dpif_linux_init();
245 /* Create or look up datapath. */
246 dpif_linux_dp_init(&dp_request);
247 dp_request.cmd = create ? OVS_DP_CMD_NEW : OVS_DP_CMD_GET;
248 dp_request.name = name;
249 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
254 open_dpif(&dp, dpifp);
260 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
262 struct dpif_linux *dpif;
265 dpif = xzalloc(sizeof *dpif);
266 dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
269 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
270 dp->dp_ifindex, dp->dp_ifindex);
272 dpif->dp_ifindex = dp->dp_ifindex;
273 sset_init(&dpif->changed_ports);
274 *dpifp = &dpif->dpif;
276 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
277 bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
278 for (i = 1; i < LRU_MAX_PORTS; i++) {
279 dpif_linux_push_port(dpif, i);
284 destroy_upcall_socks(struct dpif_linux *dpif)
288 for (i = 0; i < N_UPCALL_SOCKS; i++) {
289 nl_sock_destroy(dpif->upcall_socks[i]);
290 dpif->upcall_socks[i] = NULL;
295 dpif_linux_close(struct dpif *dpif_)
297 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
299 nln_notifier_destroy(dpif->port_notifier);
300 destroy_upcall_socks(dpif);
301 sset_destroy(&dpif->changed_ports);
302 free(dpif->lru_bitmap);
307 dpif_linux_destroy(struct dpif *dpif_)
309 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
310 struct dpif_linux_dp dp;
312 dpif_linux_dp_init(&dp);
313 dp.cmd = OVS_DP_CMD_DEL;
314 dp.dp_ifindex = dpif->dp_ifindex;
315 return dpif_linux_dp_transact(&dp, NULL, NULL);
319 dpif_linux_run(struct dpif *dpif OVS_UNUSED)
327 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
335 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
337 struct dpif_linux_dp dp;
341 error = dpif_linux_dp_get(dpif_, &dp, &buf);
343 stats->n_frags = dp.stats.n_frags;
344 stats->n_hit = dp.stats.n_hit;
345 stats->n_missed = dp.stats.n_missed;
346 stats->n_lost = dp.stats.n_lost;
347 stats->n_flows = dp.stats.n_flows;
354 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
356 struct dpif_linux_dp dp;
360 error = dpif_linux_dp_get(dpif_, &dp, &buf);
362 *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
369 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
371 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
372 struct dpif_linux_dp dp;
374 dpif_linux_dp_init(&dp);
375 dp.cmd = OVS_DP_CMD_SET;
376 dp.dp_ifindex = dpif->dp_ifindex;
377 dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
378 return dpif_linux_dp_transact(&dp, NULL, NULL);
382 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
385 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
386 const char *name = netdev_get_name(netdev);
387 const char *type = netdev_get_type(netdev);
388 struct dpif_linux_vport request, reply;
389 const struct ofpbuf *options;
393 dpif_linux_vport_init(&request);
394 request.cmd = OVS_VPORT_CMD_NEW;
395 request.dp_ifindex = dpif->dp_ifindex;
396 request.type = netdev_vport_get_vport_type(netdev);
397 if (request.type == OVS_VPORT_TYPE_UNSPEC) {
398 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
399 "unsupported type `%s'",
400 dpif_name(dpif_), name, type);
405 options = netdev_vport_get_options(netdev);
406 if (options && options->size) {
407 request.options = options->data;
408 request.options_len = options->size;
411 if (request.type == OVS_VPORT_TYPE_NETDEV) {
412 netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
415 /* Loop until we find a port that isn't used. */
419 request.port_no = dpif_linux_pop_port(dpif);
420 upcall_pid = dpif_linux_port_get_pid__(dpif_, request.port_no,
422 request.upcall_pid = &upcall_pid;
423 error = dpif_linux_vport_transact(&request, &reply, &buf);
426 *port_nop = reply.port_no;
427 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
428 dpif_name(dpif_), request.port_no, upcall_pid);
431 } while (request.port_no != UINT32_MAX
432 && (error == EBUSY || error == EFBIG));
438 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
440 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
441 struct dpif_linux_vport vport;
444 dpif_linux_vport_init(&vport);
445 vport.cmd = OVS_VPORT_CMD_DEL;
446 vport.dp_ifindex = dpif->dp_ifindex;
447 vport.port_no = port_no;
448 error = dpif_linux_vport_transact(&vport, NULL, NULL);
451 dpif_linux_push_port(dpif, port_no);
457 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
458 const char *port_name, struct dpif_port *dpif_port)
460 struct dpif_linux_vport request;
461 struct dpif_linux_vport reply;
465 dpif_linux_vport_init(&request);
466 request.cmd = OVS_VPORT_CMD_GET;
467 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
468 request.port_no = port_no;
469 request.name = port_name;
471 error = dpif_linux_vport_transact(&request, &reply, &buf);
473 dpif_port->name = xstrdup(reply.name);
474 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
475 dpif_port->port_no = reply.port_no;
482 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
483 struct dpif_port *dpif_port)
485 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
489 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
490 struct dpif_port *dpif_port)
492 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
496 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
498 /* If the datapath increases its range of supported ports, then it should
499 * start reporting that. */
504 dpif_linux_port_get_pid__(const struct dpif *dpif_, uint16_t port_no,
505 enum dpif_upcall_type upcall_type)
507 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
509 if (!(dpif->listen_mask & (1u << upcall_type))) {
512 int idx = port_no & (N_UPCALL_SOCKS - 1);
513 return nl_sock_pid(dpif->upcall_socks[idx]);
518 dpif_linux_port_get_pid(const struct dpif *dpif, uint16_t port_no)
520 return dpif_linux_port_get_pid__(dpif, port_no, DPIF_UC_ACTION);
524 dpif_linux_flow_flush(struct dpif *dpif_)
526 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
527 struct dpif_linux_flow flow;
529 dpif_linux_flow_init(&flow);
530 flow.cmd = OVS_FLOW_CMD_DEL;
531 flow.dp_ifindex = dpif->dp_ifindex;
532 return dpif_linux_flow_transact(&flow, NULL, NULL);
535 struct dpif_linux_port_state {
537 unsigned long *port_bitmap; /* Ports in the datapath. */
538 bool complete; /* Dump completed without error. */
542 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
544 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
545 struct dpif_linux_port_state *state;
546 struct dpif_linux_vport request;
549 *statep = state = xmalloc(sizeof *state);
550 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
551 state->complete = false;
553 dpif_linux_vport_init(&request);
554 request.cmd = OVS_DP_CMD_GET;
555 request.dp_ifindex = dpif->dp_ifindex;
557 buf = ofpbuf_new(1024);
558 dpif_linux_vport_to_ofpbuf(&request, buf);
559 nl_dump_start(&state->dump, genl_sock, buf);
566 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
567 struct dpif_port *dpif_port)
569 struct dpif_linux_port_state *state = state_;
570 struct dpif_linux_vport vport;
574 if (!nl_dump_next(&state->dump, &buf)) {
575 state->complete = true;
579 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
584 if (vport.port_no < LRU_MAX_PORTS) {
585 bitmap_set1(state->port_bitmap, vport.port_no);
588 dpif_port->name = (char *) vport.name;
589 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
590 dpif_port->port_no = vport.port_no;
595 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
597 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
598 struct dpif_linux_port_state *state = state_;
599 int error = nl_dump_done(&state->dump);
601 if (state->complete) {
604 for (i = 0; i < LRU_MAX_PORTS; i++) {
605 if (!bitmap_is_set(state->port_bitmap, i)) {
606 dpif_linux_push_port(dpif, i);
611 free(state->port_bitmap);
617 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
619 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
621 if (dpif->change_error) {
622 dpif->change_error = false;
623 sset_clear(&dpif->changed_ports);
625 } else if (!sset_is_empty(&dpif->changed_ports)) {
626 *devnamep = sset_pop(&dpif->changed_ports);
634 dpif_linux_port_poll_wait(const struct dpif *dpif_)
636 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
637 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
638 poll_immediate_wake();
643 dpif_linux_flow_get__(const struct dpif *dpif_,
644 const struct nlattr *key, size_t key_len,
645 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
647 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
648 struct dpif_linux_flow request;
650 dpif_linux_flow_init(&request);
651 request.cmd = OVS_FLOW_CMD_GET;
652 request.dp_ifindex = dpif->dp_ifindex;
654 request.key_len = key_len;
655 return dpif_linux_flow_transact(&request, reply, bufp);
659 dpif_linux_flow_get(const struct dpif *dpif_,
660 const struct nlattr *key, size_t key_len,
661 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
663 struct dpif_linux_flow reply;
667 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
670 dpif_linux_flow_get_stats(&reply, stats);
673 buf->data = (void *) reply.actions;
674 buf->size = reply.actions_len;
684 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
685 const struct nlattr *key, size_t key_len,
686 const struct nlattr *actions, size_t actions_len,
687 struct dpif_flow_stats *stats)
689 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
690 struct dpif_linux_flow request, reply;
691 struct nlattr dummy_action;
695 dpif_linux_flow_init(&request);
696 request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
697 request.dp_ifindex = dpif->dp_ifindex;
699 request.key_len = key_len;
700 /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
701 request.actions = actions ? actions : &dummy_action;
702 request.actions_len = actions_len;
703 if (flags & DPIF_FP_ZERO_STATS) {
704 request.clear = true;
706 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
707 error = dpif_linux_flow_transact(&request,
708 stats ? &reply : NULL,
709 stats ? &buf : NULL);
710 if (!error && stats) {
711 dpif_linux_flow_get_stats(&reply, stats);
718 dpif_linux_flow_del(struct dpif *dpif_,
719 const struct nlattr *key, size_t key_len,
720 struct dpif_flow_stats *stats)
722 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
723 struct dpif_linux_flow request, reply;
727 dpif_linux_flow_init(&request);
728 request.cmd = OVS_FLOW_CMD_DEL;
729 request.dp_ifindex = dpif->dp_ifindex;
731 request.key_len = key_len;
732 error = dpif_linux_flow_transact(&request,
733 stats ? &reply : NULL,
734 stats ? &buf : NULL);
735 if (!error && stats) {
736 dpif_linux_flow_get_stats(&reply, stats);
742 struct dpif_linux_flow_state {
744 struct dpif_linux_flow flow;
745 struct dpif_flow_stats stats;
750 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
752 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
753 struct dpif_linux_flow_state *state;
754 struct dpif_linux_flow request;
757 *statep = state = xmalloc(sizeof *state);
759 dpif_linux_flow_init(&request);
760 request.cmd = OVS_DP_CMD_GET;
761 request.dp_ifindex = dpif->dp_ifindex;
763 buf = ofpbuf_new(1024);
764 dpif_linux_flow_to_ofpbuf(&request, buf);
765 nl_dump_start(&state->dump, genl_sock, buf);
774 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
775 const struct nlattr **key, size_t *key_len,
776 const struct nlattr **actions, size_t *actions_len,
777 const struct dpif_flow_stats **stats)
779 struct dpif_linux_flow_state *state = state_;
784 ofpbuf_delete(state->buf);
787 if (!nl_dump_next(&state->dump, &buf)) {
791 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
796 if (actions && !state->flow.actions) {
797 error = dpif_linux_flow_get__(dpif_, state->flow.key,
799 &state->flow, &state->buf);
800 if (error == ENOENT) {
801 VLOG_DBG("dumped flow disappeared on get");
803 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
809 *actions = state->flow.actions;
810 *actions_len = state->flow.actions_len;
813 *key = state->flow.key;
814 *key_len = state->flow.key_len;
817 dpif_linux_flow_get_stats(&state->flow, &state->stats);
818 *stats = &state->stats;
824 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
826 struct dpif_linux_flow_state *state = state_;
827 int error = nl_dump_done(&state->dump);
828 ofpbuf_delete(state->buf);
834 dpif_linux_execute__(int dp_ifindex, const struct nlattr *key, size_t key_len,
835 const struct nlattr *actions, size_t actions_len,
836 const struct ofpbuf *packet)
838 struct ovs_header *execute;
842 buf = ofpbuf_new(128 + actions_len + packet->size);
844 nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
845 OVS_PACKET_CMD_EXECUTE, 1);
847 execute = ofpbuf_put_uninit(buf, sizeof *execute);
848 execute->dp_ifindex = dp_ifindex;
850 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
851 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
852 nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
854 error = nl_sock_transact(genl_sock, buf, NULL);
860 dpif_linux_execute(struct dpif *dpif_,
861 const struct nlattr *key, size_t key_len,
862 const struct nlattr *actions, size_t actions_len,
863 const struct ofpbuf *packet)
865 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
867 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
868 actions, actions_len, packet);
872 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
874 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
875 *listen_mask = dpif->listen_mask;
880 set_upcall_pids(struct dpif *dpif_)
882 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
883 struct dpif_port_dump port_dump;
884 struct dpif_port port;
887 DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
888 uint32_t upcall_pid = dpif_linux_port_get_pid__(dpif_, port.port_no,
890 struct dpif_linux_vport vport_request;
892 dpif_linux_vport_init(&vport_request);
893 vport_request.cmd = OVS_VPORT_CMD_SET;
894 vport_request.dp_ifindex = dpif->dp_ifindex;
895 vport_request.port_no = port.port_no;
896 vport_request.upcall_pid = &upcall_pid;
897 error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
899 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
900 dpif_name(&dpif->dpif), vport_request.port_no,
903 VLOG_WARN_RL(&error_rl, "%s: failed to set upcall pid on port: %s",
904 dpif_name(&dpif->dpif), strerror(error));
910 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
912 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
914 if (listen_mask == dpif->listen_mask) {
919 destroy_upcall_socks(dpif);
920 } else if (!dpif->listen_mask) {
924 for (i = 0; i < N_UPCALL_SOCKS; i++) {
925 error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
927 destroy_upcall_socks(dpif);
933 dpif->listen_mask = listen_mask;
934 set_upcall_pids(dpif_);
940 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
941 uint32_t queue_id, uint32_t *priority)
943 if (queue_id < 0xf000) {
944 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
952 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
955 static const struct nl_policy ovs_packet_policy[] = {
956 /* Always present. */
957 [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
958 .min_len = ETH_HEADER_LEN },
959 [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
961 /* OVS_PACKET_CMD_ACTION only. */
962 [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
965 struct ovs_header *ovs_header;
966 struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
967 struct nlmsghdr *nlmsg;
968 struct genlmsghdr *genl;
972 ofpbuf_use_const(&b, buf->data, buf->size);
974 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
975 genl = ofpbuf_try_pull(&b, sizeof *genl);
976 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
977 if (!nlmsg || !genl || !ovs_header
978 || nlmsg->nlmsg_type != ovs_packet_family
979 || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
980 ARRAY_SIZE(ovs_packet_policy))) {
984 type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
985 : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
991 memset(upcall, 0, sizeof *upcall);
993 upcall->packet = buf;
994 upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
995 upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
996 upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
997 upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
998 upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
999 ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1001 *dp_ifindex = ovs_header->dp_ifindex;
1007 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
1009 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1013 if (!dpif->listen_mask) {
1017 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1018 struct nl_sock *upcall_sock;
1019 dpif->last_read_upcall = (dpif->last_read_upcall + 1) &
1020 (N_UPCALL_SOCKS - 1);
1021 upcall_sock = dpif->upcall_socks[dpif->last_read_upcall];
1023 if (nl_sock_woke(upcall_sock)) {
1030 if (++read_tries > 50) {
1034 error = nl_sock_recv(upcall_sock, &buf, false);
1035 if (error == EAGAIN) {
1041 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1043 && dp_ifindex == dpif->dp_ifindex
1044 && dpif->listen_mask & (1u << upcall->type)) {
1060 dpif_linux_recv_wait(struct dpif *dpif_)
1062 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1065 if (!dpif->listen_mask) {
1069 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1070 nl_sock_wait(dpif->upcall_socks[i], POLLIN);
1075 dpif_linux_recv_purge(struct dpif *dpif_)
1077 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1080 if (!dpif->listen_mask) {
1084 for (i = 0; i < N_UPCALL_SOCKS; i++) {
1085 nl_sock_drain(dpif->upcall_socks[i]);
1089 const struct dpif_class dpif_linux_class = {
1091 dpif_linux_enumerate,
1097 dpif_linux_get_stats,
1098 dpif_linux_get_drop_frags,
1099 dpif_linux_set_drop_frags,
1100 dpif_linux_port_add,
1101 dpif_linux_port_del,
1102 dpif_linux_port_query_by_number,
1103 dpif_linux_port_query_by_name,
1104 dpif_linux_get_max_ports,
1105 dpif_linux_port_get_pid,
1106 dpif_linux_port_dump_start,
1107 dpif_linux_port_dump_next,
1108 dpif_linux_port_dump_done,
1109 dpif_linux_port_poll,
1110 dpif_linux_port_poll_wait,
1111 dpif_linux_flow_get,
1112 dpif_linux_flow_put,
1113 dpif_linux_flow_del,
1114 dpif_linux_flow_flush,
1115 dpif_linux_flow_dump_start,
1116 dpif_linux_flow_dump_next,
1117 dpif_linux_flow_dump_done,
1119 dpif_linux_recv_get_mask,
1120 dpif_linux_recv_set_mask,
1121 dpif_linux_queue_to_priority,
1123 dpif_linux_recv_wait,
1124 dpif_linux_recv_purge,
1128 dpif_linux_init(void)
1130 static int error = -1;
1133 unsigned int ovs_vport_mcgroup;
1135 error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1136 &ovs_datapath_family);
1138 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1139 "The Open vSwitch kernel module is probably not loaded.",
1140 OVS_DATAPATH_FAMILY);
1143 error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1146 error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1149 error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1150 &ovs_packet_family);
1153 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1156 error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1158 OVS_VPORT_MCGROUP_FALLBACK_ID);
1161 static struct dpif_linux_vport vport;
1162 nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1163 dpif_linux_nln_parse, &vport);
1171 dpif_linux_is_internal_device(const char *name)
1173 struct dpif_linux_vport reply;
1177 error = dpif_linux_vport_get(name, &reply, &buf);
1180 } else if (error != ENODEV && error != ENOENT) {
1181 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1182 name, strerror(error));
1185 return reply.type == OVS_VPORT_TYPE_INTERNAL;
1189 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1190 const void *data, size_t size)
1192 struct ofpbuf actions, key, packet;
1193 struct odputil_keybuf keybuf;
1197 ofpbuf_use_const(&packet, data, size);
1198 flow_extract(&packet, htonll(0), 0, &flow);
1200 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1201 odp_flow_key_from_flow(&key, &flow);
1203 ofpbuf_use_stack(&actions, &action, sizeof action);
1204 nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1206 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1207 actions.data, actions.size, &packet);
1211 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1213 struct dpif_linux_vport *vport = vport_;
1214 return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1218 dpif_linux_port_changed(const void *vport_, void *dpif_)
1220 const struct dpif_linux_vport *vport = vport_;
1221 struct dpif_linux *dpif = dpif_;
1224 if (vport->dp_ifindex == dpif->dp_ifindex
1225 && (vport->cmd == OVS_VPORT_CMD_NEW
1226 || vport->cmd == OVS_VPORT_CMD_DEL
1227 || vport->cmd == OVS_VPORT_CMD_SET)) {
1228 VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1229 dpif->dpif.full_name, vport->name, vport->cmd);
1230 sset_add(&dpif->changed_ports, vport->name);
1233 dpif->change_error = true;
1237 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1238 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1239 * positive errno value.
1241 * 'vport' will contain pointers into 'buf', so the caller should not free
1242 * 'buf' while 'vport' is still in use. */
1244 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1245 const struct ofpbuf *buf)
1247 static const struct nl_policy ovs_vport_policy[] = {
1248 [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1249 [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1250 [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1251 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1252 [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1253 .min_len = sizeof(struct ovs_vport_stats),
1254 .max_len = sizeof(struct ovs_vport_stats),
1256 [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1257 .min_len = ETH_ADDR_LEN,
1258 .max_len = ETH_ADDR_LEN,
1260 [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1263 struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1264 struct ovs_header *ovs_header;
1265 struct nlmsghdr *nlmsg;
1266 struct genlmsghdr *genl;
1269 dpif_linux_vport_init(vport);
1271 ofpbuf_use_const(&b, buf->data, buf->size);
1272 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1273 genl = ofpbuf_try_pull(&b, sizeof *genl);
1274 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1275 if (!nlmsg || !genl || !ovs_header
1276 || nlmsg->nlmsg_type != ovs_vport_family
1277 || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1278 ARRAY_SIZE(ovs_vport_policy))) {
1282 vport->cmd = genl->cmd;
1283 vport->dp_ifindex = ovs_header->dp_ifindex;
1284 vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1285 vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1286 vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1287 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1288 vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1290 if (a[OVS_VPORT_ATTR_STATS]) {
1291 vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1293 if (a[OVS_VPORT_ATTR_ADDRESS]) {
1294 vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1296 if (a[OVS_VPORT_ATTR_OPTIONS]) {
1297 vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1298 vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1303 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1304 * followed by Netlink attributes corresponding to 'vport'. */
1306 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1309 struct ovs_header *ovs_header;
1311 nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1314 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1315 ovs_header->dp_ifindex = vport->dp_ifindex;
1317 if (vport->port_no != UINT32_MAX) {
1318 nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1321 if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1322 nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1326 nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1329 if (vport->upcall_pid) {
1330 nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1334 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1335 vport->stats, sizeof *vport->stats);
1338 if (vport->address) {
1339 nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1340 vport->address, ETH_ADDR_LEN);
1343 if (vport->options) {
1344 nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1345 vport->options, vport->options_len);
1349 /* Clears 'vport' to "empty" values. */
1351 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1353 memset(vport, 0, sizeof *vport);
1354 vport->port_no = UINT32_MAX;
1357 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1358 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1359 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1360 * result of the command is expected to be an ovs_vport also, which is decoded
1361 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1362 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1364 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1365 struct dpif_linux_vport *reply,
1366 struct ofpbuf **bufp)
1368 struct ofpbuf *request_buf;
1371 assert((reply != NULL) == (bufp != NULL));
1373 error = dpif_linux_init();
1377 dpif_linux_vport_init(reply);
1382 request_buf = ofpbuf_new(1024);
1383 dpif_linux_vport_to_ofpbuf(request, request_buf);
1384 error = nl_sock_transact(genl_sock, request_buf, bufp);
1385 ofpbuf_delete(request_buf);
1389 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1392 dpif_linux_vport_init(reply);
1393 ofpbuf_delete(*bufp);
1400 /* Obtains information about the kernel vport named 'name' and stores it into
1401 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1402 * longer needed ('reply' will contain pointers into '*bufp'). */
1404 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1405 struct ofpbuf **bufp)
1407 struct dpif_linux_vport request;
1409 dpif_linux_vport_init(&request);
1410 request.cmd = OVS_VPORT_CMD_GET;
1411 request.name = name;
1413 return dpif_linux_vport_transact(&request, reply, bufp);
1416 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1417 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1418 * positive errno value.
1420 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1421 * while 'dp' is still in use. */
1423 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1425 static const struct nl_policy ovs_datapath_policy[] = {
1426 [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1427 [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1428 .min_len = sizeof(struct ovs_dp_stats),
1429 .max_len = sizeof(struct ovs_dp_stats),
1431 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1434 struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1435 struct ovs_header *ovs_header;
1436 struct nlmsghdr *nlmsg;
1437 struct genlmsghdr *genl;
1440 dpif_linux_dp_init(dp);
1442 ofpbuf_use_const(&b, buf->data, buf->size);
1443 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1444 genl = ofpbuf_try_pull(&b, sizeof *genl);
1445 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1446 if (!nlmsg || !genl || !ovs_header
1447 || nlmsg->nlmsg_type != ovs_datapath_family
1448 || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1449 ARRAY_SIZE(ovs_datapath_policy))) {
1453 dp->cmd = genl->cmd;
1454 dp->dp_ifindex = ovs_header->dp_ifindex;
1455 dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1456 if (a[OVS_DP_ATTR_STATS]) {
1457 /* Can't use structure assignment because Netlink doesn't ensure
1458 * sufficient alignment for 64-bit members. */
1459 memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1462 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1463 dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1469 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1471 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1473 struct ovs_header *ovs_header;
1475 nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1476 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1478 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1479 ovs_header->dp_ifindex = dp->dp_ifindex;
1482 nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1485 if (dp->upcall_pid) {
1486 nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1489 /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1491 if (dp->ipv4_frags) {
1492 nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1496 /* Clears 'dp' to "empty" values. */
1498 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1500 memset(dp, 0, sizeof *dp);
1504 dpif_linux_dp_dump_start(struct nl_dump *dump)
1506 struct dpif_linux_dp request;
1509 dpif_linux_dp_init(&request);
1510 request.cmd = OVS_DP_CMD_GET;
1512 buf = ofpbuf_new(1024);
1513 dpif_linux_dp_to_ofpbuf(&request, buf);
1514 nl_dump_start(dump, genl_sock, buf);
1518 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1519 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1520 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1521 * result of the command is expected to be of the same form, which is decoded
1522 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1523 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1525 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1526 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1528 struct ofpbuf *request_buf;
1531 assert((reply != NULL) == (bufp != NULL));
1533 request_buf = ofpbuf_new(1024);
1534 dpif_linux_dp_to_ofpbuf(request, request_buf);
1535 error = nl_sock_transact(genl_sock, request_buf, bufp);
1536 ofpbuf_delete(request_buf);
1540 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1543 dpif_linux_dp_init(reply);
1544 ofpbuf_delete(*bufp);
1551 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1552 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1553 * will contain pointers into '*bufp'). */
1555 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1556 struct ofpbuf **bufp)
1558 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1559 struct dpif_linux_dp request;
1561 dpif_linux_dp_init(&request);
1562 request.cmd = OVS_DP_CMD_GET;
1563 request.dp_ifindex = dpif->dp_ifindex;
1565 return dpif_linux_dp_transact(&request, reply, bufp);
1568 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1569 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1570 * positive errno value.
1572 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1573 * while 'flow' is still in use. */
1575 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1576 const struct ofpbuf *buf)
1578 static const struct nl_policy ovs_flow_policy[] = {
1579 [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1580 [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1581 [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1582 .min_len = sizeof(struct ovs_flow_stats),
1583 .max_len = sizeof(struct ovs_flow_stats),
1585 [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1586 [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1587 /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1590 struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1591 struct ovs_header *ovs_header;
1592 struct nlmsghdr *nlmsg;
1593 struct genlmsghdr *genl;
1596 dpif_linux_flow_init(flow);
1598 ofpbuf_use_const(&b, buf->data, buf->size);
1599 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1600 genl = ofpbuf_try_pull(&b, sizeof *genl);
1601 ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1602 if (!nlmsg || !genl || !ovs_header
1603 || nlmsg->nlmsg_type != ovs_flow_family
1604 || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1605 ARRAY_SIZE(ovs_flow_policy))) {
1609 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1610 flow->dp_ifindex = ovs_header->dp_ifindex;
1611 flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1612 flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1613 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1614 flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1615 flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1617 if (a[OVS_FLOW_ATTR_STATS]) {
1618 flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1620 if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1621 flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1623 if (a[OVS_FLOW_ATTR_USED]) {
1624 flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1629 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1630 * followed by Netlink attributes corresponding to 'flow'. */
1632 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1635 struct ovs_header *ovs_header;
1637 nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1638 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1641 ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1642 ovs_header->dp_ifindex = flow->dp_ifindex;
1644 if (flow->key_len) {
1645 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1648 if (flow->actions || flow->actions_len) {
1649 nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1650 flow->actions, flow->actions_len);
1653 /* We never need to send these to the kernel. */
1654 assert(!flow->stats);
1655 assert(!flow->tcp_flags);
1656 assert(!flow->used);
1659 nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1663 /* Clears 'flow' to "empty" values. */
1665 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1667 memset(flow, 0, sizeof *flow);
1670 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1671 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1672 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1673 * result of the command is expected to be a flow also, which is decoded and
1674 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1675 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1677 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1678 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1680 struct ofpbuf *request_buf;
1683 assert((reply != NULL) == (bufp != NULL));
1685 request_buf = ofpbuf_new(1024);
1686 dpif_linux_flow_to_ofpbuf(request, request_buf);
1687 error = nl_sock_transact(genl_sock, request_buf, bufp);
1688 ofpbuf_delete(request_buf);
1692 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1695 dpif_linux_flow_init(reply);
1696 ofpbuf_delete(*bufp);
1704 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1705 struct dpif_flow_stats *stats)
1708 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1709 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1711 stats->n_packets = 0;
1714 stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1715 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;