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/ethtool.h>
29 #include <linux/pkt_sched.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/sockios.h>
33 #include <sys/ioctl.h>
37 #include "dpif-provider.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 struct dpif_linux_dp {
58 /* ioctl command argument. */
61 /* struct odp_datapath header. */
65 const char *name; /* ODP_DP_ATTR_NAME. */
66 struct odp_stats stats; /* ODP_DP_ATTR_STATS. */
67 enum odp_frag_handling ipv4_frags; /* ODP_DP_ATTR_IPV4_FRAGS. */
68 const uint32_t *sampling; /* ODP_DP_ATTR_SAMPLING. */
69 uint32_t mcgroups[DPIF_N_UC_TYPES]; /* ODP_DP_ATTR_MCGROUPS. */
72 static void dpif_linux_dp_init(struct dpif_linux_dp *);
73 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
74 struct dpif_linux_dp *reply,
75 struct ofpbuf **bufp);
76 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
77 struct ofpbuf **bufp);
79 struct dpif_linux_flow {
80 /* ioctl command argument. */
83 /* struct odp_flow header. */
84 unsigned int nlmsg_flags;
89 * The 'stats', 'used', and 'state' members point to 64-bit data that might
90 * only be aligned on 32-bit boundaries, so get_unaligned_u64() should be
91 * used to access their values. */
92 const struct nlattr *key; /* ODP_FLOW_ATTR_KEY. */
94 const struct nlattr *actions; /* ODP_FLOW_ATTR_ACTIONS. */
96 const struct odp_flow_stats *stats; /* ODP_FLOW_ATTR_STATS. */
97 const uint8_t *tcp_flags; /* ODP_FLOW_ATTR_TCP_FLAGS. */
98 const uint64_t *used; /* ODP_FLOW_ATTR_USED. */
99 bool clear; /* ODP_FLOW_ATTR_CLEAR. */
100 const uint64_t *state; /* ODP_FLOW_ATTR_STATE. */
103 static void dpif_linux_flow_init(struct dpif_linux_flow *);
104 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
105 struct dpif_linux_flow *reply,
106 struct ofpbuf **bufp);
107 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
108 struct dpif_flow_stats *);
110 /* Datapath interface for the openvswitch Linux kernel module. */
115 /* Multicast group messages. */
116 struct nl_sock *mc_sock;
117 uint32_t mcgroups[DPIF_N_UC_TYPES];
118 unsigned int listen_mask;
120 /* Used by dpif_linux_get_all_names(). */
124 /* Change notification. */
125 int local_ifindex; /* Ifindex of local port. */
126 struct shash changed_ports; /* Ports that have changed. */
127 struct rtnetlink_notifier port_notifier;
131 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
133 /* Generic Netlink family numbers for ODP. */
134 static int odp_packet_family;
136 /* Generic Netlink socket. */
137 static struct nl_sock *genl_sock;
139 static int dpif_linux_init(void);
140 static int open_dpif(const struct dpif_linux_dp *,
141 const struct dpif_linux_vport *local_vport,
143 static int get_openvswitch_major(void);
144 static int open_minor(int minor, int *fdp);
145 static int make_openvswitch_device(int minor, char **fnp);
146 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
149 static struct dpif_linux *
150 dpif_linux_cast(const struct dpif *dpif)
152 dpif_assert_class(dpif, &dpif_linux_class);
153 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
157 dpif_linux_enumerate(struct svec *all_dps)
163 err = dpif_linux_init();
168 /* Check that the Open vSwitch module is loaded. */
169 major = get_openvswitch_major();
176 struct dpif_linux_dp request, reply;
181 dpif_linux_dp_init(&request);
182 request.dp_idx = dp_idx;
183 request.cmd = ODP_DP_DUMP;
185 error = dpif_linux_dp_transact(&request, &reply, &buf);
187 return error == ENODEV ? 0 : error;
191 sprintf(devname, "dp%d", reply.dp_idx);
192 svec_add(all_dps, devname);
194 dp_idx = reply.dp_idx + 1;
199 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
200 bool create, struct dpif **dpifp)
202 struct dpif_linux_vport vport_request, vport;
203 struct dpif_linux_dp dp_request, dp;
208 error = dpif_linux_init();
213 minor = !strncmp(name, "dp", 2)
214 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
216 /* Create or look up datapath. */
217 dpif_linux_dp_init(&dp_request);
218 dp_request.cmd = create ? ODP_DP_NEW : ODP_DP_GET;
219 dp_request.dp_idx = minor;
220 dp_request.name = minor < 0 ? name : NULL;
221 error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
225 ofpbuf_delete(buf); /* Pointers inside 'dp' are now invalid! */
227 /* Look up local port. */
228 dpif_linux_vport_init(&vport_request);
229 vport_request.cmd = ODP_VPORT_GET;
230 vport_request.dp_idx = dp.dp_idx;
231 vport_request.port_no = ODPP_LOCAL;
232 vport_request.name = minor < 0 ? name : NULL;
233 error = dpif_linux_vport_transact(&vport_request, &vport, &buf);
236 } else if (vport.port_no != ODPP_LOCAL) {
237 /* This is an Open vSwitch device but not the local port. We
238 * intentionally support only using the name of the local port as the
239 * name of a datapath; otherwise, it would be too difficult to
240 * enumerate all the names of a datapath. */
243 error = open_dpif(&dp, &vport, dpifp);
250 open_dpif(const struct dpif_linux_dp *dp,
251 const struct dpif_linux_vport *local_vport, struct dpif **dpifp)
253 int dp_idx = local_vport->dp_idx;
254 struct dpif_linux *dpif;
260 error = open_minor(dp_idx, &fd);
265 dpif = xmalloc(sizeof *dpif);
266 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
267 dpif_linux_port_changed, dpif);
272 name = xasprintf("dp%d", dp_idx);
273 dpif_init(&dpif->dpif, &dpif_linux_class, name, dp_idx, dp_idx);
277 dpif->mc_sock = NULL;
278 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
279 dpif->mcgroups[i] = dp->mcgroups[i];
281 dpif->listen_mask = 0;
282 dpif->local_ifname = xstrdup(local_vport->name);
283 dpif->local_ifindex = local_vport->ifindex;
284 dpif->minor = dp_idx;
285 shash_init(&dpif->changed_ports);
286 dpif->change_error = false;
287 *dpifp = &dpif->dpif;
299 dpif_linux_close(struct dpif *dpif_)
301 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
302 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
303 shash_destroy(&dpif->changed_ports);
304 free(dpif->local_ifname);
310 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
312 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
314 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
315 svec_add(all_names, dpif->local_ifname);
320 dpif_linux_destroy(struct dpif *dpif_)
322 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
323 struct dpif_linux_dp dp;
325 dpif_linux_dp_init(&dp);
327 dp.dp_idx = dpif->minor;
328 return dpif_linux_dp_transact(&dp, NULL, NULL);
332 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
334 struct dpif_linux_dp dp;
338 error = dpif_linux_dp_get(dpif_, &dp, &buf);
347 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
349 struct dpif_linux_dp dp;
353 error = dpif_linux_dp_get(dpif_, &dp, &buf);
355 *drop_fragsp = dp.ipv4_frags == ODP_DP_FRAG_DROP;
362 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
364 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
365 struct dpif_linux_dp dp;
367 dpif_linux_dp_init(&dp);
369 dp.dp_idx = dpif->minor;
370 dp.ipv4_frags = drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO;
371 return dpif_linux_dp_transact(&dp, NULL, NULL);
375 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
378 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
379 const char *name = netdev_get_name(netdev);
380 const char *type = netdev_get_type(netdev);
381 struct dpif_linux_vport request, reply;
382 const struct ofpbuf *options;
386 dpif_linux_vport_init(&request);
387 request.cmd = ODP_VPORT_NEW;
388 request.dp_idx = dpif->minor;
389 request.type = netdev_vport_get_vport_type(netdev);
390 if (request.type == ODP_VPORT_TYPE_UNSPEC) {
391 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
392 "unsupported type `%s'",
393 dpif_name(dpif_), name, type);
398 options = netdev_vport_get_options(netdev);
399 if (options && options->size) {
400 request.options = options->data;
401 request.options_len = options->size;
404 error = dpif_linux_vport_transact(&request, &reply, &buf);
406 *port_nop = reply.port_no;
414 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
416 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
417 struct dpif_linux_vport vport;
419 dpif_linux_vport_init(&vport);
420 vport.cmd = ODP_VPORT_DEL;
421 vport.dp_idx = dpif->minor;
422 vport.port_no = port_no;
423 return dpif_linux_vport_transact(&vport, NULL, NULL);
427 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
428 const char *port_name, struct dpif_port *dpif_port)
430 struct dpif_linux_vport request;
431 struct dpif_linux_vport reply;
435 dpif_linux_vport_init(&request);
436 request.cmd = ODP_VPORT_GET;
437 request.dp_idx = dpif_linux_cast(dpif)->minor;
438 request.port_no = port_no;
439 request.name = port_name;
441 error = dpif_linux_vport_transact(&request, &reply, &buf);
443 dpif_port->name = xstrdup(reply.name);
444 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
445 dpif_port->port_no = reply.port_no;
452 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
453 struct dpif_port *dpif_port)
455 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
459 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
460 struct dpif_port *dpif_port)
462 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
466 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
468 /* If the datapath increases its range of supported ports, then it should
469 * start reporting that. */
474 dpif_linux_flow_flush(struct dpif *dpif_)
476 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
477 return ioctl(dpif->fd, ODP_FLOW_FLUSH, dpif->minor) ? errno : 0;
480 struct dpif_linux_port_state {
486 dpif_linux_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
488 *statep = xzalloc(sizeof(struct dpif_linux_port_state));
493 dpif_linux_port_dump_next(const struct dpif *dpif, void *state_,
494 struct dpif_port *dpif_port)
496 struct dpif_linux_port_state *state = state_;
497 struct dpif_linux_vport request, reply;
501 ofpbuf_delete(state->buf);
504 dpif_linux_vport_init(&request);
505 request.cmd = ODP_VPORT_DUMP;
506 request.dp_idx = dpif_linux_cast(dpif)->minor;
507 request.port_no = state->next;
508 error = dpif_linux_vport_transact(&request, &reply, &buf);
510 return error == ENODEV ? EOF : error;
512 dpif_port->name = (char *) reply.name;
513 dpif_port->type = (char *) netdev_vport_get_netdev_type(&reply);
514 dpif_port->port_no = reply.port_no;
516 state->next = reply.port_no + 1;
522 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
524 struct dpif_linux_port_state *state = state_;
525 ofpbuf_delete(state->buf);
531 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
533 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
535 if (dpif->change_error) {
536 dpif->change_error = false;
537 shash_clear(&dpif->changed_ports);
539 } else if (!shash_is_empty(&dpif->changed_ports)) {
540 struct shash_node *node = shash_first(&dpif->changed_ports);
541 *devnamep = shash_steal(&dpif->changed_ports, node);
549 dpif_linux_port_poll_wait(const struct dpif *dpif_)
551 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
552 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
553 poll_immediate_wake();
555 rtnetlink_link_notifier_wait();
560 dpif_linux_flow_get(const struct dpif *dpif_,
561 const struct nlattr *key, size_t key_len,
562 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
564 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
565 struct dpif_linux_flow request, reply;
569 dpif_linux_flow_init(&request);
570 request.cmd = ODP_FLOW_GET;
571 request.dp_idx = dpif->minor;
573 request.key_len = key_len;
574 error = dpif_linux_flow_transact(&request, &reply, &buf);
577 dpif_linux_flow_get_stats(&reply, stats);
580 buf->data = (void *) reply.actions;
581 buf->size = reply.actions_len;
591 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
592 const struct nlattr *key, size_t key_len,
593 const struct nlattr *actions, size_t actions_len,
594 struct dpif_flow_stats *stats)
596 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
597 struct dpif_linux_flow request, reply;
601 dpif_linux_flow_init(&request);
602 request.cmd = flags & DPIF_FP_CREATE ? ODP_FLOW_NEW : ODP_FLOW_SET;
603 request.dp_idx = dpif->minor;
605 request.key_len = key_len;
606 request.actions = actions;
607 request.actions_len = actions_len;
608 if (flags & DPIF_FP_ZERO_STATS) {
609 request.clear = true;
611 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
612 error = dpif_linux_flow_transact(&request,
613 stats ? &reply : NULL,
614 stats ? &buf : NULL);
615 if (!error && stats) {
616 dpif_linux_flow_get_stats(&reply, stats);
623 dpif_linux_flow_del(struct dpif *dpif_,
624 const struct nlattr *key, size_t key_len,
625 struct dpif_flow_stats *stats)
627 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
628 struct dpif_linux_flow request, reply;
632 dpif_linux_flow_init(&request);
633 request.cmd = ODP_FLOW_DEL;
634 request.dp_idx = dpif->minor;
636 request.key_len = key_len;
637 error = dpif_linux_flow_transact(&request,
638 stats ? &reply : NULL,
639 stats ? &buf : NULL);
640 if (!error && stats) {
641 dpif_linux_flow_get_stats(&reply, stats);
647 struct dpif_linux_flow_state {
648 struct dpif_linux_flow flow;
650 struct dpif_flow_stats stats;
654 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
656 *statep = xzalloc(sizeof(struct dpif_linux_flow_state));
661 dpif_linux_flow_dump_next(const struct dpif *dpif_, void *state_,
662 const struct nlattr **key, size_t *key_len,
663 const struct nlattr **actions, size_t *actions_len,
664 const struct dpif_flow_stats **stats)
666 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
667 struct dpif_linux_flow_state *state = state_;
668 struct ofpbuf *old_buf = state->buf;
669 struct dpif_linux_flow request;
672 dpif_linux_flow_init(&request);
673 request.cmd = ODP_FLOW_DUMP;
674 request.dp_idx = dpif->minor;
675 request.state = state->flow.state;
676 error = dpif_linux_flow_transact(&request, &state->flow, &state->buf);
677 ofpbuf_delete(old_buf);
681 *key = state->flow.key;
682 *key_len = state->flow.key_len;
685 *actions = state->flow.actions;
686 *actions_len = state->flow.actions_len;
689 dpif_linux_flow_get_stats(&state->flow, &state->stats);
690 *stats = &state->stats;
693 return error == ENODEV ? EOF : error;
697 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
699 struct dpif_linux_flow_state *state = state_;
701 ofpbuf_delete(state->buf);
707 dpif_linux_execute(struct dpif *dpif_,
708 const struct nlattr *actions, size_t actions_len,
709 const struct ofpbuf *packet)
711 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
712 struct odp_header *execute;
716 buf = ofpbuf_new(128 + actions_len + packet->size);
718 nl_msg_put_genlmsghdr(buf, 0, odp_packet_family, NLM_F_REQUEST,
719 ODP_PACKET_CMD_EXECUTE, 1);
721 execute = ofpbuf_put_uninit(buf, sizeof *execute);
722 execute->dp_idx = dpif->minor;
724 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, packet->data, packet->size);
725 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_ACTIONS, actions, actions_len);
727 error = nl_sock_transact(genl_sock, buf, NULL);
733 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
735 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
736 *listen_mask = dpif->listen_mask;
741 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
743 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
747 if (listen_mask == dpif->listen_mask) {
749 } else if (!listen_mask) {
750 nl_sock_destroy(dpif->mc_sock);
751 dpif->mc_sock = NULL;
752 dpif->listen_mask = 0;
754 } else if (!dpif->mc_sock) {
755 error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
761 /* Unsubscribe from old groups. */
762 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
763 if (dpif->listen_mask & (1u << i)) {
764 nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
768 /* Update listen_mask. */
769 dpif->listen_mask = listen_mask;
771 /* Subscribe to new groups. */
773 for (i = 0; i < DPIF_N_UC_TYPES; i++) {
774 if (dpif->listen_mask & (1u << i)) {
777 retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
787 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
788 uint32_t *probability)
790 struct dpif_linux_dp dp;
794 error = dpif_linux_dp_get(dpif_, &dp, &buf);
796 *probability = dp.sampling ? *dp.sampling : 0;
803 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
805 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
806 struct dpif_linux_dp dp;
808 dpif_linux_dp_init(&dp);
810 dp.dp_idx = dpif->minor;
811 dp.sampling = &probability;
812 return dpif_linux_dp_transact(&dp, NULL, NULL);
816 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
817 uint32_t queue_id, uint32_t *priority)
819 if (queue_id < 0xf000) {
820 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
828 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
831 static const struct nl_policy odp_packet_policy[] = {
832 /* Always present. */
833 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
834 .min_len = ETH_HEADER_LEN },
835 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
837 /* ODP_PACKET_CMD_ACTION only. */
838 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
840 /* ODP_PACKET_CMD_SAMPLE only. */
841 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
842 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
845 struct odp_header *odp_header;
846 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
847 struct nlmsghdr *nlmsg;
848 struct genlmsghdr *genl;
851 ofpbuf_use_const(&b, buf->data, buf->size);
853 nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
854 genl = ofpbuf_try_pull(&b, sizeof *genl);
855 odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
856 if (!nlmsg || !genl || !odp_header
857 || !nl_policy_parse(&b, 0, odp_packet_policy, a,
858 ARRAY_SIZE(odp_packet_policy))) {
862 memset(upcall, 0, sizeof *upcall);
864 upcall->type = (genl->cmd == ODP_PACKET_CMD_MISS ? DPIF_UC_MISS
865 : genl->cmd == ODP_PACKET_CMD_ACTION ? DPIF_UC_ACTION
866 : genl->cmd == ODP_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
869 upcall->packet = buf;
870 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
871 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
872 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
873 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
874 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
875 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
877 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
878 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
880 if (a[ODP_PACKET_ATTR_ACTIONS]) {
881 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
882 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
885 *dp_idx = odp_header->dp_idx;
891 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
893 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
898 if (!dpif->mc_sock) {
902 for (i = 0; i < 50; i++) {
905 error = nl_sock_recv(dpif->mc_sock, &buf, false);
910 error = parse_odp_packet(buf, upcall, &dp_idx);
912 && dp_idx == dpif->minor
913 && dpif->listen_mask & (1u << upcall->type)) {
927 dpif_linux_recv_wait(struct dpif *dpif_)
929 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
931 nl_sock_wait(dpif->mc_sock, POLLIN);
936 dpif_linux_recv_purge(struct dpif *dpif_)
938 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
941 nl_sock_drain(dpif->mc_sock);
945 const struct dpif_class dpif_linux_class = {
949 dpif_linux_enumerate,
952 dpif_linux_get_all_names,
954 dpif_linux_get_stats,
955 dpif_linux_get_drop_frags,
956 dpif_linux_set_drop_frags,
959 dpif_linux_port_query_by_number,
960 dpif_linux_port_query_by_name,
961 dpif_linux_get_max_ports,
962 dpif_linux_port_dump_start,
963 dpif_linux_port_dump_next,
964 dpif_linux_port_dump_done,
965 dpif_linux_port_poll,
966 dpif_linux_port_poll_wait,
970 dpif_linux_flow_flush,
971 dpif_linux_flow_dump_start,
972 dpif_linux_flow_dump_next,
973 dpif_linux_flow_dump_done,
975 dpif_linux_recv_get_mask,
976 dpif_linux_recv_set_mask,
977 dpif_linux_get_sflow_probability,
978 dpif_linux_set_sflow_probability,
979 dpif_linux_queue_to_priority,
981 dpif_linux_recv_wait,
982 dpif_linux_recv_purge,
985 static int get_major(const char *target);
988 dpif_linux_init(void)
990 static int error = -1;
993 error = nl_lookup_genl_family(ODP_PACKET_FAMILY, &odp_packet_family);
995 error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1003 dpif_linux_is_internal_device(const char *name)
1005 struct dpif_linux_vport reply;
1009 error = dpif_linux_vport_get(name, &reply, &buf);
1012 } else if (error != ENODEV) {
1013 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1014 name, strerror(error));
1017 return reply.type == ODP_VPORT_TYPE_INTERNAL;
1021 make_openvswitch_device(int minor, char **fnp)
1023 const char dirname[] = "/dev/net";
1031 major = get_openvswitch_major();
1035 dev = makedev(major, minor);
1037 sprintf(fn, "%s/dp%d", dirname, minor);
1038 if (!stat(fn, &s)) {
1039 if (!S_ISCHR(s.st_mode)) {
1040 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
1042 } else if (s.st_rdev != dev) {
1043 VLOG_WARN_RL(&error_rl,
1044 "%s is device %u:%u but should be %u:%u, fixing",
1045 fn, major(s.st_rdev), minor(s.st_rdev),
1046 major(dev), minor(dev));
1051 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
1052 fn, strerror(errno));
1055 } else if (errno == ENOENT) {
1056 if (stat(dirname, &s)) {
1057 if (errno == ENOENT) {
1058 if (mkdir(dirname, 0755)) {
1059 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
1060 dirname, strerror(errno));
1064 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
1065 dirname, strerror(errno));
1070 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
1074 /* The device needs to be created. */
1075 if (mknod(fn, S_IFCHR | 0700, dev)) {
1076 VLOG_WARN_RL(&error_rl,
1077 "%s: creating character device %u:%u failed (%s)",
1078 fn, major(dev), minor(dev), strerror(errno));
1087 /* Return the major device number of the Open vSwitch device. If it
1088 * cannot be determined, a negative errno is returned. */
1090 get_openvswitch_major(void)
1092 static int openvswitch_major = -1;
1093 if (openvswitch_major < 0) {
1094 openvswitch_major = get_major("openvswitch");
1096 return openvswitch_major;
1100 get_major(const char *target)
1102 const char fn[] = "/proc/devices";
1107 file = fopen(fn, "r");
1109 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
1113 for (ln = 1; fgets(line, sizeof line, file); ln++) {
1117 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
1118 /* Nothing to do. */
1119 } else if (!strncmp(line, "Block", 5)) {
1120 /* We only want character devices, so skip the rest of the file. */
1122 } else if (sscanf(line, "%d %63s", &major, name)) {
1123 if (!strcmp(name, target)) {
1128 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
1134 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
1139 open_minor(int minor, int *fdp)
1144 error = make_openvswitch_device(minor, &fn);
1149 *fdp = open(fn, O_RDONLY | O_NONBLOCK);
1152 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
1161 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1164 struct dpif_linux *dpif = dpif_;
1167 if (change->master_ifindex == dpif->local_ifindex
1168 && (change->nlmsg_type == RTM_NEWLINK
1169 || change->nlmsg_type == RTM_DELLINK))
1171 /* Our datapath changed, either adding a new port or deleting an
1173 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
1176 dpif->change_error = true;
1181 get_dp0_fd(int *dp0_fdp)
1183 static int dp0_fd = -1;
1188 error = open_minor(0, &fd);
1198 /* Parses the contents of 'buf', which contains a "struct odp_vport" followed
1199 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1200 * positive errno value.
1202 * 'vport' will contain pointers into 'buf', so the caller should not free
1203 * 'buf' while 'vport' is still in use. */
1205 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1206 const struct ofpbuf *buf)
1208 static const struct nl_policy odp_vport_policy[] = {
1209 [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1210 [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1211 [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1212 [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1213 .min_len = sizeof(struct rtnl_link_stats64),
1214 .max_len = sizeof(struct rtnl_link_stats64),
1216 [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1217 .min_len = ETH_ADDR_LEN,
1218 .max_len = ETH_ADDR_LEN,
1220 [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1221 [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1222 [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1223 [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1226 struct odp_vport *odp_vport;
1227 struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
1229 dpif_linux_vport_init(vport);
1231 if (!nl_policy_parse(buf, sizeof *odp_vport, odp_vport_policy,
1232 a, ARRAY_SIZE(odp_vport_policy))) {
1235 odp_vport = buf->data;
1237 vport->dp_idx = odp_vport->dp_idx;
1238 vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1239 vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1240 vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
1241 if (a[ODP_VPORT_ATTR_STATS]) {
1242 vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
1244 if (a[ODP_VPORT_ATTR_ADDRESS]) {
1245 vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
1247 if (a[ODP_VPORT_ATTR_MTU]) {
1248 vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
1250 if (a[ODP_VPORT_ATTR_OPTIONS]) {
1251 vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
1252 vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
1254 if (a[ODP_VPORT_ATTR_IFINDEX]) {
1255 vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
1257 if (a[ODP_VPORT_ATTR_IFLINK]) {
1258 vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
1263 /* Appends to 'buf' (which must initially be empty) a "struct odp_vport"
1264 * followed by Netlink attributes corresponding to 'vport'. */
1266 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1269 struct odp_vport *odp_vport;
1271 ofpbuf_reserve(buf, sizeof odp_vport);
1273 if (vport->port_no != UINT32_MAX) {
1274 nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1277 if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
1278 nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1282 nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1286 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1287 vport->stats, sizeof *vport->stats);
1290 if (vport->address) {
1291 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1292 vport->address, ETH_ADDR_LEN);
1296 nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1299 if (vport->options) {
1300 nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1301 vport->options, vport->options_len);
1304 if (vport->ifindex) {
1305 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1308 if (vport->iflink) {
1309 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1312 odp_vport = ofpbuf_push_uninit(buf, sizeof *odp_vport);
1313 odp_vport->dp_idx = vport->dp_idx;
1314 odp_vport->len = buf->size;
1315 odp_vport->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1318 /* Clears 'vport' to "empty" values. */
1320 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1322 memset(vport, 0, sizeof *vport);
1323 vport->dp_idx = UINT32_MAX;
1324 vport->port_no = UINT32_MAX;
1327 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1328 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1329 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1330 * result of the command is expected to be an odp_vport also, which is decoded
1331 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1332 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1334 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1335 struct dpif_linux_vport *reply,
1336 struct ofpbuf **bufp)
1338 struct ofpbuf *buf = NULL;
1342 assert((reply != NULL) == (bufp != NULL));
1344 error = get_dp0_fd(&fd);
1349 buf = ofpbuf_new(1024);
1350 dpif_linux_vport_to_ofpbuf(request, buf);
1352 error = ioctl(fd, request->cmd, buf->data) ? errno : 0;
1358 buf->size = ((struct odp_vport *) buf->data)->len;
1359 error = dpif_linux_vport_from_ofpbuf(reply, buf);
1372 memset(reply, 0, sizeof *reply);
1378 /* Obtains information about the kernel vport named 'name' and stores it into
1379 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1380 * longer needed ('reply' will contain pointers into '*bufp'). */
1382 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1383 struct ofpbuf **bufp)
1385 struct dpif_linux_vport request;
1387 dpif_linux_vport_init(&request);
1388 request.cmd = ODP_VPORT_GET;
1389 request.name = name;
1391 return dpif_linux_vport_transact(&request, reply, bufp);
1394 /* Parses the contents of 'buf', which contains a "struct odp_datapath"
1395 * followed by Netlink attributes, into 'dp'. Returns 0 if successful,
1396 * otherwise a positive errno value.
1398 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1399 * while 'dp' is still in use. */
1401 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1403 static const struct nl_policy odp_datapath_policy[] = {
1404 [ODP_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1405 [ODP_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1406 .min_len = sizeof(struct odp_stats),
1407 .max_len = sizeof(struct odp_stats),
1409 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1410 [ODP_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1411 [ODP_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1414 struct odp_datapath *odp_dp;
1415 struct nlattr *a[ARRAY_SIZE(odp_datapath_policy)];
1417 dpif_linux_dp_init(dp);
1419 if (!nl_policy_parse(buf, sizeof *odp_dp, odp_datapath_policy,
1420 a, ARRAY_SIZE(odp_datapath_policy))) {
1425 dp->dp_idx = odp_dp->dp_idx;
1426 dp->name = nl_attr_get_string(a[ODP_DP_ATTR_NAME]);
1427 if (a[ODP_DP_ATTR_STATS]) {
1428 /* Can't use structure assignment because Netlink doesn't ensure
1429 * sufficient alignment for 64-bit members. */
1430 memcpy(&dp->stats, nl_attr_get(a[ODP_DP_ATTR_STATS]),
1433 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1434 dp->ipv4_frags = nl_attr_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1436 if (a[ODP_DP_ATTR_SAMPLING]) {
1437 dp->sampling = nl_attr_get(a[ODP_DP_ATTR_SAMPLING]);
1440 if (a[ODP_DP_ATTR_MCGROUPS]) {
1441 static const struct nl_policy odp_mcgroup_policy[] = {
1442 [ODP_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1443 [ODP_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1444 [ODP_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1447 struct nlattr *mcgroups[ARRAY_SIZE(odp_mcgroup_policy)];
1449 if (!nl_parse_nested(a[ODP_DP_ATTR_MCGROUPS], odp_mcgroup_policy,
1450 mcgroups, ARRAY_SIZE(odp_mcgroup_policy))) {
1454 if (mcgroups[ODP_PACKET_CMD_MISS]) {
1455 dp->mcgroups[DPIF_UC_MISS]
1456 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_MISS]);
1458 if (mcgroups[ODP_PACKET_CMD_ACTION]) {
1459 dp->mcgroups[DPIF_UC_ACTION]
1460 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_ACTION]);
1462 if (mcgroups[ODP_PACKET_CMD_SAMPLE]) {
1463 dp->mcgroups[DPIF_UC_SAMPLE]
1464 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_SAMPLE]);
1471 /* Appends to 'buf' (which must initially be empty) a "struct odp_datapath"
1472 * followed by Netlink attributes corresponding to 'dp'. */
1474 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1476 struct odp_datapath *odp_dp;
1478 ofpbuf_reserve(buf, sizeof odp_dp);
1481 nl_msg_put_string(buf, ODP_DP_ATTR_NAME, dp->name);
1484 /* Skip ODP_DP_ATTR_STATS since we never have a reason to serialize it. */
1486 if (dp->ipv4_frags) {
1487 nl_msg_put_u32(buf, ODP_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1491 nl_msg_put_u32(buf, ODP_DP_ATTR_SAMPLING, *dp->sampling);
1494 odp_dp = ofpbuf_push_uninit(buf, sizeof *odp_dp);
1495 odp_dp->dp_idx = dp->dp_idx;
1496 odp_dp->len = buf->size;
1497 odp_dp->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1500 /* Clears 'dp' to "empty" values. */
1502 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1504 memset(dp, 0, sizeof *dp);
1508 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1509 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1510 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1511 * result of the command is expected to be an odp_datapath also, which is
1512 * decoded and stored in '*reply' and '*bufp'. The caller must free '*bufp'
1513 * when the reply is no longer needed ('reply' will contain pointers into
1516 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1517 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1519 struct ofpbuf *buf = NULL;
1523 assert((reply != NULL) == (bufp != NULL));
1525 error = get_dp0_fd(&fd);
1530 buf = ofpbuf_new(1024);
1531 dpif_linux_dp_to_ofpbuf(request, buf);
1533 error = ioctl(fd, request->cmd, buf->data) ? errno : 0;
1539 buf->size = ((struct odp_datapath *) buf->data)->len;
1540 error = dpif_linux_dp_from_ofpbuf(reply, buf);
1553 memset(reply, 0, sizeof *reply);
1559 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1560 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1561 * will contain pointers into '*bufp'). */
1563 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1564 struct ofpbuf **bufp)
1566 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1567 struct dpif_linux_dp request;
1569 dpif_linux_dp_init(&request);
1570 request.cmd = ODP_DP_GET;
1571 request.dp_idx = dpif->minor;
1573 return dpif_linux_dp_transact(&request, reply, bufp);
1576 /* Parses the contents of 'buf', which contains a "struct odp_flow" followed by
1577 * Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1578 * positive errno value.
1580 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1581 * while 'flow' is still in use. */
1583 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1584 const struct ofpbuf *buf)
1586 static const struct nl_policy odp_flow_policy[] = {
1587 [ODP_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1588 [ODP_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1589 [ODP_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1590 .min_len = sizeof(struct odp_flow_stats),
1591 .max_len = sizeof(struct odp_flow_stats),
1593 [ODP_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1594 [ODP_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1595 /* The kernel never uses ODP_FLOW_ATTR_CLEAR. */
1596 [ODP_FLOW_ATTR_STATE] = { .type = NL_A_U64, .optional = true },
1599 struct odp_flow *odp_flow;
1600 struct nlattr *a[ARRAY_SIZE(odp_flow_policy)];
1602 dpif_linux_flow_init(flow);
1604 if (!nl_policy_parse(buf, sizeof *odp_flow, odp_flow_policy,
1605 a, ARRAY_SIZE(odp_flow_policy))) {
1608 odp_flow = buf->data;
1610 flow->nlmsg_flags = odp_flow->nlmsg_flags;
1611 flow->dp_idx = odp_flow->dp_idx;
1612 flow->key = nl_attr_get(a[ODP_FLOW_ATTR_KEY]);
1613 flow->key_len = nl_attr_get_size(a[ODP_FLOW_ATTR_KEY]);
1614 if (a[ODP_FLOW_ATTR_ACTIONS]) {
1615 flow->actions = nl_attr_get(a[ODP_FLOW_ATTR_ACTIONS]);
1616 flow->actions_len = nl_attr_get_size(a[ODP_FLOW_ATTR_ACTIONS]);
1618 if (a[ODP_FLOW_ATTR_STATS]) {
1619 flow->stats = nl_attr_get(a[ODP_FLOW_ATTR_STATS]);
1621 if (a[ODP_FLOW_ATTR_TCP_FLAGS]) {
1622 flow->tcp_flags = nl_attr_get(a[ODP_FLOW_ATTR_TCP_FLAGS]);
1624 if (a[ODP_FLOW_ATTR_STATE]) {
1625 flow->state = nl_attr_get(a[ODP_FLOW_ATTR_STATE]);
1630 /* Appends to 'buf' (which must initially be empty) a "struct odp_flow"
1631 * followed by Netlink attributes corresponding to 'flow'. */
1633 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1636 struct odp_flow *odp_flow;
1638 ofpbuf_reserve(buf, sizeof odp_flow);
1640 if (flow->key_len) {
1641 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_KEY, flow->key, flow->key_len);
1644 if (flow->actions_len) {
1645 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_ACTIONS,
1646 flow->actions, flow->actions_len);
1649 /* We never need to send these to the kernel. */
1650 assert(!flow->stats);
1651 assert(!flow->tcp_flags);
1652 assert(!flow->used);
1655 nl_msg_put_flag(buf, ODP_FLOW_ATTR_CLEAR);
1659 nl_msg_put_u64(buf, ODP_FLOW_ATTR_STATE,
1660 get_unaligned_u64(flow->state));
1663 odp_flow = ofpbuf_push_uninit(buf, sizeof *odp_flow);
1664 odp_flow->nlmsg_flags = flow->nlmsg_flags;
1665 odp_flow->dp_idx = flow->dp_idx;
1666 odp_flow->len = buf->size;
1667 odp_flow->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1670 /* Clears 'flow' to "empty" values. */
1672 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1674 memset(flow, 0, sizeof *flow);
1677 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1678 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1679 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1680 * result of the command is expected to be an odp_flow also, which is decoded
1681 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1682 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1684 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1685 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1687 struct ofpbuf *buf = NULL;
1691 assert((reply != NULL) == (bufp != NULL));
1693 error = get_dp0_fd(&fd);
1698 buf = ofpbuf_new(1024);
1699 dpif_linux_flow_to_ofpbuf(request, buf);
1701 error = ioctl(fd, request->cmd, buf->data) ? errno : 0;
1707 buf->size = ((struct odp_flow *) buf->data)->len;
1708 error = dpif_linux_flow_from_ofpbuf(reply, buf);
1721 memset(reply, 0, sizeof *reply);
1728 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1729 struct dpif_flow_stats *stats)
1732 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1733 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1735 stats->n_packets = 0;
1738 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1739 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;