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-socket.h"
46 #include "openvswitch/tunnel.h"
48 #include "poll-loop.h"
49 #include "rtnetlink.h"
50 #include "rtnetlink-link.h"
53 #include "unaligned.h"
57 VLOG_DEFINE_THIS_MODULE(dpif_linux);
59 enum { LRU_MAX_PORTS = 1024 };
60 enum { LRU_MASK = LRU_MAX_PORTS - 1};
61 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
63 struct dpif_linux_dp {
64 /* Generic Netlink header. */
67 /* struct odp_header. */
71 const char *name; /* ODP_DP_ATTR_NAME. */
72 struct odp_stats stats; /* ODP_DP_ATTR_STATS. */
73 enum odp_frag_handling ipv4_frags; /* ODP_DP_ATTR_IPV4_FRAGS. */
74 const uint32_t *sampling; /* ODP_DP_ATTR_SAMPLING. */
75 uint32_t mcgroups[DPIF_N_UC_TYPES]; /* ODP_DP_ATTR_MCGROUPS. */
78 static void dpif_linux_dp_init(struct dpif_linux_dp *);
79 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
80 const struct ofpbuf *);
81 static void dpif_linux_dp_dump_start(struct nl_dump *);
82 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
83 struct dpif_linux_dp *reply,
84 struct ofpbuf **bufp);
85 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
86 struct ofpbuf **bufp);
88 struct dpif_linux_flow {
89 /* Generic Netlink header. */
92 /* struct odp_header. */
93 unsigned int nlmsg_flags;
98 * The 'stats' and 'used' members point to 64-bit data that might only be
99 * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
100 * access their values.
102 * If 'actions' is nonnull then ODP_FLOW_ATTR_ACTIONS will be included in
103 * the Netlink version of the command, even if actions_len is zero. */
104 const struct nlattr *key; /* ODP_FLOW_ATTR_KEY. */
106 const struct nlattr *actions; /* ODP_FLOW_ATTR_ACTIONS. */
108 const struct odp_flow_stats *stats; /* ODP_FLOW_ATTR_STATS. */
109 const uint8_t *tcp_flags; /* ODP_FLOW_ATTR_TCP_FLAGS. */
110 const uint64_t *used; /* ODP_FLOW_ATTR_USED. */
111 bool clear; /* ODP_FLOW_ATTR_CLEAR. */
114 static void dpif_linux_flow_init(struct dpif_linux_flow *);
115 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
116 const struct ofpbuf *);
117 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
119 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
120 struct dpif_linux_flow *reply,
121 struct ofpbuf **bufp);
122 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
123 struct dpif_flow_stats *);
125 /* Datapath interface for the openvswitch Linux kernel module. */
130 /* Multicast group messages. */
131 struct nl_sock *mc_sock;
132 uint32_t mcgroups[DPIF_N_UC_TYPES];
133 unsigned int listen_mask;
135 /* Change notification. */
136 struct sset changed_ports; /* Ports that have changed. */
137 struct rtnetlink_notifier port_notifier;
140 /* Queue of unused ports. */
141 unsigned long *lru_bitmap;
142 uint16_t lru_ports[LRU_MAX_PORTS];
147 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
149 /* Generic Netlink family numbers for ODP. */
150 static int odp_datapath_family;
151 static int odp_vport_family;
152 static int odp_flow_family;
153 static int odp_packet_family;
155 /* Generic Netlink socket. */
156 static struct nl_sock *genl_sock;
158 static int dpif_linux_init(void);
159 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
160 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
163 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
165 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
166 const struct ofpbuf *);
168 static struct dpif_linux *
169 dpif_linux_cast(const struct dpif *dpif)
171 dpif_assert_class(dpif, &dpif_linux_class);
172 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
176 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
178 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
179 bitmap_set1(dp->lru_bitmap, port);
180 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
185 dpif_linux_pop_port(struct dpif_linux *dp)
189 if (dp->lru_head == dp->lru_tail) {
193 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
194 bitmap_set0(dp->lru_bitmap, port);
199 dpif_linux_enumerate(struct sset *all_dps)
205 error = dpif_linux_init();
210 dpif_linux_dp_dump_start(&dump);
211 while (nl_dump_next(&dump, &msg)) {
212 struct dpif_linux_dp dp;
214 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
215 sset_add(all_dps, dp.name);
218 return nl_dump_done(&dump);
222 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
223 bool create, struct dpif **dpifp)
225 struct dpif_linux_dp dp_request, dp;
229 error = dpif_linux_init();
234 /* Create or look up datapath. */
235 dpif_linux_dp_init(&dp_request);
236 dp_request.cmd = create ? ODP_DP_CMD_NEW : ODP_DP_CMD_GET;
237 dp_request.name = name;
238 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
242 error = open_dpif(&dp, dpifp);
249 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
251 struct dpif_linux *dpif;
255 dpif = xmalloc(sizeof *dpif);
256 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
257 dpif_linux_port_changed, dpif);
262 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
263 dp->dp_ifindex, dp->dp_ifindex);
265 dpif->mc_sock = NULL;
266 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
267 dpif->mcgroups[i] = dp->mcgroups[i];
269 dpif->listen_mask = 0;
270 dpif->dp_ifindex = dp->dp_ifindex;
271 sset_init(&dpif->changed_ports);
272 dpif->change_error = false;
273 *dpifp = &dpif->dpif;
275 dpif->lru_head = dpif->lru_tail = 0;
276 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
277 bitmap_set1(dpif->lru_bitmap, ODPP_LOCAL);
278 for (i = 1; i < LRU_MAX_PORTS; i++) {
279 dpif_linux_push_port(dpif, i);
289 dpif_linux_close(struct dpif *dpif_)
291 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
292 nl_sock_destroy(dpif->mc_sock);
293 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
294 sset_destroy(&dpif->changed_ports);
295 free(dpif->lru_bitmap);
300 dpif_linux_destroy(struct dpif *dpif_)
302 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
303 struct dpif_linux_dp dp;
305 dpif_linux_dp_init(&dp);
306 dp.cmd = ODP_DP_CMD_DEL;
307 dp.dp_ifindex = dpif->dp_ifindex;
308 return dpif_linux_dp_transact(&dp, NULL, NULL);
312 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
314 struct dpif_linux_dp dp;
318 error = dpif_linux_dp_get(dpif_, &dp, &buf);
327 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
329 struct dpif_linux_dp dp;
333 error = dpif_linux_dp_get(dpif_, &dp, &buf);
335 *drop_fragsp = dp.ipv4_frags == ODP_DP_FRAG_DROP;
342 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
344 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
345 struct dpif_linux_dp dp;
347 dpif_linux_dp_init(&dp);
348 dp.cmd = ODP_DP_CMD_SET;
349 dp.dp_ifindex = dpif->dp_ifindex;
350 dp.ipv4_frags = drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO;
351 return dpif_linux_dp_transact(&dp, NULL, NULL);
355 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
358 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
359 const char *name = netdev_get_name(netdev);
360 const char *type = netdev_get_type(netdev);
361 struct dpif_linux_vport request, reply;
362 const struct ofpbuf *options;
366 dpif_linux_vport_init(&request);
367 request.cmd = ODP_VPORT_CMD_NEW;
368 request.dp_ifindex = dpif->dp_ifindex;
369 request.type = netdev_vport_get_vport_type(netdev);
370 if (request.type == ODP_VPORT_TYPE_UNSPEC) {
371 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
372 "unsupported type `%s'",
373 dpif_name(dpif_), name, type);
378 options = netdev_vport_get_options(netdev);
379 if (options && options->size) {
380 request.options = options->data;
381 request.options_len = options->size;
384 /* Loop until we find a port that isn't used. */
386 request.port_no = dpif_linux_pop_port(dpif);
387 error = dpif_linux_vport_transact(&request, &reply, &buf);
390 *port_nop = reply.port_no;
393 } while (request.port_no != UINT32_MAX
394 && (error == EBUSY || error == EFBIG));
400 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
402 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
403 struct dpif_linux_vport vport;
406 dpif_linux_vport_init(&vport);
407 vport.cmd = ODP_VPORT_CMD_DEL;
408 vport.dp_ifindex = dpif->dp_ifindex;
409 vport.port_no = port_no;
410 error = dpif_linux_vport_transact(&vport, NULL, NULL);
413 dpif_linux_push_port(dpif, port_no);
419 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
420 const char *port_name, struct dpif_port *dpif_port)
422 struct dpif_linux_vport request;
423 struct dpif_linux_vport reply;
427 dpif_linux_vport_init(&request);
428 request.cmd = ODP_VPORT_CMD_GET;
429 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
430 request.port_no = port_no;
431 request.name = port_name;
433 error = dpif_linux_vport_transact(&request, &reply, &buf);
435 dpif_port->name = xstrdup(reply.name);
436 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
437 dpif_port->port_no = reply.port_no;
439 netdev_stats_from_rtnl_link_stats64(&dpif_port->stats,
442 memset(&dpif_port->stats, 0xff, sizeof dpif_port->stats);
450 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
451 struct dpif_port *dpif_port)
453 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
457 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
458 struct dpif_port *dpif_port)
460 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
464 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
466 /* If the datapath increases its range of supported ports, then it should
467 * start reporting that. */
472 dpif_linux_flow_flush(struct dpif *dpif_)
474 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
475 struct dpif_linux_flow flow;
477 dpif_linux_flow_init(&flow);
478 flow.cmd = ODP_FLOW_CMD_DEL;
479 flow.dp_ifindex = dpif->dp_ifindex;
480 return dpif_linux_flow_transact(&flow, NULL, NULL);
483 struct dpif_linux_port_state {
485 unsigned long *port_bitmap; /* Ports in the datapath. */
486 bool complete; /* Dump completed without error. */
490 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
492 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
493 struct dpif_linux_port_state *state;
494 struct dpif_linux_vport request;
497 *statep = state = xmalloc(sizeof *state);
498 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
499 state->complete = false;
501 dpif_linux_vport_init(&request);
502 request.cmd = ODP_DP_CMD_GET;
503 request.dp_ifindex = dpif->dp_ifindex;
505 buf = ofpbuf_new(1024);
506 dpif_linux_vport_to_ofpbuf(&request, buf);
507 nl_dump_start(&state->dump, genl_sock, buf);
514 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
515 struct dpif_port *dpif_port)
517 struct dpif_linux_port_state *state = state_;
518 struct dpif_linux_vport vport;
522 if (!nl_dump_next(&state->dump, &buf)) {
523 state->complete = true;
527 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
532 if (vport.port_no < LRU_MAX_PORTS) {
533 bitmap_set1(state->port_bitmap, vport.port_no);
536 dpif_port->name = (char *) vport.name;
537 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
538 dpif_port->port_no = vport.port_no;
540 netdev_stats_from_rtnl_link_stats64(&dpif_port->stats, vport.stats);
542 memset(&dpif_port->stats, 0xff, sizeof dpif_port->stats);
548 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
550 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
551 struct dpif_linux_port_state *state = state_;
552 int error = nl_dump_done(&state->dump);
554 if (state->complete) {
557 for (i = 0; i < LRU_MAX_PORTS; i++) {
558 if (!bitmap_is_set(state->port_bitmap, i)) {
559 dpif_linux_push_port(dpif, i);
564 free(state->port_bitmap);
570 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
572 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
574 if (dpif->change_error) {
575 dpif->change_error = false;
576 sset_clear(&dpif->changed_ports);
578 } else if (!sset_is_empty(&dpif->changed_ports)) {
579 *devnamep = sset_pop(&dpif->changed_ports);
587 dpif_linux_port_poll_wait(const struct dpif *dpif_)
589 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
590 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
591 poll_immediate_wake();
593 rtnetlink_link_notifier_wait();
598 dpif_linux_flow_get__(const struct dpif *dpif_,
599 const struct nlattr *key, size_t key_len,
600 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
602 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
603 struct dpif_linux_flow request;
605 dpif_linux_flow_init(&request);
606 request.cmd = ODP_FLOW_CMD_GET;
607 request.dp_ifindex = dpif->dp_ifindex;
609 request.key_len = key_len;
610 return dpif_linux_flow_transact(&request, reply, bufp);
614 dpif_linux_flow_get(const struct dpif *dpif_,
615 const struct nlattr *key, size_t key_len,
616 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
618 struct dpif_linux_flow reply;
622 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
625 dpif_linux_flow_get_stats(&reply, stats);
628 buf->data = (void *) reply.actions;
629 buf->size = reply.actions_len;
639 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
640 const struct nlattr *key, size_t key_len,
641 const struct nlattr *actions, size_t actions_len,
642 struct dpif_flow_stats *stats)
644 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
645 struct dpif_linux_flow request, reply;
646 struct nlattr dummy_action;
650 dpif_linux_flow_init(&request);
651 request.cmd = flags & DPIF_FP_CREATE ? ODP_FLOW_CMD_NEW : ODP_FLOW_CMD_SET;
652 request.dp_ifindex = dpif->dp_ifindex;
654 request.key_len = key_len;
655 /* Ensure that ODP_FLOW_ATTR_ACTIONS will always be included. */
656 request.actions = actions ? actions : &dummy_action;
657 request.actions_len = actions_len;
658 if (flags & DPIF_FP_ZERO_STATS) {
659 request.clear = true;
661 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
662 error = dpif_linux_flow_transact(&request,
663 stats ? &reply : NULL,
664 stats ? &buf : NULL);
665 if (!error && stats) {
666 dpif_linux_flow_get_stats(&reply, stats);
673 dpif_linux_flow_del(struct dpif *dpif_,
674 const struct nlattr *key, size_t key_len,
675 struct dpif_flow_stats *stats)
677 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
678 struct dpif_linux_flow request, reply;
682 dpif_linux_flow_init(&request);
683 request.cmd = ODP_FLOW_CMD_DEL;
684 request.dp_ifindex = dpif->dp_ifindex;
686 request.key_len = key_len;
687 error = dpif_linux_flow_transact(&request,
688 stats ? &reply : NULL,
689 stats ? &buf : NULL);
690 if (!error && stats) {
691 dpif_linux_flow_get_stats(&reply, stats);
697 struct dpif_linux_flow_state {
699 struct dpif_linux_flow flow;
700 struct dpif_flow_stats stats;
705 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
707 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
708 struct dpif_linux_flow_state *state;
709 struct dpif_linux_flow request;
712 *statep = state = xmalloc(sizeof *state);
714 dpif_linux_flow_init(&request);
715 request.cmd = ODP_DP_CMD_GET;
716 request.dp_ifindex = dpif->dp_ifindex;
718 buf = ofpbuf_new(1024);
719 dpif_linux_flow_to_ofpbuf(&request, buf);
720 nl_dump_start(&state->dump, genl_sock, buf);
729 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
730 const struct nlattr **key, size_t *key_len,
731 const struct nlattr **actions, size_t *actions_len,
732 const struct dpif_flow_stats **stats)
734 struct dpif_linux_flow_state *state = state_;
739 ofpbuf_delete(state->buf);
742 if (!nl_dump_next(&state->dump, &buf)) {
746 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
751 if (actions && !state->flow.actions) {
752 error = dpif_linux_flow_get__(dpif_, state->flow.key,
754 &state->flow, &state->buf);
755 if (error == ENOENT) {
756 VLOG_DBG("dumped flow disappeared on get");
758 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
764 *actions = state->flow.actions;
765 *actions_len = state->flow.actions_len;
768 *key = state->flow.key;
769 *key_len = state->flow.key_len;
772 dpif_linux_flow_get_stats(&state->flow, &state->stats);
773 *stats = &state->stats;
779 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
781 struct dpif_linux_flow_state *state = state_;
782 int error = nl_dump_done(&state->dump);
783 ofpbuf_delete(state->buf);
789 dpif_linux_execute__(int dp_ifindex,
790 const struct nlattr *key, size_t key_len,
791 const struct nlattr *actions, size_t actions_len,
792 const struct ofpbuf *packet)
794 struct odp_header *execute;
798 buf = ofpbuf_new(128 + actions_len + packet->size);
800 nl_msg_put_genlmsghdr(buf, 0, odp_packet_family, NLM_F_REQUEST,
801 ODP_PACKET_CMD_EXECUTE, 1);
803 execute = ofpbuf_put_uninit(buf, sizeof *execute);
804 execute->dp_ifindex = dp_ifindex;
806 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, packet->data, packet->size);
807 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_KEY, key, key_len);
808 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_ACTIONS, actions, actions_len);
810 error = nl_sock_transact(genl_sock, buf, NULL);
816 dpif_linux_execute(struct dpif *dpif_,
817 const struct nlattr *key, size_t key_len,
818 const struct nlattr *actions, size_t actions_len,
819 const struct ofpbuf *packet)
821 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
823 return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
824 actions, actions_len, packet);
828 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
830 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
831 *listen_mask = dpif->listen_mask;
836 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
838 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
842 if (listen_mask == dpif->listen_mask) {
844 } else if (!listen_mask) {
845 nl_sock_destroy(dpif->mc_sock);
846 dpif->mc_sock = NULL;
847 dpif->listen_mask = 0;
849 } else if (!dpif->mc_sock) {
850 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
856 /* Unsubscribe from old groups. */
857 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
858 if (dpif->listen_mask & (1u << i)) {
859 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
863 /* Update listen_mask. */
864 dpif->listen_mask = listen_mask;
866 /* Subscribe to new groups. */
868 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
869 if (dpif->listen_mask & (1u << i)) {
872 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
882 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
883 uint32_t *probability)
885 struct dpif_linux_dp dp;
889 error = dpif_linux_dp_get(dpif_, &dp, &buf);
891 *probability = dp.sampling ? *dp.sampling : 0;
898 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
900 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
901 struct dpif_linux_dp dp;
903 dpif_linux_dp_init(&dp);
904 dp.cmd = ODP_DP_CMD_SET;
905 dp.dp_ifindex = dpif->dp_ifindex;
906 dp.sampling = &probability;
907 return dpif_linux_dp_transact(&dp, NULL, NULL);
911 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
912 uint32_t queue_id, uint32_t *priority)
914 if (queue_id < 0xf000) {
915 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
923 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
926 static const struct nl_policy odp_packet_policy[] = {
927 /* Always present. */
928 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
929 .min_len = ETH_HEADER_LEN },
930 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
932 /* ODP_PACKET_CMD_ACTION only. */
933 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
935 /* ODP_PACKET_CMD_SAMPLE only. */
936 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
937 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
940 struct odp_header *odp_header;
941 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
942 struct nlmsghdr *nlmsg;
943 struct genlmsghdr *genl;
947 ofpbuf_use_const(&b, buf->data, buf->size);
949 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
950 genl = ofpbuf_try_pull(&b, sizeof *genl);
951 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
952 if (!nlmsg || !genl || !odp_header
953 || nlmsg->nlmsg_type != odp_packet_family
954 || !nl_policy_parse(&b, 0, odp_packet_policy, a,
955 ARRAY_SIZE(odp_packet_policy))) {
959 type = (genl->cmd == ODP_PACKET_CMD_MISS ? DPIF_UC_MISS
960 : genl->cmd == ODP_PACKET_CMD_ACTION ? DPIF_UC_ACTION
961 : genl->cmd == ODP_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
967 memset(upcall, 0, sizeof *upcall);
969 upcall->packet = buf;
970 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
971 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
972 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
973 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
974 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
975 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
977 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
978 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
980 if (a[ODP_PACKET_ATTR_ACTIONS]) {
981 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
982 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
985 *dp_ifindex = odp_header->dp_ifindex;
991 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
993 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
998 if (!dpif->mc_sock) {
1002 for (i = 0; i < 50; i++) {
1005 error = nl_sock_recv(dpif->mc_sock, &buf, false);
1010 error = parse_odp_packet(buf, upcall, &dp_ifindex);
1012 && dp_ifindex == dpif->dp_ifindex
1013 && dpif->listen_mask & (1u << upcall->type)) {
1027 dpif_linux_recv_wait(struct dpif *dpif_)
1029 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1030 if (dpif->mc_sock) {
1031 nl_sock_wait(dpif->mc_sock, POLLIN);
1036 dpif_linux_recv_purge(struct dpif *dpif_)
1038 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1040 if (dpif->mc_sock) {
1041 nl_sock_drain(dpif->mc_sock);
1045 const struct dpif_class dpif_linux_class = {
1047 dpif_linux_enumerate,
1053 dpif_linux_get_stats,
1054 dpif_linux_get_drop_frags,
1055 dpif_linux_set_drop_frags,
1056 dpif_linux_port_add,
1057 dpif_linux_port_del,
1058 dpif_linux_port_query_by_number,
1059 dpif_linux_port_query_by_name,
1060 dpif_linux_get_max_ports,
1061 dpif_linux_port_dump_start,
1062 dpif_linux_port_dump_next,
1063 dpif_linux_port_dump_done,
1064 dpif_linux_port_poll,
1065 dpif_linux_port_poll_wait,
1066 dpif_linux_flow_get,
1067 dpif_linux_flow_put,
1068 dpif_linux_flow_del,
1069 dpif_linux_flow_flush,
1070 dpif_linux_flow_dump_start,
1071 dpif_linux_flow_dump_next,
1072 dpif_linux_flow_dump_done,
1074 dpif_linux_recv_get_mask,
1075 dpif_linux_recv_set_mask,
1076 dpif_linux_get_sflow_probability,
1077 dpif_linux_set_sflow_probability,
1078 dpif_linux_queue_to_priority,
1080 dpif_linux_recv_wait,
1081 dpif_linux_recv_purge,
1085 dpif_linux_init(void)
1087 static int error = -1;
1090 error = nl_lookup_genl_family(ODP_DATAPATH_FAMILY,
1091 &odp_datapath_family);
1093 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1094 "The Open vSwitch kernel module is probably not loaded.",
1095 ODP_DATAPATH_FAMILY);
1098 error = nl_lookup_genl_family(ODP_VPORT_FAMILY, &odp_vport_family);
1101 error = nl_lookup_genl_family(ODP_FLOW_FAMILY, &odp_flow_family);
1104 error = nl_lookup_genl_family(ODP_PACKET_FAMILY,
1105 &odp_packet_family);
1108 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1116 dpif_linux_is_internal_device(const char *name)
1118 struct dpif_linux_vport reply;
1122 error = dpif_linux_vport_get(name, &reply, &buf);
1125 } else if (error != ENODEV && error != ENOENT) {
1126 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1127 name, strerror(error));
1130 return reply.type == ODP_VPORT_TYPE_INTERNAL;
1134 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1135 const void *data, size_t size)
1137 struct ofpbuf actions, key, packet;
1138 struct odputil_keybuf keybuf;
1142 ofpbuf_use_const(&packet, data, size);
1143 flow_extract(&packet, htonll(0), 0, &flow);
1145 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1146 odp_flow_key_from_flow(&key, &flow);
1148 ofpbuf_use_stack(&actions, &action, sizeof action);
1149 nl_msg_put_u32(&actions, ODP_ACTION_ATTR_OUTPUT, port_no);
1151 return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1152 actions.data, actions.size, &packet);
1156 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1159 struct dpif_linux *dpif = dpif_;
1162 if (change->master_ifindex == dpif->dp_ifindex
1163 && (change->nlmsg_type == RTM_NEWLINK
1164 || change->nlmsg_type == RTM_DELLINK))
1166 /* Our datapath changed, either adding a new port or deleting an
1168 sset_add(&dpif->changed_ports, change->ifname);
1171 dpif->change_error = true;
1175 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1176 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1177 * positive errno value.
1179 * 'vport' will contain pointers into 'buf', so the caller should not free
1180 * 'buf' while 'vport' is still in use. */
1182 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1183 const struct ofpbuf *buf)
1185 static const struct nl_policy odp_vport_policy[] = {
1186 [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1187 [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1188 [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1189 [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1190 .min_len = sizeof(struct rtnl_link_stats64),
1191 .max_len = sizeof(struct rtnl_link_stats64),
1193 [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1194 .min_len = ETH_ADDR_LEN,
1195 .max_len = ETH_ADDR_LEN,
1197 [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1198 [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1199 [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1200 [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1203 struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
1204 struct odp_header *odp_header;
1205 struct nlmsghdr *nlmsg;
1206 struct genlmsghdr *genl;
1209 dpif_linux_vport_init(vport);
1211 ofpbuf_use_const(&b, buf->data, buf->size);
1212 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1213 genl = ofpbuf_try_pull(&b, sizeof *genl);
1214 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1215 if (!nlmsg || !genl || !odp_header
1216 || nlmsg->nlmsg_type != odp_vport_family
1217 || !nl_policy_parse(&b, 0, odp_vport_policy, a,
1218 ARRAY_SIZE(odp_vport_policy))) {
1222 vport->cmd = genl->cmd;
1223 vport->dp_ifindex = odp_header->dp_ifindex;
1224 vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1225 vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1226 vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
1227 if (a[ODP_VPORT_ATTR_STATS]) {
1228 vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
1230 if (a[ODP_VPORT_ATTR_ADDRESS]) {
1231 vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
1233 if (a[ODP_VPORT_ATTR_MTU]) {
1234 vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
1236 vport->mtu = INT_MAX;
1238 if (a[ODP_VPORT_ATTR_OPTIONS]) {
1239 vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
1240 vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
1242 if (a[ODP_VPORT_ATTR_IFINDEX]) {
1243 vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
1245 if (a[ODP_VPORT_ATTR_IFLINK]) {
1246 vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
1251 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1252 * followed by Netlink attributes corresponding to 'vport'. */
1254 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1257 struct odp_header *odp_header;
1259 nl_msg_put_genlmsghdr(buf, 0, odp_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1262 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1263 odp_header->dp_ifindex = vport->dp_ifindex;
1265 if (vport->port_no != UINT32_MAX) {
1266 nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1269 if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
1270 nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1274 nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1278 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1279 vport->stats, sizeof *vport->stats);
1282 if (vport->address) {
1283 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1284 vport->address, ETH_ADDR_LEN);
1287 if (vport->mtu && vport->mtu != INT_MAX) {
1288 nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1291 if (vport->options) {
1292 nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1293 vport->options, vport->options_len);
1296 if (vport->ifindex) {
1297 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1300 if (vport->iflink) {
1301 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1305 /* Clears 'vport' to "empty" values. */
1307 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1309 memset(vport, 0, sizeof *vport);
1310 vport->port_no = UINT32_MAX;
1313 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1314 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1315 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1316 * result of the command is expected to be an odp_vport also, which is decoded
1317 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1318 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1320 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1321 struct dpif_linux_vport *reply,
1322 struct ofpbuf **bufp)
1324 struct ofpbuf *request_buf;
1327 assert((reply != NULL) == (bufp != NULL));
1329 error = dpif_linux_init();
1333 dpif_linux_vport_init(reply);
1338 request_buf = ofpbuf_new(1024);
1339 dpif_linux_vport_to_ofpbuf(request, request_buf);
1340 error = nl_sock_transact(genl_sock, request_buf, bufp);
1341 ofpbuf_delete(request_buf);
1345 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1348 dpif_linux_vport_init(reply);
1349 ofpbuf_delete(*bufp);
1356 /* Obtains information about the kernel vport named 'name' and stores it into
1357 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1358 * longer needed ('reply' will contain pointers into '*bufp'). */
1360 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1361 struct ofpbuf **bufp)
1363 struct dpif_linux_vport request;
1365 dpif_linux_vport_init(&request);
1366 request.cmd = ODP_VPORT_CMD_GET;
1367 request.name = name;
1369 return dpif_linux_vport_transact(&request, reply, bufp);
1372 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1373 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1374 * positive errno value.
1376 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1377 * while 'dp' is still in use. */
1379 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1381 static const struct nl_policy odp_datapath_policy[] = {
1382 [ODP_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1383 [ODP_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1384 .min_len = sizeof(struct odp_stats),
1385 .max_len = sizeof(struct odp_stats),
1387 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1388 [ODP_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1389 [ODP_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1392 struct nlattr *a[ARRAY_SIZE(odp_datapath_policy)];
1393 struct odp_header *odp_header;
1394 struct nlmsghdr *nlmsg;
1395 struct genlmsghdr *genl;
1398 dpif_linux_dp_init(dp);
1400 ofpbuf_use_const(&b, buf->data, buf->size);
1401 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1402 genl = ofpbuf_try_pull(&b, sizeof *genl);
1403 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1404 if (!nlmsg || !genl || !odp_header
1405 || nlmsg->nlmsg_type != odp_datapath_family
1406 || !nl_policy_parse(&b, 0, odp_datapath_policy, a,
1407 ARRAY_SIZE(odp_datapath_policy))) {
1411 dp->cmd = genl->cmd;
1412 dp->dp_ifindex = odp_header->dp_ifindex;
1413 dp->name = nl_attr_get_string(a[ODP_DP_ATTR_NAME]);
1414 if (a[ODP_DP_ATTR_STATS]) {
1415 /* Can't use structure assignment because Netlink doesn't ensure
1416 * sufficient alignment for 64-bit members. */
1417 memcpy(&dp->stats, nl_attr_get(a[ODP_DP_ATTR_STATS]),
1420 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1421 dp->ipv4_frags = nl_attr_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1423 if (a[ODP_DP_ATTR_SAMPLING]) {
1424 dp->sampling = nl_attr_get(a[ODP_DP_ATTR_SAMPLING]);
1427 if (a[ODP_DP_ATTR_MCGROUPS]) {
1428 static const struct nl_policy odp_mcgroup_policy[] = {
1429 [ODP_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1430 [ODP_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1431 [ODP_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1434 struct nlattr *mcgroups[ARRAY_SIZE(odp_mcgroup_policy)];
1436 if (!nl_parse_nested(a[ODP_DP_ATTR_MCGROUPS], odp_mcgroup_policy,
1437 mcgroups, ARRAY_SIZE(odp_mcgroup_policy))) {
1441 if (mcgroups[ODP_PACKET_CMD_MISS]) {
1442 dp->mcgroups[DPIF_UC_MISS]
1443 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_MISS]);
1445 if (mcgroups[ODP_PACKET_CMD_ACTION]) {
1446 dp->mcgroups[DPIF_UC_ACTION]
1447 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_ACTION]);
1449 if (mcgroups[ODP_PACKET_CMD_SAMPLE]) {
1450 dp->mcgroups[DPIF_UC_SAMPLE]
1451 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_SAMPLE]);
1458 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1460 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1462 struct odp_header *odp_header;
1464 nl_msg_put_genlmsghdr(buf, 0, odp_datapath_family,
1465 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1467 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1468 odp_header->dp_ifindex = dp->dp_ifindex;
1471 nl_msg_put_string(buf, ODP_DP_ATTR_NAME, dp->name);
1474 /* Skip ODP_DP_ATTR_STATS since we never have a reason to serialize it. */
1476 if (dp->ipv4_frags) {
1477 nl_msg_put_u32(buf, ODP_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1481 nl_msg_put_u32(buf, ODP_DP_ATTR_SAMPLING, *dp->sampling);
1485 /* Clears 'dp' to "empty" values. */
1487 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1489 memset(dp, 0, sizeof *dp);
1493 dpif_linux_dp_dump_start(struct nl_dump *dump)
1495 struct dpif_linux_dp request;
1498 dpif_linux_dp_init(&request);
1499 request.cmd = ODP_DP_CMD_GET;
1501 buf = ofpbuf_new(1024);
1502 dpif_linux_dp_to_ofpbuf(&request, buf);
1503 nl_dump_start(dump, genl_sock, buf);
1507 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1508 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1509 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1510 * result of the command is expected to be of the same form, which is decoded
1511 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1512 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1514 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1515 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1517 struct ofpbuf *request_buf;
1520 assert((reply != NULL) == (bufp != NULL));
1522 request_buf = ofpbuf_new(1024);
1523 dpif_linux_dp_to_ofpbuf(request, request_buf);
1524 error = nl_sock_transact(genl_sock, request_buf, bufp);
1525 ofpbuf_delete(request_buf);
1529 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1532 dpif_linux_dp_init(reply);
1533 ofpbuf_delete(*bufp);
1540 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1541 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1542 * will contain pointers into '*bufp'). */
1544 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1545 struct ofpbuf **bufp)
1547 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1548 struct dpif_linux_dp request;
1550 dpif_linux_dp_init(&request);
1551 request.cmd = ODP_DP_CMD_GET;
1552 request.dp_ifindex = dpif->dp_ifindex;
1554 return dpif_linux_dp_transact(&request, reply, bufp);
1557 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1558 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1559 * positive errno value.
1561 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1562 * while 'flow' is still in use. */
1564 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1565 const struct ofpbuf *buf)
1567 static const struct nl_policy odp_flow_policy[] = {
1568 [ODP_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1569 [ODP_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1570 [ODP_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1571 .min_len = sizeof(struct odp_flow_stats),
1572 .max_len = sizeof(struct odp_flow_stats),
1574 [ODP_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1575 [ODP_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1576 /* The kernel never uses ODP_FLOW_ATTR_CLEAR. */
1579 struct nlattr *a[ARRAY_SIZE(odp_flow_policy)];
1580 struct odp_header *odp_header;
1581 struct nlmsghdr *nlmsg;
1582 struct genlmsghdr *genl;
1585 dpif_linux_flow_init(flow);
1587 ofpbuf_use_const(&b, buf->data, buf->size);
1588 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1589 genl = ofpbuf_try_pull(&b, sizeof *genl);
1590 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1591 if (!nlmsg || !genl || !odp_header
1592 || nlmsg->nlmsg_type != odp_flow_family
1593 || !nl_policy_parse(&b, 0, odp_flow_policy, a,
1594 ARRAY_SIZE(odp_flow_policy))) {
1598 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1599 flow->dp_ifindex = odp_header->dp_ifindex;
1600 flow->key = nl_attr_get(a[ODP_FLOW_ATTR_KEY]);
1601 flow->key_len = nl_attr_get_size(a[ODP_FLOW_ATTR_KEY]);
1602 if (a[ODP_FLOW_ATTR_ACTIONS]) {
1603 flow->actions = nl_attr_get(a[ODP_FLOW_ATTR_ACTIONS]);
1604 flow->actions_len = nl_attr_get_size(a[ODP_FLOW_ATTR_ACTIONS]);
1606 if (a[ODP_FLOW_ATTR_STATS]) {
1607 flow->stats = nl_attr_get(a[ODP_FLOW_ATTR_STATS]);
1609 if (a[ODP_FLOW_ATTR_TCP_FLAGS]) {
1610 flow->tcp_flags = nl_attr_get(a[ODP_FLOW_ATTR_TCP_FLAGS]);
1612 if (a[ODP_FLOW_ATTR_USED]) {
1613 flow->used = nl_attr_get(a[ODP_FLOW_ATTR_USED]);
1618 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1619 * followed by Netlink attributes corresponding to 'flow'. */
1621 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1624 struct odp_header *odp_header;
1626 nl_msg_put_genlmsghdr(buf, 0, odp_flow_family,
1627 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1630 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1631 odp_header->dp_ifindex = flow->dp_ifindex;
1633 if (flow->key_len) {
1634 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_KEY, flow->key, flow->key_len);
1637 if (flow->actions || flow->actions_len) {
1638 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_ACTIONS,
1639 flow->actions, flow->actions_len);
1642 /* We never need to send these to the kernel. */
1643 assert(!flow->stats);
1644 assert(!flow->tcp_flags);
1645 assert(!flow->used);
1648 nl_msg_put_flag(buf, ODP_FLOW_ATTR_CLEAR);
1652 /* Clears 'flow' to "empty" values. */
1654 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1656 memset(flow, 0, sizeof *flow);
1659 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1660 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1661 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1662 * result of the command is expected to be a flow also, which is decoded and
1663 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1664 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1666 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1667 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1669 struct ofpbuf *request_buf;
1672 assert((reply != NULL) == (bufp != NULL));
1674 request_buf = ofpbuf_new(1024);
1675 dpif_linux_flow_to_ofpbuf(request, request_buf);
1676 error = nl_sock_transact(genl_sock, request_buf, bufp);
1677 ofpbuf_delete(request_buf);
1681 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1684 dpif_linux_flow_init(reply);
1685 ofpbuf_delete(*bufp);
1693 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1694 struct dpif_flow_stats *stats)
1697 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1698 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1700 stats->n_packets = 0;
1703 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1704 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;