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"
39 #include "netdev-linux.h"
40 #include "netdev-vport.h"
41 #include "netlink-socket.h"
45 #include "openvswitch/tunnel.h"
47 #include "poll-loop.h"
48 #include "rtnetlink.h"
49 #include "rtnetlink-link.h"
52 #include "unaligned.h"
56 VLOG_DEFINE_THIS_MODULE(dpif_linux);
58 enum { LRU_MAX_PORTS = 1024 };
59 enum { LRU_MASK = LRU_MAX_PORTS - 1};
60 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
62 struct dpif_linux_dp {
63 /* Generic Netlink header. */
66 /* struct odp_header. */
70 const char *name; /* ODP_DP_ATTR_NAME. */
71 struct odp_stats stats; /* ODP_DP_ATTR_STATS. */
72 enum odp_frag_handling ipv4_frags; /* ODP_DP_ATTR_IPV4_FRAGS. */
73 const uint32_t *sampling; /* ODP_DP_ATTR_SAMPLING. */
74 uint32_t mcgroups[DPIF_N_UC_TYPES]; /* ODP_DP_ATTR_MCGROUPS. */
77 static void dpif_linux_dp_init(struct dpif_linux_dp *);
78 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
79 const struct ofpbuf *);
80 static void dpif_linux_dp_dump_start(struct nl_dump *);
81 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
82 struct dpif_linux_dp *reply,
83 struct ofpbuf **bufp);
84 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
85 struct ofpbuf **bufp);
87 struct dpif_linux_flow {
88 /* Generic Netlink header. */
91 /* struct odp_header. */
92 unsigned int nlmsg_flags;
97 * The 'stats' and 'used' members point to 64-bit data that might only be
98 * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
99 * access their values.
101 * If 'actions' is nonnull then ODP_FLOW_ATTR_ACTIONS will be included in
102 * the Netlink version of the command, even if actions_len is zero. */
103 const struct nlattr *key; /* ODP_FLOW_ATTR_KEY. */
105 const struct nlattr *actions; /* ODP_FLOW_ATTR_ACTIONS. */
107 const struct odp_flow_stats *stats; /* ODP_FLOW_ATTR_STATS. */
108 const uint8_t *tcp_flags; /* ODP_FLOW_ATTR_TCP_FLAGS. */
109 const uint64_t *used; /* ODP_FLOW_ATTR_USED. */
110 bool clear; /* ODP_FLOW_ATTR_CLEAR. */
113 static void dpif_linux_flow_init(struct dpif_linux_flow *);
114 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
115 const struct ofpbuf *);
116 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
118 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
119 struct dpif_linux_flow *reply,
120 struct ofpbuf **bufp);
121 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
122 struct dpif_flow_stats *);
124 /* Datapath interface for the openvswitch Linux kernel module. */
129 /* Multicast group messages. */
130 struct nl_sock *mc_sock;
131 uint32_t mcgroups[DPIF_N_UC_TYPES];
132 unsigned int listen_mask;
134 /* Change notification. */
135 struct sset changed_ports; /* Ports that have changed. */
136 struct rtnetlink_notifier port_notifier;
139 /* Queue of unused ports. */
140 unsigned long *lru_bitmap;
141 uint16_t lru_ports[LRU_MAX_PORTS];
146 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
148 /* Generic Netlink family numbers for ODP. */
149 static int odp_datapath_family;
150 static int odp_vport_family;
151 static int odp_flow_family;
152 static int odp_packet_family;
154 /* Generic Netlink socket. */
155 static struct nl_sock *genl_sock;
157 static int dpif_linux_init(void);
158 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
159 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
162 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
164 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
165 const struct ofpbuf *);
167 static struct dpif_linux *
168 dpif_linux_cast(const struct dpif *dpif)
170 dpif_assert_class(dpif, &dpif_linux_class);
171 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
175 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
177 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
178 bitmap_set1(dp->lru_bitmap, port);
179 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
184 dpif_linux_pop_port(struct dpif_linux *dp)
188 if (dp->lru_head == dp->lru_tail) {
192 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
193 bitmap_set0(dp->lru_bitmap, port);
198 dpif_linux_enumerate(struct sset *all_dps)
204 error = dpif_linux_init();
209 dpif_linux_dp_dump_start(&dump);
210 while (nl_dump_next(&dump, &msg)) {
211 struct dpif_linux_dp dp;
213 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
214 sset_add(all_dps, dp.name);
217 return nl_dump_done(&dump);
221 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
222 bool create, struct dpif **dpifp)
224 struct dpif_linux_dp dp_request, dp;
228 error = dpif_linux_init();
233 /* Create or look up datapath. */
234 dpif_linux_dp_init(&dp_request);
235 dp_request.cmd = create ? ODP_DP_CMD_NEW : ODP_DP_CMD_GET;
236 dp_request.name = name;
237 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
241 error = open_dpif(&dp, dpifp);
248 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
250 struct dpif_linux *dpif;
254 dpif = xmalloc(sizeof *dpif);
255 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
256 dpif_linux_port_changed, dpif);
261 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
262 dp->dp_ifindex, dp->dp_ifindex);
264 dpif->mc_sock = NULL;
265 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
266 dpif->mcgroups[i] = dp->mcgroups[i];
268 dpif->listen_mask = 0;
269 dpif->dp_ifindex = dp->dp_ifindex;
270 sset_init(&dpif->changed_ports);
271 dpif->change_error = false;
272 *dpifp = &dpif->dpif;
274 dpif->lru_head = dpif->lru_tail = 0;
275 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
276 bitmap_set1(dpif->lru_bitmap, ODPP_LOCAL);
277 for (i = 1; i < LRU_MAX_PORTS; i++) {
278 dpif_linux_push_port(dpif, i);
288 dpif_linux_close(struct dpif *dpif_)
290 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
291 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
292 sset_destroy(&dpif->changed_ports);
293 free(dpif->lru_bitmap);
298 dpif_linux_destroy(struct dpif *dpif_)
300 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
301 struct dpif_linux_dp dp;
303 dpif_linux_dp_init(&dp);
304 dp.cmd = ODP_DP_CMD_DEL;
305 dp.dp_ifindex = dpif->dp_ifindex;
306 return dpif_linux_dp_transact(&dp, NULL, NULL);
310 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
312 struct dpif_linux_dp dp;
316 error = dpif_linux_dp_get(dpif_, &dp, &buf);
325 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
327 struct dpif_linux_dp dp;
331 error = dpif_linux_dp_get(dpif_, &dp, &buf);
333 *drop_fragsp = dp.ipv4_frags == ODP_DP_FRAG_DROP;
340 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
342 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
343 struct dpif_linux_dp dp;
345 dpif_linux_dp_init(&dp);
346 dp.cmd = ODP_DP_CMD_SET;
347 dp.dp_ifindex = dpif->dp_ifindex;
348 dp.ipv4_frags = drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO;
349 return dpif_linux_dp_transact(&dp, NULL, NULL);
353 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
356 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
357 const char *name = netdev_get_name(netdev);
358 const char *type = netdev_get_type(netdev);
359 struct dpif_linux_vport request, reply;
360 const struct ofpbuf *options;
364 dpif_linux_vport_init(&request);
365 request.cmd = ODP_VPORT_CMD_NEW;
366 request.dp_ifindex = dpif->dp_ifindex;
367 request.type = netdev_vport_get_vport_type(netdev);
368 if (request.type == ODP_VPORT_TYPE_UNSPEC) {
369 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
370 "unsupported type `%s'",
371 dpif_name(dpif_), name, type);
376 options = netdev_vport_get_options(netdev);
377 if (options && options->size) {
378 request.options = options->data;
379 request.options_len = options->size;
382 /* Loop until we find a port that isn't used. */
384 request.port_no = dpif_linux_pop_port(dpif);
385 error = dpif_linux_vport_transact(&request, &reply, &buf);
388 *port_nop = reply.port_no;
391 } while (request.port_no != UINT32_MAX
392 && (error == EBUSY || error == EFBIG));
398 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
400 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
401 struct dpif_linux_vport vport;
404 dpif_linux_vport_init(&vport);
405 vport.cmd = ODP_VPORT_CMD_DEL;
406 vport.dp_ifindex = dpif->dp_ifindex;
407 vport.port_no = port_no;
408 error = dpif_linux_vport_transact(&vport, NULL, NULL);
411 dpif_linux_push_port(dpif, port_no);
417 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
418 const char *port_name, struct dpif_port *dpif_port)
420 struct dpif_linux_vport request;
421 struct dpif_linux_vport reply;
425 dpif_linux_vport_init(&request);
426 request.cmd = ODP_VPORT_CMD_GET;
427 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
428 request.port_no = port_no;
429 request.name = port_name;
431 error = dpif_linux_vport_transact(&request, &reply, &buf);
433 dpif_port->name = xstrdup(reply.name);
434 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
435 dpif_port->port_no = reply.port_no;
437 netdev_stats_from_rtnl_link_stats64(&dpif_port->stats,
440 memset(&dpif_port->stats, 0xff, sizeof dpif_port->stats);
448 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
449 struct dpif_port *dpif_port)
451 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
455 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
456 struct dpif_port *dpif_port)
458 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
462 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
464 /* If the datapath increases its range of supported ports, then it should
465 * start reporting that. */
470 dpif_linux_flow_flush(struct dpif *dpif_)
472 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
473 struct dpif_linux_flow flow;
475 dpif_linux_flow_init(&flow);
476 flow.cmd = ODP_FLOW_CMD_DEL;
477 flow.dp_ifindex = dpif->dp_ifindex;
478 return dpif_linux_flow_transact(&flow, NULL, NULL);
481 struct dpif_linux_port_state {
483 unsigned long *port_bitmap; /* Ports in the datapath. */
484 bool complete; /* Dump completed without error. */
488 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
490 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
491 struct dpif_linux_port_state *state;
492 struct dpif_linux_vport request;
495 *statep = state = xmalloc(sizeof *state);
496 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
497 state->complete = false;
499 dpif_linux_vport_init(&request);
500 request.cmd = ODP_DP_CMD_GET;
501 request.dp_ifindex = dpif->dp_ifindex;
503 buf = ofpbuf_new(1024);
504 dpif_linux_vport_to_ofpbuf(&request, buf);
505 nl_dump_start(&state->dump, genl_sock, buf);
512 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
513 struct dpif_port *dpif_port)
515 struct dpif_linux_port_state *state = state_;
516 struct dpif_linux_vport vport;
520 if (!nl_dump_next(&state->dump, &buf)) {
521 state->complete = true;
525 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
530 if (vport.port_no < LRU_MAX_PORTS) {
531 bitmap_set1(state->port_bitmap, vport.port_no);
534 dpif_port->name = (char *) vport.name;
535 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
536 dpif_port->port_no = vport.port_no;
538 netdev_stats_from_rtnl_link_stats64(&dpif_port->stats, vport.stats);
540 memset(&dpif_port->stats, 0xff, sizeof dpif_port->stats);
546 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
548 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
549 struct dpif_linux_port_state *state = state_;
550 int error = nl_dump_done(&state->dump);
552 if (state->complete) {
555 for (i = 0; i < LRU_MAX_PORTS; i++) {
556 if (!bitmap_is_set(state->port_bitmap, i)) {
557 dpif_linux_push_port(dpif, i);
562 free(state->port_bitmap);
568 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
570 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
572 if (dpif->change_error) {
573 dpif->change_error = false;
574 sset_clear(&dpif->changed_ports);
576 } else if (!sset_is_empty(&dpif->changed_ports)) {
577 *devnamep = sset_pop(&dpif->changed_ports);
585 dpif_linux_port_poll_wait(const struct dpif *dpif_)
587 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
588 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
589 poll_immediate_wake();
591 rtnetlink_link_notifier_wait();
596 dpif_linux_flow_get__(const struct dpif *dpif_,
597 const struct nlattr *key, size_t key_len,
598 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
600 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
601 struct dpif_linux_flow request;
603 dpif_linux_flow_init(&request);
604 request.cmd = ODP_FLOW_CMD_GET;
605 request.dp_ifindex = dpif->dp_ifindex;
607 request.key_len = key_len;
608 return dpif_linux_flow_transact(&request, reply, bufp);
612 dpif_linux_flow_get(const struct dpif *dpif_,
613 const struct nlattr *key, size_t key_len,
614 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
616 struct dpif_linux_flow reply;
620 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
623 dpif_linux_flow_get_stats(&reply, stats);
626 buf->data = (void *) reply.actions;
627 buf->size = reply.actions_len;
637 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
638 const struct nlattr *key, size_t key_len,
639 const struct nlattr *actions, size_t actions_len,
640 struct dpif_flow_stats *stats)
642 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
643 struct dpif_linux_flow request, reply;
644 struct nlattr dummy_action;
648 dpif_linux_flow_init(&request);
649 request.cmd = flags & DPIF_FP_CREATE ? ODP_FLOW_CMD_NEW : ODP_FLOW_CMD_SET;
650 request.dp_ifindex = dpif->dp_ifindex;
652 request.key_len = key_len;
653 /* Ensure that ODP_FLOW_ATTR_ACTIONS will always be included. */
654 request.actions = actions ? actions : &dummy_action;
655 request.actions_len = actions_len;
656 if (flags & DPIF_FP_ZERO_STATS) {
657 request.clear = true;
659 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
660 error = dpif_linux_flow_transact(&request,
661 stats ? &reply : NULL,
662 stats ? &buf : NULL);
663 if (!error && stats) {
664 dpif_linux_flow_get_stats(&reply, stats);
671 dpif_linux_flow_del(struct dpif *dpif_,
672 const struct nlattr *key, size_t key_len,
673 struct dpif_flow_stats *stats)
675 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
676 struct dpif_linux_flow request, reply;
680 dpif_linux_flow_init(&request);
681 request.cmd = ODP_FLOW_CMD_DEL;
682 request.dp_ifindex = dpif->dp_ifindex;
684 request.key_len = key_len;
685 error = dpif_linux_flow_transact(&request,
686 stats ? &reply : NULL,
687 stats ? &buf : NULL);
688 if (!error && stats) {
689 dpif_linux_flow_get_stats(&reply, stats);
695 struct dpif_linux_flow_state {
697 struct dpif_linux_flow flow;
698 struct dpif_flow_stats stats;
703 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
705 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
706 struct dpif_linux_flow_state *state;
707 struct dpif_linux_flow request;
710 *statep = state = xmalloc(sizeof *state);
712 dpif_linux_flow_init(&request);
713 request.cmd = ODP_DP_CMD_GET;
714 request.dp_ifindex = dpif->dp_ifindex;
716 buf = ofpbuf_new(1024);
717 dpif_linux_flow_to_ofpbuf(&request, buf);
718 nl_dump_start(&state->dump, genl_sock, buf);
727 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
728 const struct nlattr **key, size_t *key_len,
729 const struct nlattr **actions, size_t *actions_len,
730 const struct dpif_flow_stats **stats)
732 struct dpif_linux_flow_state *state = state_;
737 ofpbuf_delete(state->buf);
740 if (!nl_dump_next(&state->dump, &buf)) {
744 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
749 if (actions && !state->flow.actions) {
750 error = dpif_linux_flow_get__(dpif_, state->flow.key,
752 &state->flow, &state->buf);
753 if (error == ENOENT) {
754 VLOG_DBG("dumped flow disappeared on get");
756 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
762 *actions = state->flow.actions;
763 *actions_len = state->flow.actions_len;
766 *key = state->flow.key;
767 *key_len = state->flow.key_len;
770 dpif_linux_flow_get_stats(&state->flow, &state->stats);
771 *stats = &state->stats;
777 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
779 struct dpif_linux_flow_state *state = state_;
780 int error = nl_dump_done(&state->dump);
781 ofpbuf_delete(state->buf);
787 dpif_linux_execute(struct dpif *dpif_,
788 const struct nlattr *key, size_t key_len,
789 const struct nlattr *actions, size_t actions_len,
790 const struct ofpbuf *packet)
792 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
793 struct odp_header *execute;
797 buf = ofpbuf_new(128 + actions_len + packet->size);
799 nl_msg_put_genlmsghdr(buf, 0, odp_packet_family, NLM_F_REQUEST,
800 ODP_PACKET_CMD_EXECUTE, 1);
802 execute = ofpbuf_put_uninit(buf, sizeof *execute);
803 execute->dp_ifindex = dpif->dp_ifindex;
805 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, packet->data, packet->size);
806 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_KEY, key, key_len);
807 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_ACTIONS, actions, actions_len);
809 error = nl_sock_transact(genl_sock, buf, NULL);
815 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
817 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
818 *listen_mask = dpif->listen_mask;
823 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
825 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
829 if (listen_mask == dpif->listen_mask) {
831 } else if (!listen_mask) {
832 nl_sock_destroy(dpif->mc_sock);
833 dpif->mc_sock = NULL;
834 dpif->listen_mask = 0;
836 } else if (!dpif->mc_sock) {
837 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
843 /* Unsubscribe from old groups. */
844 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
845 if (dpif->listen_mask & (1u << i)) {
846 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
850 /* Update listen_mask. */
851 dpif->listen_mask = listen_mask;
853 /* Subscribe to new groups. */
855 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
856 if (dpif->listen_mask & (1u << i)) {
859 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
869 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
870 uint32_t *probability)
872 struct dpif_linux_dp dp;
876 error = dpif_linux_dp_get(dpif_, &dp, &buf);
878 *probability = dp.sampling ? *dp.sampling : 0;
885 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
887 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
888 struct dpif_linux_dp dp;
890 dpif_linux_dp_init(&dp);
891 dp.cmd = ODP_DP_CMD_SET;
892 dp.dp_ifindex = dpif->dp_ifindex;
893 dp.sampling = &probability;
894 return dpif_linux_dp_transact(&dp, NULL, NULL);
898 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
899 uint32_t queue_id, uint32_t *priority)
901 if (queue_id < 0xf000) {
902 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
910 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
913 static const struct nl_policy odp_packet_policy[] = {
914 /* Always present. */
915 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
916 .min_len = ETH_HEADER_LEN },
917 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
919 /* ODP_PACKET_CMD_ACTION only. */
920 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
922 /* ODP_PACKET_CMD_SAMPLE only. */
923 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
924 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
927 struct odp_header *odp_header;
928 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
929 struct nlmsghdr *nlmsg;
930 struct genlmsghdr *genl;
934 ofpbuf_use_const(&b, buf->data, buf->size);
936 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
937 genl = ofpbuf_try_pull(&b, sizeof *genl);
938 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
939 if (!nlmsg || !genl || !odp_header
940 || nlmsg->nlmsg_type != odp_packet_family
941 || !nl_policy_parse(&b, 0, odp_packet_policy, a,
942 ARRAY_SIZE(odp_packet_policy))) {
946 type = (genl->cmd == ODP_PACKET_CMD_MISS ? DPIF_UC_MISS
947 : genl->cmd == ODP_PACKET_CMD_ACTION ? DPIF_UC_ACTION
948 : genl->cmd == ODP_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
954 memset(upcall, 0, sizeof *upcall);
956 upcall->packet = buf;
957 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
958 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
959 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
960 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
961 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
962 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
964 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
965 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
967 if (a[ODP_PACKET_ATTR_ACTIONS]) {
968 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
969 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
972 *dp_ifindex = odp_header->dp_ifindex;
978 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
980 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
985 if (!dpif->mc_sock) {
989 for (i = 0; i < 50; i++) {
992 error = nl_sock_recv(dpif->mc_sock, &buf, false);
997 error = parse_odp_packet(buf, upcall, &dp_ifindex);
999 && dp_ifindex == dpif->dp_ifindex
1000 && dpif->listen_mask & (1u << upcall->type)) {
1014 dpif_linux_recv_wait(struct dpif *dpif_)
1016 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1017 if (dpif->mc_sock) {
1018 nl_sock_wait(dpif->mc_sock, POLLIN);
1023 dpif_linux_recv_purge(struct dpif *dpif_)
1025 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1027 if (dpif->mc_sock) {
1028 nl_sock_drain(dpif->mc_sock);
1032 const struct dpif_class dpif_linux_class = {
1034 dpif_linux_enumerate,
1040 dpif_linux_get_stats,
1041 dpif_linux_get_drop_frags,
1042 dpif_linux_set_drop_frags,
1043 dpif_linux_port_add,
1044 dpif_linux_port_del,
1045 dpif_linux_port_query_by_number,
1046 dpif_linux_port_query_by_name,
1047 dpif_linux_get_max_ports,
1048 dpif_linux_port_dump_start,
1049 dpif_linux_port_dump_next,
1050 dpif_linux_port_dump_done,
1051 dpif_linux_port_poll,
1052 dpif_linux_port_poll_wait,
1053 dpif_linux_flow_get,
1054 dpif_linux_flow_put,
1055 dpif_linux_flow_del,
1056 dpif_linux_flow_flush,
1057 dpif_linux_flow_dump_start,
1058 dpif_linux_flow_dump_next,
1059 dpif_linux_flow_dump_done,
1061 dpif_linux_recv_get_mask,
1062 dpif_linux_recv_set_mask,
1063 dpif_linux_get_sflow_probability,
1064 dpif_linux_set_sflow_probability,
1065 dpif_linux_queue_to_priority,
1067 dpif_linux_recv_wait,
1068 dpif_linux_recv_purge,
1072 dpif_linux_init(void)
1074 static int error = -1;
1077 error = nl_lookup_genl_family(ODP_DATAPATH_FAMILY,
1078 &odp_datapath_family);
1080 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1081 "The Open vSwitch kernel module is probably not loaded.",
1082 ODP_DATAPATH_FAMILY);
1085 error = nl_lookup_genl_family(ODP_VPORT_FAMILY, &odp_vport_family);
1088 error = nl_lookup_genl_family(ODP_FLOW_FAMILY, &odp_flow_family);
1091 error = nl_lookup_genl_family(ODP_PACKET_FAMILY,
1092 &odp_packet_family);
1095 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1103 dpif_linux_is_internal_device(const char *name)
1105 struct dpif_linux_vport reply;
1109 error = dpif_linux_vport_get(name, &reply, &buf);
1112 } else if (error != ENODEV && error != ENOENT) {
1113 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1114 name, strerror(error));
1117 return reply.type == ODP_VPORT_TYPE_INTERNAL;
1121 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1122 const void *data, size_t size)
1124 struct odp_header *execute;
1129 buf = ofpbuf_new(128 + size);
1131 nl_msg_put_genlmsghdr(buf, 0, odp_packet_family, NLM_F_REQUEST,
1132 ODP_PACKET_CMD_EXECUTE, 1);
1134 execute = ofpbuf_put_uninit(buf, sizeof *execute);
1135 execute->dp_ifindex = dp_ifindex;
1137 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, data, size);
1139 actions_ofs = nl_msg_start_nested(buf, ODP_PACKET_ATTR_ACTIONS);
1140 nl_msg_put_u32(buf, ODP_ACTION_ATTR_OUTPUT, port_no);
1141 nl_msg_end_nested(buf, actions_ofs);
1143 error = nl_sock_transact(genl_sock, buf, NULL);
1149 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1152 struct dpif_linux *dpif = dpif_;
1155 if (change->master_ifindex == dpif->dp_ifindex
1156 && (change->nlmsg_type == RTM_NEWLINK
1157 || change->nlmsg_type == RTM_DELLINK))
1159 /* Our datapath changed, either adding a new port or deleting an
1161 sset_add(&dpif->changed_ports, change->ifname);
1164 dpif->change_error = true;
1168 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1169 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1170 * positive errno value.
1172 * 'vport' will contain pointers into 'buf', so the caller should not free
1173 * 'buf' while 'vport' is still in use. */
1175 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1176 const struct ofpbuf *buf)
1178 static const struct nl_policy odp_vport_policy[] = {
1179 [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1180 [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1181 [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1182 [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1183 .min_len = sizeof(struct rtnl_link_stats64),
1184 .max_len = sizeof(struct rtnl_link_stats64),
1186 [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1187 .min_len = ETH_ADDR_LEN,
1188 .max_len = ETH_ADDR_LEN,
1190 [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1191 [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1192 [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1193 [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1196 struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
1197 struct odp_header *odp_header;
1198 struct nlmsghdr *nlmsg;
1199 struct genlmsghdr *genl;
1202 dpif_linux_vport_init(vport);
1204 ofpbuf_use_const(&b, buf->data, buf->size);
1205 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1206 genl = ofpbuf_try_pull(&b, sizeof *genl);
1207 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1208 if (!nlmsg || !genl || !odp_header
1209 || nlmsg->nlmsg_type != odp_vport_family
1210 || !nl_policy_parse(&b, 0, odp_vport_policy, a,
1211 ARRAY_SIZE(odp_vport_policy))) {
1215 vport->cmd = genl->cmd;
1216 vport->dp_ifindex = odp_header->dp_ifindex;
1217 vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1218 vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1219 vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
1220 if (a[ODP_VPORT_ATTR_STATS]) {
1221 vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
1223 if (a[ODP_VPORT_ATTR_ADDRESS]) {
1224 vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
1226 if (a[ODP_VPORT_ATTR_MTU]) {
1227 vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
1229 vport->mtu = INT_MAX;
1231 if (a[ODP_VPORT_ATTR_OPTIONS]) {
1232 vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
1233 vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
1235 if (a[ODP_VPORT_ATTR_IFINDEX]) {
1236 vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
1238 if (a[ODP_VPORT_ATTR_IFLINK]) {
1239 vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
1244 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1245 * followed by Netlink attributes corresponding to 'vport'. */
1247 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1250 struct odp_header *odp_header;
1252 nl_msg_put_genlmsghdr(buf, 0, odp_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1255 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1256 odp_header->dp_ifindex = vport->dp_ifindex;
1258 if (vport->port_no != UINT32_MAX) {
1259 nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1262 if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
1263 nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1267 nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1271 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1272 vport->stats, sizeof *vport->stats);
1275 if (vport->address) {
1276 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1277 vport->address, ETH_ADDR_LEN);
1280 if (vport->mtu && vport->mtu != INT_MAX) {
1281 nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1284 if (vport->options) {
1285 nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1286 vport->options, vport->options_len);
1289 if (vport->ifindex) {
1290 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1293 if (vport->iflink) {
1294 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1298 /* Clears 'vport' to "empty" values. */
1300 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1302 memset(vport, 0, sizeof *vport);
1303 vport->port_no = UINT32_MAX;
1306 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1307 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1308 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1309 * result of the command is expected to be an odp_vport also, which is decoded
1310 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1311 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1313 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1314 struct dpif_linux_vport *reply,
1315 struct ofpbuf **bufp)
1317 struct ofpbuf *request_buf;
1320 assert((reply != NULL) == (bufp != NULL));
1322 error = dpif_linux_init();
1326 dpif_linux_vport_init(reply);
1331 request_buf = ofpbuf_new(1024);
1332 dpif_linux_vport_to_ofpbuf(request, request_buf);
1333 error = nl_sock_transact(genl_sock, request_buf, bufp);
1334 ofpbuf_delete(request_buf);
1338 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1341 dpif_linux_vport_init(reply);
1342 ofpbuf_delete(*bufp);
1349 /* Obtains information about the kernel vport named 'name' and stores it into
1350 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1351 * longer needed ('reply' will contain pointers into '*bufp'). */
1353 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1354 struct ofpbuf **bufp)
1356 struct dpif_linux_vport request;
1358 dpif_linux_vport_init(&request);
1359 request.cmd = ODP_VPORT_CMD_GET;
1360 request.name = name;
1362 return dpif_linux_vport_transact(&request, reply, bufp);
1365 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1366 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1367 * positive errno value.
1369 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1370 * while 'dp' is still in use. */
1372 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1374 static const struct nl_policy odp_datapath_policy[] = {
1375 [ODP_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1376 [ODP_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1377 .min_len = sizeof(struct odp_stats),
1378 .max_len = sizeof(struct odp_stats),
1380 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1381 [ODP_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1382 [ODP_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1385 struct nlattr *a[ARRAY_SIZE(odp_datapath_policy)];
1386 struct odp_header *odp_header;
1387 struct nlmsghdr *nlmsg;
1388 struct genlmsghdr *genl;
1391 dpif_linux_dp_init(dp);
1393 ofpbuf_use_const(&b, buf->data, buf->size);
1394 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1395 genl = ofpbuf_try_pull(&b, sizeof *genl);
1396 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1397 if (!nlmsg || !genl || !odp_header
1398 || nlmsg->nlmsg_type != odp_datapath_family
1399 || !nl_policy_parse(&b, 0, odp_datapath_policy, a,
1400 ARRAY_SIZE(odp_datapath_policy))) {
1404 dp->cmd = genl->cmd;
1405 dp->dp_ifindex = odp_header->dp_ifindex;
1406 dp->name = nl_attr_get_string(a[ODP_DP_ATTR_NAME]);
1407 if (a[ODP_DP_ATTR_STATS]) {
1408 /* Can't use structure assignment because Netlink doesn't ensure
1409 * sufficient alignment for 64-bit members. */
1410 memcpy(&dp->stats, nl_attr_get(a[ODP_DP_ATTR_STATS]),
1413 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1414 dp->ipv4_frags = nl_attr_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1416 if (a[ODP_DP_ATTR_SAMPLING]) {
1417 dp->sampling = nl_attr_get(a[ODP_DP_ATTR_SAMPLING]);
1420 if (a[ODP_DP_ATTR_MCGROUPS]) {
1421 static const struct nl_policy odp_mcgroup_policy[] = {
1422 [ODP_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1423 [ODP_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1424 [ODP_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1427 struct nlattr *mcgroups[ARRAY_SIZE(odp_mcgroup_policy)];
1429 if (!nl_parse_nested(a[ODP_DP_ATTR_MCGROUPS], odp_mcgroup_policy,
1430 mcgroups, ARRAY_SIZE(odp_mcgroup_policy))) {
1434 if (mcgroups[ODP_PACKET_CMD_MISS]) {
1435 dp->mcgroups[DPIF_UC_MISS]
1436 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_MISS]);
1438 if (mcgroups[ODP_PACKET_CMD_ACTION]) {
1439 dp->mcgroups[DPIF_UC_ACTION]
1440 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_ACTION]);
1442 if (mcgroups[ODP_PACKET_CMD_SAMPLE]) {
1443 dp->mcgroups[DPIF_UC_SAMPLE]
1444 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_SAMPLE]);
1451 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1453 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1455 struct odp_header *odp_header;
1457 nl_msg_put_genlmsghdr(buf, 0, odp_datapath_family,
1458 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1460 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1461 odp_header->dp_ifindex = dp->dp_ifindex;
1464 nl_msg_put_string(buf, ODP_DP_ATTR_NAME, dp->name);
1467 /* Skip ODP_DP_ATTR_STATS since we never have a reason to serialize it. */
1469 if (dp->ipv4_frags) {
1470 nl_msg_put_u32(buf, ODP_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1474 nl_msg_put_u32(buf, ODP_DP_ATTR_SAMPLING, *dp->sampling);
1478 /* Clears 'dp' to "empty" values. */
1480 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1482 memset(dp, 0, sizeof *dp);
1486 dpif_linux_dp_dump_start(struct nl_dump *dump)
1488 struct dpif_linux_dp request;
1491 dpif_linux_dp_init(&request);
1492 request.cmd = ODP_DP_CMD_GET;
1494 buf = ofpbuf_new(1024);
1495 dpif_linux_dp_to_ofpbuf(&request, buf);
1496 nl_dump_start(dump, genl_sock, buf);
1500 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1501 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1502 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1503 * result of the command is expected to be of the same form, which is decoded
1504 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1505 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1507 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1508 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1510 struct ofpbuf *request_buf;
1513 assert((reply != NULL) == (bufp != NULL));
1515 request_buf = ofpbuf_new(1024);
1516 dpif_linux_dp_to_ofpbuf(request, request_buf);
1517 error = nl_sock_transact(genl_sock, request_buf, bufp);
1518 ofpbuf_delete(request_buf);
1522 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1525 dpif_linux_dp_init(reply);
1526 ofpbuf_delete(*bufp);
1533 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1534 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1535 * will contain pointers into '*bufp'). */
1537 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1538 struct ofpbuf **bufp)
1540 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1541 struct dpif_linux_dp request;
1543 dpif_linux_dp_init(&request);
1544 request.cmd = ODP_DP_CMD_GET;
1545 request.dp_ifindex = dpif->dp_ifindex;
1547 return dpif_linux_dp_transact(&request, reply, bufp);
1550 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1551 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1552 * positive errno value.
1554 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1555 * while 'flow' is still in use. */
1557 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1558 const struct ofpbuf *buf)
1560 static const struct nl_policy odp_flow_policy[] = {
1561 [ODP_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1562 [ODP_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1563 [ODP_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1564 .min_len = sizeof(struct odp_flow_stats),
1565 .max_len = sizeof(struct odp_flow_stats),
1567 [ODP_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1568 [ODP_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1569 /* The kernel never uses ODP_FLOW_ATTR_CLEAR. */
1572 struct nlattr *a[ARRAY_SIZE(odp_flow_policy)];
1573 struct odp_header *odp_header;
1574 struct nlmsghdr *nlmsg;
1575 struct genlmsghdr *genl;
1578 dpif_linux_flow_init(flow);
1580 ofpbuf_use_const(&b, buf->data, buf->size);
1581 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1582 genl = ofpbuf_try_pull(&b, sizeof *genl);
1583 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1584 if (!nlmsg || !genl || !odp_header
1585 || nlmsg->nlmsg_type != odp_flow_family
1586 || !nl_policy_parse(&b, 0, odp_flow_policy, a,
1587 ARRAY_SIZE(odp_flow_policy))) {
1591 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1592 flow->dp_ifindex = odp_header->dp_ifindex;
1593 flow->key = nl_attr_get(a[ODP_FLOW_ATTR_KEY]);
1594 flow->key_len = nl_attr_get_size(a[ODP_FLOW_ATTR_KEY]);
1595 if (a[ODP_FLOW_ATTR_ACTIONS]) {
1596 flow->actions = nl_attr_get(a[ODP_FLOW_ATTR_ACTIONS]);
1597 flow->actions_len = nl_attr_get_size(a[ODP_FLOW_ATTR_ACTIONS]);
1599 if (a[ODP_FLOW_ATTR_STATS]) {
1600 flow->stats = nl_attr_get(a[ODP_FLOW_ATTR_STATS]);
1602 if (a[ODP_FLOW_ATTR_TCP_FLAGS]) {
1603 flow->tcp_flags = nl_attr_get(a[ODP_FLOW_ATTR_TCP_FLAGS]);
1605 if (a[ODP_FLOW_ATTR_USED]) {
1606 flow->used = nl_attr_get(a[ODP_FLOW_ATTR_USED]);
1611 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1612 * followed by Netlink attributes corresponding to 'flow'. */
1614 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1617 struct odp_header *odp_header;
1619 nl_msg_put_genlmsghdr(buf, 0, odp_flow_family,
1620 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1623 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1624 odp_header->dp_ifindex = flow->dp_ifindex;
1626 if (flow->key_len) {
1627 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_KEY, flow->key, flow->key_len);
1630 if (flow->actions || flow->actions_len) {
1631 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_ACTIONS,
1632 flow->actions, flow->actions_len);
1635 /* We never need to send these to the kernel. */
1636 assert(!flow->stats);
1637 assert(!flow->tcp_flags);
1638 assert(!flow->used);
1641 nl_msg_put_flag(buf, ODP_FLOW_ATTR_CLEAR);
1645 /* Clears 'flow' to "empty" values. */
1647 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1649 memset(flow, 0, sizeof *flow);
1652 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1653 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1654 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1655 * result of the command is expected to be a flow also, which is decoded and
1656 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1657 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1659 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1660 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1662 struct ofpbuf *request_buf;
1665 assert((reply != NULL) == (bufp != NULL));
1667 request_buf = ofpbuf_new(1024);
1668 dpif_linux_flow_to_ofpbuf(request, request_buf);
1669 error = nl_sock_transact(genl_sock, request_buf, bufp);
1670 ofpbuf_delete(request_buf);
1674 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1677 dpif_linux_flow_init(reply);
1678 ofpbuf_delete(*bufp);
1686 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1687 struct dpif_flow_stats *stats)
1690 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1691 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1693 stats->n_packets = 0;
1696 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1697 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;