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>
35 #include "dpif-provider.h"
37 #include "netdev-vport.h"
38 #include "netlink-socket.h"
42 #include "openvswitch/tunnel.h"
44 #include "poll-loop.h"
45 #include "rtnetlink.h"
46 #include "rtnetlink-link.h"
49 #include "unaligned.h"
53 VLOG_DEFINE_THIS_MODULE(dpif_linux);
55 struct dpif_linux_dp {
56 /* Generic Netlink header. */
59 /* struct odp_header. */
63 const char *name; /* ODP_DP_ATTR_NAME. */
64 struct odp_stats stats; /* ODP_DP_ATTR_STATS. */
65 enum odp_frag_handling ipv4_frags; /* ODP_DP_ATTR_IPV4_FRAGS. */
66 const uint32_t *sampling; /* ODP_DP_ATTR_SAMPLING. */
67 uint32_t mcgroups[DPIF_N_UC_TYPES]; /* ODP_DP_ATTR_MCGROUPS. */
70 static void dpif_linux_dp_init(struct dpif_linux_dp *);
71 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
72 const struct ofpbuf *);
73 static void dpif_linux_dp_dump_start(struct nl_dump *);
74 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
75 struct dpif_linux_dp *reply,
76 struct ofpbuf **bufp);
77 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
78 struct ofpbuf **bufp);
80 struct dpif_linux_flow {
81 /* Generic Netlink header. */
84 /* struct odp_header. */
85 unsigned int nlmsg_flags;
90 * The 'stats' and 'used' members point to 64-bit data that might only be
91 * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
92 * access their values. */
93 const struct nlattr *key; /* ODP_FLOW_ATTR_KEY. */
95 const struct nlattr *actions; /* ODP_FLOW_ATTR_ACTIONS. */
97 const struct odp_flow_stats *stats; /* ODP_FLOW_ATTR_STATS. */
98 const uint8_t *tcp_flags; /* ODP_FLOW_ATTR_TCP_FLAGS. */
99 const uint64_t *used; /* ODP_FLOW_ATTR_USED. */
100 bool clear; /* ODP_FLOW_ATTR_CLEAR. */
103 static void dpif_linux_flow_init(struct dpif_linux_flow *);
104 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
105 const struct ofpbuf *);
106 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
108 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
109 struct dpif_linux_flow *reply,
110 struct ofpbuf **bufp);
111 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
112 struct dpif_flow_stats *);
114 /* Datapath interface for the openvswitch Linux kernel module. */
118 /* Multicast group messages. */
119 struct nl_sock *mc_sock;
120 uint32_t mcgroups[DPIF_N_UC_TYPES];
121 unsigned int listen_mask;
123 /* Used by dpif_linux_get_all_names(). */
127 /* Change notification. */
128 int local_ifindex; /* Ifindex of local port. */
129 struct shash changed_ports; /* Ports that have changed. */
130 struct rtnetlink_notifier port_notifier;
134 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
136 /* Generic Netlink family numbers for ODP. */
137 static int odp_datapath_family;
138 static int odp_vport_family;
139 static int odp_flow_family;
140 static int odp_packet_family;
142 /* Generic Netlink socket. */
143 static struct nl_sock *genl_sock;
145 static int dpif_linux_init(void);
146 static int open_dpif(const struct dpif_linux_dp *,
147 const struct dpif_linux_vport *local_vport,
149 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
152 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
154 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
155 const struct ofpbuf *);
157 static struct dpif_linux *
158 dpif_linux_cast(const struct dpif *dpif)
160 dpif_assert_class(dpif, &dpif_linux_class);
161 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
165 dpif_linux_enumerate(struct svec *all_dps)
171 error = dpif_linux_init();
176 dpif_linux_dp_dump_start(&dump);
177 while (nl_dump_next(&dump, &msg)) {
178 struct dpif_linux_dp dp;
180 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
181 svec_add(all_dps, dp.name);
184 return nl_dump_done(&dump);
188 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
189 bool create, struct dpif **dpifp)
191 struct dpif_linux_vport vport_request, vport;
192 struct dpif_linux_dp dp_request, dp;
197 error = dpif_linux_init();
202 minor = !strncmp(name, "dp", 2)
203 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
205 /* Create or look up datapath. */
206 dpif_linux_dp_init(&dp_request);
207 dp_request.cmd = create ? ODP_DP_CMD_NEW : ODP_DP_CMD_GET;
208 dp_request.dp_idx = minor;
209 dp_request.name = minor < 0 ? name : NULL;
210 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
214 ofpbuf_delete(buf); /* Pointers inside 'dp' are now invalid! */
216 /* Look up local port. */
217 dpif_linux_vport_init(&vport_request);
218 vport_request.cmd = ODP_VPORT_CMD_GET;
219 vport_request.dp_idx = dp.dp_idx;
220 vport_request.port_no = ODPP_LOCAL;
221 vport_request.name = minor < 0 ? name : NULL;
222 error = dpif_linux_vport_transact(&vport_request, &vport, &buf);
225 } else if (vport.port_no != ODPP_LOCAL) {
226 /* This is an Open vSwitch device but not the local port. We
227 * intentionally support only using the name of the local port as the
228 * name of a datapath; otherwise, it would be too difficult to
229 * enumerate all the names of a datapath. */
232 error = open_dpif(&dp, &vport, dpifp);
239 open_dpif(const struct dpif_linux_dp *dp,
240 const struct dpif_linux_vport *local_vport, struct dpif **dpifp)
242 int dp_idx = local_vport->dp_idx;
243 struct dpif_linux *dpif;
248 dpif = xmalloc(sizeof *dpif);
249 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
250 dpif_linux_port_changed, dpif);
255 name = xasprintf("dp%d", dp_idx);
256 dpif_init(&dpif->dpif, &dpif_linux_class, name, dp_idx, dp_idx);
259 dpif->mc_sock = NULL;
260 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
261 dpif->mcgroups[i] = dp->mcgroups[i];
263 dpif->listen_mask = 0;
264 dpif->local_ifname = xstrdup(local_vport->name);
265 dpif->local_ifindex = local_vport->ifindex;
266 dpif->minor = dp_idx;
267 shash_init(&dpif->changed_ports);
268 dpif->change_error = false;
269 *dpifp = &dpif->dpif;
279 dpif_linux_close(struct dpif *dpif_)
281 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
282 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
283 shash_destroy(&dpif->changed_ports);
284 free(dpif->local_ifname);
289 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
291 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
293 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
294 svec_add(all_names, dpif->local_ifname);
299 dpif_linux_destroy(struct dpif *dpif_)
301 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
302 struct dpif_linux_dp dp;
304 dpif_linux_dp_init(&dp);
305 dp.cmd = ODP_DP_CMD_DEL;
306 dp.dp_idx = dpif->minor;
307 return dpif_linux_dp_transact(&dp, NULL, NULL);
311 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
313 struct dpif_linux_dp dp;
317 error = dpif_linux_dp_get(dpif_, &dp, &buf);
326 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
328 struct dpif_linux_dp dp;
332 error = dpif_linux_dp_get(dpif_, &dp, &buf);
334 *drop_fragsp = dp.ipv4_frags == ODP_DP_FRAG_DROP;
341 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
343 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
344 struct dpif_linux_dp dp;
346 dpif_linux_dp_init(&dp);
347 dp.cmd = ODP_DP_CMD_SET;
348 dp.dp_idx = dpif->minor;
349 dp.ipv4_frags = drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO;
350 return dpif_linux_dp_transact(&dp, NULL, NULL);
354 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
357 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
358 const char *name = netdev_get_name(netdev);
359 const char *type = netdev_get_type(netdev);
360 struct dpif_linux_vport request, reply;
361 const struct ofpbuf *options;
365 dpif_linux_vport_init(&request);
366 request.cmd = ODP_VPORT_CMD_NEW;
367 request.dp_idx = dpif->minor;
368 request.type = netdev_vport_get_vport_type(netdev);
369 if (request.type == ODP_VPORT_TYPE_UNSPEC) {
370 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
371 "unsupported type `%s'",
372 dpif_name(dpif_), name, type);
377 options = netdev_vport_get_options(netdev);
378 if (options && options->size) {
379 request.options = options->data;
380 request.options_len = options->size;
383 error = dpif_linux_vport_transact(&request, &reply, &buf);
385 *port_nop = reply.port_no;
393 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
395 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
396 struct dpif_linux_vport vport;
398 dpif_linux_vport_init(&vport);
399 vport.cmd = ODP_VPORT_CMD_DEL;
400 vport.dp_idx = dpif->minor;
401 vport.port_no = port_no;
402 return dpif_linux_vport_transact(&vport, NULL, NULL);
406 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
407 const char *port_name, struct dpif_port *dpif_port)
409 struct dpif_linux_vport request;
410 struct dpif_linux_vport reply;
414 dpif_linux_vport_init(&request);
415 request.cmd = ODP_VPORT_CMD_GET;
416 request.dp_idx = dpif_linux_cast(dpif)->minor;
417 request.port_no = port_no;
418 request.name = port_name;
420 error = dpif_linux_vport_transact(&request, &reply, &buf);
422 dpif_port->name = xstrdup(reply.name);
423 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
424 dpif_port->port_no = reply.port_no;
431 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
432 struct dpif_port *dpif_port)
434 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
438 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
439 struct dpif_port *dpif_port)
441 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
445 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
447 /* If the datapath increases its range of supported ports, then it should
448 * start reporting that. */
453 dpif_linux_flow_flush(struct dpif *dpif_)
455 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
456 struct dpif_linux_flow flow;
458 dpif_linux_flow_init(&flow);
459 flow.cmd = ODP_FLOW_CMD_DEL;
460 flow.dp_idx = dpif->minor;
461 return dpif_linux_flow_transact(&flow, NULL, NULL);
464 struct dpif_linux_port_state {
469 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
471 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
472 struct dpif_linux_port_state *state;
473 struct dpif_linux_vport request;
476 *statep = state = xmalloc(sizeof *state);
478 dpif_linux_vport_init(&request);
479 request.cmd = ODP_DP_CMD_GET;
480 request.dp_idx = dpif->minor;
482 buf = ofpbuf_new(1024);
483 dpif_linux_vport_to_ofpbuf(&request, buf);
484 nl_dump_start(&state->dump, genl_sock, buf);
491 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
492 struct dpif_port *dpif_port)
494 struct dpif_linux_port_state *state = state_;
495 struct dpif_linux_vport vport;
499 if (!nl_dump_next(&state->dump, &buf)) {
503 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
508 dpif_port->name = (char *) vport.name;
509 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
510 dpif_port->port_no = vport.port_no;
515 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
517 struct dpif_linux_port_state *state = state_;
518 int error = nl_dump_done(&state->dump);
524 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
526 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
528 if (dpif->change_error) {
529 dpif->change_error = false;
530 shash_clear(&dpif->changed_ports);
532 } else if (!shash_is_empty(&dpif->changed_ports)) {
533 struct shash_node *node = shash_first(&dpif->changed_ports);
534 *devnamep = shash_steal(&dpif->changed_ports, node);
542 dpif_linux_port_poll_wait(const struct dpif *dpif_)
544 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
545 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
546 poll_immediate_wake();
548 rtnetlink_link_notifier_wait();
553 dpif_linux_flow_get(const struct dpif *dpif_,
554 const struct nlattr *key, size_t key_len,
555 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
557 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
558 struct dpif_linux_flow request, reply;
562 dpif_linux_flow_init(&request);
563 request.cmd = ODP_FLOW_CMD_GET;
564 request.dp_idx = dpif->minor;
566 request.key_len = key_len;
567 error = dpif_linux_flow_transact(&request, &reply, &buf);
570 dpif_linux_flow_get_stats(&reply, stats);
573 buf->data = (void *) reply.actions;
574 buf->size = reply.actions_len;
584 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
585 const struct nlattr *key, size_t key_len,
586 const struct nlattr *actions, size_t actions_len,
587 struct dpif_flow_stats *stats)
589 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
590 struct dpif_linux_flow request, reply;
594 dpif_linux_flow_init(&request);
595 request.cmd = flags & DPIF_FP_CREATE ? ODP_FLOW_CMD_NEW : ODP_FLOW_CMD_SET;
596 request.dp_idx = dpif->minor;
598 request.key_len = key_len;
599 request.actions = actions;
600 request.actions_len = actions_len;
601 if (flags & DPIF_FP_ZERO_STATS) {
602 request.clear = true;
604 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
605 error = dpif_linux_flow_transact(&request,
606 stats ? &reply : NULL,
607 stats ? &buf : NULL);
608 if (!error && stats) {
609 dpif_linux_flow_get_stats(&reply, stats);
616 dpif_linux_flow_del(struct dpif *dpif_,
617 const struct nlattr *key, size_t key_len,
618 struct dpif_flow_stats *stats)
620 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
621 struct dpif_linux_flow request, reply;
625 dpif_linux_flow_init(&request);
626 request.cmd = ODP_FLOW_CMD_DEL;
627 request.dp_idx = dpif->minor;
629 request.key_len = key_len;
630 error = dpif_linux_flow_transact(&request,
631 stats ? &reply : NULL,
632 stats ? &buf : NULL);
633 if (!error && stats) {
634 dpif_linux_flow_get_stats(&reply, stats);
640 struct dpif_linux_flow_state {
642 struct dpif_linux_flow flow;
643 struct dpif_flow_stats stats;
647 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
649 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
650 struct dpif_linux_flow_state *state;
651 struct dpif_linux_flow request;
654 *statep = state = xmalloc(sizeof *state);
656 dpif_linux_flow_init(&request);
657 request.cmd = ODP_DP_CMD_GET;
658 request.dp_idx = dpif->minor;
660 buf = ofpbuf_new(1024);
661 dpif_linux_flow_to_ofpbuf(&request, buf);
662 nl_dump_start(&state->dump, genl_sock, buf);
669 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
670 const struct nlattr **key, size_t *key_len,
671 const struct nlattr **actions, size_t *actions_len,
672 const struct dpif_flow_stats **stats)
674 struct dpif_linux_flow_state *state = state_;
678 if (!nl_dump_next(&state->dump, &buf)) {
682 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
685 *key = state->flow.key;
686 *key_len = state->flow.key_len;
689 *actions = state->flow.actions;
690 *actions_len = state->flow.actions_len;
693 dpif_linux_flow_get_stats(&state->flow, &state->stats);
694 *stats = &state->stats;
701 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
703 struct dpif_linux_flow_state *state = state_;
704 int error = nl_dump_done(&state->dump);
710 dpif_linux_execute(struct dpif *dpif_,
711 const struct nlattr *actions, size_t actions_len,
712 const struct ofpbuf *packet)
714 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
715 struct odp_header *execute;
719 buf = ofpbuf_new(128 + actions_len + packet->size);
721 nl_msg_put_genlmsghdr(buf, 0, odp_packet_family, NLM_F_REQUEST,
722 ODP_PACKET_CMD_EXECUTE, 1);
724 execute = ofpbuf_put_uninit(buf, sizeof *execute);
725 execute->dp_idx = dpif->minor;
727 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, packet->data, packet->size);
728 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_ACTIONS, actions, actions_len);
730 error = nl_sock_transact(genl_sock, buf, NULL);
736 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
738 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
739 *listen_mask = dpif->listen_mask;
744 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
746 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
750 if (listen_mask == dpif->listen_mask) {
752 } else if (!listen_mask) {
753 nl_sock_destroy(dpif->mc_sock);
754 dpif->mc_sock = NULL;
755 dpif->listen_mask = 0;
757 } else if (!dpif->mc_sock) {
758 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
764 /* Unsubscribe from old groups. */
765 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
766 if (dpif->listen_mask & (1u << i)) {
767 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
771 /* Update listen_mask. */
772 dpif->listen_mask = listen_mask;
774 /* Subscribe to new groups. */
776 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
777 if (dpif->listen_mask & (1u << i)) {
780 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
790 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
791 uint32_t *probability)
793 struct dpif_linux_dp dp;
797 error = dpif_linux_dp_get(dpif_, &dp, &buf);
799 *probability = dp.sampling ? *dp.sampling : 0;
806 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
808 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
809 struct dpif_linux_dp dp;
811 dpif_linux_dp_init(&dp);
812 dp.cmd = ODP_DP_CMD_SET;
813 dp.dp_idx = dpif->minor;
814 dp.sampling = &probability;
815 return dpif_linux_dp_transact(&dp, NULL, NULL);
819 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
820 uint32_t queue_id, uint32_t *priority)
822 if (queue_id < 0xf000) {
823 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
831 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
834 static const struct nl_policy odp_packet_policy[] = {
835 /* Always present. */
836 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
837 .min_len = ETH_HEADER_LEN },
838 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
840 /* ODP_PACKET_CMD_ACTION only. */
841 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
843 /* ODP_PACKET_CMD_SAMPLE only. */
844 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
845 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
848 struct odp_header *odp_header;
849 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
850 struct nlmsghdr *nlmsg;
851 struct genlmsghdr *genl;
855 ofpbuf_use_const(&b, buf->data, buf->size);
857 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
858 genl = ofpbuf_try_pull(&b, sizeof *genl);
859 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
860 if (!nlmsg || !genl || !odp_header
861 || nlmsg->nlmsg_type != odp_packet_family
862 || !nl_policy_parse(&b, 0, odp_packet_policy, a,
863 ARRAY_SIZE(odp_packet_policy))) {
867 type = (genl->cmd == ODP_PACKET_CMD_MISS ? DPIF_UC_MISS
868 : genl->cmd == ODP_PACKET_CMD_ACTION ? DPIF_UC_ACTION
869 : genl->cmd == ODP_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
875 memset(upcall, 0, sizeof *upcall);
877 upcall->packet = buf;
878 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
879 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
880 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
881 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
882 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
883 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
885 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
886 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
888 if (a[ODP_PACKET_ATTR_ACTIONS]) {
889 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
890 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
893 *dp_idx = odp_header->dp_idx;
899 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
901 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
906 if (!dpif->mc_sock) {
910 for (i = 0; i < 50; i++) {
913 error = nl_sock_recv(dpif->mc_sock, &buf, false);
918 error = parse_odp_packet(buf, upcall, &dp_idx);
920 && dp_idx == dpif->minor
921 && dpif->listen_mask & (1u << upcall->type)) {
935 dpif_linux_recv_wait(struct dpif *dpif_)
937 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
939 nl_sock_wait(dpif->mc_sock, POLLIN);
944 dpif_linux_recv_purge(struct dpif *dpif_)
946 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
949 nl_sock_drain(dpif->mc_sock);
953 const struct dpif_class dpif_linux_class = {
957 dpif_linux_enumerate,
960 dpif_linux_get_all_names,
962 dpif_linux_get_stats,
963 dpif_linux_get_drop_frags,
964 dpif_linux_set_drop_frags,
967 dpif_linux_port_query_by_number,
968 dpif_linux_port_query_by_name,
969 dpif_linux_get_max_ports,
970 dpif_linux_port_dump_start,
971 dpif_linux_port_dump_next,
972 dpif_linux_port_dump_done,
973 dpif_linux_port_poll,
974 dpif_linux_port_poll_wait,
978 dpif_linux_flow_flush,
979 dpif_linux_flow_dump_start,
980 dpif_linux_flow_dump_next,
981 dpif_linux_flow_dump_done,
983 dpif_linux_recv_get_mask,
984 dpif_linux_recv_set_mask,
985 dpif_linux_get_sflow_probability,
986 dpif_linux_set_sflow_probability,
987 dpif_linux_queue_to_priority,
989 dpif_linux_recv_wait,
990 dpif_linux_recv_purge,
994 dpif_linux_init(void)
996 static int error = -1;
999 error = nl_lookup_genl_family(ODP_DATAPATH_FAMILY,
1000 &odp_datapath_family);
1002 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1003 "The Open vSwitch kernel module is probably not loaded.",
1004 ODP_DATAPATH_FAMILY);
1007 error = nl_lookup_genl_family(ODP_VPORT_FAMILY, &odp_vport_family);
1010 error = nl_lookup_genl_family(ODP_FLOW_FAMILY, &odp_flow_family);
1013 error = nl_lookup_genl_family(ODP_PACKET_FAMILY,
1014 &odp_packet_family);
1017 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1025 dpif_linux_is_internal_device(const char *name)
1027 struct dpif_linux_vport reply;
1031 error = dpif_linux_vport_get(name, &reply, &buf);
1034 } else if (error != ENODEV) {
1035 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1036 name, strerror(error));
1039 return reply.type == ODP_VPORT_TYPE_INTERNAL;
1043 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1046 struct dpif_linux *dpif = dpif_;
1049 if (change->master_ifindex == dpif->local_ifindex
1050 && (change->nlmsg_type == RTM_NEWLINK
1051 || change->nlmsg_type == RTM_DELLINK))
1053 /* Our datapath changed, either adding a new port or deleting an
1055 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
1058 dpif->change_error = true;
1062 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1063 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1064 * positive errno value.
1066 * 'vport' will contain pointers into 'buf', so the caller should not free
1067 * 'buf' while 'vport' is still in use. */
1069 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1070 const struct ofpbuf *buf)
1072 static const struct nl_policy odp_vport_policy[] = {
1073 [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1074 [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1075 [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1076 [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1077 .min_len = sizeof(struct rtnl_link_stats64),
1078 .max_len = sizeof(struct rtnl_link_stats64),
1080 [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1081 .min_len = ETH_ADDR_LEN,
1082 .max_len = ETH_ADDR_LEN,
1084 [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1085 [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1086 [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1087 [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1090 struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
1091 struct odp_header *odp_header;
1092 struct nlmsghdr *nlmsg;
1093 struct genlmsghdr *genl;
1096 dpif_linux_vport_init(vport);
1098 ofpbuf_use_const(&b, buf->data, buf->size);
1099 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1100 genl = ofpbuf_try_pull(&b, sizeof *genl);
1101 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1102 if (!nlmsg || !genl || !odp_header
1103 || nlmsg->nlmsg_type != odp_vport_family
1104 || !nl_policy_parse(&b, 0, odp_vport_policy, a,
1105 ARRAY_SIZE(odp_vport_policy))) {
1109 vport->cmd = genl->cmd;
1110 vport->dp_idx = odp_header->dp_idx;
1111 vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1112 vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1113 vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
1114 if (a[ODP_VPORT_ATTR_STATS]) {
1115 vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
1117 if (a[ODP_VPORT_ATTR_ADDRESS]) {
1118 vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
1120 if (a[ODP_VPORT_ATTR_MTU]) {
1121 vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
1123 if (a[ODP_VPORT_ATTR_OPTIONS]) {
1124 vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
1125 vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
1127 if (a[ODP_VPORT_ATTR_IFINDEX]) {
1128 vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
1130 if (a[ODP_VPORT_ATTR_IFLINK]) {
1131 vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
1136 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1137 * followed by Netlink attributes corresponding to 'vport'. */
1139 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1142 struct odp_header *odp_header;
1144 nl_msg_put_genlmsghdr(buf, 0, odp_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1147 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1148 odp_header->dp_idx = vport->dp_idx;
1150 if (vport->port_no != UINT32_MAX) {
1151 nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1154 if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
1155 nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1159 nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1163 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1164 vport->stats, sizeof *vport->stats);
1167 if (vport->address) {
1168 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1169 vport->address, ETH_ADDR_LEN);
1173 nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1176 if (vport->options) {
1177 nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1178 vport->options, vport->options_len);
1181 if (vport->ifindex) {
1182 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1185 if (vport->iflink) {
1186 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1190 /* Clears 'vport' to "empty" values. */
1192 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1194 memset(vport, 0, sizeof *vport);
1195 vport->dp_idx = UINT32_MAX;
1196 vport->port_no = UINT32_MAX;
1199 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1200 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1201 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1202 * result of the command is expected to be an odp_vport also, which is decoded
1203 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1204 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1206 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1207 struct dpif_linux_vport *reply,
1208 struct ofpbuf **bufp)
1210 struct ofpbuf *request_buf;
1213 assert((reply != NULL) == (bufp != NULL));
1215 request_buf = ofpbuf_new(1024);
1216 dpif_linux_vport_to_ofpbuf(request, request_buf);
1217 error = nl_sock_transact(genl_sock, request_buf, bufp);
1218 ofpbuf_delete(request_buf);
1222 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1225 dpif_linux_vport_init(reply);
1226 ofpbuf_delete(*bufp);
1233 /* Obtains information about the kernel vport named 'name' and stores it into
1234 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1235 * longer needed ('reply' will contain pointers into '*bufp'). */
1237 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1238 struct ofpbuf **bufp)
1240 struct dpif_linux_vport request;
1242 dpif_linux_vport_init(&request);
1243 request.cmd = ODP_VPORT_CMD_GET;
1244 request.name = name;
1246 return dpif_linux_vport_transact(&request, reply, bufp);
1249 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1250 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1251 * positive errno value.
1253 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1254 * while 'dp' is still in use. */
1256 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1258 static const struct nl_policy odp_datapath_policy[] = {
1259 [ODP_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1260 [ODP_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1261 .min_len = sizeof(struct odp_stats),
1262 .max_len = sizeof(struct odp_stats),
1264 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1265 [ODP_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1266 [ODP_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1269 struct nlattr *a[ARRAY_SIZE(odp_datapath_policy)];
1270 struct odp_header *odp_header;
1271 struct nlmsghdr *nlmsg;
1272 struct genlmsghdr *genl;
1275 dpif_linux_dp_init(dp);
1277 ofpbuf_use_const(&b, buf->data, buf->size);
1278 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1279 genl = ofpbuf_try_pull(&b, sizeof *genl);
1280 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1281 if (!nlmsg || !genl || !odp_header
1282 || nlmsg->nlmsg_type != odp_datapath_family
1283 || !nl_policy_parse(&b, 0, odp_datapath_policy, a,
1284 ARRAY_SIZE(odp_datapath_policy))) {
1288 dp->cmd = genl->cmd;
1289 dp->dp_idx = odp_header->dp_idx;
1290 dp->name = nl_attr_get_string(a[ODP_DP_ATTR_NAME]);
1291 if (a[ODP_DP_ATTR_STATS]) {
1292 /* Can't use structure assignment because Netlink doesn't ensure
1293 * sufficient alignment for 64-bit members. */
1294 memcpy(&dp->stats, nl_attr_get(a[ODP_DP_ATTR_STATS]),
1297 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1298 dp->ipv4_frags = nl_attr_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1300 if (a[ODP_DP_ATTR_SAMPLING]) {
1301 dp->sampling = nl_attr_get(a[ODP_DP_ATTR_SAMPLING]);
1304 if (a[ODP_DP_ATTR_MCGROUPS]) {
1305 static const struct nl_policy odp_mcgroup_policy[] = {
1306 [ODP_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1307 [ODP_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1308 [ODP_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1311 struct nlattr *mcgroups[ARRAY_SIZE(odp_mcgroup_policy)];
1313 if (!nl_parse_nested(a[ODP_DP_ATTR_MCGROUPS], odp_mcgroup_policy,
1314 mcgroups, ARRAY_SIZE(odp_mcgroup_policy))) {
1318 if (mcgroups[ODP_PACKET_CMD_MISS]) {
1319 dp->mcgroups[DPIF_UC_MISS]
1320 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_MISS]);
1322 if (mcgroups[ODP_PACKET_CMD_ACTION]) {
1323 dp->mcgroups[DPIF_UC_ACTION]
1324 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_ACTION]);
1326 if (mcgroups[ODP_PACKET_CMD_SAMPLE]) {
1327 dp->mcgroups[DPIF_UC_SAMPLE]
1328 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_SAMPLE]);
1335 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1337 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1339 struct odp_header *odp_header;
1341 nl_msg_put_genlmsghdr(buf, 0, odp_datapath_family,
1342 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1344 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1345 odp_header->dp_idx = dp->dp_idx;
1348 nl_msg_put_string(buf, ODP_DP_ATTR_NAME, dp->name);
1351 /* Skip ODP_DP_ATTR_STATS since we never have a reason to serialize it. */
1353 if (dp->ipv4_frags) {
1354 nl_msg_put_u32(buf, ODP_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1358 nl_msg_put_u32(buf, ODP_DP_ATTR_SAMPLING, *dp->sampling);
1362 /* Clears 'dp' to "empty" values. */
1364 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1366 memset(dp, 0, sizeof *dp);
1371 dpif_linux_dp_dump_start(struct nl_dump *dump)
1373 struct dpif_linux_dp request;
1376 dpif_linux_dp_init(&request);
1377 request.cmd = ODP_DP_CMD_GET;
1379 buf = ofpbuf_new(1024);
1380 dpif_linux_dp_to_ofpbuf(&request, buf);
1381 nl_dump_start(dump, genl_sock, buf);
1385 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1386 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1387 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1388 * result of the command is expected to be of the same form, which is decoded
1389 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1390 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1392 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1393 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1395 struct ofpbuf *request_buf;
1398 assert((reply != NULL) == (bufp != NULL));
1400 request_buf = ofpbuf_new(1024);
1401 dpif_linux_dp_to_ofpbuf(request, request_buf);
1402 error = nl_sock_transact(genl_sock, request_buf, bufp);
1403 ofpbuf_delete(request_buf);
1407 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1410 dpif_linux_dp_init(reply);
1411 ofpbuf_delete(*bufp);
1418 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1419 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1420 * will contain pointers into '*bufp'). */
1422 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1423 struct ofpbuf **bufp)
1425 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1426 struct dpif_linux_dp request;
1428 dpif_linux_dp_init(&request);
1429 request.cmd = ODP_DP_CMD_GET;
1430 request.dp_idx = dpif->minor;
1432 return dpif_linux_dp_transact(&request, reply, bufp);
1435 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1436 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1437 * positive errno value.
1439 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1440 * while 'flow' is still in use. */
1442 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1443 const struct ofpbuf *buf)
1445 static const struct nl_policy odp_flow_policy[] = {
1446 [ODP_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1447 [ODP_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1448 [ODP_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1449 .min_len = sizeof(struct odp_flow_stats),
1450 .max_len = sizeof(struct odp_flow_stats),
1452 [ODP_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1453 [ODP_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1454 /* The kernel never uses ODP_FLOW_ATTR_CLEAR. */
1457 struct nlattr *a[ARRAY_SIZE(odp_flow_policy)];
1458 struct odp_header *odp_header;
1459 struct nlmsghdr *nlmsg;
1460 struct genlmsghdr *genl;
1463 dpif_linux_flow_init(flow);
1465 ofpbuf_use_const(&b, buf->data, buf->size);
1466 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1467 genl = ofpbuf_try_pull(&b, sizeof *genl);
1468 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1469 if (!nlmsg || !genl || !odp_header
1470 || nlmsg->nlmsg_type != odp_flow_family
1471 || !nl_policy_parse(&b, 0, odp_flow_policy, a,
1472 ARRAY_SIZE(odp_flow_policy))) {
1476 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1477 flow->dp_idx = odp_header->dp_idx;
1478 flow->key = nl_attr_get(a[ODP_FLOW_ATTR_KEY]);
1479 flow->key_len = nl_attr_get_size(a[ODP_FLOW_ATTR_KEY]);
1480 if (a[ODP_FLOW_ATTR_ACTIONS]) {
1481 flow->actions = nl_attr_get(a[ODP_FLOW_ATTR_ACTIONS]);
1482 flow->actions_len = nl_attr_get_size(a[ODP_FLOW_ATTR_ACTIONS]);
1484 if (a[ODP_FLOW_ATTR_STATS]) {
1485 flow->stats = nl_attr_get(a[ODP_FLOW_ATTR_STATS]);
1487 if (a[ODP_FLOW_ATTR_TCP_FLAGS]) {
1488 flow->tcp_flags = nl_attr_get(a[ODP_FLOW_ATTR_TCP_FLAGS]);
1493 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1494 * followed by Netlink attributes corresponding to 'flow'. */
1496 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1499 struct odp_header *odp_header;
1501 nl_msg_put_genlmsghdr(buf, 0, odp_flow_family,
1502 NLM_F_REQUEST | flow->nlmsg_flags, flow->cmd, 1);
1504 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1505 odp_header->dp_idx = flow->dp_idx;
1507 if (flow->key_len) {
1508 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_KEY, flow->key, flow->key_len);
1511 if (flow->actions_len) {
1512 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_ACTIONS,
1513 flow->actions, flow->actions_len);
1516 /* We never need to send these to the kernel. */
1517 assert(!flow->stats);
1518 assert(!flow->tcp_flags);
1519 assert(!flow->used);
1522 nl_msg_put_flag(buf, ODP_FLOW_ATTR_CLEAR);
1526 /* Clears 'flow' to "empty" values. */
1528 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1530 memset(flow, 0, sizeof *flow);
1533 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1534 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1535 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1536 * result of the command is expected to be a flow also, which is decoded and
1537 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1538 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1540 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1541 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1543 struct ofpbuf *request_buf;
1546 assert((reply != NULL) == (bufp != NULL));
1548 request_buf = ofpbuf_new(1024);
1549 dpif_linux_flow_to_ofpbuf(request, request_buf);
1550 error = nl_sock_transact(genl_sock, request_buf, bufp);
1551 ofpbuf_delete(request_buf);
1555 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1558 dpif_linux_flow_init(reply);
1559 ofpbuf_delete(*bufp);
1567 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1568 struct dpif_flow_stats *stats)
1571 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1572 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1574 stats->n_packets = 0;
1577 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1578 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;