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.
94 * If 'actions' is nonnull then ODP_FLOW_ATTR_ACTIONS will be included in
95 * the Netlink version of the command, even if actions_len is zero. */
96 const struct nlattr *key; /* ODP_FLOW_ATTR_KEY. */
98 const struct nlattr *actions; /* ODP_FLOW_ATTR_ACTIONS. */
100 const struct odp_flow_stats *stats; /* ODP_FLOW_ATTR_STATS. */
101 const uint8_t *tcp_flags; /* ODP_FLOW_ATTR_TCP_FLAGS. */
102 const uint64_t *used; /* ODP_FLOW_ATTR_USED. */
103 bool clear; /* ODP_FLOW_ATTR_CLEAR. */
106 static void dpif_linux_flow_init(struct dpif_linux_flow *);
107 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
108 const struct ofpbuf *);
109 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
111 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
112 struct dpif_linux_flow *reply,
113 struct ofpbuf **bufp);
114 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
115 struct dpif_flow_stats *);
117 /* Datapath interface for the openvswitch Linux kernel module. */
122 /* Multicast group messages. */
123 struct nl_sock *mc_sock;
124 uint32_t mcgroups[DPIF_N_UC_TYPES];
125 unsigned int listen_mask;
127 /* Change notification. */
128 struct shash changed_ports; /* Ports that have changed. */
129 struct rtnetlink_notifier port_notifier;
133 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
135 /* Generic Netlink family numbers for ODP. */
136 static int odp_datapath_family;
137 static int odp_vport_family;
138 static int odp_flow_family;
139 static int odp_packet_family;
141 /* Generic Netlink socket. */
142 static struct nl_sock *genl_sock;
144 static int dpif_linux_init(void);
145 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
146 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
149 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
151 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
152 const struct ofpbuf *);
154 static struct dpif_linux *
155 dpif_linux_cast(const struct dpif *dpif)
157 dpif_assert_class(dpif, &dpif_linux_class);
158 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
162 dpif_linux_enumerate(struct svec *all_dps)
168 error = dpif_linux_init();
173 dpif_linux_dp_dump_start(&dump);
174 while (nl_dump_next(&dump, &msg)) {
175 struct dpif_linux_dp dp;
177 if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
178 svec_add(all_dps, dp.name);
181 return nl_dump_done(&dump);
185 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
186 bool create, struct dpif **dpifp)
188 struct dpif_linux_dp dp_request, dp;
192 error = dpif_linux_init();
197 /* Create or look up datapath. */
198 dpif_linux_dp_init(&dp_request);
199 dp_request.cmd = create ? ODP_DP_CMD_NEW : ODP_DP_CMD_GET;
200 dp_request.name = name;
201 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
205 error = open_dpif(&dp, dpifp);
212 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
214 struct dpif_linux *dpif;
218 dpif = xmalloc(sizeof *dpif);
219 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
220 dpif_linux_port_changed, dpif);
225 dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
226 dp->dp_ifindex, dp->dp_ifindex);
228 dpif->mc_sock = NULL;
229 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
230 dpif->mcgroups[i] = dp->mcgroups[i];
232 dpif->listen_mask = 0;
233 dpif->dp_ifindex = dp->dp_ifindex;
234 shash_init(&dpif->changed_ports);
235 dpif->change_error = false;
236 *dpifp = &dpif->dpif;
246 dpif_linux_close(struct dpif *dpif_)
248 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
249 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
250 shash_destroy(&dpif->changed_ports);
255 dpif_linux_destroy(struct dpif *dpif_)
257 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
258 struct dpif_linux_dp dp;
260 dpif_linux_dp_init(&dp);
261 dp.cmd = ODP_DP_CMD_DEL;
262 dp.dp_ifindex = dpif->dp_ifindex;
263 return dpif_linux_dp_transact(&dp, NULL, NULL);
267 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
269 struct dpif_linux_dp dp;
273 error = dpif_linux_dp_get(dpif_, &dp, &buf);
282 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
284 struct dpif_linux_dp dp;
288 error = dpif_linux_dp_get(dpif_, &dp, &buf);
290 *drop_fragsp = dp.ipv4_frags == ODP_DP_FRAG_DROP;
297 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
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_SET;
304 dp.dp_ifindex = dpif->dp_ifindex;
305 dp.ipv4_frags = drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO;
306 return dpif_linux_dp_transact(&dp, NULL, NULL);
310 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
313 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
314 const char *name = netdev_get_name(netdev);
315 const char *type = netdev_get_type(netdev);
316 struct dpif_linux_vport request, reply;
317 const struct ofpbuf *options;
321 dpif_linux_vport_init(&request);
322 request.cmd = ODP_VPORT_CMD_NEW;
323 request.dp_ifindex = dpif->dp_ifindex;
324 request.type = netdev_vport_get_vport_type(netdev);
325 if (request.type == ODP_VPORT_TYPE_UNSPEC) {
326 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
327 "unsupported type `%s'",
328 dpif_name(dpif_), name, type);
333 options = netdev_vport_get_options(netdev);
334 if (options && options->size) {
335 request.options = options->data;
336 request.options_len = options->size;
339 error = dpif_linux_vport_transact(&request, &reply, &buf);
341 *port_nop = reply.port_no;
349 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
351 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
352 struct dpif_linux_vport vport;
354 dpif_linux_vport_init(&vport);
355 vport.cmd = ODP_VPORT_CMD_DEL;
356 vport.dp_ifindex = dpif->dp_ifindex;
357 vport.port_no = port_no;
358 return dpif_linux_vport_transact(&vport, NULL, NULL);
362 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
363 const char *port_name, struct dpif_port *dpif_port)
365 struct dpif_linux_vport request;
366 struct dpif_linux_vport reply;
370 dpif_linux_vport_init(&request);
371 request.cmd = ODP_VPORT_CMD_GET;
372 request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
373 request.port_no = port_no;
374 request.name = port_name;
376 error = dpif_linux_vport_transact(&request, &reply, &buf);
378 dpif_port->name = xstrdup(reply.name);
379 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
380 dpif_port->port_no = reply.port_no;
387 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
388 struct dpif_port *dpif_port)
390 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
394 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
395 struct dpif_port *dpif_port)
397 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
401 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
403 /* If the datapath increases its range of supported ports, then it should
404 * start reporting that. */
409 dpif_linux_flow_flush(struct dpif *dpif_)
411 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
412 struct dpif_linux_flow flow;
414 dpif_linux_flow_init(&flow);
415 flow.cmd = ODP_FLOW_CMD_DEL;
416 flow.dp_ifindex = dpif->dp_ifindex;
417 return dpif_linux_flow_transact(&flow, NULL, NULL);
420 struct dpif_linux_port_state {
425 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
427 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
428 struct dpif_linux_port_state *state;
429 struct dpif_linux_vport request;
432 *statep = state = xmalloc(sizeof *state);
434 dpif_linux_vport_init(&request);
435 request.cmd = ODP_DP_CMD_GET;
436 request.dp_ifindex = dpif->dp_ifindex;
438 buf = ofpbuf_new(1024);
439 dpif_linux_vport_to_ofpbuf(&request, buf);
440 nl_dump_start(&state->dump, genl_sock, buf);
447 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
448 struct dpif_port *dpif_port)
450 struct dpif_linux_port_state *state = state_;
451 struct dpif_linux_vport vport;
455 if (!nl_dump_next(&state->dump, &buf)) {
459 error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
464 dpif_port->name = (char *) vport.name;
465 dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
466 dpif_port->port_no = vport.port_no;
471 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
473 struct dpif_linux_port_state *state = state_;
474 int error = nl_dump_done(&state->dump);
480 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
482 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
484 if (dpif->change_error) {
485 dpif->change_error = false;
486 shash_clear(&dpif->changed_ports);
488 } else if (!shash_is_empty(&dpif->changed_ports)) {
489 struct shash_node *node = shash_first(&dpif->changed_ports);
490 *devnamep = shash_steal(&dpif->changed_ports, node);
498 dpif_linux_port_poll_wait(const struct dpif *dpif_)
500 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
501 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
502 poll_immediate_wake();
504 rtnetlink_link_notifier_wait();
509 dpif_linux_flow_get__(const struct dpif *dpif_,
510 const struct nlattr *key, size_t key_len,
511 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
513 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
514 struct dpif_linux_flow request;
516 dpif_linux_flow_init(&request);
517 request.cmd = ODP_FLOW_CMD_GET;
518 request.dp_ifindex = dpif->dp_ifindex;
520 request.key_len = key_len;
521 return dpif_linux_flow_transact(&request, reply, bufp);
525 dpif_linux_flow_get(const struct dpif *dpif_,
526 const struct nlattr *key, size_t key_len,
527 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
529 struct dpif_linux_flow reply;
533 error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
536 dpif_linux_flow_get_stats(&reply, stats);
539 buf->data = (void *) reply.actions;
540 buf->size = reply.actions_len;
550 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
551 const struct nlattr *key, size_t key_len,
552 const struct nlattr *actions, size_t actions_len,
553 struct dpif_flow_stats *stats)
555 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
556 struct dpif_linux_flow request, reply;
557 struct nlattr dummy_action;
561 dpif_linux_flow_init(&request);
562 request.cmd = flags & DPIF_FP_CREATE ? ODP_FLOW_CMD_NEW : ODP_FLOW_CMD_SET;
563 request.dp_ifindex = dpif->dp_ifindex;
565 request.key_len = key_len;
566 /* Ensure that ODP_FLOW_ATTR_ACTIONS will always be included. */
567 request.actions = actions ? actions : &dummy_action;
568 request.actions_len = actions_len;
569 if (flags & DPIF_FP_ZERO_STATS) {
570 request.clear = true;
572 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
573 error = dpif_linux_flow_transact(&request,
574 stats ? &reply : NULL,
575 stats ? &buf : NULL);
576 if (!error && stats) {
577 dpif_linux_flow_get_stats(&reply, stats);
584 dpif_linux_flow_del(struct dpif *dpif_,
585 const struct nlattr *key, size_t key_len,
586 struct dpif_flow_stats *stats)
588 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
589 struct dpif_linux_flow request, reply;
593 dpif_linux_flow_init(&request);
594 request.cmd = ODP_FLOW_CMD_DEL;
595 request.dp_ifindex = dpif->dp_ifindex;
597 request.key_len = key_len;
598 error = dpif_linux_flow_transact(&request,
599 stats ? &reply : NULL,
600 stats ? &buf : NULL);
601 if (!error && stats) {
602 dpif_linux_flow_get_stats(&reply, stats);
608 struct dpif_linux_flow_state {
610 struct dpif_linux_flow flow;
611 struct dpif_flow_stats stats;
616 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
618 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
619 struct dpif_linux_flow_state *state;
620 struct dpif_linux_flow request;
623 *statep = state = xmalloc(sizeof *state);
625 dpif_linux_flow_init(&request);
626 request.cmd = ODP_DP_CMD_GET;
627 request.dp_ifindex = dpif->dp_ifindex;
629 buf = ofpbuf_new(1024);
630 dpif_linux_flow_to_ofpbuf(&request, buf);
631 nl_dump_start(&state->dump, genl_sock, buf);
640 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
641 const struct nlattr **key, size_t *key_len,
642 const struct nlattr **actions, size_t *actions_len,
643 const struct dpif_flow_stats **stats)
645 struct dpif_linux_flow_state *state = state_;
650 ofpbuf_delete(state->buf);
653 if (!nl_dump_next(&state->dump, &buf)) {
657 error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
662 if (actions && !state->flow.actions) {
663 error = dpif_linux_flow_get__(dpif_, state->flow.key,
665 &state->flow, &state->buf);
666 if (error == ENOENT) {
667 VLOG_DBG("dumped flow disappeared on get");
669 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
675 *actions = state->flow.actions;
676 *actions_len = state->flow.actions_len;
679 *key = state->flow.key;
680 *key_len = state->flow.key_len;
683 dpif_linux_flow_get_stats(&state->flow, &state->stats);
684 *stats = &state->stats;
690 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
692 struct dpif_linux_flow_state *state = state_;
693 int error = nl_dump_done(&state->dump);
694 ofpbuf_delete(state->buf);
700 dpif_linux_execute(struct dpif *dpif_,
701 const struct nlattr *actions, size_t actions_len,
702 const struct ofpbuf *packet)
704 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
705 struct odp_header *execute;
709 buf = ofpbuf_new(128 + actions_len + packet->size);
711 nl_msg_put_genlmsghdr(buf, 0, odp_packet_family, NLM_F_REQUEST,
712 ODP_PACKET_CMD_EXECUTE, 1);
714 execute = ofpbuf_put_uninit(buf, sizeof *execute);
715 execute->dp_ifindex = dpif->dp_ifindex;
717 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, packet->data, packet->size);
718 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_ACTIONS, actions, actions_len);
720 error = nl_sock_transact(genl_sock, buf, NULL);
726 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
728 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
729 *listen_mask = dpif->listen_mask;
734 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
736 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
740 if (listen_mask == dpif->listen_mask) {
742 } else if (!listen_mask) {
743 nl_sock_destroy(dpif->mc_sock);
744 dpif->mc_sock = NULL;
745 dpif->listen_mask = 0;
747 } else if (!dpif->mc_sock) {
748 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
754 /* Unsubscribe from old groups. */
755 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
756 if (dpif->listen_mask & (1u << i)) {
757 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
761 /* Update listen_mask. */
762 dpif->listen_mask = listen_mask;
764 /* Subscribe to new groups. */
766 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
767 if (dpif->listen_mask & (1u << i)) {
770 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
780 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
781 uint32_t *probability)
783 struct dpif_linux_dp dp;
787 error = dpif_linux_dp_get(dpif_, &dp, &buf);
789 *probability = dp.sampling ? *dp.sampling : 0;
796 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
798 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
799 struct dpif_linux_dp dp;
801 dpif_linux_dp_init(&dp);
802 dp.cmd = ODP_DP_CMD_SET;
803 dp.dp_ifindex = dpif->dp_ifindex;
804 dp.sampling = &probability;
805 return dpif_linux_dp_transact(&dp, NULL, NULL);
809 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
810 uint32_t queue_id, uint32_t *priority)
812 if (queue_id < 0xf000) {
813 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
821 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
824 static const struct nl_policy odp_packet_policy[] = {
825 /* Always present. */
826 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
827 .min_len = ETH_HEADER_LEN },
828 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
830 /* ODP_PACKET_CMD_ACTION only. */
831 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
833 /* ODP_PACKET_CMD_SAMPLE only. */
834 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
835 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
838 struct odp_header *odp_header;
839 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
840 struct nlmsghdr *nlmsg;
841 struct genlmsghdr *genl;
845 ofpbuf_use_const(&b, buf->data, buf->size);
847 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
848 genl = ofpbuf_try_pull(&b, sizeof *genl);
849 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
850 if (!nlmsg || !genl || !odp_header
851 || nlmsg->nlmsg_type != odp_packet_family
852 || !nl_policy_parse(&b, 0, odp_packet_policy, a,
853 ARRAY_SIZE(odp_packet_policy))) {
857 type = (genl->cmd == ODP_PACKET_CMD_MISS ? DPIF_UC_MISS
858 : genl->cmd == ODP_PACKET_CMD_ACTION ? DPIF_UC_ACTION
859 : genl->cmd == ODP_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
865 memset(upcall, 0, sizeof *upcall);
867 upcall->packet = buf;
868 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
869 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
870 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
871 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
872 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
873 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
875 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
876 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
878 if (a[ODP_PACKET_ATTR_ACTIONS]) {
879 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
880 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
883 *dp_ifindex = odp_header->dp_ifindex;
889 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
891 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
896 if (!dpif->mc_sock) {
900 for (i = 0; i < 50; i++) {
903 error = nl_sock_recv(dpif->mc_sock, &buf, false);
908 error = parse_odp_packet(buf, upcall, &dp_ifindex);
910 && dp_ifindex == dpif->dp_ifindex
911 && dpif->listen_mask & (1u << upcall->type)) {
925 dpif_linux_recv_wait(struct dpif *dpif_)
927 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
929 nl_sock_wait(dpif->mc_sock, POLLIN);
934 dpif_linux_recv_purge(struct dpif *dpif_)
936 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
939 nl_sock_drain(dpif->mc_sock);
943 const struct dpif_class dpif_linux_class = {
947 dpif_linux_enumerate,
951 dpif_linux_get_stats,
952 dpif_linux_get_drop_frags,
953 dpif_linux_set_drop_frags,
956 dpif_linux_port_query_by_number,
957 dpif_linux_port_query_by_name,
958 dpif_linux_get_max_ports,
959 dpif_linux_port_dump_start,
960 dpif_linux_port_dump_next,
961 dpif_linux_port_dump_done,
962 dpif_linux_port_poll,
963 dpif_linux_port_poll_wait,
967 dpif_linux_flow_flush,
968 dpif_linux_flow_dump_start,
969 dpif_linux_flow_dump_next,
970 dpif_linux_flow_dump_done,
972 dpif_linux_recv_get_mask,
973 dpif_linux_recv_set_mask,
974 dpif_linux_get_sflow_probability,
975 dpif_linux_set_sflow_probability,
976 dpif_linux_queue_to_priority,
978 dpif_linux_recv_wait,
979 dpif_linux_recv_purge,
983 dpif_linux_init(void)
985 static int error = -1;
988 error = nl_lookup_genl_family(ODP_DATAPATH_FAMILY,
989 &odp_datapath_family);
991 VLOG_ERR("Generic Netlink family '%s' does not exist. "
992 "The Open vSwitch kernel module is probably not loaded.",
993 ODP_DATAPATH_FAMILY);
996 error = nl_lookup_genl_family(ODP_VPORT_FAMILY, &odp_vport_family);
999 error = nl_lookup_genl_family(ODP_FLOW_FAMILY, &odp_flow_family);
1002 error = nl_lookup_genl_family(ODP_PACKET_FAMILY,
1003 &odp_packet_family);
1006 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1014 dpif_linux_is_internal_device(const char *name)
1016 struct dpif_linux_vport reply;
1020 error = dpif_linux_vport_get(name, &reply, &buf);
1023 } else if (error != ENODEV) {
1024 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1025 name, strerror(error));
1028 return reply.type == ODP_VPORT_TYPE_INTERNAL;
1032 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1035 struct dpif_linux *dpif = dpif_;
1038 if (change->master_ifindex == dpif->dp_ifindex
1039 && (change->nlmsg_type == RTM_NEWLINK
1040 || change->nlmsg_type == RTM_DELLINK))
1042 /* Our datapath changed, either adding a new port or deleting an
1044 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
1047 dpif->change_error = true;
1051 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1052 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1053 * positive errno value.
1055 * 'vport' will contain pointers into 'buf', so the caller should not free
1056 * 'buf' while 'vport' is still in use. */
1058 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1059 const struct ofpbuf *buf)
1061 static const struct nl_policy odp_vport_policy[] = {
1062 [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1063 [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1064 [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1065 [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1066 .min_len = sizeof(struct rtnl_link_stats64),
1067 .max_len = sizeof(struct rtnl_link_stats64),
1069 [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1070 .min_len = ETH_ADDR_LEN,
1071 .max_len = ETH_ADDR_LEN,
1073 [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1074 [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1075 [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1076 [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1079 struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
1080 struct odp_header *odp_header;
1081 struct nlmsghdr *nlmsg;
1082 struct genlmsghdr *genl;
1085 dpif_linux_vport_init(vport);
1087 ofpbuf_use_const(&b, buf->data, buf->size);
1088 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1089 genl = ofpbuf_try_pull(&b, sizeof *genl);
1090 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1091 if (!nlmsg || !genl || !odp_header
1092 || nlmsg->nlmsg_type != odp_vport_family
1093 || !nl_policy_parse(&b, 0, odp_vport_policy, a,
1094 ARRAY_SIZE(odp_vport_policy))) {
1098 vport->cmd = genl->cmd;
1099 vport->dp_ifindex = odp_header->dp_ifindex;
1100 vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1101 vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1102 vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
1103 if (a[ODP_VPORT_ATTR_STATS]) {
1104 vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
1106 if (a[ODP_VPORT_ATTR_ADDRESS]) {
1107 vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
1109 if (a[ODP_VPORT_ATTR_MTU]) {
1110 vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
1112 if (a[ODP_VPORT_ATTR_OPTIONS]) {
1113 vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
1114 vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
1116 if (a[ODP_VPORT_ATTR_IFINDEX]) {
1117 vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
1119 if (a[ODP_VPORT_ATTR_IFLINK]) {
1120 vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
1125 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1126 * followed by Netlink attributes corresponding to 'vport'. */
1128 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1131 struct odp_header *odp_header;
1133 nl_msg_put_genlmsghdr(buf, 0, odp_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1136 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1137 odp_header->dp_ifindex = vport->dp_ifindex;
1139 if (vport->port_no != UINT32_MAX) {
1140 nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1143 if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
1144 nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1148 nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1152 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1153 vport->stats, sizeof *vport->stats);
1156 if (vport->address) {
1157 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1158 vport->address, ETH_ADDR_LEN);
1162 nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1165 if (vport->options) {
1166 nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1167 vport->options, vport->options_len);
1170 if (vport->ifindex) {
1171 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1174 if (vport->iflink) {
1175 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1179 /* Clears 'vport' to "empty" values. */
1181 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1183 memset(vport, 0, sizeof *vport);
1184 vport->port_no = UINT32_MAX;
1187 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1188 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1189 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1190 * result of the command is expected to be an odp_vport also, which is decoded
1191 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1192 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1194 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1195 struct dpif_linux_vport *reply,
1196 struct ofpbuf **bufp)
1198 struct ofpbuf *request_buf;
1201 assert((reply != NULL) == (bufp != NULL));
1203 request_buf = ofpbuf_new(1024);
1204 dpif_linux_vport_to_ofpbuf(request, request_buf);
1205 error = nl_sock_transact(genl_sock, request_buf, bufp);
1206 ofpbuf_delete(request_buf);
1210 error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1213 dpif_linux_vport_init(reply);
1214 ofpbuf_delete(*bufp);
1221 /* Obtains information about the kernel vport named 'name' and stores it into
1222 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1223 * longer needed ('reply' will contain pointers into '*bufp'). */
1225 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1226 struct ofpbuf **bufp)
1228 struct dpif_linux_vport request;
1230 dpif_linux_vport_init(&request);
1231 request.cmd = ODP_VPORT_CMD_GET;
1232 request.name = name;
1234 return dpif_linux_vport_transact(&request, reply, bufp);
1237 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1238 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1239 * positive errno value.
1241 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1242 * while 'dp' is still in use. */
1244 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1246 static const struct nl_policy odp_datapath_policy[] = {
1247 [ODP_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1248 [ODP_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1249 .min_len = sizeof(struct odp_stats),
1250 .max_len = sizeof(struct odp_stats),
1252 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1253 [ODP_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1254 [ODP_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1257 struct nlattr *a[ARRAY_SIZE(odp_datapath_policy)];
1258 struct odp_header *odp_header;
1259 struct nlmsghdr *nlmsg;
1260 struct genlmsghdr *genl;
1263 dpif_linux_dp_init(dp);
1265 ofpbuf_use_const(&b, buf->data, buf->size);
1266 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1267 genl = ofpbuf_try_pull(&b, sizeof *genl);
1268 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1269 if (!nlmsg || !genl || !odp_header
1270 || nlmsg->nlmsg_type != odp_datapath_family
1271 || !nl_policy_parse(&b, 0, odp_datapath_policy, a,
1272 ARRAY_SIZE(odp_datapath_policy))) {
1276 dp->cmd = genl->cmd;
1277 dp->dp_ifindex = odp_header->dp_ifindex;
1278 dp->name = nl_attr_get_string(a[ODP_DP_ATTR_NAME]);
1279 if (a[ODP_DP_ATTR_STATS]) {
1280 /* Can't use structure assignment because Netlink doesn't ensure
1281 * sufficient alignment for 64-bit members. */
1282 memcpy(&dp->stats, nl_attr_get(a[ODP_DP_ATTR_STATS]),
1285 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1286 dp->ipv4_frags = nl_attr_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1288 if (a[ODP_DP_ATTR_SAMPLING]) {
1289 dp->sampling = nl_attr_get(a[ODP_DP_ATTR_SAMPLING]);
1292 if (a[ODP_DP_ATTR_MCGROUPS]) {
1293 static const struct nl_policy odp_mcgroup_policy[] = {
1294 [ODP_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1295 [ODP_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1296 [ODP_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1299 struct nlattr *mcgroups[ARRAY_SIZE(odp_mcgroup_policy)];
1301 if (!nl_parse_nested(a[ODP_DP_ATTR_MCGROUPS], odp_mcgroup_policy,
1302 mcgroups, ARRAY_SIZE(odp_mcgroup_policy))) {
1306 if (mcgroups[ODP_PACKET_CMD_MISS]) {
1307 dp->mcgroups[DPIF_UC_MISS]
1308 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_MISS]);
1310 if (mcgroups[ODP_PACKET_CMD_ACTION]) {
1311 dp->mcgroups[DPIF_UC_ACTION]
1312 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_ACTION]);
1314 if (mcgroups[ODP_PACKET_CMD_SAMPLE]) {
1315 dp->mcgroups[DPIF_UC_SAMPLE]
1316 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_SAMPLE]);
1323 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1325 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1327 struct odp_header *odp_header;
1329 nl_msg_put_genlmsghdr(buf, 0, odp_datapath_family,
1330 NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1332 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1333 odp_header->dp_ifindex = dp->dp_ifindex;
1336 nl_msg_put_string(buf, ODP_DP_ATTR_NAME, dp->name);
1339 /* Skip ODP_DP_ATTR_STATS since we never have a reason to serialize it. */
1341 if (dp->ipv4_frags) {
1342 nl_msg_put_u32(buf, ODP_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1346 nl_msg_put_u32(buf, ODP_DP_ATTR_SAMPLING, *dp->sampling);
1350 /* Clears 'dp' to "empty" values. */
1352 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1354 memset(dp, 0, sizeof *dp);
1358 dpif_linux_dp_dump_start(struct nl_dump *dump)
1360 struct dpif_linux_dp request;
1363 dpif_linux_dp_init(&request);
1364 request.cmd = ODP_DP_CMD_GET;
1366 buf = ofpbuf_new(1024);
1367 dpif_linux_dp_to_ofpbuf(&request, buf);
1368 nl_dump_start(dump, genl_sock, buf);
1372 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1373 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1374 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1375 * result of the command is expected to be of the same form, which is decoded
1376 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1377 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1379 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1380 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1382 struct ofpbuf *request_buf;
1385 assert((reply != NULL) == (bufp != NULL));
1387 request_buf = ofpbuf_new(1024);
1388 dpif_linux_dp_to_ofpbuf(request, request_buf);
1389 error = nl_sock_transact(genl_sock, request_buf, bufp);
1390 ofpbuf_delete(request_buf);
1394 error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1397 dpif_linux_dp_init(reply);
1398 ofpbuf_delete(*bufp);
1405 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1406 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1407 * will contain pointers into '*bufp'). */
1409 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1410 struct ofpbuf **bufp)
1412 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1413 struct dpif_linux_dp request;
1415 dpif_linux_dp_init(&request);
1416 request.cmd = ODP_DP_CMD_GET;
1417 request.dp_ifindex = dpif->dp_ifindex;
1419 return dpif_linux_dp_transact(&request, reply, bufp);
1422 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1423 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1424 * positive errno value.
1426 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1427 * while 'flow' is still in use. */
1429 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1430 const struct ofpbuf *buf)
1432 static const struct nl_policy odp_flow_policy[] = {
1433 [ODP_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1434 [ODP_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1435 [ODP_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1436 .min_len = sizeof(struct odp_flow_stats),
1437 .max_len = sizeof(struct odp_flow_stats),
1439 [ODP_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1440 [ODP_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1441 /* The kernel never uses ODP_FLOW_ATTR_CLEAR. */
1444 struct nlattr *a[ARRAY_SIZE(odp_flow_policy)];
1445 struct odp_header *odp_header;
1446 struct nlmsghdr *nlmsg;
1447 struct genlmsghdr *genl;
1450 dpif_linux_flow_init(flow);
1452 ofpbuf_use_const(&b, buf->data, buf->size);
1453 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1454 genl = ofpbuf_try_pull(&b, sizeof *genl);
1455 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1456 if (!nlmsg || !genl || !odp_header
1457 || nlmsg->nlmsg_type != odp_flow_family
1458 || !nl_policy_parse(&b, 0, odp_flow_policy, a,
1459 ARRAY_SIZE(odp_flow_policy))) {
1463 flow->nlmsg_flags = nlmsg->nlmsg_flags;
1464 flow->dp_ifindex = odp_header->dp_ifindex;
1465 flow->key = nl_attr_get(a[ODP_FLOW_ATTR_KEY]);
1466 flow->key_len = nl_attr_get_size(a[ODP_FLOW_ATTR_KEY]);
1467 if (a[ODP_FLOW_ATTR_ACTIONS]) {
1468 flow->actions = nl_attr_get(a[ODP_FLOW_ATTR_ACTIONS]);
1469 flow->actions_len = nl_attr_get_size(a[ODP_FLOW_ATTR_ACTIONS]);
1471 if (a[ODP_FLOW_ATTR_STATS]) {
1472 flow->stats = nl_attr_get(a[ODP_FLOW_ATTR_STATS]);
1474 if (a[ODP_FLOW_ATTR_TCP_FLAGS]) {
1475 flow->tcp_flags = nl_attr_get(a[ODP_FLOW_ATTR_TCP_FLAGS]);
1477 if (a[ODP_FLOW_ATTR_USED]) {
1478 flow->used = nl_attr_get(a[ODP_FLOW_ATTR_USED]);
1483 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1484 * followed by Netlink attributes corresponding to 'flow'. */
1486 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1489 struct odp_header *odp_header;
1491 nl_msg_put_genlmsghdr(buf, 0, odp_flow_family,
1492 NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1495 odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1496 odp_header->dp_ifindex = flow->dp_ifindex;
1498 if (flow->key_len) {
1499 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_KEY, flow->key, flow->key_len);
1502 if (flow->actions || flow->actions_len) {
1503 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_ACTIONS,
1504 flow->actions, flow->actions_len);
1507 /* We never need to send these to the kernel. */
1508 assert(!flow->stats);
1509 assert(!flow->tcp_flags);
1510 assert(!flow->used);
1513 nl_msg_put_flag(buf, ODP_FLOW_ATTR_CLEAR);
1517 /* Clears 'flow' to "empty" values. */
1519 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1521 memset(flow, 0, sizeof *flow);
1524 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1525 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1526 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1527 * result of the command is expected to be a flow also, which is decoded and
1528 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1529 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1531 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1532 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1534 struct ofpbuf *request_buf;
1537 assert((reply != NULL) == (bufp != NULL));
1539 request_buf = ofpbuf_new(1024);
1540 dpif_linux_flow_to_ofpbuf(request, request_buf);
1541 error = nl_sock_transact(genl_sock, request_buf, bufp);
1542 ofpbuf_delete(request_buf);
1546 error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1549 dpif_linux_flow_init(reply);
1550 ofpbuf_delete(*bufp);
1558 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1559 struct dpif_flow_stats *stats)
1562 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1563 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1565 stats->n_packets = 0;
1568 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1569 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;