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"
38 #include "netdev-linux.h"
39 #include "netdev-vport.h"
40 #include "netlink-socket.h"
44 #include "openvswitch/tunnel.h"
46 #include "poll-loop.h"
47 #include "rtnetlink.h"
48 #include "rtnetlink-link.h"
51 #include "unaligned.h"
55 VLOG_DEFINE_THIS_MODULE(dpif_linux);
57 enum { LRU_MAX_PORTS = 1024 };
58 enum { LRU_MASK = LRU_MAX_PORTS - 1};
59 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
61 struct dpif_linux_dp {
62 /* Generic Netlink header. */
65 /* struct odp_header. */
69 const char *name; /* ODP_DP_ATTR_NAME. */
70 struct odp_stats stats; /* ODP_DP_ATTR_STATS. */
71 enum odp_frag_handling ipv4_frags; /* ODP_DP_ATTR_IPV4_FRAGS. */
72 const uint32_t *sampling; /* ODP_DP_ATTR_SAMPLING. */
73 uint32_t mcgroups[DPIF_N_UC_TYPES]; /* ODP_DP_ATTR_MCGROUPS. */
76 static void dpif_linux_dp_init(struct dpif_linux_dp *);
77 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
78 const struct ofpbuf *);
79 static void dpif_linux_dp_dump_start(struct nl_dump *);
80 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
81 struct dpif_linux_dp *reply,
82 struct ofpbuf **bufp);
83 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
84 struct ofpbuf **bufp);
86 struct dpif_linux_flow {
87 /* Generic Netlink header. */
90 /* struct odp_header. */
91 unsigned int nlmsg_flags;
96 * The 'stats' and 'used' members point to 64-bit data that might only be
97 * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
98 * access their values.
100 * If 'actions' is nonnull then ODP_FLOW_ATTR_ACTIONS will be included in
101 * the Netlink version of the command, even if actions_len is zero. */
102 const struct nlattr *key; /* ODP_FLOW_ATTR_KEY. */
104 const struct nlattr *actions; /* ODP_FLOW_ATTR_ACTIONS. */
106 const struct odp_flow_stats *stats; /* ODP_FLOW_ATTR_STATS. */
107 const uint8_t *tcp_flags; /* ODP_FLOW_ATTR_TCP_FLAGS. */
108 const uint64_t *used; /* ODP_FLOW_ATTR_USED. */
109 bool clear; /* ODP_FLOW_ATTR_CLEAR. */
112 static void dpif_linux_flow_init(struct dpif_linux_flow *);
113 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
114 const struct ofpbuf *);
115 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
117 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
118 struct dpif_linux_flow *reply,
119 struct ofpbuf **bufp);
120 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
121 struct dpif_flow_stats *);
123 /* Datapath interface for the openvswitch Linux kernel module. */
128 /* Multicast group messages. */
129 struct nl_sock *mc_sock;
130 uint32_t mcgroups[DPIF_N_UC_TYPES];
131 unsigned int listen_mask;
133 /* Change notification. */
134 struct sset changed_ports; /* Ports that have changed. */
135 struct rtnetlink_notifier port_notifier;
138 /* Queue of unused ports. */
139 unsigned long *lru_bitmap;
140 uint16_t lru_ports[LRU_MAX_PORTS];
145 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
147 /* Generic Netlink family numbers for ODP. */
148 static int odp_datapath_family;
149 static int odp_vport_family;
150 static int odp_flow_family;
151 static int odp_packet_family;
153 /* Generic Netlink socket. */
154 static struct nl_sock *genl_sock;
156 static int dpif_linux_init(void);
157 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
158 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
161 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
163 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
164 const struct ofpbuf *);
166 static struct dpif_linux *
167 dpif_linux_cast(const struct dpif *dpif)
169 dpif_assert_class(dpif, &dpif_linux_class);
170 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
174 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
176 if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
177 bitmap_set1(dp->lru_bitmap, port);
178 dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
183 dpif_linux_pop_port(struct dpif_linux *dp)
187 if (dp->lru_head == dp->lru_tail) {
191 port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
192 bitmap_set0(dp->lru_bitmap, port);
197 dpif_linux_enumerate(struct sset *all_dps)
203 error = dpif_linux_init();
208 dpif_linux_dp_dump_start(&dump);
209 while (nl_dump_next(&dump, &msg)) {
210 struct dpif_linux_dp dp;
212 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
213 sset_add(all_dps, dp.name);
216 return nl_dump_done(&dump);
220 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
221 bool create, struct dpif **dpifp)
223 struct dpif_linux_dp dp_request, dp;
227 error = dpif_linux_init();
232 /* Create or look up datapath. */
233 dpif_linux_dp_init(&dp_request);
234 dp_request.cmd = create ? ODP_DP_CMD_NEW : ODP_DP_CMD_GET;
235 dp_request.name = name;
236 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
240 error = open_dpif(&dp, dpifp);
247 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
249 struct dpif_linux *dpif;
253 dpif = xmalloc(sizeof *dpif);
254 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
255 dpif_linux_port_changed, dpif);
260 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
261 dp->dp_ifindex, dp->dp_ifindex);
263 dpif->mc_sock = NULL;
264 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
265 dpif->mcgroups[i] = dp->mcgroups[i];
267 dpif->listen_mask = 0;
268 dpif->dp_ifindex = dp->dp_ifindex;
269 sset_init(&dpif->changed_ports);
270 dpif->change_error = false;
271 *dpifp = &dpif->dpif;
273 dpif->lru_head = dpif->lru_tail = 0;
274 dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
275 bitmap_set1(dpif->lru_bitmap, ODPP_LOCAL);
276 for (i = 1; i < LRU_MAX_PORTS; i++) {
277 dpif_linux_push_port(dpif, i);
287 dpif_linux_close(struct dpif *dpif_)
289 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
290 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
291 sset_destroy(&dpif->changed_ports);
292 free(dpif->lru_bitmap);
297 dpif_linux_destroy(struct dpif *dpif_)
299 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
300 struct dpif_linux_dp dp;
302 dpif_linux_dp_init(&dp);
303 dp.cmd = ODP_DP_CMD_DEL;
304 dp.dp_ifindex = dpif->dp_ifindex;
305 return dpif_linux_dp_transact(&dp, NULL, NULL);
309 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
311 struct dpif_linux_dp dp;
315 error = dpif_linux_dp_get(dpif_, &dp, &buf);
324 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
326 struct dpif_linux_dp dp;
330 error = dpif_linux_dp_get(dpif_, &dp, &buf);
332 *drop_fragsp = dp.ipv4_frags == ODP_DP_FRAG_DROP;
339 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
341 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
342 struct dpif_linux_dp dp;
344 dpif_linux_dp_init(&dp);
345 dp.cmd = ODP_DP_CMD_SET;
346 dp.dp_ifindex = dpif->dp_ifindex;
347 dp.ipv4_frags = drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO;
348 return dpif_linux_dp_transact(&dp, NULL, NULL);
352 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
355 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
356 const char *name = netdev_get_name(netdev);
357 const char *type = netdev_get_type(netdev);
358 struct dpif_linux_vport request, reply;
359 const struct ofpbuf *options;
363 dpif_linux_vport_init(&request);
364 request.cmd = ODP_VPORT_CMD_NEW;
365 request.dp_ifindex = dpif->dp_ifindex;
366 request.type = netdev_vport_get_vport_type(netdev);
367 if (request.type == ODP_VPORT_TYPE_UNSPEC) {
368 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
369 "unsupported type `%s'",
370 dpif_name(dpif_), name, type);
375 options = netdev_vport_get_options(netdev);
376 if (options && options->size) {
377 request.options = options->data;
378 request.options_len = options->size;
381 /* Loop until we find a port that isn't used. */
383 request.port_no = dpif_linux_pop_port(dpif);
384 error = dpif_linux_vport_transact(&request, &reply, &buf);
387 *port_nop = reply.port_no;
390 } while (request.port_no != UINT32_MAX
391 && (error == EBUSY || error == EFBIG));
397 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
399 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
400 struct dpif_linux_vport vport;
403 dpif_linux_vport_init(&vport);
404 vport.cmd = ODP_VPORT_CMD_DEL;
405 vport.dp_ifindex = dpif->dp_ifindex;
406 vport.port_no = port_no;
407 error = dpif_linux_vport_transact(&vport, NULL, NULL);
410 dpif_linux_push_port(dpif, port_no);
416 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
417 const char *port_name, struct dpif_port *dpif_port)
419 struct dpif_linux_vport request;
420 struct dpif_linux_vport reply;
424 dpif_linux_vport_init(&request);
425 request.cmd = ODP_VPORT_CMD_GET;
426 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
427 request.port_no = port_no;
428 request.name = port_name;
430 error = dpif_linux_vport_transact(&request, &reply, &buf);
432 dpif_port->name = xstrdup(reply.name);
433 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
434 dpif_port->port_no = reply.port_no;
436 netdev_stats_from_rtnl_link_stats64(&dpif_port->stats,
439 memset(&dpif_port->stats, 0xff, sizeof dpif_port->stats);
447 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
448 struct dpif_port *dpif_port)
450 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
454 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
455 struct dpif_port *dpif_port)
457 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
461 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
463 /* If the datapath increases its range of supported ports, then it should
464 * start reporting that. */
469 dpif_linux_flow_flush(struct dpif *dpif_)
471 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
472 struct dpif_linux_flow flow;
474 dpif_linux_flow_init(&flow);
475 flow.cmd = ODP_FLOW_CMD_DEL;
476 flow.dp_ifindex = dpif->dp_ifindex;
477 return dpif_linux_flow_transact(&flow, NULL, NULL);
480 struct dpif_linux_port_state {
482 unsigned long *port_bitmap; /* Ports in the datapath. */
483 bool complete; /* Dump completed without error. */
487 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
489 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
490 struct dpif_linux_port_state *state;
491 struct dpif_linux_vport request;
494 *statep = state = xmalloc(sizeof *state);
495 state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
496 state->complete = false;
498 dpif_linux_vport_init(&request);
499 request.cmd = ODP_DP_CMD_GET;
500 request.dp_ifindex = dpif->dp_ifindex;
502 buf = ofpbuf_new(1024);
503 dpif_linux_vport_to_ofpbuf(&request, buf);
504 nl_dump_start(&state->dump, genl_sock, buf);
511 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
512 struct dpif_port *dpif_port)
514 struct dpif_linux_port_state *state = state_;
515 struct dpif_linux_vport vport;
519 if (!nl_dump_next(&state->dump, &buf)) {
520 state->complete = true;
524 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
529 if (vport.port_no < LRU_MAX_PORTS) {
530 bitmap_set1(state->port_bitmap, vport.port_no);
533 dpif_port->name = (char *) vport.name;
534 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
535 dpif_port->port_no = vport.port_no;
537 netdev_stats_from_rtnl_link_stats64(&dpif_port->stats, vport.stats);
539 memset(&dpif_port->stats, 0xff, sizeof dpif_port->stats);
545 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
547 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
548 struct dpif_linux_port_state *state = state_;
549 int error = nl_dump_done(&state->dump);
551 if (state->complete) {
554 for (i = 0; i < LRU_MAX_PORTS; i++) {
555 if (!bitmap_is_set(state->port_bitmap, i)) {
556 dpif_linux_push_port(dpif, i);
561 free(state->port_bitmap);
567 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
569 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
571 if (dpif->change_error) {
572 dpif->change_error = false;
573 sset_clear(&dpif->changed_ports);
575 } else if (!sset_is_empty(&dpif->changed_ports)) {
576 *devnamep = sset_pop(&dpif->changed_ports);
584 dpif_linux_port_poll_wait(const struct dpif *dpif_)
586 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
587 if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
588 poll_immediate_wake();
590 rtnetlink_link_notifier_wait();
595 dpif_linux_flow_get__(const struct dpif *dpif_,
596 const struct nlattr *key, size_t key_len,
597 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
599 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
600 struct dpif_linux_flow request;
602 dpif_linux_flow_init(&request);
603 request.cmd = ODP_FLOW_CMD_GET;
604 request.dp_ifindex = dpif->dp_ifindex;
606 request.key_len = key_len;
607 return dpif_linux_flow_transact(&request, reply, bufp);
611 dpif_linux_flow_get(const struct dpif *dpif_,
612 const struct nlattr *key, size_t key_len,
613 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
615 struct dpif_linux_flow reply;
619 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
622 dpif_linux_flow_get_stats(&reply, stats);
625 buf->data = (void *) reply.actions;
626 buf->size = reply.actions_len;
636 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
637 const struct nlattr *key, size_t key_len,
638 const struct nlattr *actions, size_t actions_len,
639 struct dpif_flow_stats *stats)
641 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
642 struct dpif_linux_flow request, reply;
643 struct nlattr dummy_action;
647 dpif_linux_flow_init(&request);
648 request.cmd = flags & DPIF_FP_CREATE ? ODP_FLOW_CMD_NEW : ODP_FLOW_CMD_SET;
649 request.dp_ifindex = dpif->dp_ifindex;
651 request.key_len = key_len;
652 /* Ensure that ODP_FLOW_ATTR_ACTIONS will always be included. */
653 request.actions = actions ? actions : &dummy_action;
654 request.actions_len = actions_len;
655 if (flags & DPIF_FP_ZERO_STATS) {
656 request.clear = true;
658 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
659 error = dpif_linux_flow_transact(&request,
660 stats ? &reply : NULL,
661 stats ? &buf : NULL);
662 if (!error && stats) {
663 dpif_linux_flow_get_stats(&reply, stats);
670 dpif_linux_flow_del(struct dpif *dpif_,
671 const struct nlattr *key, size_t key_len,
672 struct dpif_flow_stats *stats)
674 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
675 struct dpif_linux_flow request, reply;
679 dpif_linux_flow_init(&request);
680 request.cmd = ODP_FLOW_CMD_DEL;
681 request.dp_ifindex = dpif->dp_ifindex;
683 request.key_len = key_len;
684 error = dpif_linux_flow_transact(&request,
685 stats ? &reply : NULL,
686 stats ? &buf : NULL);
687 if (!error && stats) {
688 dpif_linux_flow_get_stats(&reply, stats);
694 struct dpif_linux_flow_state {
696 struct dpif_linux_flow flow;
697 struct dpif_flow_stats stats;
702 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
704 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
705 struct dpif_linux_flow_state *state;
706 struct dpif_linux_flow request;
709 *statep = state = xmalloc(sizeof *state);
711 dpif_linux_flow_init(&request);
712 request.cmd = ODP_DP_CMD_GET;
713 request.dp_ifindex = dpif->dp_ifindex;
715 buf = ofpbuf_new(1024);
716 dpif_linux_flow_to_ofpbuf(&request, buf);
717 nl_dump_start(&state->dump, genl_sock, buf);
726 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
727 const struct nlattr **key, size_t *key_len,
728 const struct nlattr **actions, size_t *actions_len,
729 const struct dpif_flow_stats **stats)
731 struct dpif_linux_flow_state *state = state_;
736 ofpbuf_delete(state->buf);
739 if (!nl_dump_next(&state->dump, &buf)) {
743 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
748 if (actions && !state->flow.actions) {
749 error = dpif_linux_flow_get__(dpif_, state->flow.key,
751 &state->flow, &state->buf);
752 if (error == ENOENT) {
753 VLOG_DBG("dumped flow disappeared on get");
755 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
761 *actions = state->flow.actions;
762 *actions_len = state->flow.actions_len;
765 *key = state->flow.key;
766 *key_len = state->flow.key_len;
769 dpif_linux_flow_get_stats(&state->flow, &state->stats);
770 *stats = &state->stats;
776 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
778 struct dpif_linux_flow_state *state = state_;
779 int error = nl_dump_done(&state->dump);
780 ofpbuf_delete(state->buf);
786 dpif_linux_execute(struct dpif *dpif_,
787 const struct nlattr *actions, size_t actions_len,
788 const struct ofpbuf *packet)
790 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
791 struct odp_header *execute;
795 buf = ofpbuf_new(128 + actions_len + packet->size);
797 nl_msg_put_genlmsghdr(buf, 0, odp_packet_family, NLM_F_REQUEST,
798 ODP_PACKET_CMD_EXECUTE, 1);
800 execute = ofpbuf_put_uninit(buf, sizeof *execute);
801 execute->dp_ifindex = dpif->dp_ifindex;
803 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, packet->data, packet->size);
804 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_ACTIONS, actions, actions_len);
806 error = nl_sock_transact(genl_sock, buf, NULL);
812 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
814 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
815 *listen_mask = dpif->listen_mask;
820 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
822 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
826 if (listen_mask == dpif->listen_mask) {
828 } else if (!listen_mask) {
829 nl_sock_destroy(dpif->mc_sock);
830 dpif->mc_sock = NULL;
831 dpif->listen_mask = 0;
833 } else if (!dpif->mc_sock) {
834 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
840 /* Unsubscribe from old groups. */
841 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
842 if (dpif->listen_mask & (1u << i)) {
843 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
847 /* Update listen_mask. */
848 dpif->listen_mask = listen_mask;
850 /* Subscribe to new groups. */
852 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
853 if (dpif->listen_mask & (1u << i)) {
856 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
866 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
867 uint32_t *probability)
869 struct dpif_linux_dp dp;
873 error = dpif_linux_dp_get(dpif_, &dp, &buf);
875 *probability = dp.sampling ? *dp.sampling : 0;
882 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
884 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
885 struct dpif_linux_dp dp;
887 dpif_linux_dp_init(&dp);
888 dp.cmd = ODP_DP_CMD_SET;
889 dp.dp_ifindex = dpif->dp_ifindex;
890 dp.sampling = &probability;
891 return dpif_linux_dp_transact(&dp, NULL, NULL);
895 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
896 uint32_t queue_id, uint32_t *priority)
898 if (queue_id < 0xf000) {
899 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
907 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
910 static const struct nl_policy odp_packet_policy[] = {
911 /* Always present. */
912 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
913 .min_len = ETH_HEADER_LEN },
914 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
916 /* ODP_PACKET_CMD_ACTION only. */
917 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
919 /* ODP_PACKET_CMD_SAMPLE only. */
920 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
921 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
924 struct odp_header *odp_header;
925 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
926 struct nlmsghdr *nlmsg;
927 struct genlmsghdr *genl;
931 ofpbuf_use_const(&b, buf->data, buf->size);
933 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
934 genl = ofpbuf_try_pull(&b, sizeof *genl);
935 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
936 if (!nlmsg || !genl || !odp_header
937 || nlmsg->nlmsg_type != odp_packet_family
938 || !nl_policy_parse(&b, 0, odp_packet_policy, a,
939 ARRAY_SIZE(odp_packet_policy))) {
943 type = (genl->cmd == ODP_PACKET_CMD_MISS ? DPIF_UC_MISS
944 : genl->cmd == ODP_PACKET_CMD_ACTION ? DPIF_UC_ACTION
945 : genl->cmd == ODP_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
951 memset(upcall, 0, sizeof *upcall);
953 upcall->packet = buf;
954 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
955 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
956 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
957 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
958 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
959 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
961 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
962 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
964 if (a[ODP_PACKET_ATTR_ACTIONS]) {
965 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
966 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
969 *dp_ifindex = odp_header->dp_ifindex;
975 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
977 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
982 if (!dpif->mc_sock) {
986 for (i = 0; i < 50; i++) {
989 error = nl_sock_recv(dpif->mc_sock, &buf, false);
994 error = parse_odp_packet(buf, upcall, &dp_ifindex);
996 && dp_ifindex == dpif->dp_ifindex
997 && dpif->listen_mask & (1u << upcall->type)) {
1011 dpif_linux_recv_wait(struct dpif *dpif_)
1013 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1014 if (dpif->mc_sock) {
1015 nl_sock_wait(dpif->mc_sock, POLLIN);
1020 dpif_linux_recv_purge(struct dpif *dpif_)
1022 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1024 if (dpif->mc_sock) {
1025 nl_sock_drain(dpif->mc_sock);
1029 const struct dpif_class dpif_linux_class = {
1031 dpif_linux_enumerate,
1037 dpif_linux_get_stats,
1038 dpif_linux_get_drop_frags,
1039 dpif_linux_set_drop_frags,
1040 dpif_linux_port_add,
1041 dpif_linux_port_del,
1042 dpif_linux_port_query_by_number,
1043 dpif_linux_port_query_by_name,
1044 dpif_linux_get_max_ports,
1045 dpif_linux_port_dump_start,
1046 dpif_linux_port_dump_next,
1047 dpif_linux_port_dump_done,
1048 dpif_linux_port_poll,
1049 dpif_linux_port_poll_wait,
1050 dpif_linux_flow_get,
1051 dpif_linux_flow_put,
1052 dpif_linux_flow_del,
1053 dpif_linux_flow_flush,
1054 dpif_linux_flow_dump_start,
1055 dpif_linux_flow_dump_next,
1056 dpif_linux_flow_dump_done,
1058 dpif_linux_recv_get_mask,
1059 dpif_linux_recv_set_mask,
1060 dpif_linux_get_sflow_probability,
1061 dpif_linux_set_sflow_probability,
1062 dpif_linux_queue_to_priority,
1064 dpif_linux_recv_wait,
1065 dpif_linux_recv_purge,
1069 dpif_linux_init(void)
1071 static int error = -1;
1074 error = nl_lookup_genl_family(ODP_DATAPATH_FAMILY,
1075 &odp_datapath_family);
1077 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1078 "The Open vSwitch kernel module is probably not loaded.",
1079 ODP_DATAPATH_FAMILY);
1082 error = nl_lookup_genl_family(ODP_VPORT_FAMILY, &odp_vport_family);
1085 error = nl_lookup_genl_family(ODP_FLOW_FAMILY, &odp_flow_family);
1088 error = nl_lookup_genl_family(ODP_PACKET_FAMILY,
1089 &odp_packet_family);
1092 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1100 dpif_linux_is_internal_device(const char *name)
1102 struct dpif_linux_vport reply;
1106 error = dpif_linux_vport_get(name, &reply, &buf);
1109 } else if (error != ENODEV && error != ENOENT) {
1110 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1111 name, strerror(error));
1114 return reply.type == ODP_VPORT_TYPE_INTERNAL;
1118 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1119 const void *data, size_t size)
1121 struct odp_header *execute;
1126 buf = ofpbuf_new(128 + size);
1128 nl_msg_put_genlmsghdr(buf, 0, odp_packet_family, NLM_F_REQUEST,
1129 ODP_PACKET_CMD_EXECUTE, 1);
1131 execute = ofpbuf_put_uninit(buf, sizeof *execute);
1132 execute->dp_ifindex = dp_ifindex;
1134 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, data, size);
1136 actions_ofs = nl_msg_start_nested(buf, ODP_PACKET_ATTR_ACTIONS);
1137 nl_msg_put_u32(buf, ODP_ACTION_ATTR_OUTPUT, port_no);
1138 nl_msg_end_nested(buf, actions_ofs);
1140 error = nl_sock_transact(genl_sock, buf, NULL);
1146 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1149 struct dpif_linux *dpif = dpif_;
1152 if (change->master_ifindex == dpif->dp_ifindex
1153 && (change->nlmsg_type == RTM_NEWLINK
1154 || change->nlmsg_type == RTM_DELLINK))
1156 /* Our datapath changed, either adding a new port or deleting an
1158 sset_add(&dpif->changed_ports, change->ifname);
1161 dpif->change_error = true;
1165 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1166 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1167 * positive errno value.
1169 * 'vport' will contain pointers into 'buf', so the caller should not free
1170 * 'buf' while 'vport' is still in use. */
1172 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1173 const struct ofpbuf *buf)
1175 static const struct nl_policy odp_vport_policy[] = {
1176 [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1177 [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1178 [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1179 [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1180 .min_len = sizeof(struct rtnl_link_stats64),
1181 .max_len = sizeof(struct rtnl_link_stats64),
1183 [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1184 .min_len = ETH_ADDR_LEN,
1185 .max_len = ETH_ADDR_LEN,
1187 [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1188 [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1189 [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1190 [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1193 struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
1194 struct odp_header *odp_header;
1195 struct nlmsghdr *nlmsg;
1196 struct genlmsghdr *genl;
1199 dpif_linux_vport_init(vport);
1201 ofpbuf_use_const(&b, buf->data, buf->size);
1202 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1203 genl = ofpbuf_try_pull(&b, sizeof *genl);
1204 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1205 if (!nlmsg || !genl || !odp_header
1206 || nlmsg->nlmsg_type != odp_vport_family
1207 || !nl_policy_parse(&b, 0, odp_vport_policy, a,
1208 ARRAY_SIZE(odp_vport_policy))) {
1212 vport->cmd = genl->cmd;
1213 vport->dp_ifindex = odp_header->dp_ifindex;
1214 vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1215 vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1216 vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
1217 if (a[ODP_VPORT_ATTR_STATS]) {
1218 vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
1220 if (a[ODP_VPORT_ATTR_ADDRESS]) {
1221 vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
1223 if (a[ODP_VPORT_ATTR_MTU]) {
1224 vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
1226 vport->mtu = INT_MAX;
1228 if (a[ODP_VPORT_ATTR_OPTIONS]) {
1229 vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
1230 vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
1232 if (a[ODP_VPORT_ATTR_IFINDEX]) {
1233 vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
1235 if (a[ODP_VPORT_ATTR_IFLINK]) {
1236 vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
1241 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1242 * followed by Netlink attributes corresponding to 'vport'. */
1244 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1247 struct odp_header *odp_header;
1249 nl_msg_put_genlmsghdr(buf, 0, odp_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1252 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1253 odp_header->dp_ifindex = vport->dp_ifindex;
1255 if (vport->port_no != UINT32_MAX) {
1256 nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1259 if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
1260 nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1264 nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1268 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1269 vport->stats, sizeof *vport->stats);
1272 if (vport->address) {
1273 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1274 vport->address, ETH_ADDR_LEN);
1277 if (vport->mtu && vport->mtu != INT_MAX) {
1278 nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1281 if (vport->options) {
1282 nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1283 vport->options, vport->options_len);
1286 if (vport->ifindex) {
1287 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1290 if (vport->iflink) {
1291 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1295 /* Clears 'vport' to "empty" values. */
1297 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1299 memset(vport, 0, sizeof *vport);
1300 vport->port_no = UINT32_MAX;
1303 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1304 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1305 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1306 * result of the command is expected to be an odp_vport also, which is decoded
1307 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1308 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1310 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1311 struct dpif_linux_vport *reply,
1312 struct ofpbuf **bufp)
1314 struct ofpbuf *request_buf;
1317 assert((reply != NULL) == (bufp != NULL));
1319 error = dpif_linux_init();
1323 dpif_linux_vport_init(reply);
1328 request_buf = ofpbuf_new(1024);
1329 dpif_linux_vport_to_ofpbuf(request, request_buf);
1330 error = nl_sock_transact(genl_sock, request_buf, bufp);
1331 ofpbuf_delete(request_buf);
1335 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1338 dpif_linux_vport_init(reply);
1339 ofpbuf_delete(*bufp);
1346 /* Obtains information about the kernel vport named 'name' and stores it into
1347 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1348 * longer needed ('reply' will contain pointers into '*bufp'). */
1350 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1351 struct ofpbuf **bufp)
1353 struct dpif_linux_vport request;
1355 dpif_linux_vport_init(&request);
1356 request.cmd = ODP_VPORT_CMD_GET;
1357 request.name = name;
1359 return dpif_linux_vport_transact(&request, reply, bufp);
1362 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1363 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1364 * positive errno value.
1366 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1367 * while 'dp' is still in use. */
1369 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1371 static const struct nl_policy odp_datapath_policy[] = {
1372 [ODP_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1373 [ODP_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1374 .min_len = sizeof(struct odp_stats),
1375 .max_len = sizeof(struct odp_stats),
1377 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1378 [ODP_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1379 [ODP_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1382 struct nlattr *a[ARRAY_SIZE(odp_datapath_policy)];
1383 struct odp_header *odp_header;
1384 struct nlmsghdr *nlmsg;
1385 struct genlmsghdr *genl;
1388 dpif_linux_dp_init(dp);
1390 ofpbuf_use_const(&b, buf->data, buf->size);
1391 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1392 genl = ofpbuf_try_pull(&b, sizeof *genl);
1393 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1394 if (!nlmsg || !genl || !odp_header
1395 || nlmsg->nlmsg_type != odp_datapath_family
1396 || !nl_policy_parse(&b, 0, odp_datapath_policy, a,
1397 ARRAY_SIZE(odp_datapath_policy))) {
1401 dp->cmd = genl->cmd;
1402 dp->dp_ifindex = odp_header->dp_ifindex;
1403 dp->name = nl_attr_get_string(a[ODP_DP_ATTR_NAME]);
1404 if (a[ODP_DP_ATTR_STATS]) {
1405 /* Can't use structure assignment because Netlink doesn't ensure
1406 * sufficient alignment for 64-bit members. */
1407 memcpy(&dp->stats, nl_attr_get(a[ODP_DP_ATTR_STATS]),
1410 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1411 dp->ipv4_frags = nl_attr_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1413 if (a[ODP_DP_ATTR_SAMPLING]) {
1414 dp->sampling = nl_attr_get(a[ODP_DP_ATTR_SAMPLING]);
1417 if (a[ODP_DP_ATTR_MCGROUPS]) {
1418 static const struct nl_policy odp_mcgroup_policy[] = {
1419 [ODP_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1420 [ODP_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1421 [ODP_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1424 struct nlattr *mcgroups[ARRAY_SIZE(odp_mcgroup_policy)];
1426 if (!nl_parse_nested(a[ODP_DP_ATTR_MCGROUPS], odp_mcgroup_policy,
1427 mcgroups, ARRAY_SIZE(odp_mcgroup_policy))) {
1431 if (mcgroups[ODP_PACKET_CMD_MISS]) {
1432 dp->mcgroups[DPIF_UC_MISS]
1433 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_MISS]);
1435 if (mcgroups[ODP_PACKET_CMD_ACTION]) {
1436 dp->mcgroups[DPIF_UC_ACTION]
1437 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_ACTION]);
1439 if (mcgroups[ODP_PACKET_CMD_SAMPLE]) {
1440 dp->mcgroups[DPIF_UC_SAMPLE]
1441 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_SAMPLE]);
1448 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1450 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1452 struct odp_header *odp_header;
1454 nl_msg_put_genlmsghdr(buf, 0, odp_datapath_family,
1455 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1457 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1458 odp_header->dp_ifindex = dp->dp_ifindex;
1461 nl_msg_put_string(buf, ODP_DP_ATTR_NAME, dp->name);
1464 /* Skip ODP_DP_ATTR_STATS since we never have a reason to serialize it. */
1466 if (dp->ipv4_frags) {
1467 nl_msg_put_u32(buf, ODP_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1471 nl_msg_put_u32(buf, ODP_DP_ATTR_SAMPLING, *dp->sampling);
1475 /* Clears 'dp' to "empty" values. */
1477 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1479 memset(dp, 0, sizeof *dp);
1483 dpif_linux_dp_dump_start(struct nl_dump *dump)
1485 struct dpif_linux_dp request;
1488 dpif_linux_dp_init(&request);
1489 request.cmd = ODP_DP_CMD_GET;
1491 buf = ofpbuf_new(1024);
1492 dpif_linux_dp_to_ofpbuf(&request, buf);
1493 nl_dump_start(dump, genl_sock, buf);
1497 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1498 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1499 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1500 * result of the command is expected to be of the same form, which is decoded
1501 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1502 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1504 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1505 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1507 struct ofpbuf *request_buf;
1510 assert((reply != NULL) == (bufp != NULL));
1512 request_buf = ofpbuf_new(1024);
1513 dpif_linux_dp_to_ofpbuf(request, request_buf);
1514 error = nl_sock_transact(genl_sock, request_buf, bufp);
1515 ofpbuf_delete(request_buf);
1519 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1522 dpif_linux_dp_init(reply);
1523 ofpbuf_delete(*bufp);
1530 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1531 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1532 * will contain pointers into '*bufp'). */
1534 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1535 struct ofpbuf **bufp)
1537 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1538 struct dpif_linux_dp request;
1540 dpif_linux_dp_init(&request);
1541 request.cmd = ODP_DP_CMD_GET;
1542 request.dp_ifindex = dpif->dp_ifindex;
1544 return dpif_linux_dp_transact(&request, reply, bufp);
1547 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1548 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1549 * positive errno value.
1551 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1552 * while 'flow' is still in use. */
1554 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1555 const struct ofpbuf *buf)
1557 static const struct nl_policy odp_flow_policy[] = {
1558 [ODP_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1559 [ODP_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1560 [ODP_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1561 .min_len = sizeof(struct odp_flow_stats),
1562 .max_len = sizeof(struct odp_flow_stats),
1564 [ODP_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1565 [ODP_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1566 /* The kernel never uses ODP_FLOW_ATTR_CLEAR. */
1569 struct nlattr *a[ARRAY_SIZE(odp_flow_policy)];
1570 struct odp_header *odp_header;
1571 struct nlmsghdr *nlmsg;
1572 struct genlmsghdr *genl;
1575 dpif_linux_flow_init(flow);
1577 ofpbuf_use_const(&b, buf->data, buf->size);
1578 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1579 genl = ofpbuf_try_pull(&b, sizeof *genl);
1580 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1581 if (!nlmsg || !genl || !odp_header
1582 || nlmsg->nlmsg_type != odp_flow_family
1583 || !nl_policy_parse(&b, 0, odp_flow_policy, a,
1584 ARRAY_SIZE(odp_flow_policy))) {
1588 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1589 flow->dp_ifindex = odp_header->dp_ifindex;
1590 flow->key = nl_attr_get(a[ODP_FLOW_ATTR_KEY]);
1591 flow->key_len = nl_attr_get_size(a[ODP_FLOW_ATTR_KEY]);
1592 if (a[ODP_FLOW_ATTR_ACTIONS]) {
1593 flow->actions = nl_attr_get(a[ODP_FLOW_ATTR_ACTIONS]);
1594 flow->actions_len = nl_attr_get_size(a[ODP_FLOW_ATTR_ACTIONS]);
1596 if (a[ODP_FLOW_ATTR_STATS]) {
1597 flow->stats = nl_attr_get(a[ODP_FLOW_ATTR_STATS]);
1599 if (a[ODP_FLOW_ATTR_TCP_FLAGS]) {
1600 flow->tcp_flags = nl_attr_get(a[ODP_FLOW_ATTR_TCP_FLAGS]);
1602 if (a[ODP_FLOW_ATTR_USED]) {
1603 flow->used = nl_attr_get(a[ODP_FLOW_ATTR_USED]);
1608 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1609 * followed by Netlink attributes corresponding to 'flow'. */
1611 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1614 struct odp_header *odp_header;
1616 nl_msg_put_genlmsghdr(buf, 0, odp_flow_family,
1617 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1620 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1621 odp_header->dp_ifindex = flow->dp_ifindex;
1623 if (flow->key_len) {
1624 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_KEY, flow->key, flow->key_len);
1627 if (flow->actions || flow->actions_len) {
1628 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_ACTIONS,
1629 flow->actions, flow->actions_len);
1632 /* We never need to send these to the kernel. */
1633 assert(!flow->stats);
1634 assert(!flow->tcp_flags);
1635 assert(!flow->used);
1638 nl_msg_put_flag(buf, ODP_FLOW_ATTR_CLEAR);
1642 /* Clears 'flow' to "empty" values. */
1644 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1646 memset(flow, 0, sizeof *flow);
1649 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1650 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1651 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1652 * result of the command is expected to be a flow also, which is decoded and
1653 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1654 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1656 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1657 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1659 struct ofpbuf *request_buf;
1662 assert((reply != NULL) == (bufp != NULL));
1664 request_buf = ofpbuf_new(1024);
1665 dpif_linux_flow_to_ofpbuf(request, request_buf);
1666 error = nl_sock_transact(genl_sock, request_buf, bufp);
1667 ofpbuf_delete(request_buf);
1671 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1674 dpif_linux_flow_init(reply);
1675 ofpbuf_delete(*bufp);
1683 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1684 struct dpif_flow_stats *stats)
1687 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1688 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1690 stats->n_packets = 0;
1693 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1694 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;