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"
53 VLOG_DEFINE_THIS_MODULE(dpif_linux);
55 /* Datapath interface for the openvswitch Linux kernel module. */
60 /* Used by dpif_linux_get_all_names(). */
64 /* Change notification. */
65 int local_ifindex; /* Ifindex of local port. */
66 struct shash changed_ports; /* Ports that have changed. */
67 struct rtnetlink_notifier port_notifier;
71 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
73 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
74 static int open_dpif(const struct dpif_linux_vport *local_vport,
76 static int get_openvswitch_major(void);
77 static int create_minor(const char *name, int minor);
78 static int open_minor(int minor, int *fdp);
79 static int make_openvswitch_device(int minor, char **fnp);
80 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
83 static struct dpif_linux *
84 dpif_linux_cast(const struct dpif *dpif)
86 dpif_assert_class(dpif, &dpif_linux_class);
87 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
91 dpif_linux_enumerate(struct svec *all_dps)
97 /* Check that the Open vSwitch module is loaded. */
98 major = get_openvswitch_major();
104 for (i = 0; i < ODP_MAX; i++) {
109 sprintf(devname, "dp%d", i);
110 retval = dpif_open(devname, "system", &dpif);
112 svec_add(all_dps, devname);
113 dpif_uninit(dpif, true);
114 } else if (retval != ENODEV && !error) {
122 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
123 bool create, struct dpif **dpifp)
125 struct dpif_linux_vport request, reply;
130 minor = !strncmp(name, "dp", 2)
131 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
134 error = create_minor(name, minor);
139 /* Scan for unused minor number. */
140 for (minor = 0; ; minor++) {
141 if (minor >= ODP_MAX) {
142 /* All datapath numbers in use. */
146 error = create_minor(name, minor);
149 } else if (error != EBUSY) {
156 dpif_linux_vport_init(&request);
157 request.cmd = ODP_VPORT_GET;
158 request.port_no = ODPP_LOCAL;
160 request.dp_idx = minor;
165 error = dpif_linux_vport_transact(&request, &reply, &buf);
168 } else if (reply.port_no != ODPP_LOCAL) {
169 /* This is an Open vSwitch device but not the local port. We
170 * intentionally support only using the name of the local port as the
171 * name of a datapath; otherwise, it would be too difficult to
172 * enumerate all the names of a datapath. */
175 error = open_dpif(&reply, dpifp);
183 open_dpif(const struct dpif_linux_vport *local_vport, struct dpif **dpifp)
185 int dp_idx = local_vport->dp_idx;
186 struct dpif_linux *dpif;
191 error = open_minor(dp_idx, &fd);
196 dpif = xmalloc(sizeof *dpif);
197 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
198 dpif_linux_port_changed, dpif);
203 name = xasprintf("dp%d", dp_idx);
204 dpif_init(&dpif->dpif, &dpif_linux_class, name, dp_idx, dp_idx);
208 dpif->local_ifname = xstrdup(local_vport->name);
209 dpif->local_ifindex = local_vport->ifindex;
210 dpif->minor = dp_idx;
211 shash_init(&dpif->changed_ports);
212 dpif->change_error = false;
213 *dpifp = &dpif->dpif;
225 dpif_linux_close(struct dpif *dpif_)
227 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
228 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
229 shash_destroy(&dpif->changed_ports);
230 free(dpif->local_ifname);
236 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
238 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
240 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
241 svec_add(all_names, dpif->local_ifname);
246 dpif_linux_destroy(struct dpif *dpif_)
248 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
252 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
254 memset(stats, 0, sizeof *stats);
255 return do_ioctl(dpif_, ODP_DP_STATS, stats);
259 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
264 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
266 *drop_fragsp = drop_frags & 1;
272 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
274 int drop_frags_int = drop_frags;
275 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
279 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
282 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
283 const char *name = netdev_get_name(netdev);
284 const char *type = netdev_get_type(netdev);
285 struct dpif_linux_vport request, reply;
286 const struct ofpbuf *options;
290 dpif_linux_vport_init(&request);
291 request.cmd = ODP_VPORT_NEW;
292 request.dp_idx = dpif->minor;
293 request.type = netdev_vport_get_vport_type(netdev);
294 if (request.type == ODP_VPORT_TYPE_UNSPEC) {
295 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
296 "unsupported type `%s'",
297 dpif_name(dpif_), name, type);
302 options = netdev_vport_get_options(netdev);
303 if (options && options->size) {
304 request.options = options->data;
305 request.options_len = options->size;
308 error = dpif_linux_vport_transact(&request, &reply, &buf);
310 *port_nop = reply.port_no;
318 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
320 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
321 struct dpif_linux_vport vport;
323 dpif_linux_vport_init(&vport);
324 vport.cmd = ODP_VPORT_DEL;
325 vport.dp_idx = dpif->minor;
326 vport.port_no = port_no;
327 return dpif_linux_vport_transact(&vport, NULL, NULL);
331 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
332 const char *port_name, struct dpif_port *dpif_port)
334 struct dpif_linux_vport request;
335 struct dpif_linux_vport reply;
339 dpif_linux_vport_init(&request);
340 request.cmd = ODP_VPORT_GET;
341 request.dp_idx = dpif_linux_cast(dpif)->minor;
342 request.port_no = port_no;
343 request.name = port_name;
345 error = dpif_linux_vport_transact(&request, &reply, &buf);
347 dpif_port->name = xstrdup(reply.name);
348 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
349 dpif_port->port_no = reply.port_no;
356 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
357 struct dpif_port *dpif_port)
359 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
363 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
364 struct dpif_port *dpif_port)
366 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
370 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
372 /* If the datapath increases its range of supported ports, then it should
373 * start reporting that. */
378 dpif_linux_flow_flush(struct dpif *dpif_)
380 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
383 struct dpif_linux_port_state {
389 dpif_linux_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
391 *statep = xzalloc(sizeof(struct dpif_linux_port_state));
396 dpif_linux_port_dump_next(const struct dpif *dpif, void *state_,
397 struct dpif_port *dpif_port)
399 struct dpif_linux_port_state *state = state_;
400 struct dpif_linux_vport request, reply;
404 ofpbuf_delete(state->buf);
407 dpif_linux_vport_init(&request);
408 request.cmd = ODP_VPORT_DUMP;
409 request.dp_idx = dpif_linux_cast(dpif)->minor;
410 request.port_no = state->next;
411 error = dpif_linux_vport_transact(&request, &reply, &buf);
413 return error == ENODEV ? EOF : error;
415 dpif_port->name = (char *) reply.name;
416 dpif_port->type = (char *) netdev_vport_get_netdev_type(&reply);
417 dpif_port->port_no = reply.port_no;
419 state->next = reply.port_no + 1;
425 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
427 struct dpif_linux_port_state *state = state_;
428 ofpbuf_delete(state->buf);
434 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
436 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
438 if (dpif->change_error) {
439 dpif->change_error = false;
440 shash_clear(&dpif->changed_ports);
442 } else if (!shash_is_empty(&dpif->changed_ports)) {
443 struct shash_node *node = shash_first(&dpif->changed_ports);
444 *devnamep = shash_steal(&dpif->changed_ports, node);
452 dpif_linux_port_poll_wait(const struct dpif *dpif_)
454 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
455 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
456 poll_immediate_wake();
458 rtnetlink_link_notifier_wait();
463 odp_flow_stats_to_dpif_flow_stats(const struct odp_flow_stats *ofs,
464 struct dpif_flow_stats *dfs)
466 dfs->n_packets = ofs->n_packets;
467 dfs->n_bytes = ofs->n_bytes;
468 dfs->used = ofs->used_sec * 1000 + ofs->used_nsec / 1000000;
469 dfs->tcp_flags = ofs->tcp_flags;
473 dpif_linux_flow_get(const struct dpif *dpif_, int flags,
474 const struct nlattr *key, size_t key_len,
475 struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
477 struct ofpbuf *actions = NULL;
478 struct odp_flow odp_flow;
481 memset(&odp_flow, 0, sizeof odp_flow);
482 odp_flow.key = (struct nlattr *) key;
483 odp_flow.key_len = key_len;
485 actions = *actionsp = ofpbuf_new(65536);
486 odp_flow.actions = actions->base;
487 odp_flow.actions_len = actions->allocated;
489 odp_flow.flags = flags;
491 error = do_ioctl(dpif_, ODP_FLOW_GET, &odp_flow);
494 odp_flow_stats_to_dpif_flow_stats(&odp_flow.stats, stats);
497 actions->size = odp_flow.actions_len;
498 ofpbuf_trim(actions);
502 ofpbuf_delete(actions);
509 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
510 const struct nlattr *key, size_t key_len,
511 const struct nlattr *actions, size_t actions_len,
512 struct dpif_flow_stats *stats)
514 struct odp_flow_put put;
517 memset(&put, 0, sizeof put);
518 put.flow.key = (struct nlattr *) key;
519 put.flow.key_len = key_len;
520 put.flow.actions = (struct nlattr *) actions;
521 put.flow.actions_len = actions_len;
524 if (flags & DPIF_FP_CREATE) {
525 put.flags |= ODPPF_CREATE;
527 if (flags & DPIF_FP_MODIFY) {
528 put.flags |= ODPPF_MODIFY;
530 if (flags & DPIF_FP_ZERO_STATS) {
531 put.flags |= ODPPF_ZERO_STATS;
533 error = do_ioctl(dpif_, ODP_FLOW_PUT, &put);
534 if (!error && stats) {
535 odp_flow_stats_to_dpif_flow_stats(&put.flow.stats, stats);
541 dpif_linux_flow_del(struct dpif *dpif_,
542 const struct nlattr *key, size_t key_len,
543 struct dpif_flow_stats *stats)
545 struct odp_flow odp_flow;
548 memset(&odp_flow, 0, sizeof odp_flow);
549 odp_flow.key = (struct nlattr *) key;
550 odp_flow.key_len = key_len;
551 error = do_ioctl(dpif_, ODP_FLOW_DEL, &odp_flow);
552 if (!error && stats) {
553 odp_flow_stats_to_dpif_flow_stats(&odp_flow.stats, stats);
558 struct dpif_linux_flow_state {
559 struct odp_flow_dump dump;
560 struct odp_flow flow;
561 uint32_t keybuf[ODPUTIL_FLOW_KEY_U32S];
562 uint32_t actionsbuf[65536 / sizeof(uint32_t)];
563 struct dpif_flow_stats stats;
567 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
569 struct dpif_linux_flow_state *state;
571 *statep = state = xmalloc(sizeof *state);
572 state->dump.state[0] = 0;
573 state->dump.state[1] = 0;
574 state->dump.flow = &state->flow;
579 dpif_linux_flow_dump_next(const struct dpif *dpif, void *state_,
580 const struct nlattr **key, size_t *key_len,
581 const struct nlattr **actions, size_t *actions_len,
582 const struct dpif_flow_stats **stats)
584 struct dpif_linux_flow_state *state = state_;
587 memset(&state->flow, 0, sizeof state->flow);
588 state->flow.key = (struct nlattr *) state->keybuf;
589 state->flow.key_len = sizeof state->keybuf;
591 state->flow.actions = (struct nlattr *) state->actionsbuf;
592 state->flow.actions_len = sizeof state->actionsbuf;
595 error = do_ioctl(dpif, ODP_FLOW_DUMP, &state->dump);
597 if (state->flow.flags & ODPFF_EOF) {
601 *key = (const struct nlattr *) state->keybuf;
602 *key_len = state->flow.key_len;
605 *actions = (const struct nlattr *) state->actionsbuf;
606 *actions_len = state->flow.actions_len;
609 odp_flow_stats_to_dpif_flow_stats(&state->flow.stats,
611 *stats = &state->stats;
618 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
625 dpif_linux_execute(struct dpif *dpif_,
626 const struct nlattr *actions, size_t actions_len,
627 const struct ofpbuf *buf)
629 struct odp_execute execute;
630 memset(&execute, 0, sizeof execute);
631 execute.actions = (struct nlattr *) actions;
632 execute.actions_len = actions_len;
633 execute.data = buf->data;
634 execute.length = buf->size;
635 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
639 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
641 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
645 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
647 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
651 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
652 uint32_t *probability)
654 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
658 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
660 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
664 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
665 uint32_t queue_id, uint32_t *priority)
667 if (queue_id < 0xf000) {
668 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
676 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall)
678 static const struct nl_policy odp_packet_policy[] = {
679 /* Always present. */
680 [ODP_PACKET_ATTR_TYPE] = { .type = NL_A_U32 },
681 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
682 .min_len = ETH_HEADER_LEN },
683 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
685 /* _ODPL_ACTION_NR only. */
686 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
688 /* _ODPL_SFLOW_NR only. */
689 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
690 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
693 struct odp_packet *odp_packet = buf->data;
694 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
696 if (!nl_policy_parse(buf, sizeof *odp_packet, odp_packet_policy,
697 a, ARRAY_SIZE(odp_packet_policy))) {
701 memset(upcall, 0, sizeof *upcall);
702 upcall->type = nl_attr_get_u32(a[ODP_PACKET_ATTR_TYPE]);
703 upcall->packet = buf;
704 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
705 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
706 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
707 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
708 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
709 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
711 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
712 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
714 if (a[ODP_PACKET_ATTR_ACTIONS]) {
715 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
716 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
723 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
725 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
730 buf = ofpbuf_new(65536);
731 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
734 if (error != EAGAIN) {
735 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
736 dpif_name(dpif_), strerror(error));
738 } else if (retval >= sizeof(struct odp_packet)) {
739 struct odp_packet *odp_packet = buf->data;
742 if (odp_packet->len <= retval) {
743 error = parse_odp_packet(buf, upcall);
745 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
746 "from %"PRIu32" bytes to %d",
747 dpif_name(dpif_), odp_packet->len, retval);
750 } else if (!retval) {
751 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
754 VLOG_WARN_RL(&error_rl, "%s: discarding too-short message (%d bytes)",
755 dpif_name(dpif_), retval);
766 dpif_linux_recv_wait(struct dpif *dpif_)
768 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
769 poll_fd_wait(dpif->fd, POLLIN);
773 dpif_linux_recv_purge(struct dpif *dpif_)
775 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
778 /* This is somewhat bogus because it assumes that the following macros have
779 * fixed values, but it's going to go away later. */
780 #define DP_N_QUEUES 3
781 #define DP_MAX_QUEUE_LEN 100
782 for (i = 0; i < DP_N_QUEUES * DP_MAX_QUEUE_LEN; i++) {
783 /* Reading even 1 byte discards a whole datagram and saves time. */
786 if (read(dpif->fd, &buffer, 1) != 1) {
792 const struct dpif_class dpif_linux_class = {
796 dpif_linux_enumerate,
799 dpif_linux_get_all_names,
801 dpif_linux_get_stats,
802 dpif_linux_get_drop_frags,
803 dpif_linux_set_drop_frags,
806 dpif_linux_port_query_by_number,
807 dpif_linux_port_query_by_name,
808 dpif_linux_get_max_ports,
809 dpif_linux_port_dump_start,
810 dpif_linux_port_dump_next,
811 dpif_linux_port_dump_done,
812 dpif_linux_port_poll,
813 dpif_linux_port_poll_wait,
817 dpif_linux_flow_flush,
818 dpif_linux_flow_dump_start,
819 dpif_linux_flow_dump_next,
820 dpif_linux_flow_dump_done,
822 dpif_linux_recv_get_mask,
823 dpif_linux_recv_set_mask,
824 dpif_linux_get_sflow_probability,
825 dpif_linux_set_sflow_probability,
826 dpif_linux_queue_to_priority,
828 dpif_linux_recv_wait,
829 dpif_linux_recv_purge,
832 static int get_openvswitch_major(void);
833 static int get_major(const char *target);
836 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
838 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
839 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
843 dpif_linux_is_internal_device(const char *name)
845 struct dpif_linux_vport reply;
849 error = dpif_linux_vport_get(name, &reply, &buf);
852 } else if (error != ENODEV) {
853 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
854 name, strerror(error));
857 return reply.type == ODP_VPORT_TYPE_INTERNAL;
861 make_openvswitch_device(int minor, char **fnp)
863 const char dirname[] = "/dev/net";
871 major = get_openvswitch_major();
875 dev = makedev(major, minor);
877 sprintf(fn, "%s/dp%d", dirname, minor);
879 if (!S_ISCHR(s.st_mode)) {
880 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
882 } else if (s.st_rdev != dev) {
883 VLOG_WARN_RL(&error_rl,
884 "%s is device %u:%u but should be %u:%u, fixing",
885 fn, major(s.st_rdev), minor(s.st_rdev),
886 major(dev), minor(dev));
891 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
892 fn, strerror(errno));
895 } else if (errno == ENOENT) {
896 if (stat(dirname, &s)) {
897 if (errno == ENOENT) {
898 if (mkdir(dirname, 0755)) {
899 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
900 dirname, strerror(errno));
904 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
905 dirname, strerror(errno));
910 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
914 /* The device needs to be created. */
915 if (mknod(fn, S_IFCHR | 0700, dev)) {
916 VLOG_WARN_RL(&error_rl,
917 "%s: creating character device %u:%u failed (%s)",
918 fn, major(dev), minor(dev), strerror(errno));
927 /* Return the major device number of the Open vSwitch device. If it
928 * cannot be determined, a negative errno is returned. */
930 get_openvswitch_major(void)
932 static int openvswitch_major = -1;
933 if (openvswitch_major < 0) {
934 openvswitch_major = get_major("openvswitch");
936 return openvswitch_major;
940 get_major(const char *target)
942 const char fn[] = "/proc/devices";
947 file = fopen(fn, "r");
949 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
953 for (ln = 1; fgets(line, sizeof line, file); ln++) {
957 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
959 } else if (!strncmp(line, "Block", 5)) {
960 /* We only want character devices, so skip the rest of the file. */
962 } else if (sscanf(line, "%d %63s", &major, name)) {
963 if (!strcmp(name, target)) {
968 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
974 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
979 create_minor(const char *name, int minor)
984 error = open_minor(minor, &fd);
989 error = ioctl(fd, ODP_DP_CREATE, name) ? errno : 0;
995 open_minor(int minor, int *fdp)
1000 error = make_openvswitch_device(minor, &fn);
1005 *fdp = open(fn, O_RDONLY | O_NONBLOCK);
1008 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
1017 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1020 struct dpif_linux *dpif = dpif_;
1023 if (change->master_ifindex == dpif->local_ifindex
1024 && (change->nlmsg_type == RTM_NEWLINK
1025 || change->nlmsg_type == RTM_DELLINK))
1027 /* Our datapath changed, either adding a new port or deleting an
1029 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
1032 dpif->change_error = true;
1036 /* Parses the contents of 'buf', which contains a "struct odp_vport" followed
1037 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1038 * positive errno value.
1040 * 'vport' will contain pointers into 'buf', so the caller should not free
1041 * 'buf' while 'vport' is still in use. */
1043 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1044 const struct ofpbuf *buf)
1046 static const struct nl_policy odp_vport_policy[] = {
1047 [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1048 [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1049 [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1050 [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1051 .min_len = sizeof(struct rtnl_link_stats64),
1052 .max_len = sizeof(struct rtnl_link_stats64),
1054 [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1055 .min_len = ETH_ADDR_LEN,
1056 .max_len = ETH_ADDR_LEN,
1058 [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1059 [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1060 [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1061 [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1064 struct odp_vport *odp_vport;
1065 struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
1067 dpif_linux_vport_init(vport);
1069 if (!nl_policy_parse(buf, sizeof *odp_vport, odp_vport_policy,
1070 a, ARRAY_SIZE(odp_vport_policy))) {
1073 odp_vport = buf->data;
1075 vport->dp_idx = odp_vport->dp_idx;
1076 vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1077 vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1078 vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
1079 if (a[ODP_VPORT_ATTR_STATS]) {
1080 vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
1082 if (a[ODP_VPORT_ATTR_ADDRESS]) {
1083 vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
1085 if (a[ODP_VPORT_ATTR_MTU]) {
1086 vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
1088 if (a[ODP_VPORT_ATTR_OPTIONS]) {
1089 vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
1090 vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
1092 if (a[ODP_VPORT_ATTR_IFINDEX]) {
1093 vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
1095 if (a[ODP_VPORT_ATTR_IFLINK]) {
1096 vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
1101 /* Appends to 'buf' (which must initially be empty) a "struct odp_vport"
1102 * followed by Netlink attributes corresponding to 'vport'. */
1104 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1107 struct odp_vport *odp_vport;
1109 ofpbuf_reserve(buf, sizeof odp_vport);
1111 if (vport->port_no != UINT32_MAX) {
1112 nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1115 if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
1116 nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1120 nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1124 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1125 vport->stats, sizeof *vport->stats);
1128 if (vport->address) {
1129 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1130 vport->address, ETH_ADDR_LEN);
1134 nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1137 if (vport->options) {
1138 nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1139 vport->options, vport->options_len);
1142 if (vport->ifindex) {
1143 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1146 if (vport->iflink) {
1147 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1150 odp_vport = ofpbuf_push_uninit(buf, sizeof *odp_vport);
1151 odp_vport->dp_idx = vport->dp_idx;
1152 odp_vport->len = buf->size;
1153 odp_vport->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1156 /* Clears 'vport' to "empty" values. */
1158 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1160 memset(vport, 0, sizeof *vport);
1161 vport->dp_idx = UINT32_MAX;
1162 vport->port_no = UINT32_MAX;
1165 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1166 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1167 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1168 * result of the command is expected to be an odp_vport also, which is decoded
1169 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1170 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1172 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1173 struct dpif_linux_vport *reply,
1174 struct ofpbuf **bufp)
1176 static int dp0_fd = -1;
1177 struct ofpbuf *buf = NULL;
1180 assert((reply != NULL) == (bufp != NULL));
1184 error = open_minor(0, &fd);
1191 buf = ofpbuf_new(1024);
1192 dpif_linux_vport_to_ofpbuf(request, buf);
1194 error = ioctl(dp0_fd, request->cmd, buf->data) ? errno : 0;
1200 buf->size = ((struct odp_vport *) buf->data)->len;
1201 error = dpif_linux_vport_from_ofpbuf(reply, buf);
1214 memset(reply, 0, sizeof *reply);
1220 /* Obtains information about the kernel vport named 'name' and stores it into
1221 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1222 * longer needed ('reply' will contain pointers into '*bufp'). */
1224 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1225 struct ofpbuf **bufp)
1227 struct dpif_linux_vport request;
1229 dpif_linux_vport_init(&request);
1230 request.cmd = ODP_VPORT_GET;
1231 request.name = name;
1233 return dpif_linux_vport_transact(&request, reply, bufp);