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"
42 #include "openvswitch/tunnel.h"
44 #include "poll-loop.h"
45 #include "rtnetlink.h"
46 #include "rtnetlink-link.h"
52 VLOG_DEFINE_THIS_MODULE(dpif_linux);
54 /* Datapath interface for the openvswitch Linux kernel module. */
59 /* Used by dpif_linux_get_all_names(). */
63 /* Change notification. */
64 int local_ifindex; /* Ifindex of local port. */
65 struct shash changed_ports; /* Ports that have changed. */
66 struct rtnetlink_notifier port_notifier;
70 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
72 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
73 static int open_dpif(const struct dpif_linux_vport *local_vport,
75 static int get_openvswitch_major(void);
76 static int create_minor(const char *name, int minor);
77 static int open_minor(int minor, int *fdp);
78 static int make_openvswitch_device(int minor, char **fnp);
79 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
82 static struct dpif_linux *
83 dpif_linux_cast(const struct dpif *dpif)
85 dpif_assert_class(dpif, &dpif_linux_class);
86 return CONTAINER_OF(dpif, struct dpif_linux, dpif);
90 dpif_linux_enumerate(struct svec *all_dps)
96 /* Check that the Open vSwitch module is loaded. */
97 major = get_openvswitch_major();
103 for (i = 0; i < ODP_MAX; i++) {
108 sprintf(devname, "dp%d", i);
109 retval = dpif_open(devname, "system", &dpif);
111 svec_add(all_dps, devname);
112 dpif_uninit(dpif, true);
113 } else if (retval != ENODEV && !error) {
121 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
122 bool create, struct dpif **dpifp)
124 struct dpif_linux_vport request, reply;
129 minor = !strncmp(name, "dp", 2)
130 && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
133 error = create_minor(name, minor);
138 /* Scan for unused minor number. */
139 for (minor = 0; ; minor++) {
140 if (minor >= ODP_MAX) {
141 /* All datapath numbers in use. */
145 error = create_minor(name, minor);
148 } else if (error != EBUSY) {
155 dpif_linux_vport_init(&request);
156 request.cmd = ODP_VPORT_GET;
157 request.port_no = ODPP_LOCAL;
159 request.dp_idx = minor;
164 error = dpif_linux_vport_transact(&request, &reply, &buf);
167 } else if (reply.port_no != ODPP_LOCAL) {
168 /* This is an Open vSwitch device but not the local port. We
169 * intentionally support only using the name of the local port as the
170 * name of a datapath; otherwise, it would be too difficult to
171 * enumerate all the names of a datapath. */
174 error = open_dpif(&reply, dpifp);
182 open_dpif(const struct dpif_linux_vport *local_vport, struct dpif **dpifp)
184 int dp_idx = local_vport->dp_idx;
185 struct dpif_linux *dpif;
190 error = open_minor(dp_idx, &fd);
195 dpif = xmalloc(sizeof *dpif);
196 error = rtnetlink_link_notifier_register(&dpif->port_notifier,
197 dpif_linux_port_changed, dpif);
202 name = xasprintf("dp%d", dp_idx);
203 dpif_init(&dpif->dpif, &dpif_linux_class, name, dp_idx, dp_idx);
207 dpif->local_ifname = xstrdup(local_vport->name);
208 dpif->local_ifindex = local_vport->ifindex;
209 dpif->minor = dp_idx;
210 shash_init(&dpif->changed_ports);
211 dpif->change_error = false;
212 *dpifp = &dpif->dpif;
224 dpif_linux_close(struct dpif *dpif_)
226 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
227 rtnetlink_link_notifier_unregister(&dpif->port_notifier);
228 shash_destroy(&dpif->changed_ports);
229 free(dpif->local_ifname);
235 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
237 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
239 svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
240 svec_add(all_names, dpif->local_ifname);
245 dpif_linux_destroy(struct dpif *dpif_)
247 return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
251 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
253 memset(stats, 0, sizeof *stats);
254 return do_ioctl(dpif_, ODP_DP_STATS, stats);
258 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
263 error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
265 *drop_fragsp = drop_frags & 1;
271 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
273 int drop_frags_int = drop_frags;
274 return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
278 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
281 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
282 const char *name = netdev_get_name(netdev);
283 const char *type = netdev_get_type(netdev);
284 struct dpif_linux_vport request, reply;
285 const struct ofpbuf *options;
289 dpif_linux_vport_init(&request);
290 request.cmd = ODP_VPORT_NEW;
291 request.dp_idx = dpif->minor;
292 request.type = netdev_vport_get_vport_type(netdev);
293 if (request.type == ODP_VPORT_TYPE_UNSPEC) {
294 VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
295 "unsupported type `%s'",
296 dpif_name(dpif_), name, type);
301 options = netdev_vport_get_options(netdev);
302 if (options && options->size) {
303 request.options = options->data;
304 request.options_len = options->size;
307 error = dpif_linux_vport_transact(&request, &reply, &buf);
309 *port_nop = reply.port_no;
317 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
319 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
320 struct dpif_linux_vport vport;
322 dpif_linux_vport_init(&vport);
323 vport.cmd = ODP_VPORT_DEL;
324 vport.dp_idx = dpif->minor;
325 vport.port_no = port_no;
326 return dpif_linux_vport_transact(&vport, NULL, NULL);
330 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
331 const char *port_name, struct dpif_port *dpif_port)
333 struct dpif_linux_vport request;
334 struct dpif_linux_vport reply;
338 dpif_linux_vport_init(&request);
339 request.cmd = ODP_VPORT_GET;
340 request.dp_idx = dpif_linux_cast(dpif)->minor;
341 request.port_no = port_no;
342 request.name = port_name;
344 error = dpif_linux_vport_transact(&request, &reply, &buf);
346 dpif_port->name = xstrdup(reply.name);
347 dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
348 dpif_port->port_no = reply.port_no;
355 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
356 struct dpif_port *dpif_port)
358 return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
362 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
363 struct dpif_port *dpif_port)
365 return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
369 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
371 /* If the datapath increases its range of supported ports, then it should
372 * start reporting that. */
377 dpif_linux_flow_flush(struct dpif *dpif_)
379 return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
382 struct dpif_linux_port_state {
388 dpif_linux_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
390 *statep = xzalloc(sizeof(struct dpif_linux_port_state));
395 dpif_linux_port_dump_next(const struct dpif *dpif, void *state_,
396 struct dpif_port *dpif_port)
398 struct dpif_linux_port_state *state = state_;
399 struct dpif_linux_vport request, reply;
403 ofpbuf_delete(state->buf);
406 dpif_linux_vport_init(&request);
407 request.cmd = ODP_VPORT_DUMP;
408 request.dp_idx = dpif_linux_cast(dpif)->minor;
409 request.port_no = state->next;
410 error = dpif_linux_vport_transact(&request, &reply, &buf);
412 return error == ENODEV ? EOF : error;
414 dpif_port->name = (char *) reply.name;
415 dpif_port->type = (char *) netdev_vport_get_netdev_type(&reply);
416 dpif_port->port_no = reply.port_no;
418 state->next = reply.port_no + 1;
424 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
426 struct dpif_linux_port_state *state = state_;
427 ofpbuf_delete(state->buf);
433 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
435 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
437 if (dpif->change_error) {
438 dpif->change_error = false;
439 shash_clear(&dpif->changed_ports);
441 } else if (!shash_is_empty(&dpif->changed_ports)) {
442 struct shash_node *node = shash_first(&dpif->changed_ports);
443 *devnamep = shash_steal(&dpif->changed_ports, node);
451 dpif_linux_port_poll_wait(const struct dpif *dpif_)
453 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
454 if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
455 poll_immediate_wake();
457 rtnetlink_link_notifier_wait();
462 dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow *flow)
464 return do_ioctl(dpif_, ODP_FLOW_GET, flow);
468 dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
470 return do_ioctl(dpif_, ODP_FLOW_PUT, put);
474 dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
476 return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
480 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
482 *statep = xzalloc(sizeof(struct odp_flow_dump));
487 dpif_linux_flow_dump_next(const struct dpif *dpif, void *state,
488 struct odp_flow *flow)
490 struct odp_flow_dump *dump = state;
494 error = do_ioctl(dpif, ODP_FLOW_DUMP, dump);
495 return error ? error : flow->flags & ODPFF_EOF ? EOF : 0;
499 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
506 dpif_linux_execute(struct dpif *dpif_,
507 const struct nlattr *actions, size_t actions_len,
508 const struct ofpbuf *buf)
510 struct odp_execute execute;
511 memset(&execute, 0, sizeof execute);
512 execute.actions = (struct nlattr *) actions;
513 execute.actions_len = actions_len;
514 execute.data = buf->data;
515 execute.length = buf->size;
516 return do_ioctl(dpif_, ODP_EXECUTE, &execute);
520 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
522 return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
526 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
528 return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
532 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
533 uint32_t *probability)
535 return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
539 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
541 return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
545 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
546 uint32_t queue_id, uint32_t *priority)
548 if (queue_id < 0xf000) {
549 *priority = TC_H_MAKE(1 << 16, queue_id + 1);
557 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall)
559 static const struct nl_policy odp_packet_policy[] = {
560 /* Always present. */
561 [ODP_PACKET_ATTR_TYPE] = { .type = NL_A_U32 },
562 [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
563 .min_len = ETH_HEADER_LEN },
564 [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
566 /* _ODPL_ACTION_NR only. */
567 [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
569 /* _ODPL_SFLOW_NR only. */
570 [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
571 [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
574 struct odp_packet *odp_packet = buf->data;
575 struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
577 if (!nl_policy_parse(buf, sizeof *odp_packet, odp_packet_policy,
578 a, ARRAY_SIZE(odp_packet_policy))) {
582 memset(upcall, 0, sizeof *upcall);
583 upcall->type = nl_attr_get_u32(a[ODP_PACKET_ATTR_TYPE]);
584 upcall->packet = buf;
585 upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
586 upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
587 upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
588 upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
589 upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
590 ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
592 upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
593 ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
595 if (a[ODP_PACKET_ATTR_ACTIONS]) {
596 upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
597 upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
604 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
606 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
611 buf = ofpbuf_new(65536);
612 retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
615 if (error != EAGAIN) {
616 VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
617 dpif_name(dpif_), strerror(error));
619 } else if (retval >= sizeof(struct odp_packet)) {
620 struct odp_packet *odp_packet = buf->data;
623 if (odp_packet->len <= retval) {
624 error = parse_odp_packet(buf, upcall);
626 VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
627 "from %"PRIu32" bytes to %d",
628 dpif_name(dpif_), odp_packet->len, retval);
631 } else if (!retval) {
632 VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
635 VLOG_WARN_RL(&error_rl, "%s: discarding too-short message (%d bytes)",
636 dpif_name(dpif_), retval);
647 dpif_linux_recv_wait(struct dpif *dpif_)
649 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
650 poll_fd_wait(dpif->fd, POLLIN);
654 dpif_linux_recv_purge(struct dpif *dpif_)
656 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
659 /* This is somewhat bogus because it assumes that the following macros have
660 * fixed values, but it's going to go away later. */
661 #define DP_N_QUEUES 3
662 #define DP_MAX_QUEUE_LEN 100
663 for (i = 0; i < DP_N_QUEUES * DP_MAX_QUEUE_LEN; i++) {
664 /* Reading even 1 byte discards a whole datagram and saves time. */
667 if (read(dpif->fd, &buffer, 1) != 1) {
673 const struct dpif_class dpif_linux_class = {
677 dpif_linux_enumerate,
680 dpif_linux_get_all_names,
682 dpif_linux_get_stats,
683 dpif_linux_get_drop_frags,
684 dpif_linux_set_drop_frags,
687 dpif_linux_port_query_by_number,
688 dpif_linux_port_query_by_name,
689 dpif_linux_get_max_ports,
690 dpif_linux_port_dump_start,
691 dpif_linux_port_dump_next,
692 dpif_linux_port_dump_done,
693 dpif_linux_port_poll,
694 dpif_linux_port_poll_wait,
698 dpif_linux_flow_flush,
699 dpif_linux_flow_dump_start,
700 dpif_linux_flow_dump_next,
701 dpif_linux_flow_dump_done,
703 dpif_linux_recv_get_mask,
704 dpif_linux_recv_set_mask,
705 dpif_linux_get_sflow_probability,
706 dpif_linux_set_sflow_probability,
707 dpif_linux_queue_to_priority,
709 dpif_linux_recv_wait,
710 dpif_linux_recv_purge,
713 static int get_openvswitch_major(void);
714 static int get_major(const char *target);
717 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
719 struct dpif_linux *dpif = dpif_linux_cast(dpif_);
720 return ioctl(dpif->fd, cmd, arg) ? errno : 0;
724 dpif_linux_is_internal_device(const char *name)
726 struct dpif_linux_vport reply;
730 error = dpif_linux_vport_get(name, &reply, &buf);
733 } else if (error != ENODEV) {
734 VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
735 name, strerror(error));
738 return reply.type == ODP_VPORT_TYPE_INTERNAL;
742 make_openvswitch_device(int minor, char **fnp)
744 const char dirname[] = "/dev/net";
752 major = get_openvswitch_major();
756 dev = makedev(major, minor);
758 sprintf(fn, "%s/dp%d", dirname, minor);
760 if (!S_ISCHR(s.st_mode)) {
761 VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
763 } else if (s.st_rdev != dev) {
764 VLOG_WARN_RL(&error_rl,
765 "%s is device %u:%u but should be %u:%u, fixing",
766 fn, major(s.st_rdev), minor(s.st_rdev),
767 major(dev), minor(dev));
772 VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
773 fn, strerror(errno));
776 } else if (errno == ENOENT) {
777 if (stat(dirname, &s)) {
778 if (errno == ENOENT) {
779 if (mkdir(dirname, 0755)) {
780 VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
781 dirname, strerror(errno));
785 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
786 dirname, strerror(errno));
791 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
795 /* The device needs to be created. */
796 if (mknod(fn, S_IFCHR | 0700, dev)) {
797 VLOG_WARN_RL(&error_rl,
798 "%s: creating character device %u:%u failed (%s)",
799 fn, major(dev), minor(dev), strerror(errno));
808 /* Return the major device number of the Open vSwitch device. If it
809 * cannot be determined, a negative errno is returned. */
811 get_openvswitch_major(void)
813 static int openvswitch_major = -1;
814 if (openvswitch_major < 0) {
815 openvswitch_major = get_major("openvswitch");
817 return openvswitch_major;
821 get_major(const char *target)
823 const char fn[] = "/proc/devices";
828 file = fopen(fn, "r");
830 VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
834 for (ln = 1; fgets(line, sizeof line, file); ln++) {
838 if (!strncmp(line, "Character", 9) || line[0] == '\0') {
840 } else if (!strncmp(line, "Block", 5)) {
841 /* We only want character devices, so skip the rest of the file. */
843 } else if (sscanf(line, "%d %63s", &major, name)) {
844 if (!strcmp(name, target)) {
849 VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
855 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
860 create_minor(const char *name, int minor)
865 error = open_minor(minor, &fd);
870 error = ioctl(fd, ODP_DP_CREATE, name) ? errno : 0;
876 open_minor(int minor, int *fdp)
881 error = make_openvswitch_device(minor, &fn);
886 *fdp = open(fn, O_RDONLY | O_NONBLOCK);
889 VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
898 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
901 struct dpif_linux *dpif = dpif_;
904 if (change->master_ifindex == dpif->local_ifindex
905 && (change->nlmsg_type == RTM_NEWLINK
906 || change->nlmsg_type == RTM_DELLINK))
908 /* Our datapath changed, either adding a new port or deleting an
910 shash_add_once(&dpif->changed_ports, change->ifname, NULL);
913 dpif->change_error = true;
917 /* Parses the contents of 'buf', which contains a "struct odp_vport" followed
918 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
919 * positive errno value.
921 * 'vport' will contain pointers into 'buf', so the caller should not free
922 * 'buf' while 'vport' is still in use. */
924 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
925 const struct ofpbuf *buf)
927 static const struct nl_policy odp_vport_policy[] = {
928 [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
929 [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
930 [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
931 [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
932 .min_len = sizeof(struct rtnl_link_stats64),
933 .max_len = sizeof(struct rtnl_link_stats64),
935 [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
936 .min_len = ETH_ADDR_LEN,
937 .max_len = ETH_ADDR_LEN,
939 [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
940 [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
941 [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
942 [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
945 struct odp_vport *odp_vport;
946 struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
948 dpif_linux_vport_init(vport);
950 if (!nl_policy_parse(buf, sizeof *odp_vport, odp_vport_policy,
951 a, ARRAY_SIZE(odp_vport_policy))) {
954 odp_vport = buf->data;
956 vport->dp_idx = odp_vport->dp_idx;
957 vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
958 vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
959 vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
960 if (a[ODP_VPORT_ATTR_STATS]) {
961 vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
963 if (a[ODP_VPORT_ATTR_ADDRESS]) {
964 vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
966 if (a[ODP_VPORT_ATTR_MTU]) {
967 vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
969 if (a[ODP_VPORT_ATTR_OPTIONS]) {
970 vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
971 vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
973 if (a[ODP_VPORT_ATTR_IFINDEX]) {
974 vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
976 if (a[ODP_VPORT_ATTR_IFLINK]) {
977 vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
982 /* Appends to 'buf' (which must initially be empty) a "struct odp_vport"
983 * followed by Netlink attributes corresponding to 'vport'. */
985 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
988 struct odp_vport *odp_vport;
990 ofpbuf_reserve(buf, sizeof odp_vport);
992 if (vport->port_no != UINT32_MAX) {
993 nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
996 if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
997 nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1001 nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1005 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1006 vport->stats, sizeof *vport->stats);
1009 if (vport->address) {
1010 nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1011 vport->address, ETH_ADDR_LEN);
1015 nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1018 if (vport->options) {
1019 nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1020 vport->options, vport->options_len);
1023 if (vport->ifindex) {
1024 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1027 if (vport->iflink) {
1028 nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1031 odp_vport = ofpbuf_push_uninit(buf, sizeof *odp_vport);
1032 odp_vport->dp_idx = vport->dp_idx;
1033 odp_vport->len = buf->size;
1034 odp_vport->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1037 /* Clears 'vport' to "empty" values. */
1039 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1041 memset(vport, 0, sizeof *vport);
1042 vport->dp_idx = UINT32_MAX;
1043 vport->port_no = UINT32_MAX;
1046 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1047 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1048 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1049 * result of the command is expected to be an odp_vport also, which is decoded
1050 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1051 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1053 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1054 struct dpif_linux_vport *reply,
1055 struct ofpbuf **bufp)
1057 static int dp0_fd = -1;
1058 struct ofpbuf *buf = NULL;
1061 assert((reply != NULL) == (bufp != NULL));
1065 error = open_minor(0, &fd);
1072 buf = ofpbuf_new(1024);
1073 dpif_linux_vport_to_ofpbuf(request, buf);
1075 error = ioctl(dp0_fd, request->cmd, buf->data) ? errno : 0;
1081 buf->size = ((struct odp_vport *) buf->data)->len;
1082 error = dpif_linux_vport_from_ofpbuf(reply, buf);
1095 memset(reply, 0, sizeof *reply);
1101 /* Obtains information about the kernel vport named 'name' and stores it into
1102 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1103 * longer needed ('reply' will contain pointers into '*bufp'). */
1105 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1106 struct ofpbuf **bufp)
1108 struct dpif_linux_vport request;
1110 dpif_linux_vport_init(&request);
1111 request.cmd = ODP_VPORT_GET;
1112 request.name = name;
1114 return dpif_linux_vport_transact(&request, reply, bufp);