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"
43 #include "openvswitch/tunnel.h"
45 #include "poll-loop.h"
46 #include "rtnetlink.h"
47 #include "rtnetlink-link.h"
50 #include "unaligned.h"
54 VLOG_DEFINE_THIS_MODULE(dpif_linux);
56 struct dpif_linux_dp {
57 /* ioctl command argument. */
60 /* struct odp_datapath header. */
64 const char *name; /* ODP_DP_ATTR_NAME. */
65 struct odp_stats stats; /* ODP_DP_ATTR_STATS. */
66 enum odp_frag_handling ipv4_frags; /* ODP_DP_ATTR_IPV4_FRAGS. */
67 const uint32_t *sampling; /* ODP_DP_ATTR_SAMPLING. */
70 static void dpif_linux_dp_init(struct dpif_linux_dp *);
71 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
72 struct dpif_linux_dp *reply,
73 struct ofpbuf **bufp);
74 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
75 struct ofpbuf **bufp);
77 struct dpif_linux_flow {
78 /* ioctl command argument. */
81 /* struct odp_flow header. */
82 unsigned int nlmsg_flags;
87 * The 'stats', 'used', and 'state' members point to 64-bit data that might
88 * only be aligned on 32-bit boundaries, so get_unaligned_u64() should be
89 * used to access their values. */
90 const struct nlattr *key; /* ODP_FLOW_ATTR_KEY. */
92 const struct nlattr *actions; /* ODP_FLOW_ATTR_ACTIONS. */
94 const struct odp_flow_stats *stats; /* ODP_FLOW_ATTR_STATS. */
95 const uint8_t *tcp_flags; /* ODP_FLOW_ATTR_TCP_FLAGS. */
96 const uint64_t *used; /* ODP_FLOW_ATTR_USED. */
97 bool clear; /* ODP_FLOW_ATTR_CLEAR. */
98 const uint64_t *state; /* ODP_FLOW_ATTR_STATE. */
101 static void dpif_linux_flow_init(struct dpif_linux_flow *);
102 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
103 struct dpif_linux_flow *reply,
104 struct ofpbuf **bufp);
105 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
106 struct dpif_flow_stats *);
108 /* Datapath interface for the openvswitch Linux kernel module. */
113 /* Used by dpif_linux_get_all_names(). */
117 /* Change notification. */
118 int local_ifindex; /* Ifindex of local port. */
119 struct shash changed_ports; /* Ports that have changed. */
120 struct rtnetlink_notifier port_notifier;
124 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
126 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
127 static int open_dpif(const struct dpif_linux_vport *local_vport,
129 static int get_openvswitch_major(void);
130 static int open_minor(int minor, int *fdp);
131 static int make_openvswitch_device(int minor, char **fnp);
132 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
135 static struct dpif_linux *
136 dpif_linux_cast(const struct dpif *dpif)
138 dpif_assert_class(dpif, &dpif_linux_class);
139 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
143 dpif_linux_enumerate(struct svec *all_dps)
148 /* Check that the Open vSwitch module is loaded. */
149 major = get_openvswitch_major();
156 struct dpif_linux_dp request, reply;
161 dpif_linux_dp_init(&request);
162 request.dp_idx = dp_idx;
163 request.cmd = ODP_DP_DUMP;
165 error = dpif_linux_dp_transact(&request, &reply, &buf);
167 return error == ENODEV ? 0 : error;
171 sprintf(devname, "dp%d", reply.dp_idx);
172 svec_add(all_dps, devname);
174 dp_idx = reply.dp_idx + 1;
179 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
180 bool create, struct dpif **dpifp)
182 struct dpif_linux_vport request, reply;
187 minor = !strncmp(name, "dp", 2)
188 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
190 struct dpif_linux_dp request, reply;
194 dpif_linux_dp_init(&request);
195 request.cmd = ODP_DP_NEW;
196 request.dp_idx = minor;
198 error = dpif_linux_dp_transact(&request, &reply, &buf);
202 minor = reply.dp_idx;
206 dpif_linux_vport_init(&request);
207 request.cmd = ODP_VPORT_GET;
208 request.port_no = ODPP_LOCAL;
210 request.dp_idx = minor;
215 error = dpif_linux_vport_transact(&request, &reply, &buf);
218 } else if (reply.port_no != ODPP_LOCAL) {
219 /* This is an Open vSwitch device but not the local port. We
220 * intentionally support only using the name of the local port as the
221 * name of a datapath; otherwise, it would be too difficult to
222 * enumerate all the names of a datapath. */
225 error = open_dpif(&reply, dpifp);
233 open_dpif(const struct dpif_linux_vport *local_vport, struct dpif **dpifp)
235 int dp_idx = local_vport->dp_idx;
236 struct dpif_linux *dpif;
241 error = open_minor(dp_idx, &fd);
246 dpif = xmalloc(sizeof *dpif);
247 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
248 dpif_linux_port_changed, dpif);
253 name = xasprintf("dp%d", dp_idx);
254 dpif_init(&dpif->dpif, &dpif_linux_class, name, dp_idx, dp_idx);
258 dpif->local_ifname = xstrdup(local_vport->name);
259 dpif->local_ifindex = local_vport->ifindex;
260 dpif->minor = dp_idx;
261 shash_init(&dpif->changed_ports);
262 dpif->change_error = false;
263 *dpifp = &dpif->dpif;
275 dpif_linux_close(struct dpif *dpif_)
277 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
278 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
279 shash_destroy(&dpif->changed_ports);
280 free(dpif->local_ifname);
286 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
288 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
290 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
291 svec_add(all_names, dpif->local_ifname);
296 dpif_linux_destroy(struct dpif *dpif_)
298 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
299 struct dpif_linux_dp dp;
301 dpif_linux_dp_init(&dp);
303 dp.dp_idx = dpif->minor;
304 return dpif_linux_dp_transact(&dp, NULL, NULL);
308 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
310 struct dpif_linux_dp dp;
314 error = dpif_linux_dp_get(dpif_, &dp, &buf);
323 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
325 struct dpif_linux_dp dp;
329 error = dpif_linux_dp_get(dpif_, &dp, &buf);
331 *drop_fragsp = dp.ipv4_frags == ODP_DP_FRAG_DROP;
338 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
340 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
341 struct dpif_linux_dp dp;
343 dpif_linux_dp_init(&dp);
345 dp.dp_idx = dpif->minor;
346 dp.ipv4_frags = drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO;
347 return dpif_linux_dp_transact(&dp, NULL, NULL);
351 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
354 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
355 const char *name = netdev_get_name(netdev);
356 const char *type = netdev_get_type(netdev);
357 struct dpif_linux_vport request, reply;
358 const struct ofpbuf *options;
362 dpif_linux_vport_init(&request);
363 request.cmd = ODP_VPORT_NEW;
364 request.dp_idx = dpif->minor;
365 request.type = netdev_vport_get_vport_type(netdev);
366 if (request.type == ODP_VPORT_TYPE_UNSPEC) {
367 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
368 "unsupported type `%s'",
369 dpif_name(dpif_), name, type);
374 options = netdev_vport_get_options(netdev);
375 if (options && options->size) {
376 request.options = options->data;
377 request.options_len = options->size;
380 error = dpif_linux_vport_transact(&request, &reply, &buf);
382 *port_nop = reply.port_no;
390 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
392 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
393 struct dpif_linux_vport vport;
395 dpif_linux_vport_init(&vport);
396 vport.cmd = ODP_VPORT_DEL;
397 vport.dp_idx = dpif->minor;
398 vport.port_no = port_no;
399 return dpif_linux_vport_transact(&vport, NULL, NULL);
403 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
404 const char *port_name, struct dpif_port *dpif_port)
406 struct dpif_linux_vport request;
407 struct dpif_linux_vport reply;
411 dpif_linux_vport_init(&request);
412 request.cmd = ODP_VPORT_GET;
413 request.dp_idx = dpif_linux_cast(dpif)->minor;
414 request.port_no = port_no;
415 request.name = port_name;
417 error = dpif_linux_vport_transact(&request, &reply, &buf);
419 dpif_port->name = xstrdup(reply.name);
420 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
421 dpif_port->port_no = reply.port_no;
428 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
429 struct dpif_port *dpif_port)
431 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
435 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
436 struct dpif_port *dpif_port)
438 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
442 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
444 /* If the datapath increases its range of supported ports, then it should
445 * start reporting that. */
450 dpif_linux_flow_flush(struct dpif *dpif_)
452 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
453 return ioctl(dpif->fd, ODP_FLOW_FLUSH, dpif->minor) ? errno : 0;
456 struct dpif_linux_port_state {
462 dpif_linux_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
464 *statep = xzalloc(sizeof(struct dpif_linux_port_state));
469 dpif_linux_port_dump_next(const struct dpif *dpif, void *state_,
470 struct dpif_port *dpif_port)
472 struct dpif_linux_port_state *state = state_;
473 struct dpif_linux_vport request, reply;
477 ofpbuf_delete(state->buf);
480 dpif_linux_vport_init(&request);
481 request.cmd = ODP_VPORT_DUMP;
482 request.dp_idx = dpif_linux_cast(dpif)->minor;
483 request.port_no = state->next;
484 error = dpif_linux_vport_transact(&request, &reply, &buf);
486 return error == ENODEV ? EOF : error;
488 dpif_port->name = (char *) reply.name;
489 dpif_port->type = (char *) netdev_vport_get_netdev_type(&reply);
490 dpif_port->port_no = reply.port_no;
492 state->next = reply.port_no + 1;
498 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
500 struct dpif_linux_port_state *state = state_;
501 ofpbuf_delete(state->buf);
507 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
509 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
511 if (dpif->change_error) {
512 dpif->change_error = false;
513 shash_clear(&dpif->changed_ports);
515 } else if (!shash_is_empty(&dpif->changed_ports)) {
516 struct shash_node *node = shash_first(&dpif->changed_ports);
517 *devnamep = shash_steal(&dpif->changed_ports, node);
525 dpif_linux_port_poll_wait(const struct dpif *dpif_)
527 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
528 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
529 poll_immediate_wake();
531 rtnetlink_link_notifier_wait();
536 dpif_linux_flow_get(const struct dpif *dpif_,
537 const struct nlattr *key, size_t key_len,
538 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
540 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
541 struct dpif_linux_flow request, reply;
545 dpif_linux_flow_init(&request);
546 request.cmd = ODP_FLOW_GET;
547 request.dp_idx = dpif->minor;
549 request.key_len = key_len;
550 error = dpif_linux_flow_transact(&request, &reply, &buf);
553 dpif_linux_flow_get_stats(&reply, stats);
556 buf->data = (void *) reply.actions;
557 buf->size = reply.actions_len;
567 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
568 const struct nlattr *key, size_t key_len,
569 const struct nlattr *actions, size_t actions_len,
570 struct dpif_flow_stats *stats)
572 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
573 struct dpif_linux_flow request, reply;
577 dpif_linux_flow_init(&request);
578 request.cmd = flags & DPIF_FP_CREATE ? ODP_FLOW_NEW : ODP_FLOW_SET;
579 request.dp_idx = dpif->minor;
581 request.key_len = key_len;
582 request.actions = actions;
583 request.actions_len = actions_len;
584 if (flags & DPIF_FP_ZERO_STATS) {
585 request.clear = true;
587 request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
588 error = dpif_linux_flow_transact(&request,
589 stats ? &reply : NULL,
590 stats ? &buf : NULL);
591 if (!error && stats) {
592 dpif_linux_flow_get_stats(&reply, stats);
599 dpif_linux_flow_del(struct dpif *dpif_,
600 const struct nlattr *key, size_t key_len,
601 struct dpif_flow_stats *stats)
603 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
604 struct dpif_linux_flow request, reply;
608 dpif_linux_flow_init(&request);
609 request.cmd = ODP_FLOW_DEL;
610 request.dp_idx = dpif->minor;
612 request.key_len = key_len;
613 error = dpif_linux_flow_transact(&request,
614 stats ? &reply : NULL,
615 stats ? &buf : NULL);
616 if (!error && stats) {
617 dpif_linux_flow_get_stats(&reply, stats);
624 struct dpif_linux_flow_state {
625 struct dpif_linux_flow flow;
627 struct dpif_flow_stats stats;
631 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
633 *statep = xzalloc(sizeof(struct dpif_linux_flow_state));
638 dpif_linux_flow_dump_next(const struct dpif *dpif_, void *state_,
639 const struct nlattr **key, size_t *key_len,
640 const struct nlattr **actions, size_t *actions_len,
641 const struct dpif_flow_stats **stats)
643 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
644 struct dpif_linux_flow_state *state = state_;
645 struct ofpbuf *old_buf = state->buf;
646 struct dpif_linux_flow request;
649 dpif_linux_flow_init(&request);
650 request.cmd = ODP_FLOW_DUMP;
651 request.dp_idx = dpif->minor;
652 request.state = state->flow.state;
653 error = dpif_linux_flow_transact(&request, &state->flow, &state->buf);
654 ofpbuf_delete(old_buf);
658 *key = state->flow.key;
659 *key_len = state->flow.key_len;
662 *actions = state->flow.actions;
663 *actions_len = state->flow.actions_len;
666 dpif_linux_flow_get_stats(&state->flow, &state->stats);
667 *stats = &state->stats;
670 return error == ENODEV ? EOF : error;
674 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
676 struct dpif_linux_flow_state *state = state_;
678 ofpbuf_delete(state->buf);
684 dpif_linux_execute(struct dpif *dpif_,
685 const struct nlattr *actions, size_t actions_len,
686 const struct ofpbuf *packet)
688 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
689 struct odp_upcall *execute;
693 buf = ofpbuf_new(128 + actions_len + packet->size);
695 ofpbuf_reserve(buf, sizeof *execute);
696 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, packet->data, packet->size);
697 nl_msg_put_unspec(buf, ODP_PACKET_ATTR_ACTIONS, actions, actions_len);
699 execute = ofpbuf_push_uninit(buf, sizeof *execute);
700 execute->dp_idx = dpif->minor;
701 execute->len = buf->size;
703 error = do_ioctl(dpif_, ODP_EXECUTE, buf->data);
710 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
712 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
716 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
718 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
722 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
723 uint32_t *probability)
725 struct dpif_linux_dp dp;
729 error = dpif_linux_dp_get(dpif_, &dp, &buf);
731 *probability = dp.sampling ? *dp.sampling : 0;
738 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
740 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
741 struct dpif_linux_dp dp;
743 dpif_linux_dp_init(&dp);
745 dp.dp_idx = dpif->minor;
746 dp.sampling = &probability;
747 return dpif_linux_dp_transact(&dp, NULL, NULL);
751 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
752 uint32_t queue_id, uint32_t *priority)
754 if (queue_id < 0xf000) {
755 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
763 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall)
765 static const struct nl_policy odp_packet_policy[] = {
766 /* Always present. */
767 [ODP_PACKET_ATTR_TYPE] = { .type = NL_A_U32 },
768 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
769 .min_len = ETH_HEADER_LEN },
770 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
772 /* _ODPL_ACTION_NR only. */
773 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
775 /* _ODPL_SFLOW_NR only. */
776 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
777 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
780 struct odp_packet *odp_packet = buf->data;
781 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
783 if (!nl_policy_parse(buf, sizeof *odp_packet, odp_packet_policy,
784 a, ARRAY_SIZE(odp_packet_policy))) {
788 memset(upcall, 0, sizeof *upcall);
789 upcall->type = nl_attr_get_u32(a[ODP_PACKET_ATTR_TYPE]);
790 upcall->packet = buf;
791 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
792 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
793 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
794 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
795 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
796 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
798 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
799 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
801 if (a[ODP_PACKET_ATTR_ACTIONS]) {
802 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
803 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
810 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
812 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
817 buf = ofpbuf_new(65536);
818 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
821 if (error != EAGAIN) {
822 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
823 dpif_name(dpif_), strerror(error));
825 } else if (retval >= sizeof(struct odp_packet)) {
826 struct odp_packet *odp_packet = buf->data;
829 if (odp_packet->len <= retval) {
830 error = parse_odp_packet(buf, upcall);
832 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
833 "from %"PRIu32" bytes to %d",
834 dpif_name(dpif_), odp_packet->len, retval);
837 } else if (!retval) {
838 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
841 VLOG_WARN_RL(&error_rl, "%s: discarding too-short message (%d bytes)",
842 dpif_name(dpif_), retval);
853 dpif_linux_recv_wait(struct dpif *dpif_)
855 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
856 poll_fd_wait(dpif->fd, POLLIN);
860 dpif_linux_recv_purge(struct dpif *dpif_)
862 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
865 /* This is somewhat bogus because it assumes that the following macros have
866 * fixed values, but it's going to go away later. */
867 #define DP_N_QUEUES 3
868 #define DP_MAX_QUEUE_LEN 100
869 for (i = 0; i < DP_N_QUEUES * DP_MAX_QUEUE_LEN; i++) {
870 /* Reading even 1 byte discards a whole datagram and saves time. */
873 if (read(dpif->fd, &buffer, 1) != 1) {
879 const struct dpif_class dpif_linux_class = {
883 dpif_linux_enumerate,
886 dpif_linux_get_all_names,
888 dpif_linux_get_stats,
889 dpif_linux_get_drop_frags,
890 dpif_linux_set_drop_frags,
893 dpif_linux_port_query_by_number,
894 dpif_linux_port_query_by_name,
895 dpif_linux_get_max_ports,
896 dpif_linux_port_dump_start,
897 dpif_linux_port_dump_next,
898 dpif_linux_port_dump_done,
899 dpif_linux_port_poll,
900 dpif_linux_port_poll_wait,
904 dpif_linux_flow_flush,
905 dpif_linux_flow_dump_start,
906 dpif_linux_flow_dump_next,
907 dpif_linux_flow_dump_done,
909 dpif_linux_recv_get_mask,
910 dpif_linux_recv_set_mask,
911 dpif_linux_get_sflow_probability,
912 dpif_linux_set_sflow_probability,
913 dpif_linux_queue_to_priority,
915 dpif_linux_recv_wait,
916 dpif_linux_recv_purge,
919 static int get_openvswitch_major(void);
920 static int get_major(const char *target);
923 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
925 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
926 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
930 dpif_linux_is_internal_device(const char *name)
932 struct dpif_linux_vport reply;
936 error = dpif_linux_vport_get(name, &reply, &buf);
939 } else if (error != ENODEV) {
940 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
941 name, strerror(error));
944 return reply.type == ODP_VPORT_TYPE_INTERNAL;
948 make_openvswitch_device(int minor, char **fnp)
950 const char dirname[] = "/dev/net";
958 major = get_openvswitch_major();
962 dev = makedev(major, minor);
964 sprintf(fn, "%s/dp%d", dirname, minor);
966 if (!S_ISCHR(s.st_mode)) {
967 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
969 } else if (s.st_rdev != dev) {
970 VLOG_WARN_RL(&error_rl,
971 "%s is device %u:%u but should be %u:%u, fixing",
972 fn, major(s.st_rdev), minor(s.st_rdev),
973 major(dev), minor(dev));
978 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
979 fn, strerror(errno));
982 } else if (errno == ENOENT) {
983 if (stat(dirname, &s)) {
984 if (errno == ENOENT) {
985 if (mkdir(dirname, 0755)) {
986 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
987 dirname, strerror(errno));
991 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
992 dirname, strerror(errno));
997 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
1001 /* The device needs to be created. */
1002 if (mknod(fn, S_IFCHR | 0700, dev)) {
1003 VLOG_WARN_RL(&error_rl,
1004 "%s: creating character device %u:%u failed (%s)",
1005 fn, major(dev), minor(dev), strerror(errno));
1014 /* Return the major device number of the Open vSwitch device. If it
1015 * cannot be determined, a negative errno is returned. */
1017 get_openvswitch_major(void)
1019 static int openvswitch_major = -1;
1020 if (openvswitch_major < 0) {
1021 openvswitch_major = get_major("openvswitch");
1023 return openvswitch_major;
1027 get_major(const char *target)
1029 const char fn[] = "/proc/devices";
1034 file = fopen(fn, "r");
1036 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
1040 for (ln = 1; fgets(line, sizeof line, file); ln++) {
1044 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
1045 /* Nothing to do. */
1046 } else if (!strncmp(line, "Block", 5)) {
1047 /* We only want character devices, so skip the rest of the file. */
1049 } else if (sscanf(line, "%d %63s", &major, name)) {
1050 if (!strcmp(name, target)) {
1055 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
1061 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
1066 open_minor(int minor, int *fdp)
1071 error = make_openvswitch_device(minor, &fn);
1076 *fdp = open(fn, O_RDONLY | O_NONBLOCK);
1079 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
1088 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1091 struct dpif_linux *dpif = dpif_;
1094 if (change->master_ifindex == dpif->local_ifindex
1095 && (change->nlmsg_type == RTM_NEWLINK
1096 || change->nlmsg_type == RTM_DELLINK))
1098 /* Our datapath changed, either adding a new port or deleting an
1100 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
1103 dpif->change_error = true;
1108 get_dp0_fd(int *dp0_fdp)
1110 static int dp0_fd = -1;
1115 error = open_minor(0, &fd);
1125 /* Parses the contents of 'buf', which contains a "struct odp_vport" followed
1126 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1127 * positive errno value.
1129 * 'vport' will contain pointers into 'buf', so the caller should not free
1130 * 'buf' while 'vport' is still in use. */
1132 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1133 const struct ofpbuf *buf)
1135 static const struct nl_policy odp_vport_policy[] = {
1136 [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1137 [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1138 [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1139 [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1140 .min_len = sizeof(struct rtnl_link_stats64),
1141 .max_len = sizeof(struct rtnl_link_stats64),
1143 [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1144 .min_len = ETH_ADDR_LEN,
1145 .max_len = ETH_ADDR_LEN,
1147 [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1148 [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1149 [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1150 [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1153 struct odp_vport *odp_vport;
1154 struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
1156 dpif_linux_vport_init(vport);
1158 if (!nl_policy_parse(buf, sizeof *odp_vport, odp_vport_policy,
1159 a, ARRAY_SIZE(odp_vport_policy))) {
1162 odp_vport = buf->data;
1164 vport->dp_idx = odp_vport->dp_idx;
1165 vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1166 vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1167 vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
1168 if (a[ODP_VPORT_ATTR_STATS]) {
1169 vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
1171 if (a[ODP_VPORT_ATTR_ADDRESS]) {
1172 vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
1174 if (a[ODP_VPORT_ATTR_MTU]) {
1175 vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
1177 if (a[ODP_VPORT_ATTR_OPTIONS]) {
1178 vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
1179 vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
1181 if (a[ODP_VPORT_ATTR_IFINDEX]) {
1182 vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
1184 if (a[ODP_VPORT_ATTR_IFLINK]) {
1185 vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
1190 /* Appends to 'buf' (which must initially be empty) a "struct odp_vport"
1191 * followed by Netlink attributes corresponding to 'vport'. */
1193 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1196 struct odp_vport *odp_vport;
1198 ofpbuf_reserve(buf, sizeof odp_vport);
1200 if (vport->port_no != UINT32_MAX) {
1201 nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1204 if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
1205 nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1209 nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1213 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1214 vport->stats, sizeof *vport->stats);
1217 if (vport->address) {
1218 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1219 vport->address, ETH_ADDR_LEN);
1223 nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1226 if (vport->options) {
1227 nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1228 vport->options, vport->options_len);
1231 if (vport->ifindex) {
1232 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1235 if (vport->iflink) {
1236 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1239 odp_vport = ofpbuf_push_uninit(buf, sizeof *odp_vport);
1240 odp_vport->dp_idx = vport->dp_idx;
1241 odp_vport->len = buf->size;
1242 odp_vport->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1245 /* Clears 'vport' to "empty" values. */
1247 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1249 memset(vport, 0, sizeof *vport);
1250 vport->dp_idx = UINT32_MAX;
1251 vport->port_no = UINT32_MAX;
1254 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1255 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1256 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1257 * result of the command is expected to be an odp_vport also, which is decoded
1258 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1259 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1261 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1262 struct dpif_linux_vport *reply,
1263 struct ofpbuf **bufp)
1265 struct ofpbuf *buf = NULL;
1269 assert((reply != NULL) == (bufp != NULL));
1271 error = get_dp0_fd(&fd);
1276 buf = ofpbuf_new(1024);
1277 dpif_linux_vport_to_ofpbuf(request, buf);
1279 error = ioctl(fd, request->cmd, buf->data) ? errno : 0;
1285 buf->size = ((struct odp_vport *) buf->data)->len;
1286 error = dpif_linux_vport_from_ofpbuf(reply, buf);
1299 memset(reply, 0, sizeof *reply);
1305 /* Obtains information about the kernel vport named 'name' and stores it into
1306 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1307 * longer needed ('reply' will contain pointers into '*bufp'). */
1309 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1310 struct ofpbuf **bufp)
1312 struct dpif_linux_vport request;
1314 dpif_linux_vport_init(&request);
1315 request.cmd = ODP_VPORT_GET;
1316 request.name = name;
1318 return dpif_linux_vport_transact(&request, reply, bufp);
1321 /* Parses the contents of 'buf', which contains a "struct odp_datapath"
1322 * followed by Netlink attributes, into 'dp'. Returns 0 if successful,
1323 * otherwise a positive errno value.
1325 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1326 * while 'dp' is still in use. */
1328 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1330 static const struct nl_policy odp_datapath_policy[] = {
1331 [ODP_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1332 [ODP_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1333 .min_len = sizeof(struct odp_stats),
1334 .max_len = sizeof(struct odp_stats),
1336 [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1337 [ODP_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1340 struct odp_datapath *odp_dp;
1341 struct nlattr *a[ARRAY_SIZE(odp_datapath_policy)];
1343 dpif_linux_dp_init(dp);
1345 if (!nl_policy_parse(buf, sizeof *odp_dp, odp_datapath_policy,
1346 a, ARRAY_SIZE(odp_datapath_policy))) {
1351 dp->dp_idx = odp_dp->dp_idx;
1352 dp->name = nl_attr_get_string(a[ODP_DP_ATTR_NAME]);
1353 if (a[ODP_DP_ATTR_STATS]) {
1354 /* Can't use structure assignment because Netlink doesn't ensure
1355 * sufficient alignment for 64-bit members. */
1356 memcpy(&dp->stats, nl_attr_get(a[ODP_DP_ATTR_STATS]),
1359 if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1360 dp->ipv4_frags = nl_attr_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1362 if (a[ODP_DP_ATTR_SAMPLING]) {
1363 dp->sampling = nl_attr_get(a[ODP_DP_ATTR_SAMPLING]);
1368 /* Appends to 'buf' (which must initially be empty) a "struct odp_datapath"
1369 * followed by Netlink attributes corresponding to 'dp'. */
1371 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1373 struct odp_datapath *odp_dp;
1375 ofpbuf_reserve(buf, sizeof odp_dp);
1378 nl_msg_put_string(buf, ODP_DP_ATTR_NAME, dp->name);
1381 /* Skip ODP_DP_ATTR_STATS since we never have a reason to serialize it. */
1383 if (dp->ipv4_frags) {
1384 nl_msg_put_u32(buf, ODP_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1388 nl_msg_put_u32(buf, ODP_DP_ATTR_SAMPLING, *dp->sampling);
1391 odp_dp = ofpbuf_push_uninit(buf, sizeof *odp_dp);
1392 odp_dp->dp_idx = dp->dp_idx;
1393 odp_dp->len = buf->size;
1394 odp_dp->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1397 /* Clears 'dp' to "empty" values. */
1399 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1401 memset(dp, 0, sizeof *dp);
1405 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1406 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1407 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1408 * result of the command is expected to be an odp_datapath also, which is
1409 * decoded and stored in '*reply' and '*bufp'. The caller must free '*bufp'
1410 * when the reply is no longer needed ('reply' will contain pointers into
1413 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1414 struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1416 struct ofpbuf *buf = NULL;
1420 assert((reply != NULL) == (bufp != NULL));
1422 error = get_dp0_fd(&fd);
1427 buf = ofpbuf_new(1024);
1428 dpif_linux_dp_to_ofpbuf(request, buf);
1430 error = ioctl(fd, request->cmd, buf->data) ? errno : 0;
1436 buf->size = ((struct odp_datapath *) buf->data)->len;
1437 error = dpif_linux_dp_from_ofpbuf(reply, buf);
1450 memset(reply, 0, sizeof *reply);
1456 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1457 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1458 * will contain pointers into '*bufp'). */
1460 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1461 struct ofpbuf **bufp)
1463 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1464 struct dpif_linux_dp request;
1466 dpif_linux_dp_init(&request);
1467 request.cmd = ODP_DP_GET;
1468 request.dp_idx = dpif->minor;
1470 return dpif_linux_dp_transact(&request, reply, bufp);
1473 /* Parses the contents of 'buf', which contains a "struct odp_flow" followed by
1474 * Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1475 * positive errno value.
1477 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1478 * while 'flow' is still in use. */
1480 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1481 const struct ofpbuf *buf)
1483 static const struct nl_policy odp_flow_policy[] = {
1484 [ODP_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1485 [ODP_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1486 [ODP_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1487 .min_len = sizeof(struct odp_flow_stats),
1488 .max_len = sizeof(struct odp_flow_stats),
1490 [ODP_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1491 [ODP_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1492 /* The kernel never uses ODP_FLOW_ATTR_CLEAR. */
1493 [ODP_FLOW_ATTR_STATE] = { .type = NL_A_U64, .optional = true },
1496 struct odp_flow *odp_flow;
1497 struct nlattr *a[ARRAY_SIZE(odp_flow_policy)];
1499 dpif_linux_flow_init(flow);
1501 if (!nl_policy_parse(buf, sizeof *odp_flow, odp_flow_policy,
1502 a, ARRAY_SIZE(odp_flow_policy))) {
1505 odp_flow = buf->data;
1507 flow->nlmsg_flags = odp_flow->nlmsg_flags;
1508 flow->dp_idx = odp_flow->dp_idx;
1509 flow->key = nl_attr_get(a[ODP_FLOW_ATTR_KEY]);
1510 flow->key_len = nl_attr_get_size(a[ODP_FLOW_ATTR_KEY]);
1511 if (a[ODP_FLOW_ATTR_ACTIONS]) {
1512 flow->actions = nl_attr_get(a[ODP_FLOW_ATTR_ACTIONS]);
1513 flow->actions_len = nl_attr_get_size(a[ODP_FLOW_ATTR_ACTIONS]);
1515 if (a[ODP_FLOW_ATTR_STATS]) {
1516 flow->stats = nl_attr_get(a[ODP_FLOW_ATTR_STATS]);
1518 if (a[ODP_FLOW_ATTR_TCP_FLAGS]) {
1519 flow->tcp_flags = nl_attr_get(a[ODP_FLOW_ATTR_TCP_FLAGS]);
1521 if (a[ODP_FLOW_ATTR_STATE]) {
1522 flow->state = nl_attr_get(a[ODP_FLOW_ATTR_STATE]);
1527 /* Appends to 'buf' (which must initially be empty) a "struct odp_flow"
1528 * followed by Netlink attributes corresponding to 'flow'. */
1530 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1533 struct odp_flow *odp_flow;
1535 ofpbuf_reserve(buf, sizeof odp_flow);
1537 if (flow->key_len) {
1538 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_KEY, flow->key, flow->key_len);
1541 if (flow->actions_len) {
1542 nl_msg_put_unspec(buf, ODP_FLOW_ATTR_ACTIONS,
1543 flow->actions, flow->actions_len);
1546 /* We never need to send these to the kernel. */
1547 assert(!flow->stats);
1548 assert(!flow->tcp_flags);
1549 assert(!flow->used);
1552 nl_msg_put_flag(buf, ODP_FLOW_ATTR_CLEAR);
1556 nl_msg_put_u64(buf, ODP_FLOW_ATTR_STATE,
1557 get_unaligned_u64(flow->state));
1560 odp_flow = ofpbuf_push_uninit(buf, sizeof *odp_flow);
1561 odp_flow->nlmsg_flags = flow->nlmsg_flags;
1562 odp_flow->dp_idx = flow->dp_idx;
1563 odp_flow->len = buf->size;
1564 odp_flow->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1567 /* Clears 'flow' to "empty" values. */
1569 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1571 memset(flow, 0, sizeof *flow);
1574 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1575 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1576 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1577 * result of the command is expected to be an odp_flow also, which is decoded
1578 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1579 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1581 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1582 struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1584 struct ofpbuf *buf = NULL;
1588 assert((reply != NULL) == (bufp != NULL));
1590 error = get_dp0_fd(&fd);
1595 buf = ofpbuf_new(1024);
1596 dpif_linux_flow_to_ofpbuf(request, buf);
1598 error = ioctl(fd, request->cmd, buf->data) ? errno : 0;
1604 buf->size = ((struct odp_flow *) buf->data)->len;
1605 error = dpif_linux_flow_from_ofpbuf(reply, buf);
1618 memset(reply, 0, sizeof *reply);
1625 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1626 struct dpif_flow_stats *stats)
1629 stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1630 stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1632 stats->n_packets = 0;
1635 stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1636 stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;