2 * Copyright (c) 2008, 2009, 2010 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.
26 #include <linux/ethtool.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sockios.h>
30 #include <sys/ioctl.h>
33 #include "dpif-provider.h"
35 #include "poll-loop.h"
36 #include "rtnetlink.h"
41 #define THIS_MODULE VLM_dpif_linux
43 /* Datapath interface for the openvswitch Linux kernel module. */
48 /* Used by dpif_linux_get_all_names(). */
52 /* Change notification. */
53 int local_ifindex
; /* Ifindex of local port. */
54 struct svec changed_ports
; /* Ports that have changed. */
55 struct rtnetlink_notifier port_notifier
;
59 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(9999, 5);
61 static int do_ioctl(const struct dpif
*, int cmd
, const void *arg
);
62 static int lookup_minor(const char *name
, int *minor
);
63 static int finish_open(struct dpif
*, const char *local_ifname
);
64 static int get_openvswitch_major(void);
65 static int create_minor(const char *name
, int minor
, struct dpif
**dpifp
);
66 static int open_minor(int minor
, struct dpif
**dpifp
);
67 static int make_openvswitch_device(int minor
, char **fnp
);
68 static void dpif_linux_port_changed(const struct rtnetlink_change
*,
71 static struct dpif_linux
*
72 dpif_linux_cast(const struct dpif
*dpif
)
74 dpif_assert_class(dpif
, &dpif_linux_class
);
75 return CONTAINER_OF(dpif
, struct dpif_linux
, dpif
);
79 dpif_linux_enumerate(struct svec
*all_dps
)
85 /* Check that the Open vSwitch module is loaded. */
86 major
= get_openvswitch_major();
92 for (i
= 0; i
< ODP_MAX
; i
++) {
97 sprintf(devname
, "dp%d", i
);
98 retval
= dpif_open(devname
, &dpif
);
100 svec_add(all_dps
, devname
);
102 } else if (retval
!= ENODEV
&& !error
) {
110 dpif_linux_open(const char *name OVS_UNUSED
, char *suffix
, bool create
,
115 minor
= !strncmp(name
, "dp", 2)
116 && isdigit((unsigned char)name
[2]) ? atoi(name
+ 2) : -1;
119 return create_minor(suffix
, minor
, dpifp
);
121 /* Scan for unused minor number. */
122 for (minor
= 0; minor
< ODP_MAX
; minor
++) {
123 int error
= create_minor(suffix
, minor
, dpifp
);
124 if (error
!= EBUSY
) {
129 /* All datapath numbers in use. */
133 struct dpif_linux
*dpif
;
134 struct odp_port port
;
138 error
= lookup_minor(suffix
, &minor
);
144 error
= open_minor(minor
, dpifp
);
148 dpif
= dpif_linux_cast(*dpifp
);
150 /* We need the local port's ifindex for the poll function. Start by
151 * getting the local port's name. */
152 memset(&port
, 0, sizeof port
);
153 port
.port
= ODPP_LOCAL
;
154 if (ioctl(dpif
->fd
, ODP_PORT_QUERY
, &port
)) {
156 if (error
!= ENODEV
) {
157 VLOG_WARN("%s: probe returned unexpected error: %s",
158 dpif_name(*dpifp
), strerror(error
));
164 /* Then use that to finish up opening. */
165 return finish_open(&dpif
->dpif
, port
.devname
);
170 dpif_linux_close(struct dpif
*dpif_
)
172 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
173 rtnetlink_notifier_unregister(&dpif
->port_notifier
);
174 svec_destroy(&dpif
->changed_ports
);
175 free(dpif
->local_ifname
);
181 dpif_linux_get_all_names(const struct dpif
*dpif_
, struct svec
*all_names
)
183 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
185 svec_add_nocopy(all_names
, xasprintf("dp%d", dpif
->minor
));
186 svec_add(all_names
, dpif
->local_ifname
);
191 dpif_linux_delete(struct dpif
*dpif_
)
193 return do_ioctl(dpif_
, ODP_DP_DESTROY
, NULL
);
197 dpif_linux_get_stats(const struct dpif
*dpif_
, struct odp_stats
*stats
)
199 memset(stats
, 0, sizeof *stats
);
200 return do_ioctl(dpif_
, ODP_DP_STATS
, stats
);
204 dpif_linux_get_drop_frags(const struct dpif
*dpif_
, bool *drop_fragsp
)
209 error
= do_ioctl(dpif_
, ODP_GET_DROP_FRAGS
, &drop_frags
);
211 *drop_fragsp
= drop_frags
& 1;
217 dpif_linux_set_drop_frags(struct dpif
*dpif_
, bool drop_frags
)
219 int drop_frags_int
= drop_frags
;
220 return do_ioctl(dpif_
, ODP_SET_DROP_FRAGS
, &drop_frags_int
);
224 dpif_linux_port_add(struct dpif
*dpif_
, const char *devname
, uint16_t flags
,
227 struct odp_port port
;
230 memset(&port
, 0, sizeof port
);
231 strncpy(port
.devname
, devname
, sizeof port
.devname
);
233 error
= do_ioctl(dpif_
, ODP_PORT_ADD
, &port
);
235 *port_no
= port
.port
;
241 dpif_linux_port_del(struct dpif
*dpif_
, uint16_t port_no
)
244 return do_ioctl(dpif_
, ODP_PORT_DEL
, &tmp
);
248 dpif_linux_port_query_by_number(const struct dpif
*dpif_
, uint16_t port_no
,
249 struct odp_port
*port
)
251 memset(port
, 0, sizeof *port
);
252 port
->port
= port_no
;
253 return do_ioctl(dpif_
, ODP_PORT_QUERY
, port
);
257 dpif_linux_port_query_by_name(const struct dpif
*dpif_
, const char *devname
,
258 struct odp_port
*port
)
260 memset(port
, 0, sizeof *port
);
261 strncpy(port
->devname
, devname
, sizeof port
->devname
);
262 return do_ioctl(dpif_
, ODP_PORT_QUERY
, port
);
266 dpif_linux_flow_flush(struct dpif
*dpif_
)
268 return do_ioctl(dpif_
, ODP_FLOW_FLUSH
, NULL
);
272 dpif_linux_port_list(const struct dpif
*dpif_
, struct odp_port
*ports
, int n
)
274 struct odp_portvec pv
;
279 error
= do_ioctl(dpif_
, ODP_PORT_LIST
, &pv
);
280 return error
? -error
: pv
.n_ports
;
284 dpif_linux_port_poll(const struct dpif
*dpif_
, char **devnamep
)
286 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
288 if (dpif
->change_error
) {
289 dpif
->change_error
= false;
290 svec_clear(&dpif
->changed_ports
);
292 } else if (dpif
->changed_ports
.n
) {
293 *devnamep
= dpif
->changed_ports
.names
[--dpif
->changed_ports
.n
];
301 dpif_linux_port_poll_wait(const struct dpif
*dpif_
)
303 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
304 if (dpif
->changed_ports
.n
|| dpif
->change_error
) {
305 poll_immediate_wake();
307 rtnetlink_notifier_wait();
312 dpif_linux_port_group_get(const struct dpif
*dpif_
, int group
,
313 uint16_t ports
[], int n
)
315 struct odp_port_group pg
;
318 assert(n
<= UINT16_MAX
);
322 error
= do_ioctl(dpif_
, ODP_PORT_GROUP_GET
, &pg
);
323 return error
? -error
: pg
.n_ports
;
327 dpif_linux_port_group_set(struct dpif
*dpif_
, int group
,
328 const uint16_t ports
[], int n
)
330 struct odp_port_group pg
;
332 assert(n
<= UINT16_MAX
);
334 pg
.ports
= (uint16_t *) ports
;
336 return do_ioctl(dpif_
, ODP_PORT_GROUP_SET
, &pg
);
340 dpif_linux_flow_get(const struct dpif
*dpif_
, struct odp_flow flows
[], int n
)
342 struct odp_flowvec fv
;
345 return do_ioctl(dpif_
, ODP_FLOW_GET
, &fv
);
349 dpif_linux_flow_put(struct dpif
*dpif_
, struct odp_flow_put
*put
)
351 return do_ioctl(dpif_
, ODP_FLOW_PUT
, put
);
355 dpif_linux_flow_del(struct dpif
*dpif_
, struct odp_flow
*flow
)
357 return do_ioctl(dpif_
, ODP_FLOW_DEL
, flow
);
361 dpif_linux_flow_list(const struct dpif
*dpif_
, struct odp_flow flows
[], int n
)
363 struct odp_flowvec fv
;
368 error
= do_ioctl(dpif_
, ODP_FLOW_LIST
, &fv
);
369 return error
? -error
: fv
.n_flows
;
373 dpif_linux_execute(struct dpif
*dpif_
, uint16_t in_port
,
374 const union odp_action actions
[], int n_actions
,
375 const struct ofpbuf
*buf
)
377 struct odp_execute execute
;
378 memset(&execute
, 0, sizeof execute
);
379 execute
.in_port
= in_port
;
380 execute
.actions
= (union odp_action
*) actions
;
381 execute
.n_actions
= n_actions
;
382 execute
.data
= buf
->data
;
383 execute
.length
= buf
->size
;
384 return do_ioctl(dpif_
, ODP_EXECUTE
, &execute
);
388 dpif_linux_recv_get_mask(const struct dpif
*dpif_
, int *listen_mask
)
390 return do_ioctl(dpif_
, ODP_GET_LISTEN_MASK
, listen_mask
);
394 dpif_linux_recv_set_mask(struct dpif
*dpif_
, int listen_mask
)
396 return do_ioctl(dpif_
, ODP_SET_LISTEN_MASK
, &listen_mask
);
400 dpif_linux_get_sflow_probability(const struct dpif
*dpif_
,
401 uint32_t *probability
)
403 return do_ioctl(dpif_
, ODP_GET_SFLOW_PROBABILITY
, probability
);
407 dpif_linux_set_sflow_probability(struct dpif
*dpif_
, uint32_t probability
)
409 return do_ioctl(dpif_
, ODP_SET_SFLOW_PROBABILITY
, &probability
);
413 dpif_linux_recv(struct dpif
*dpif_
, struct ofpbuf
**bufp
)
415 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
420 buf
= ofpbuf_new(65536);
421 retval
= read(dpif
->fd
, ofpbuf_tail(buf
), ofpbuf_tailroom(buf
));
424 if (error
!= EAGAIN
) {
425 VLOG_WARN_RL(&error_rl
, "%s: read failed: %s",
426 dpif_name(dpif_
), strerror(error
));
428 } else if (retval
>= sizeof(struct odp_msg
)) {
429 struct odp_msg
*msg
= buf
->data
;
430 if (msg
->length
<= retval
) {
435 VLOG_WARN_RL(&error_rl
, "%s: discarding message truncated "
436 "from %"PRIu32
" bytes to %d",
437 dpif_name(dpif_
), msg
->length
, retval
);
440 } else if (!retval
) {
441 VLOG_WARN_RL(&error_rl
, "%s: unexpected end of file", dpif_name(dpif_
));
444 VLOG_WARN_RL(&error_rl
,
445 "%s: discarding too-short message (%d bytes)",
446 dpif_name(dpif_
), retval
);
456 dpif_linux_recv_wait(struct dpif
*dpif_
)
458 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
459 poll_fd_wait(dpif
->fd
, POLLIN
);
462 const struct dpif_class dpif_linux_class
= {
463 "", /* This is the default class. */
467 dpif_linux_enumerate
,
470 dpif_linux_get_all_names
,
472 dpif_linux_get_stats
,
473 dpif_linux_get_drop_frags
,
474 dpif_linux_set_drop_frags
,
477 dpif_linux_port_query_by_number
,
478 dpif_linux_port_query_by_name
,
479 dpif_linux_port_list
,
480 dpif_linux_port_poll
,
481 dpif_linux_port_poll_wait
,
482 dpif_linux_port_group_get
,
483 dpif_linux_port_group_set
,
487 dpif_linux_flow_flush
,
488 dpif_linux_flow_list
,
490 dpif_linux_recv_get_mask
,
491 dpif_linux_recv_set_mask
,
492 dpif_linux_get_sflow_probability
,
493 dpif_linux_set_sflow_probability
,
495 dpif_linux_recv_wait
,
498 static int get_openvswitch_major(void);
499 static int get_major(const char *target
);
502 do_ioctl(const struct dpif
*dpif_
, int cmd
, const void *arg
)
504 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
505 return ioctl(dpif
->fd
, cmd
, arg
) ? errno
: 0;
509 lookup_minor(const char *name
, int *minorp
)
511 struct ethtool_drvinfo drvinfo
;
517 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
519 VLOG_WARN("socket(AF_INET) failed: %s", strerror(errno
));
524 memset(&ifr
, 0, sizeof ifr
);
525 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
526 ifr
.ifr_data
= (caddr_t
) &drvinfo
;
528 memset(&drvinfo
, 0, sizeof drvinfo
);
529 drvinfo
.cmd
= ETHTOOL_GDRVINFO
;
530 if (ioctl(sock
, SIOCETHTOOL
, &ifr
)) {
531 VLOG_WARN("ioctl(SIOCETHTOOL) failed: %s", strerror(errno
));
533 goto error_close_sock
;
536 if (strcmp(drvinfo
.driver
, "openvswitch")) {
537 VLOG_WARN("%s is not an openvswitch device", name
);
539 goto error_close_sock
;
542 if (sscanf(drvinfo
.bus_info
, "%d.%d", &minor
, &port_no
) != 2) {
543 VLOG_WARN("%s ethtool bus_info has unexpected format", name
);
545 goto error_close_sock
;
546 } else if (port_no
!= ODPP_LOCAL
) {
547 /* This is an Open vSwitch device but not the local port. We
548 * intentionally support only using the name of the local port as the
549 * name of a datapath; otherwise, it would be too difficult to
550 * enumerate all the names of a datapath. */
552 goto error_close_sock
;
566 make_openvswitch_device(int minor
, char **fnp
)
568 const char dirname
[] = "/dev/net";
576 major
= get_openvswitch_major();
580 dev
= makedev(major
, minor
);
582 sprintf(fn
, "%s/dp%d", dirname
, minor
);
584 if (!S_ISCHR(s
.st_mode
)) {
585 VLOG_WARN_RL(&error_rl
, "%s is not a character device, fixing",
587 } else if (s
.st_rdev
!= dev
) {
588 VLOG_WARN_RL(&error_rl
,
589 "%s is device %u:%u but should be %u:%u, fixing",
590 fn
, major(s
.st_rdev
), minor(s
.st_rdev
),
591 major(dev
), minor(dev
));
596 VLOG_WARN_RL(&error_rl
, "%s: unlink failed (%s)",
597 fn
, strerror(errno
));
600 } else if (errno
== ENOENT
) {
601 if (stat(dirname
, &s
)) {
602 if (errno
== ENOENT
) {
603 if (mkdir(dirname
, 0755)) {
604 VLOG_WARN_RL(&error_rl
, "%s: mkdir failed (%s)",
605 dirname
, strerror(errno
));
609 VLOG_WARN_RL(&error_rl
, "%s: stat failed (%s)",
610 dirname
, strerror(errno
));
615 VLOG_WARN_RL(&error_rl
, "%s: stat failed (%s)", fn
, strerror(errno
));
619 /* The device needs to be created. */
620 if (mknod(fn
, S_IFCHR
| 0700, dev
)) {
621 VLOG_WARN_RL(&error_rl
,
622 "%s: creating character device %u:%u failed (%s)",
623 fn
, major(dev
), minor(dev
), strerror(errno
));
632 /* Return the major device number of the Open vSwitch device. If it
633 * cannot be determined, a negative errno is returned. */
635 get_openvswitch_major(void)
637 static int openvswitch_major
= -1;
638 if (openvswitch_major
< 0) {
639 openvswitch_major
= get_major("openvswitch");
641 return openvswitch_major
;
645 get_major(const char *target
)
647 const char fn
[] = "/proc/devices";
652 file
= fopen(fn
, "r");
654 VLOG_ERR("opening %s failed (%s)", fn
, strerror(errno
));
658 for (ln
= 1; fgets(line
, sizeof line
, file
); ln
++) {
662 if (!strncmp(line
, "Character", 9) || line
[0] == '\0') {
664 } else if (!strncmp(line
, "Block", 5)) {
665 /* We only want character devices, so skip the rest of the file. */
667 } else if (sscanf(line
, "%d %63s", &major
, name
)) {
668 if (!strcmp(name
, target
)) {
675 VLOG_WARN("%s:%d: syntax error", fn
, ln
);
681 VLOG_ERR("%s: %s major not found (is the module loaded?)", fn
, target
);
686 finish_open(struct dpif
*dpif_
, const char *local_ifname
)
688 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
689 dpif
->local_ifname
= strdup(local_ifname
);
690 dpif
->local_ifindex
= if_nametoindex(local_ifname
);
691 if (!dpif
->local_ifindex
) {
694 VLOG_WARN("could not get ifindex of %s device: %s",
695 local_ifname
, strerror(errno
));
702 create_minor(const char *name
, int minor
, struct dpif
**dpifp
)
704 int error
= open_minor(minor
, dpifp
);
706 error
= do_ioctl(*dpifp
, ODP_DP_CREATE
, name
);
708 error
= finish_open(*dpifp
, name
);
717 open_minor(int minor
, struct dpif
**dpifp
)
723 error
= make_openvswitch_device(minor
, &fn
);
728 fd
= open(fn
, O_RDONLY
| O_NONBLOCK
);
730 struct dpif_linux
*dpif
= xmalloc(sizeof *dpif
);
731 error
= rtnetlink_notifier_register(&dpif
->port_notifier
,
732 dpif_linux_port_changed
, dpif
);
736 name
= xasprintf("dp%d", minor
);
737 dpif_init(&dpif
->dpif
, &dpif_linux_class
, name
, minor
, minor
);
741 dpif
->local_ifname
= NULL
;
743 dpif
->local_ifindex
= 0;
744 svec_init(&dpif
->changed_ports
);
745 dpif
->change_error
= false;
746 *dpifp
= &dpif
->dpif
;
752 VLOG_WARN("%s: open failed (%s)", fn
, strerror(error
));
760 dpif_linux_port_changed(const struct rtnetlink_change
*change
, void *dpif_
)
762 struct dpif_linux
*dpif
= dpif_
;
765 if (change
->master_ifindex
== dpif
->local_ifindex
766 && (change
->nlmsg_type
== RTM_NEWLINK
767 || change
->nlmsg_type
== RTM_DELLINK
))
769 /* Our datapath changed, either adding a new port or deleting an
771 if (!svec_contains(&dpif
->changed_ports
, change
->ifname
)) {
772 svec_add(&dpif
->changed_ports
, change
->ifname
);
773 svec_sort(&dpif
->changed_ports
);
777 dpif
->change_error
= true;