2 * Copyright (c) 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.
21 #include <arpa/inet.h>
23 #include <linux/if_tun.h>
25 #include <linux/types.h>
26 #include <linux/ethtool.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sockios.h>
29 #include <linux/version.h>
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <netpacket/packet.h>
34 #include <net/ethernet.h>
36 #include <linux/if_tunnel.h>
37 #include <net/if_arp.h>
38 #include <net/if_packet.h>
39 #include <net/route.h>
40 #include <netinet/in.h>
47 #include "dynamic-string.h"
48 #include "fatal-signal.h"
49 #include "netdev-provider.h"
52 #include "openflow/openflow.h"
53 #include "openvswitch/gre.h"
55 #include "poll-loop.h"
56 #include "rtnetlink.h"
57 #include "socket-util.h"
61 #ifndef GRE_IOCTL_ONLY
62 #include <linux/if_link.h>
65 #define THIS_MODULE VLM_netdev_linux
68 /* These were introduced in Linux 2.6.14, so they might be missing if we have
70 #ifndef ADVERTISED_Pause
71 #define ADVERTISED_Pause (1 << 13)
73 #ifndef ADVERTISED_Asym_Pause
74 #define ADVERTISED_Asym_Pause (1 << 14)
77 static struct rtnetlink_notifier netdev_linux_cache_notifier
;
78 static int cache_notifier_refcount
;
81 VALID_IFINDEX
= 1 << 0,
82 VALID_ETHERADDR
= 1 << 1,
86 VALID_CARRIER
= 1 << 5,
87 VALID_IS_INTERNAL
= 1 << 6
94 struct netdev_dev_linux
{
95 struct netdev_dev netdev_dev
;
97 struct shash_node
*shash_node
;
98 unsigned int cache_valid
;
101 uint8_t etheraddr
[ETH_ADDR_LEN
];
102 struct in_addr address
, netmask
;
109 struct tap_state tap
;
113 struct netdev_linux
{
114 struct netdev netdev
;
118 /* An AF_INET socket (used for ioctl operations). */
119 static int af_inet_sock
= -1;
134 struct nl_sock
*nl_sock
;
140 struct netdev_linux_notifier
{
141 struct netdev_notifier notifier
;
145 static struct shash netdev_linux_notifiers
=
146 SHASH_INITIALIZER(&netdev_linux_notifiers
);
147 static struct rtnetlink_notifier netdev_linux_poll_notifier
;
149 /* This is set pretty low because we probably won't learn anything from the
150 * additional log messages. */
151 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(5, 20);
153 static int destroy_gre(const char *name
);
154 static int netdev_linux_do_ethtool(const char *name
, struct ethtool_cmd
*,
155 int cmd
, const char *cmd_name
);
156 static int netdev_linux_do_ioctl(const char *name
, struct ifreq
*, int cmd
,
157 const char *cmd_name
);
158 static int netdev_linux_get_ipv4(const struct netdev
*, struct in_addr
*,
159 int cmd
, const char *cmd_name
);
160 static int get_flags(const struct netdev
*, int *flagsp
);
161 static int set_flags(struct netdev
*, int flags
);
162 static int do_get_ifindex(const char *netdev_name
);
163 static int get_ifindex(const struct netdev
*, int *ifindexp
);
164 static int do_set_addr(struct netdev
*netdev
,
165 int ioctl_nr
, const char *ioctl_name
,
166 struct in_addr addr
);
167 static int get_etheraddr(const char *netdev_name
, uint8_t ea
[ETH_ADDR_LEN
]);
168 static int set_etheraddr(const char *netdev_name
, int hwaddr_family
,
169 const uint8_t[ETH_ADDR_LEN
]);
170 static int get_stats_via_netlink(int ifindex
, struct netdev_stats
*stats
);
171 static int get_stats_via_proc(const char *netdev_name
, struct netdev_stats
*stats
);
173 static struct netdev_dev_linux
*
174 netdev_dev_linux_cast(const struct netdev_dev
*netdev_dev
)
176 const char *type
= netdev_dev_get_type(netdev_dev
);
177 assert(!strcmp(type
, "system") || !strcmp(type
, "tap")
178 || !strcmp(type
, "gre"));
179 return CONTAINER_OF(netdev_dev
, struct netdev_dev_linux
, netdev_dev
);
182 static struct netdev_linux
*
183 netdev_linux_cast(const struct netdev
*netdev
)
185 const char *type
= netdev_get_type(netdev
);
186 assert(!strcmp(type
, "system") || !strcmp(type
, "tap")
187 || !strcmp(type
, "gre"));
188 return CONTAINER_OF(netdev
, struct netdev_linux
, netdev
);
192 netdev_linux_init(void)
194 static int status
= -1;
196 af_inet_sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
197 status
= af_inet_sock
>= 0 ? 0 : errno
;
199 VLOG_ERR("failed to create inet socket: %s", strerror(status
));
206 netdev_linux_run(void)
208 rtnetlink_notifier_run();
212 netdev_linux_wait(void)
214 rtnetlink_notifier_wait();
218 netdev_linux_cache_cb(const struct rtnetlink_change
*change
,
219 void *aux OVS_UNUSED
)
221 struct netdev_dev_linux
*dev
;
223 struct netdev_dev
*base_dev
= netdev_dev_from_name(change
->ifname
);
225 dev
= netdev_dev_linux_cast(base_dev
);
226 dev
->cache_valid
= 0;
229 struct shash device_shash
;
230 struct shash_node
*node
;
232 shash_init(&device_shash
);
233 netdev_dev_get_devices(&netdev_linux_class
, &device_shash
);
234 SHASH_FOR_EACH (node
, &device_shash
) {
236 dev
->cache_valid
= 0;
238 shash_destroy(&device_shash
);
242 /* The arguments are marked as unused to prevent warnings on platforms where
243 * the Netlink interface isn't supported. */
245 setup_gre_netlink(const char *name OVS_UNUSED
,
246 struct gre_config
*config OVS_UNUSED
, bool create OVS_UNUSED
)
248 #ifdef GRE_IOCTL_ONLY
252 struct ofpbuf request
, *reply
;
253 unsigned int nl_flags
;
254 struct ifinfomsg ifinfomsg
;
255 struct nlattr
*linkinfo_hdr
;
256 struct nlattr
*info_data_hdr
;
259 uint8_t pmtudisc
= 0;
261 VLOG_DBG("%s: attempting to create gre device using netlink", name
);
263 if (!gre_descriptors
.nl_sock
) {
264 error
= nl_sock_create(NETLINK_ROUTE
, 0, 0, 0,
265 &gre_descriptors
.nl_sock
);
267 VLOG_WARN("couldn't create netlink socket: %s", strerror(error
));
272 ofpbuf_init(&request
, 0);
274 nl_flags
= NLM_F_REQUEST
;
276 nl_flags
|= NLM_F_CREATE
|NLM_F_EXCL
;
279 /* We over-reserve space, because we do some pointer arithmetic
280 * and don't want the buffer address shifting under us. */
281 nl_msg_put_nlmsghdr(&request
, gre_descriptors
.nl_sock
, 2048, RTM_NEWLINK
,
284 memset(&ifinfomsg
, 0, sizeof ifinfomsg
);
285 ifinfomsg
.ifi_family
= AF_UNSPEC
;
286 nl_msg_put(&request
, &ifinfomsg
, sizeof ifinfomsg
);
288 linkinfo_hdr
= ofpbuf_tail(&request
);
289 nl_msg_put_unspec(&request
, IFLA_LINKINFO
, NULL
, 0);
291 nl_msg_put_unspec(&request
, IFLA_INFO_KIND
, "gretap", 6);
293 info_data_hdr
= ofpbuf_tail(&request
);
294 nl_msg_put_unspec(&request
, IFLA_INFO_DATA
, NULL
, 0);
297 if (config
->have_in_key
) {
300 if (config
->have_out_key
) {
304 if (config
->in_csum
) {
307 if (config
->out_csum
) {
312 nl_msg_put_u32(&request
, IFLA_GRE_IKEY
, config
->in_key
);
313 nl_msg_put_u32(&request
, IFLA_GRE_OKEY
, config
->out_key
);
314 nl_msg_put_u16(&request
, IFLA_GRE_IFLAGS
, iflags
);
315 nl_msg_put_u16(&request
, IFLA_GRE_OFLAGS
, oflags
);
316 nl_msg_put_u32(&request
, IFLA_GRE_LOCAL
, config
->local_ip
);
317 nl_msg_put_u32(&request
, IFLA_GRE_REMOTE
, config
->remote_ip
);
318 nl_msg_put_u8(&request
, IFLA_GRE_PMTUDISC
, pmtudisc
);
319 nl_msg_put_u8(&request
, IFLA_GRE_TTL
, 0);
320 nl_msg_put_u8(&request
, IFLA_GRE_TOS
, 0);
322 info_data_hdr
->nla_len
= (char *)ofpbuf_tail(&request
)
323 - (char *)info_data_hdr
;
324 linkinfo_hdr
->nla_len
= (char *)ofpbuf_tail(&request
)
325 - (char *)linkinfo_hdr
;
327 nl_msg_put_string(&request
, IFLA_IFNAME
, name
);
329 error
= nl_sock_transact(gre_descriptors
.nl_sock
, &request
, &reply
);
330 ofpbuf_uninit(&request
);
332 VLOG_WARN("couldn't transact netlink socket: %s", strerror(error
));
335 ofpbuf_delete(reply
);
343 setup_gre_ioctl(const char *name
, struct gre_config
*config
, bool create
)
345 struct ip_tunnel_parm p
;
348 VLOG_DBG("%s: attempting to create gre device using ioctl", name
);
350 memset(&p
, 0, sizeof p
);
352 strncpy(p
.name
, name
, IFNAMSIZ
);
356 p
.iph
.protocol
= IPPROTO_GRE
;
357 p
.iph
.saddr
= config
->local_ip
;
358 p
.iph
.daddr
= config
->remote_ip
;
360 if (config
->have_in_key
) {
361 p
.i_flags
|= GRE_KEY
;
362 p
.i_key
= config
->in_key
;
364 if (config
->have_out_key
) {
365 p
.o_flags
|= GRE_KEY
;
366 p
.o_key
= config
->out_key
;
369 if (config
->in_csum
) {
370 p
.i_flags
|= GRE_CSUM
;
372 if (config
->out_csum
) {
373 p
.o_flags
|= GRE_CSUM
;
376 strncpy(ifr
.ifr_name
, create
? GRE_IOCTL_DEVICE
: name
, IFNAMSIZ
);
377 ifr
.ifr_ifru
.ifru_data
= (void *)&p
;
379 if (!gre_descriptors
.ioctl_fd
) {
380 gre_descriptors
.ioctl_fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
381 if (gre_descriptors
.ioctl_fd
< 0) {
382 VLOG_WARN("couldn't create gre ioctl socket: %s", strerror(errno
));
383 gre_descriptors
.ioctl_fd
= 0;
388 if (ioctl(gre_descriptors
.ioctl_fd
, create
? SIOCADDGRETAP
: SIOCCHGGRETAP
,
390 VLOG_WARN("couldn't do gre ioctl: %s", strerror(errno
));
397 /* The arguments are marked as unused to prevent warnings on platforms where
398 * the Netlink interface isn't supported. */
400 check_gre_device_netlink(const char *name OVS_UNUSED
)
402 #ifdef GRE_IOCTL_ONLY
405 static const struct nl_policy getlink_policy
[] = {
406 [IFLA_LINKINFO
] = { .type
= NL_A_NESTED
, .optional
= false },
409 static const struct nl_policy linkinfo_policy
[] = {
410 [IFLA_INFO_KIND
] = { .type
= NL_A_STRING
, .optional
= false },
415 struct ofpbuf request
, *reply
;
416 struct ifinfomsg ifinfomsg
;
417 struct nlattr
*getlink_attrs
[ARRAY_SIZE(getlink_policy
)];
418 struct nlattr
*linkinfo_attrs
[ARRAY_SIZE(linkinfo_policy
)];
419 struct ofpbuf linkinfo
;
420 const char *device_kind
;
422 ofpbuf_init(&request
, 0);
424 nl_msg_put_nlmsghdr(&request
, gre_descriptors
.nl_sock
,
425 NLMSG_LENGTH(sizeof ifinfomsg
), RTM_GETLINK
,
428 memset(&ifinfomsg
, 0, sizeof ifinfomsg
);
429 ifinfomsg
.ifi_family
= AF_UNSPEC
;
430 ifinfomsg
.ifi_index
= do_get_ifindex(name
);
431 nl_msg_put(&request
, &ifinfomsg
, sizeof ifinfomsg
);
433 error
= nl_sock_transact(gre_descriptors
.nl_sock
, &request
, &reply
);
434 ofpbuf_uninit(&request
);
436 VLOG_WARN("couldn't transact netlink socket: %s", strerror(error
));
440 if (!nl_policy_parse(reply
, NLMSG_HDRLEN
+ sizeof(struct ifinfomsg
),
441 getlink_policy
, getlink_attrs
,
442 ARRAY_SIZE(getlink_policy
))) {
443 VLOG_WARN("received bad rtnl message (getlink policy)");
447 linkinfo
.data
= (void *)nl_attr_get(getlink_attrs
[IFLA_LINKINFO
]);
448 linkinfo
.size
= nl_attr_get_size(getlink_attrs
[IFLA_LINKINFO
]);
449 if (!nl_policy_parse(&linkinfo
, 0, linkinfo_policy
,
450 linkinfo_attrs
, ARRAY_SIZE(linkinfo_policy
))) {
451 VLOG_WARN("received bad rtnl message (linkinfo policy)");
455 device_kind
= nl_attr_get_string(linkinfo_attrs
[IFLA_INFO_KIND
]);
456 ret
= !strcmp(device_kind
, "gretap");
459 ofpbuf_delete(reply
);
465 check_gre_device_ioctl(const char *name
)
467 struct ethtool_drvinfo drvinfo
;
470 memset(&drvinfo
, 0, sizeof drvinfo
);
471 error
= netdev_linux_do_ethtool(name
, (struct ethtool_cmd
*)&drvinfo
,
472 ETHTOOL_GDRVINFO
, "ETHTOOL_GDRVINFO");
474 return !error
&& !strcmp(drvinfo
.driver
, "ip_gre")
475 && !strcmp(drvinfo
.bus_info
, "gretap");
479 setup_gre(const char *name
, const struct shash
*args
, bool create
)
482 struct in_addr in_addr
;
483 struct shash_node
*node
;
484 struct gre_config config
;
486 memset(&config
, 0, sizeof config
);
487 config
.in_csum
= true;
488 config
.out_csum
= true;
490 SHASH_FOR_EACH (node
, args
) {
491 if (!strcmp(node
->name
, "remote_ip")) {
492 if (lookup_ip(node
->data
, &in_addr
)) {
493 VLOG_WARN("bad 'remote_ip' for gre device %s ", name
);
495 config
.remote_ip
= in_addr
.s_addr
;
497 } else if (!strcmp(node
->name
, "local_ip")) {
498 if (lookup_ip(node
->data
, &in_addr
)) {
499 VLOG_WARN("bad 'local_ip' for gre device %s ", name
);
501 config
.local_ip
= in_addr
.s_addr
;
503 } else if (!strcmp(node
->name
, "key")) {
504 config
.have_in_key
= true;
505 config
.have_out_key
= true;
506 config
.in_key
= htonl(atoi(node
->data
));
507 config
.out_key
= htonl(atoi(node
->data
));
508 } else if (!strcmp(node
->name
, "in_key")) {
509 config
.have_in_key
= true;
510 config
.in_key
= htonl(atoi(node
->data
));
511 } else if (!strcmp(node
->name
, "out_key")) {
512 config
.have_out_key
= true;
513 config
.out_key
= htonl(atoi(node
->data
));
514 } else if (!strcmp(node
->name
, "csum")) {
515 if (!strcmp(node
->data
, "false")) {
516 config
.in_csum
= false;
517 config
.out_csum
= false;
520 VLOG_WARN("unknown gre argument '%s'", node
->name
);
524 if (!config
.remote_ip
) {
525 VLOG_WARN("gre type requires valid 'remote_ip' argument");
530 if (!gre_descriptors
.use_ioctl
) {
531 error
= setup_gre_netlink(name
, &config
, create
);
532 if (error
== EOPNOTSUPP
) {
533 gre_descriptors
.use_ioctl
= true;
536 if (gre_descriptors
.use_ioctl
) {
537 error
= setup_gre_ioctl(name
, &config
, create
);
540 if (create
&& error
== EEXIST
) {
543 if (gre_descriptors
.use_ioctl
) {
544 gre_device
= check_gre_device_ioctl(name
);
546 gre_device
= check_gre_device_netlink(name
);
553 VLOG_WARN("replacing existing gre device %s", name
);
554 error
= destroy_gre(name
);
559 if (gre_descriptors
.use_ioctl
) {
560 error
= setup_gre_ioctl(name
, &config
, create
);
562 error
= setup_gre_netlink(name
, &config
, create
);
570 /* Creates the netdev device of 'type' with 'name'. */
572 netdev_linux_create_system(const char *name
, const char *type OVS_UNUSED
,
573 const struct shash
*args
, struct netdev_dev
**netdev_devp
)
575 struct netdev_dev_linux
*netdev_dev
;
578 if (!shash_is_empty(args
)) {
579 VLOG_WARN("%s: arguments for system devices should be empty", name
);
582 if (!cache_notifier_refcount
) {
583 error
= rtnetlink_notifier_register(&netdev_linux_cache_notifier
,
584 netdev_linux_cache_cb
, NULL
);
589 cache_notifier_refcount
++;
591 netdev_dev
= xzalloc(sizeof *netdev_dev
);
592 netdev_dev_init(&netdev_dev
->netdev_dev
, name
, &netdev_linux_class
);
594 *netdev_devp
= &netdev_dev
->netdev_dev
;
598 /* For most types of netdevs we open the device for each call of
599 * netdev_open(). However, this is not the case with tap devices,
600 * since it is only possible to open the device once. In this
601 * situation we share a single file descriptor, and consequently
602 * buffers, across all readers. Therefore once data is read it will
603 * be unavailable to other reads for tap devices. */
605 netdev_linux_create_tap(const char *name
, const char *type OVS_UNUSED
,
606 const struct shash
*args
, struct netdev_dev
**netdev_devp
)
608 struct netdev_dev_linux
*netdev_dev
;
609 struct tap_state
*state
;
610 static const char tap_dev
[] = "/dev/net/tun";
614 if (!shash_is_empty(args
)) {
615 VLOG_WARN("%s: arguments for TAP devices should be empty", name
);
618 netdev_dev
= xzalloc(sizeof *netdev_dev
);
619 state
= &netdev_dev
->state
.tap
;
621 /* Open tap device. */
622 state
->fd
= open(tap_dev
, O_RDWR
);
625 VLOG_WARN("opening \"%s\" failed: %s", tap_dev
, strerror(error
));
629 /* Create tap device. */
630 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
631 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
632 if (ioctl(state
->fd
, TUNSETIFF
, &ifr
) == -1) {
633 VLOG_WARN("%s: creating tap device failed: %s", name
,
639 /* Make non-blocking. */
640 error
= set_nonblocking(state
->fd
);
645 netdev_dev_init(&netdev_dev
->netdev_dev
, name
, &netdev_tap_class
);
646 *netdev_devp
= &netdev_dev
->netdev_dev
;
655 if_up(const char *name
)
659 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
660 ifr
.ifr_flags
= IFF_UP
;
662 if (ioctl(af_inet_sock
, SIOCSIFFLAGS
, &ifr
) == -1) {
663 VLOG_DBG_RL(&rl
, "%s: failed to bring device up: %s",
664 name
, strerror(errno
));
672 netdev_linux_create_gre(const char *name
, const char *type OVS_UNUSED
,
673 const struct shash
*args
, struct netdev_dev
**netdev_devp
)
675 struct netdev_dev_linux
*netdev_dev
;
678 netdev_dev
= xzalloc(sizeof *netdev_dev
);
680 error
= setup_gre(name
, args
, true);
690 netdev_dev_init(&netdev_dev
->netdev_dev
, name
, &netdev_gre_class
);
691 *netdev_devp
= &netdev_dev
->netdev_dev
;
700 netdev_linux_reconfigure_gre(struct netdev_dev
*netdev_dev_
,
701 const struct shash
*args
)
703 const char *name
= netdev_dev_get_name(netdev_dev_
);
705 return setup_gre(name
, args
, false);
708 /* The arguments are marked as unused to prevent warnings on platforms where
709 * the Netlink interface isn't supported. */
711 destroy_gre_netlink(const char *name OVS_UNUSED
)
713 #ifdef GRE_IOCTL_ONLY
717 struct ofpbuf request
, *reply
;
718 struct ifinfomsg ifinfomsg
;
721 ofpbuf_init(&request
, 0);
723 nl_msg_put_nlmsghdr(&request
, gre_descriptors
.nl_sock
, 0, RTM_DELLINK
,
726 memset(&ifinfomsg
, 0, sizeof ifinfomsg
);
727 ifinfomsg
.ifi_family
= AF_UNSPEC
;
728 nl_msg_put(&request
, &ifinfomsg
, sizeof ifinfomsg
);
730 ifindex
= do_get_ifindex(name
);
731 nl_msg_put_u32(&request
, IFLA_LINK
, ifindex
);
733 nl_msg_put_string(&request
, IFLA_IFNAME
, name
);
735 error
= nl_sock_transact(gre_descriptors
.nl_sock
, &request
, &reply
);
736 ofpbuf_uninit(&request
);
738 VLOG_WARN("couldn't transact netlink socket: %s", strerror(error
));
741 ofpbuf_delete(reply
);
749 destroy_gre_ioctl(const char *name
)
751 struct ip_tunnel_parm p
;
754 memset(&p
, 0, sizeof p
);
755 strncpy(p
.name
, name
, IFNAMSIZ
);
757 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
758 ifr
.ifr_ifru
.ifru_data
= (void *)&p
;
760 if (ioctl(gre_descriptors
.ioctl_fd
, SIOCDELGRETAP
, &ifr
) < 0) {
761 VLOG_WARN("couldn't do gre ioctl: %s\n", strerror(errno
));
769 destroy_tap(struct netdev_dev_linux
*netdev_dev
)
771 struct tap_state
*state
= &netdev_dev
->state
.tap
;
773 if (state
->fd
>= 0) {
779 destroy_gre(const char *name
)
781 if (gre_descriptors
.use_ioctl
) {
782 return destroy_gre_ioctl(name
);
784 return destroy_gre_netlink(name
);
788 /* Destroys the netdev device 'netdev_dev_'. */
790 netdev_linux_destroy(struct netdev_dev
*netdev_dev_
)
792 struct netdev_dev_linux
*netdev_dev
= netdev_dev_linux_cast(netdev_dev_
);
793 const char *type
= netdev_dev_get_type(netdev_dev_
);
795 if (!strcmp(type
, "system")) {
796 cache_notifier_refcount
--;
798 if (!cache_notifier_refcount
) {
799 rtnetlink_notifier_unregister(&netdev_linux_cache_notifier
);
801 } else if (!strcmp(type
, "tap")) {
802 destroy_tap(netdev_dev
);
803 } else if (!strcmp(type
, "gre")) {
804 destroy_gre(netdev_dev_get_name(&netdev_dev
->netdev_dev
));
811 netdev_linux_open(struct netdev_dev
*netdev_dev_
, int ethertype
,
812 struct netdev
**netdevp
)
814 struct netdev_dev_linux
*netdev_dev
= netdev_dev_linux_cast(netdev_dev_
);
815 struct netdev_linux
*netdev
;
816 enum netdev_flags flags
;
819 /* Allocate network device. */
820 netdev
= xzalloc(sizeof *netdev
);
822 netdev_init(&netdev
->netdev
, netdev_dev_
);
824 error
= netdev_get_flags(&netdev
->netdev
, &flags
);
825 if (error
== ENODEV
) {
829 if (!strcmp(netdev_dev_get_type(netdev_dev_
), "tap")) {
830 netdev
->fd
= netdev_dev
->state
.tap
.fd
;
831 } else if (ethertype
!= NETDEV_ETH_TYPE_NONE
) {
832 struct sockaddr_ll sll
;
836 /* Create file descriptor. */
837 protocol
= (ethertype
== NETDEV_ETH_TYPE_ANY
? ETH_P_ALL
838 : ethertype
== NETDEV_ETH_TYPE_802_2
? ETH_P_802_2
840 netdev
->fd
= socket(PF_PACKET
, SOCK_RAW
, htons(protocol
));
841 if (netdev
->fd
< 0) {
846 /* Set non-blocking mode. */
847 error
= set_nonblocking(netdev
->fd
);
852 /* Get ethernet device index. */
853 error
= get_ifindex(&netdev
->netdev
, &ifindex
);
858 /* Bind to specific ethernet device. */
859 memset(&sll
, 0, sizeof sll
);
860 sll
.sll_family
= AF_PACKET
;
861 sll
.sll_ifindex
= ifindex
;
863 (struct sockaddr
*) &sll
, sizeof sll
) < 0) {
865 VLOG_ERR("bind to %s failed: %s", netdev_dev_get_name(netdev_dev_
),
870 /* Between the socket() and bind() calls above, the socket receives all
871 * packets of the requested type on all system interfaces. We do not
872 * want to receive that data, but there is no way to avoid it. So we
873 * must now drain out the receive queue. */
874 error
= drain_rcvbuf(netdev
->fd
);
880 *netdevp
= &netdev
->netdev
;
884 netdev_uninit(&netdev
->netdev
, true);
888 /* Closes and destroys 'netdev'. */
890 netdev_linux_close(struct netdev
*netdev_
)
892 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
894 if (netdev
->fd
> 0 && strcmp(netdev_get_type(netdev_
), "tap")) {
900 /* Initializes 'svec' with a list of the names of all known network devices. */
902 netdev_linux_enumerate(struct svec
*svec
)
904 struct if_nameindex
*names
;
906 names
= if_nameindex();
910 for (i
= 0; names
[i
].if_name
!= NULL
; i
++) {
911 svec_add(svec
, names
[i
].if_name
);
913 if_freenameindex(names
);
916 VLOG_WARN("could not obtain list of network device names: %s",
923 netdev_linux_recv(struct netdev
*netdev_
, void *data
, size_t size
)
925 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
927 if (netdev
->fd
< 0) {
928 /* Device was opened with NETDEV_ETH_TYPE_NONE. */
933 ssize_t retval
= read(netdev
->fd
, data
, size
);
936 } else if (errno
!= EINTR
) {
937 if (errno
!= EAGAIN
) {
938 VLOG_WARN_RL(&rl
, "error receiving Ethernet packet on %s: %s",
939 strerror(errno
), netdev_get_name(netdev_
));
946 /* Registers with the poll loop to wake up from the next call to poll_block()
947 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
949 netdev_linux_recv_wait(struct netdev
*netdev_
)
951 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
952 if (netdev
->fd
>= 0) {
953 poll_fd_wait(netdev
->fd
, POLLIN
);
957 /* Discards all packets waiting to be received from 'netdev'. */
959 netdev_linux_drain(struct netdev
*netdev_
)
961 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
962 if (netdev
->fd
< 0) {
964 } else if (!strcmp(netdev_get_type(netdev_
), "tap")) {
966 int error
= netdev_linux_do_ioctl(netdev_get_name(netdev_
), &ifr
,
967 SIOCGIFTXQLEN
, "SIOCGIFTXQLEN");
971 drain_fd(netdev
->fd
, ifr
.ifr_qlen
);
974 return drain_rcvbuf(netdev
->fd
);
978 /* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
979 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
980 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
981 * the packet is too big or too small to transmit on the device.
983 * The caller retains ownership of 'buffer' in all cases.
985 * The kernel maintains a packet transmission queue, so the caller is not
986 * expected to do additional queuing of packets. */
988 netdev_linux_send(struct netdev
*netdev_
, const void *data
, size_t size
)
990 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
992 /* XXX should support sending even if 'ethertype' was NETDEV_ETH_TYPE_NONE.
994 if (netdev
->fd
< 0) {
999 ssize_t retval
= write(netdev
->fd
, data
, size
);
1001 /* The Linux AF_PACKET implementation never blocks waiting for room
1002 * for packets, instead returning ENOBUFS. Translate this into
1003 * EAGAIN for the caller. */
1004 if (errno
== ENOBUFS
) {
1006 } else if (errno
== EINTR
) {
1008 } else if (errno
!= EAGAIN
) {
1009 VLOG_WARN_RL(&rl
, "error sending Ethernet packet on %s: %s",
1010 netdev_get_name(netdev_
), strerror(errno
));
1013 } else if (retval
!= size
) {
1014 VLOG_WARN_RL(&rl
, "sent partial Ethernet packet (%zd bytes of "
1015 "%zu) on %s", retval
, size
, netdev_get_name(netdev_
));
1023 /* Registers with the poll loop to wake up from the next call to poll_block()
1024 * when the packet transmission queue has sufficient room to transmit a packet
1025 * with netdev_send().
1027 * The kernel maintains a packet transmission queue, so the client is not
1028 * expected to do additional queuing of packets. Thus, this function is
1029 * unlikely to ever be used. It is included for completeness. */
1031 netdev_linux_send_wait(struct netdev
*netdev_
)
1033 struct netdev_linux
*netdev
= netdev_linux_cast(netdev_
);
1034 if (netdev
->fd
< 0) {
1035 /* Nothing to do. */
1036 } else if (strcmp(netdev_get_type(netdev_
), "tap")) {
1037 poll_fd_wait(netdev
->fd
, POLLOUT
);
1039 /* TAP device always accepts packets.*/
1040 poll_immediate_wake();
1044 /* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
1045 * otherwise a positive errno value. */
1047 netdev_linux_set_etheraddr(struct netdev
*netdev_
,
1048 const uint8_t mac
[ETH_ADDR_LEN
])
1050 struct netdev_dev_linux
*netdev_dev
=
1051 netdev_dev_linux_cast(netdev_get_dev(netdev_
));
1054 if (!(netdev_dev
->cache_valid
& VALID_ETHERADDR
)
1055 || !eth_addr_equals(netdev_dev
->etheraddr
, mac
)) {
1056 error
= set_etheraddr(netdev_get_name(netdev_
), ARPHRD_ETHER
, mac
);
1058 netdev_dev
->cache_valid
|= VALID_ETHERADDR
;
1059 memcpy(netdev_dev
->etheraddr
, mac
, ETH_ADDR_LEN
);
1067 /* Returns a pointer to 'netdev''s MAC address. The caller must not modify or
1068 * free the returned buffer. */
1070 netdev_linux_get_etheraddr(const struct netdev
*netdev_
,
1071 uint8_t mac
[ETH_ADDR_LEN
])
1073 struct netdev_dev_linux
*netdev_dev
=
1074 netdev_dev_linux_cast(netdev_get_dev(netdev_
));
1075 if (!(netdev_dev
->cache_valid
& VALID_ETHERADDR
)) {
1076 int error
= get_etheraddr(netdev_get_name(netdev_
),
1077 netdev_dev
->etheraddr
);
1081 netdev_dev
->cache_valid
|= VALID_ETHERADDR
;
1083 memcpy(mac
, netdev_dev
->etheraddr
, ETH_ADDR_LEN
);
1087 /* Returns the maximum size of transmitted (and received) packets on 'netdev',
1088 * in bytes, not including the hardware header; thus, this is typically 1500
1089 * bytes for Ethernet devices. */
1091 netdev_linux_get_mtu(const struct netdev
*netdev_
, int *mtup
)
1093 struct netdev_dev_linux
*netdev_dev
=
1094 netdev_dev_linux_cast(netdev_get_dev(netdev_
));
1095 if (!(netdev_dev
->cache_valid
& VALID_MTU
)) {
1099 error
= netdev_linux_do_ioctl(netdev_get_name(netdev_
), &ifr
,
1100 SIOCGIFMTU
, "SIOCGIFMTU");
1104 netdev_dev
->mtu
= ifr
.ifr_mtu
;
1105 netdev_dev
->cache_valid
|= VALID_MTU
;
1107 *mtup
= netdev_dev
->mtu
;
1111 /* Returns the ifindex of 'netdev', if successful, as a positive number.
1112 * On failure, returns a negative errno value. */
1114 netdev_linux_get_ifindex(const struct netdev
*netdev
)
1118 error
= get_ifindex(netdev
, &ifindex
);
1119 return error
? -error
: ifindex
;
1123 netdev_linux_get_carrier(const struct netdev
*netdev_
, bool *carrier
)
1125 struct netdev_dev_linux
*netdev_dev
=
1126 netdev_dev_linux_cast(netdev_get_dev(netdev_
));
1131 if (!(netdev_dev
->cache_valid
& VALID_CARRIER
)) {
1135 fn
= xasprintf("/sys/class/net/%s/carrier",
1136 netdev_get_name(netdev_
));
1137 fd
= open(fn
, O_RDONLY
);
1140 VLOG_WARN_RL(&rl
, "%s: open failed: %s", fn
, strerror(error
));
1144 retval
= read(fd
, line
, sizeof line
);
1147 if (error
== EINVAL
) {
1148 /* This is the normal return value when we try to check carrier
1149 * if the network device is not up. */
1151 VLOG_WARN_RL(&rl
, "%s: read failed: %s", fn
, strerror(error
));
1154 } else if (retval
== 0) {
1156 VLOG_WARN_RL(&rl
, "%s: unexpected end of file", fn
);
1160 if (line
[0] != '0' && line
[0] != '1') {
1162 VLOG_WARN_RL(&rl
, "%s: value is %c (expected 0 or 1)",
1166 netdev_dev
->carrier
= line
[0] != '0';
1167 netdev_dev
->cache_valid
|= VALID_CARRIER
;
1169 *carrier
= netdev_dev
->carrier
;
1180 /* Check whether we can we use RTM_GETLINK to get network device statistics.
1181 * In pre-2.6.19 kernels, this was only available if wireless extensions were
1184 check_for_working_netlink_stats(void)
1186 /* Decide on the netdev_get_stats() implementation to use. Netlink is
1187 * preferable, so if that works, we'll use it. */
1188 int ifindex
= do_get_ifindex("lo");
1190 VLOG_WARN("failed to get ifindex for lo, "
1191 "obtaining netdev stats from proc");
1194 struct netdev_stats stats
;
1195 int error
= get_stats_via_netlink(ifindex
, &stats
);
1197 VLOG_DBG("obtaining netdev stats via rtnetlink");
1200 VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
1201 "via proc (you are probably running a pre-2.6.19 "
1202 "kernel)", strerror(error
));
1208 /* Retrieves current device stats for 'netdev'.
1210 * XXX All of the members of struct netdev_stats are 64 bits wide, but on
1211 * 32-bit architectures the Linux network stats are only 32 bits. */
1213 netdev_linux_get_stats(const struct netdev
*netdev_
,
1214 struct netdev_stats
*stats
)
1216 struct netdev_dev_linux
*netdev_dev
=
1217 netdev_dev_linux_cast(netdev_get_dev(netdev_
));
1218 static int use_netlink_stats
= -1;
1220 struct netdev_stats raw_stats
;
1221 struct netdev_stats
*collect_stats
= stats
;
1223 COVERAGE_INC(netdev_get_stats
);
1225 if (!(netdev_dev
->cache_valid
& VALID_IS_INTERNAL
)) {
1226 netdev_dev
->is_internal
= !strcmp(netdev_get_type(netdev_
), "tap");
1227 if (!netdev_dev
->is_internal
) {
1228 struct ethtool_drvinfo drvinfo
;
1230 memset(&drvinfo
, 0, sizeof drvinfo
);
1231 error
= netdev_linux_do_ethtool(netdev_get_name(netdev_
),
1232 (struct ethtool_cmd
*)&drvinfo
,
1234 "ETHTOOL_GDRVINFO");
1237 netdev_dev
->is_internal
= !strcmp(drvinfo
.driver
,
1242 netdev_dev
->cache_valid
|= VALID_IS_INTERNAL
;
1245 if (netdev_dev
->is_internal
) {
1246 collect_stats
= &raw_stats
;
1249 if (use_netlink_stats
< 0) {
1250 use_netlink_stats
= check_for_working_netlink_stats();
1252 if (use_netlink_stats
) {
1255 error
= get_ifindex(netdev_
, &ifindex
);
1257 error
= get_stats_via_netlink(ifindex
, collect_stats
);
1260 error
= get_stats_via_proc(netdev_get_name(netdev_
), collect_stats
);
1263 /* If this port is an internal port then the transmit and receive stats
1264 * will appear to be swapped relative to the other ports since we are the
1265 * one sending the data, not a remote computer. For consistency, we swap
1266 * them back here. */
1267 if (!error
&& netdev_dev
->is_internal
) {
1268 stats
->rx_packets
= raw_stats
.tx_packets
;
1269 stats
->tx_packets
= raw_stats
.rx_packets
;
1270 stats
->rx_bytes
= raw_stats
.tx_bytes
;
1271 stats
->tx_bytes
= raw_stats
.rx_bytes
;
1272 stats
->rx_errors
= raw_stats
.tx_errors
;
1273 stats
->tx_errors
= raw_stats
.rx_errors
;
1274 stats
->rx_dropped
= raw_stats
.tx_dropped
;
1275 stats
->tx_dropped
= raw_stats
.rx_dropped
;
1276 stats
->multicast
= raw_stats
.multicast
;
1277 stats
->collisions
= raw_stats
.collisions
;
1278 stats
->rx_length_errors
= 0;
1279 stats
->rx_over_errors
= 0;
1280 stats
->rx_crc_errors
= 0;
1281 stats
->rx_frame_errors
= 0;
1282 stats
->rx_fifo_errors
= 0;
1283 stats
->rx_missed_errors
= 0;
1284 stats
->tx_aborted_errors
= 0;
1285 stats
->tx_carrier_errors
= 0;
1286 stats
->tx_fifo_errors
= 0;
1287 stats
->tx_heartbeat_errors
= 0;
1288 stats
->tx_window_errors
= 0;
1294 /* Stores the features supported by 'netdev' into each of '*current',
1295 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
1296 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
1297 * successful, otherwise a positive errno value. */
1299 netdev_linux_get_features(struct netdev
*netdev
,
1300 uint32_t *current
, uint32_t *advertised
,
1301 uint32_t *supported
, uint32_t *peer
)
1303 struct ethtool_cmd ecmd
;
1306 memset(&ecmd
, 0, sizeof ecmd
);
1307 error
= netdev_linux_do_ethtool(netdev_get_name(netdev
), &ecmd
,
1308 ETHTOOL_GSET
, "ETHTOOL_GSET");
1313 /* Supported features. */
1315 if (ecmd
.supported
& SUPPORTED_10baseT_Half
) {
1316 *supported
|= OFPPF_10MB_HD
;
1318 if (ecmd
.supported
& SUPPORTED_10baseT_Full
) {
1319 *supported
|= OFPPF_10MB_FD
;
1321 if (ecmd
.supported
& SUPPORTED_100baseT_Half
) {
1322 *supported
|= OFPPF_100MB_HD
;
1324 if (ecmd
.supported
& SUPPORTED_100baseT_Full
) {
1325 *supported
|= OFPPF_100MB_FD
;
1327 if (ecmd
.supported
& SUPPORTED_1000baseT_Half
) {
1328 *supported
|= OFPPF_1GB_HD
;
1330 if (ecmd
.supported
& SUPPORTED_1000baseT_Full
) {
1331 *supported
|= OFPPF_1GB_FD
;
1333 if (ecmd
.supported
& SUPPORTED_10000baseT_Full
) {
1334 *supported
|= OFPPF_10GB_FD
;
1336 if (ecmd
.supported
& SUPPORTED_TP
) {
1337 *supported
|= OFPPF_COPPER
;
1339 if (ecmd
.supported
& SUPPORTED_FIBRE
) {
1340 *supported
|= OFPPF_FIBER
;
1342 if (ecmd
.supported
& SUPPORTED_Autoneg
) {
1343 *supported
|= OFPPF_AUTONEG
;
1345 if (ecmd
.supported
& SUPPORTED_Pause
) {
1346 *supported
|= OFPPF_PAUSE
;
1348 if (ecmd
.supported
& SUPPORTED_Asym_Pause
) {
1349 *supported
|= OFPPF_PAUSE_ASYM
;
1352 /* Advertised features. */
1354 if (ecmd
.advertising
& ADVERTISED_10baseT_Half
) {
1355 *advertised
|= OFPPF_10MB_HD
;
1357 if (ecmd
.advertising
& ADVERTISED_10baseT_Full
) {
1358 *advertised
|= OFPPF_10MB_FD
;
1360 if (ecmd
.advertising
& ADVERTISED_100baseT_Half
) {
1361 *advertised
|= OFPPF_100MB_HD
;
1363 if (ecmd
.advertising
& ADVERTISED_100baseT_Full
) {
1364 *advertised
|= OFPPF_100MB_FD
;
1366 if (ecmd
.advertising
& ADVERTISED_1000baseT_Half
) {
1367 *advertised
|= OFPPF_1GB_HD
;
1369 if (ecmd
.advertising
& ADVERTISED_1000baseT_Full
) {
1370 *advertised
|= OFPPF_1GB_FD
;
1372 if (ecmd
.advertising
& ADVERTISED_10000baseT_Full
) {
1373 *advertised
|= OFPPF_10GB_FD
;
1375 if (ecmd
.advertising
& ADVERTISED_TP
) {
1376 *advertised
|= OFPPF_COPPER
;
1378 if (ecmd
.advertising
& ADVERTISED_FIBRE
) {
1379 *advertised
|= OFPPF_FIBER
;
1381 if (ecmd
.advertising
& ADVERTISED_Autoneg
) {
1382 *advertised
|= OFPPF_AUTONEG
;
1384 if (ecmd
.advertising
& ADVERTISED_Pause
) {
1385 *advertised
|= OFPPF_PAUSE
;
1387 if (ecmd
.advertising
& ADVERTISED_Asym_Pause
) {
1388 *advertised
|= OFPPF_PAUSE_ASYM
;
1391 /* Current settings. */
1392 if (ecmd
.speed
== SPEED_10
) {
1393 *current
= ecmd
.duplex
? OFPPF_10MB_FD
: OFPPF_10MB_HD
;
1394 } else if (ecmd
.speed
== SPEED_100
) {
1395 *current
= ecmd
.duplex
? OFPPF_100MB_FD
: OFPPF_100MB_HD
;
1396 } else if (ecmd
.speed
== SPEED_1000
) {
1397 *current
= ecmd
.duplex
? OFPPF_1GB_FD
: OFPPF_1GB_HD
;
1398 } else if (ecmd
.speed
== SPEED_10000
) {
1399 *current
= OFPPF_10GB_FD
;
1404 if (ecmd
.port
== PORT_TP
) {
1405 *current
|= OFPPF_COPPER
;
1406 } else if (ecmd
.port
== PORT_FIBRE
) {
1407 *current
|= OFPPF_FIBER
;
1411 *current
|= OFPPF_AUTONEG
;
1414 /* Peer advertisements. */
1415 *peer
= 0; /* XXX */
1420 /* Set the features advertised by 'netdev' to 'advertise'. */
1422 netdev_linux_set_advertisements(struct netdev
*netdev
, uint32_t advertise
)
1424 struct ethtool_cmd ecmd
;
1427 memset(&ecmd
, 0, sizeof ecmd
);
1428 error
= netdev_linux_do_ethtool(netdev_get_name(netdev
), &ecmd
,
1429 ETHTOOL_GSET
, "ETHTOOL_GSET");
1434 ecmd
.advertising
= 0;
1435 if (advertise
& OFPPF_10MB_HD
) {
1436 ecmd
.advertising
|= ADVERTISED_10baseT_Half
;
1438 if (advertise
& OFPPF_10MB_FD
) {
1439 ecmd
.advertising
|= ADVERTISED_10baseT_Full
;
1441 if (advertise
& OFPPF_100MB_HD
) {
1442 ecmd
.advertising
|= ADVERTISED_100baseT_Half
;
1444 if (advertise
& OFPPF_100MB_FD
) {
1445 ecmd
.advertising
|= ADVERTISED_100baseT_Full
;
1447 if (advertise
& OFPPF_1GB_HD
) {
1448 ecmd
.advertising
|= ADVERTISED_1000baseT_Half
;
1450 if (advertise
& OFPPF_1GB_FD
) {
1451 ecmd
.advertising
|= ADVERTISED_1000baseT_Full
;
1453 if (advertise
& OFPPF_10GB_FD
) {
1454 ecmd
.advertising
|= ADVERTISED_10000baseT_Full
;
1456 if (advertise
& OFPPF_COPPER
) {
1457 ecmd
.advertising
|= ADVERTISED_TP
;
1459 if (advertise
& OFPPF_FIBER
) {
1460 ecmd
.advertising
|= ADVERTISED_FIBRE
;
1462 if (advertise
& OFPPF_AUTONEG
) {
1463 ecmd
.advertising
|= ADVERTISED_Autoneg
;
1465 if (advertise
& OFPPF_PAUSE
) {
1466 ecmd
.advertising
|= ADVERTISED_Pause
;
1468 if (advertise
& OFPPF_PAUSE_ASYM
) {
1469 ecmd
.advertising
|= ADVERTISED_Asym_Pause
;
1471 return netdev_linux_do_ethtool(netdev_get_name(netdev
), &ecmd
,
1472 ETHTOOL_SSET
, "ETHTOOL_SSET");
1475 /* If 'netdev_name' is the name of a VLAN network device (e.g. one created with
1476 * vconfig(8)), sets '*vlan_vid' to the VLAN VID associated with that device
1477 * and returns 0. Otherwise returns a errno value (specifically ENOENT if
1478 * 'netdev_name' is the name of a network device that is not a VLAN device) and
1479 * sets '*vlan_vid' to -1. */
1481 netdev_linux_get_vlan_vid(const struct netdev
*netdev
, int *vlan_vid
)
1483 const char *netdev_name
= netdev_get_name(netdev
);
1484 struct ds line
= DS_EMPTY_INITIALIZER
;
1485 FILE *stream
= NULL
;
1489 COVERAGE_INC(netdev_get_vlan_vid
);
1490 fn
= xasprintf("/proc/net/vlan/%s", netdev_name
);
1491 stream
= fopen(fn
, "r");
1497 if (ds_get_line(&line
, stream
)) {
1498 if (ferror(stream
)) {
1500 VLOG_ERR_RL(&rl
, "error reading \"%s\": %s", fn
, strerror(errno
));
1503 VLOG_ERR_RL(&rl
, "unexpected end of file reading \"%s\"", fn
);
1508 if (!sscanf(ds_cstr(&line
), "%*s VID: %d", vlan_vid
)) {
1510 VLOG_ERR_RL(&rl
, "parse error reading \"%s\" line 1: \"%s\"",
1511 fn
, ds_cstr(&line
));
1529 #define POLICE_ADD_CMD "/sbin/tc qdisc add dev %s handle ffff: ingress"
1530 #define POLICE_CONFIG_CMD "/sbin/tc filter add dev %s parent ffff: protocol ip prio 50 u32 match ip src 0.0.0.0/0 police rate %dkbit burst %dk mtu 65535 drop flowid :1"
1531 /* We redirect stderr to /dev/null because we often want to remove all
1532 * traffic control configuration on a port so its in a known state. If
1533 * this done when there is no such configuration, tc complains, so we just
1536 #define POLICE_DEL_CMD "/sbin/tc qdisc del dev %s handle ffff: ingress 2>/dev/null"
1538 /* Attempts to set input rate limiting (policing) policy. */
1540 netdev_linux_set_policing(struct netdev
*netdev
,
1541 uint32_t kbits_rate
, uint32_t kbits_burst
)
1543 const char *netdev_name
= netdev_get_name(netdev
);
1546 COVERAGE_INC(netdev_set_policing
);
1549 /* Default to 1000 kilobits if not specified. */
1553 /* xxx This should be more careful about only adding if it
1554 * xxx actually exists, as opposed to always deleting it. */
1555 snprintf(command
, sizeof(command
), POLICE_DEL_CMD
, netdev_name
);
1556 if (system(command
) == -1) {
1557 VLOG_WARN_RL(&rl
, "%s: problem removing policing", netdev_name
);
1560 snprintf(command
, sizeof(command
), POLICE_ADD_CMD
, netdev_name
);
1561 if (system(command
) != 0) {
1562 VLOG_WARN_RL(&rl
, "%s: problem adding policing", netdev_name
);
1566 snprintf(command
, sizeof(command
), POLICE_CONFIG_CMD
, netdev_name
,
1567 kbits_rate
, kbits_burst
);
1568 if (system(command
) != 0) {
1569 VLOG_WARN_RL(&rl
, "%s: problem configuring policing",
1574 snprintf(command
, sizeof(command
), POLICE_DEL_CMD
, netdev_name
);
1575 if (system(command
) == -1) {
1576 VLOG_WARN_RL(&rl
, "%s: problem removing policing", netdev_name
);
1584 netdev_linux_get_in4(const struct netdev
*netdev_
,
1585 struct in_addr
*address
, struct in_addr
*netmask
)
1587 struct netdev_dev_linux
*netdev_dev
=
1588 netdev_dev_linux_cast(netdev_get_dev(netdev_
));
1590 if (!(netdev_dev
->cache_valid
& VALID_IN4
)) {
1593 error
= netdev_linux_get_ipv4(netdev_
, &netdev_dev
->address
,
1594 SIOCGIFADDR
, "SIOCGIFADDR");
1599 error
= netdev_linux_get_ipv4(netdev_
, &netdev_dev
->netmask
,
1600 SIOCGIFNETMASK
, "SIOCGIFNETMASK");
1605 netdev_dev
->cache_valid
|= VALID_IN4
;
1607 *address
= netdev_dev
->address
;
1608 *netmask
= netdev_dev
->netmask
;
1609 return address
->s_addr
== INADDR_ANY
? EADDRNOTAVAIL
: 0;
1613 netdev_linux_set_in4(struct netdev
*netdev_
, struct in_addr address
,
1614 struct in_addr netmask
)
1616 struct netdev_dev_linux
*netdev_dev
=
1617 netdev_dev_linux_cast(netdev_get_dev(netdev_
));
1620 error
= do_set_addr(netdev_
, SIOCSIFADDR
, "SIOCSIFADDR", address
);
1622 netdev_dev
->cache_valid
|= VALID_IN4
;
1623 netdev_dev
->address
= address
;
1624 netdev_dev
->netmask
= netmask
;
1625 if (address
.s_addr
!= INADDR_ANY
) {
1626 error
= do_set_addr(netdev_
, SIOCSIFNETMASK
,
1627 "SIOCSIFNETMASK", netmask
);
1634 parse_if_inet6_line(const char *line
,
1635 struct in6_addr
*in6
, char ifname
[16 + 1])
1637 uint8_t *s6
= in6
->s6_addr
;
1638 #define X8 "%2"SCNx8
1640 " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
1641 "%*x %*x %*x %*x %16s\n",
1642 &s6
[0], &s6
[1], &s6
[2], &s6
[3],
1643 &s6
[4], &s6
[5], &s6
[6], &s6
[7],
1644 &s6
[8], &s6
[9], &s6
[10], &s6
[11],
1645 &s6
[12], &s6
[13], &s6
[14], &s6
[15],
1649 /* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
1650 * 'in6' is non-null) and returns true. Otherwise, returns false. */
1652 netdev_linux_get_in6(const struct netdev
*netdev_
, struct in6_addr
*in6
)
1654 struct netdev_dev_linux
*netdev_dev
=
1655 netdev_dev_linux_cast(netdev_get_dev(netdev_
));
1656 if (!(netdev_dev
->cache_valid
& VALID_IN6
)) {
1660 netdev_dev
->in6
= in6addr_any
;
1662 file
= fopen("/proc/net/if_inet6", "r");
1664 const char *name
= netdev_get_name(netdev_
);
1665 while (fgets(line
, sizeof line
, file
)) {
1666 struct in6_addr in6
;
1667 char ifname
[16 + 1];
1668 if (parse_if_inet6_line(line
, &in6
, ifname
)
1669 && !strcmp(name
, ifname
))
1671 netdev_dev
->in6
= in6
;
1677 netdev_dev
->cache_valid
|= VALID_IN6
;
1679 *in6
= netdev_dev
->in6
;
1684 make_in4_sockaddr(struct sockaddr
*sa
, struct in_addr addr
)
1686 struct sockaddr_in sin
;
1687 memset(&sin
, 0, sizeof sin
);
1688 sin
.sin_family
= AF_INET
;
1689 sin
.sin_addr
= addr
;
1692 memset(sa
, 0, sizeof *sa
);
1693 memcpy(sa
, &sin
, sizeof sin
);
1697 do_set_addr(struct netdev
*netdev
,
1698 int ioctl_nr
, const char *ioctl_name
, struct in_addr addr
)
1701 strncpy(ifr
.ifr_name
, netdev_get_name(netdev
), sizeof ifr
.ifr_name
);
1702 make_in4_sockaddr(&ifr
.ifr_addr
, addr
);
1704 return netdev_linux_do_ioctl(netdev_get_name(netdev
), &ifr
, ioctl_nr
,
1708 /* Adds 'router' as a default IP gateway. */
1710 netdev_linux_add_router(struct netdev
*netdev OVS_UNUSED
, struct in_addr router
)
1712 struct in_addr any
= { INADDR_ANY
};
1716 memset(&rt
, 0, sizeof rt
);
1717 make_in4_sockaddr(&rt
.rt_dst
, any
);
1718 make_in4_sockaddr(&rt
.rt_gateway
, router
);
1719 make_in4_sockaddr(&rt
.rt_genmask
, any
);
1720 rt
.rt_flags
= RTF_UP
| RTF_GATEWAY
;
1721 COVERAGE_INC(netdev_add_router
);
1722 error
= ioctl(af_inet_sock
, SIOCADDRT
, &rt
) < 0 ? errno
: 0;
1724 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error
));
1730 netdev_linux_get_next_hop(const struct in_addr
*host
, struct in_addr
*next_hop
,
1733 static const char fn
[] = "/proc/net/route";
1738 *netdev_name
= NULL
;
1739 stream
= fopen(fn
, "r");
1740 if (stream
== NULL
) {
1741 VLOG_WARN_RL(&rl
, "%s: open failed: %s", fn
, strerror(errno
));
1746 while (fgets(line
, sizeof line
, stream
)) {
1749 uint32_t dest
, gateway
, mask
;
1750 int refcnt
, metric
, mtu
;
1751 unsigned int flags
, use
, window
, irtt
;
1754 "%16s %"SCNx32
" %"SCNx32
" %04X %d %u %d %"SCNx32
1756 iface
, &dest
, &gateway
, &flags
, &refcnt
,
1757 &use
, &metric
, &mask
, &mtu
, &window
, &irtt
) != 11) {
1759 VLOG_WARN_RL(&rl
, "%s: could not parse line %d: %s",
1763 if (!(flags
& RTF_UP
)) {
1764 /* Skip routes that aren't up. */
1768 /* The output of 'dest', 'mask', and 'gateway' were given in
1769 * network byte order, so we don't need need any endian
1770 * conversions here. */
1771 if ((dest
& mask
) == (host
->s_addr
& mask
)) {
1773 /* The host is directly reachable. */
1774 next_hop
->s_addr
= 0;
1776 /* To reach the host, we must go through a gateway. */
1777 next_hop
->s_addr
= gateway
;
1779 *netdev_name
= xstrdup(iface
);
1790 /* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1791 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1792 * returns 0. Otherwise, it returns a positive errno value; in particular,
1793 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
1795 netdev_linux_arp_lookup(const struct netdev
*netdev
,
1796 uint32_t ip
, uint8_t mac
[ETH_ADDR_LEN
])
1799 struct sockaddr_in sin
;
1802 memset(&r
, 0, sizeof r
);
1803 sin
.sin_family
= AF_INET
;
1804 sin
.sin_addr
.s_addr
= ip
;
1806 memcpy(&r
.arp_pa
, &sin
, sizeof sin
);
1807 r
.arp_ha
.sa_family
= ARPHRD_ETHER
;
1809 strncpy(r
.arp_dev
, netdev_get_name(netdev
), sizeof r
.arp_dev
);
1810 COVERAGE_INC(netdev_arp_lookup
);
1811 retval
= ioctl(af_inet_sock
, SIOCGARP
, &r
) < 0 ? errno
: 0;
1813 memcpy(mac
, r
.arp_ha
.sa_data
, ETH_ADDR_LEN
);
1814 } else if (retval
!= ENXIO
) {
1815 VLOG_WARN_RL(&rl
, "%s: could not look up ARP entry for "IP_FMT
": %s",
1816 netdev_get_name(netdev
), IP_ARGS(&ip
), strerror(retval
));
1822 nd_to_iff_flags(enum netdev_flags nd
)
1825 if (nd
& NETDEV_UP
) {
1828 if (nd
& NETDEV_PROMISC
) {
1835 iff_to_nd_flags(int iff
)
1837 enum netdev_flags nd
= 0;
1841 if (iff
& IFF_PROMISC
) {
1842 nd
|= NETDEV_PROMISC
;
1848 netdev_linux_update_flags(struct netdev
*netdev
, enum netdev_flags off
,
1849 enum netdev_flags on
, enum netdev_flags
*old_flagsp
)
1851 int old_flags
, new_flags
;
1854 error
= get_flags(netdev
, &old_flags
);
1856 *old_flagsp
= iff_to_nd_flags(old_flags
);
1857 new_flags
= (old_flags
& ~nd_to_iff_flags(off
)) | nd_to_iff_flags(on
);
1858 if (new_flags
!= old_flags
) {
1859 error
= set_flags(netdev
, new_flags
);
1866 poll_notify(struct list
*list
)
1868 struct netdev_linux_notifier
*notifier
;
1869 LIST_FOR_EACH (notifier
, struct netdev_linux_notifier
, node
, list
) {
1870 struct netdev_notifier
*n
= ¬ifier
->notifier
;
1876 netdev_linux_poll_cb(const struct rtnetlink_change
*change
,
1877 void *aux OVS_UNUSED
)
1880 struct list
*list
= shash_find_data(&netdev_linux_notifiers
,
1886 struct shash_node
*node
;
1887 SHASH_FOR_EACH (node
, &netdev_linux_notifiers
) {
1888 poll_notify(node
->data
);
1894 netdev_linux_poll_add(struct netdev
*netdev
,
1895 void (*cb
)(struct netdev_notifier
*), void *aux
,
1896 struct netdev_notifier
**notifierp
)
1898 const char *netdev_name
= netdev_get_name(netdev
);
1899 struct netdev_linux_notifier
*notifier
;
1902 if (shash_is_empty(&netdev_linux_notifiers
)) {
1903 int error
= rtnetlink_notifier_register(&netdev_linux_poll_notifier
,
1904 netdev_linux_poll_cb
, NULL
);
1910 list
= shash_find_data(&netdev_linux_notifiers
, netdev_name
);
1912 list
= xmalloc(sizeof *list
);
1914 shash_add(&netdev_linux_notifiers
, netdev_name
, list
);
1917 notifier
= xmalloc(sizeof *notifier
);
1918 netdev_notifier_init(¬ifier
->notifier
, netdev
, cb
, aux
);
1919 list_push_back(list
, ¬ifier
->node
);
1920 *notifierp
= ¬ifier
->notifier
;
1925 netdev_linux_poll_remove(struct netdev_notifier
*notifier_
)
1927 struct netdev_linux_notifier
*notifier
=
1928 CONTAINER_OF(notifier_
, struct netdev_linux_notifier
, notifier
);
1931 /* Remove 'notifier' from its list. */
1932 list
= list_remove(¬ifier
->node
);
1933 if (list_is_empty(list
)) {
1934 /* The list is now empty. Remove it from the hash and free it. */
1935 const char *netdev_name
= netdev_get_name(notifier
->notifier
.netdev
);
1936 shash_delete(&netdev_linux_notifiers
,
1937 shash_find(&netdev_linux_notifiers
, netdev_name
));
1942 /* If that was the last notifier, unregister. */
1943 if (shash_is_empty(&netdev_linux_notifiers
)) {
1944 rtnetlink_notifier_unregister(&netdev_linux_poll_notifier
);
1948 const struct netdev_class netdev_linux_class
= {
1955 netdev_linux_create_system
,
1956 netdev_linux_destroy
,
1957 NULL
, /* reconfigure */
1962 netdev_linux_enumerate
,
1965 netdev_linux_recv_wait
,
1969 netdev_linux_send_wait
,
1971 netdev_linux_set_etheraddr
,
1972 netdev_linux_get_etheraddr
,
1973 netdev_linux_get_mtu
,
1974 netdev_linux_get_ifindex
,
1975 netdev_linux_get_carrier
,
1976 netdev_linux_get_stats
,
1978 netdev_linux_get_features
,
1979 netdev_linux_set_advertisements
,
1980 netdev_linux_get_vlan_vid
,
1981 netdev_linux_set_policing
,
1983 netdev_linux_get_in4
,
1984 netdev_linux_set_in4
,
1985 netdev_linux_get_in6
,
1986 netdev_linux_add_router
,
1987 netdev_linux_get_next_hop
,
1988 netdev_linux_arp_lookup
,
1990 netdev_linux_update_flags
,
1992 netdev_linux_poll_add
,
1993 netdev_linux_poll_remove
,
1996 const struct netdev_class netdev_tap_class
= {
2003 netdev_linux_create_tap
,
2004 netdev_linux_destroy
,
2005 NULL
, /* reconfigure */
2010 NULL
, /* enumerate */
2013 netdev_linux_recv_wait
,
2017 netdev_linux_send_wait
,
2019 netdev_linux_set_etheraddr
,
2020 netdev_linux_get_etheraddr
,
2021 netdev_linux_get_mtu
,
2022 netdev_linux_get_ifindex
,
2023 netdev_linux_get_carrier
,
2024 netdev_linux_get_stats
,
2026 netdev_linux_get_features
,
2027 netdev_linux_set_advertisements
,
2028 netdev_linux_get_vlan_vid
,
2029 netdev_linux_set_policing
,
2031 netdev_linux_get_in4
,
2032 netdev_linux_set_in4
,
2033 netdev_linux_get_in6
,
2034 netdev_linux_add_router
,
2035 netdev_linux_get_next_hop
,
2036 netdev_linux_arp_lookup
,
2038 netdev_linux_update_flags
,
2040 netdev_linux_poll_add
,
2041 netdev_linux_poll_remove
,
2044 const struct netdev_class netdev_gre_class
= {
2051 netdev_linux_create_gre
,
2052 netdev_linux_destroy
,
2053 netdev_linux_reconfigure_gre
,
2058 NULL
, /* enumerate */
2061 netdev_linux_recv_wait
,
2065 netdev_linux_send_wait
,
2067 netdev_linux_set_etheraddr
,
2068 netdev_linux_get_etheraddr
,
2069 netdev_linux_get_mtu
,
2070 netdev_linux_get_ifindex
,
2071 netdev_linux_get_carrier
,
2072 netdev_linux_get_stats
,
2074 netdev_linux_get_features
,
2075 netdev_linux_set_advertisements
,
2076 netdev_linux_get_vlan_vid
,
2077 netdev_linux_set_policing
,
2079 netdev_linux_get_in4
,
2080 netdev_linux_set_in4
,
2081 netdev_linux_get_in6
,
2082 netdev_linux_add_router
,
2083 netdev_linux_get_next_hop
,
2084 netdev_linux_arp_lookup
,
2086 netdev_linux_update_flags
,
2088 netdev_linux_poll_add
,
2089 netdev_linux_poll_remove
,
2093 get_stats_via_netlink(int ifindex
, struct netdev_stats
*stats
)
2095 /* Policy for RTNLGRP_LINK messages.
2097 * There are *many* more fields in these messages, but currently we only
2098 * care about these fields. */
2099 static const struct nl_policy rtnlgrp_link_policy
[] = {
2100 [IFLA_IFNAME
] = { .type
= NL_A_STRING
, .optional
= false },
2101 [IFLA_STATS
] = { .type
= NL_A_UNSPEC
, .optional
= true,
2102 .min_len
= sizeof(struct rtnl_link_stats
) },
2106 static struct nl_sock
*rtnl_sock
;
2107 struct ofpbuf request
;
2108 struct ofpbuf
*reply
;
2109 struct ifinfomsg
*ifi
;
2110 const struct rtnl_link_stats
*rtnl_stats
;
2111 struct nlattr
*attrs
[ARRAY_SIZE(rtnlgrp_link_policy
)];
2115 error
= nl_sock_create(NETLINK_ROUTE
, 0, 0, 0, &rtnl_sock
);
2117 VLOG_ERR_RL(&rl
, "failed to create rtnetlink socket: %s",
2123 ofpbuf_init(&request
, 0);
2124 nl_msg_put_nlmsghdr(&request
, rtnl_sock
, sizeof *ifi
,
2125 RTM_GETLINK
, NLM_F_REQUEST
);
2126 ifi
= ofpbuf_put_zeros(&request
, sizeof *ifi
);
2127 ifi
->ifi_family
= PF_UNSPEC
;
2128 ifi
->ifi_index
= ifindex
;
2129 error
= nl_sock_transact(rtnl_sock
, &request
, &reply
);
2130 ofpbuf_uninit(&request
);
2135 if (!nl_policy_parse(reply
, NLMSG_HDRLEN
+ sizeof(struct ifinfomsg
),
2136 rtnlgrp_link_policy
,
2137 attrs
, ARRAY_SIZE(rtnlgrp_link_policy
))) {
2138 ofpbuf_delete(reply
);
2142 if (!attrs
[IFLA_STATS
]) {
2143 VLOG_WARN_RL(&rl
, "RTM_GETLINK reply lacks stats");
2144 ofpbuf_delete(reply
);
2148 rtnl_stats
= nl_attr_get(attrs
[IFLA_STATS
]);
2149 stats
->rx_packets
= rtnl_stats
->rx_packets
;
2150 stats
->tx_packets
= rtnl_stats
->tx_packets
;
2151 stats
->rx_bytes
= rtnl_stats
->rx_bytes
;
2152 stats
->tx_bytes
= rtnl_stats
->tx_bytes
;
2153 stats
->rx_errors
= rtnl_stats
->rx_errors
;
2154 stats
->tx_errors
= rtnl_stats
->tx_errors
;
2155 stats
->rx_dropped
= rtnl_stats
->rx_dropped
;
2156 stats
->tx_dropped
= rtnl_stats
->tx_dropped
;
2157 stats
->multicast
= rtnl_stats
->multicast
;
2158 stats
->collisions
= rtnl_stats
->collisions
;
2159 stats
->rx_length_errors
= rtnl_stats
->rx_length_errors
;
2160 stats
->rx_over_errors
= rtnl_stats
->rx_over_errors
;
2161 stats
->rx_crc_errors
= rtnl_stats
->rx_crc_errors
;
2162 stats
->rx_frame_errors
= rtnl_stats
->rx_frame_errors
;
2163 stats
->rx_fifo_errors
= rtnl_stats
->rx_fifo_errors
;
2164 stats
->rx_missed_errors
= rtnl_stats
->rx_missed_errors
;
2165 stats
->tx_aborted_errors
= rtnl_stats
->tx_aborted_errors
;
2166 stats
->tx_carrier_errors
= rtnl_stats
->tx_carrier_errors
;
2167 stats
->tx_fifo_errors
= rtnl_stats
->tx_fifo_errors
;
2168 stats
->tx_heartbeat_errors
= rtnl_stats
->tx_heartbeat_errors
;
2169 stats
->tx_window_errors
= rtnl_stats
->tx_window_errors
;
2171 ofpbuf_delete(reply
);
2177 get_stats_via_proc(const char *netdev_name
, struct netdev_stats
*stats
)
2179 static const char fn
[] = "/proc/net/dev";
2184 stream
= fopen(fn
, "r");
2186 VLOG_WARN_RL(&rl
, "%s: open failed: %s", fn
, strerror(errno
));
2191 while (fgets(line
, sizeof line
, stream
)) {
2194 #define X64 "%"SCNu64
2197 X64 X64 X64 X64 X64 X64 X64
"%*u"
2198 X64 X64 X64 X64 X64 X64 X64
"%*u",
2204 &stats
->rx_fifo_errors
,
2205 &stats
->rx_frame_errors
,
2211 &stats
->tx_fifo_errors
,
2213 &stats
->tx_carrier_errors
) != 15) {
2214 VLOG_WARN_RL(&rl
, "%s:%d: parse error", fn
, ln
);
2215 } else if (!strcmp(devname
, netdev_name
)) {
2216 stats
->rx_length_errors
= UINT64_MAX
;
2217 stats
->rx_over_errors
= UINT64_MAX
;
2218 stats
->rx_crc_errors
= UINT64_MAX
;
2219 stats
->rx_missed_errors
= UINT64_MAX
;
2220 stats
->tx_aborted_errors
= UINT64_MAX
;
2221 stats
->tx_heartbeat_errors
= UINT64_MAX
;
2222 stats
->tx_window_errors
= UINT64_MAX
;
2228 VLOG_WARN_RL(&rl
, "%s: no stats for %s", fn
, netdev_name
);
2234 get_flags(const struct netdev
*netdev
, int *flags
)
2239 error
= netdev_linux_do_ioctl(netdev_get_name(netdev
), &ifr
, SIOCGIFFLAGS
,
2241 *flags
= ifr
.ifr_flags
;
2246 set_flags(struct netdev
*netdev
, int flags
)
2250 ifr
.ifr_flags
= flags
;
2251 return netdev_linux_do_ioctl(netdev_get_name(netdev
), &ifr
, SIOCSIFFLAGS
,
2256 do_get_ifindex(const char *netdev_name
)
2260 strncpy(ifr
.ifr_name
, netdev_name
, sizeof ifr
.ifr_name
);
2261 COVERAGE_INC(netdev_get_ifindex
);
2262 if (ioctl(af_inet_sock
, SIOCGIFINDEX
, &ifr
) < 0) {
2263 VLOG_WARN_RL(&rl
, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
2264 netdev_name
, strerror(errno
));
2267 return ifr
.ifr_ifindex
;
2271 get_ifindex(const struct netdev
*netdev_
, int *ifindexp
)
2273 struct netdev_dev_linux
*netdev_dev
=
2274 netdev_dev_linux_cast(netdev_get_dev(netdev_
));
2276 if (!(netdev_dev
->cache_valid
& VALID_IFINDEX
)) {
2277 int ifindex
= do_get_ifindex(netdev_get_name(netdev_
));
2281 netdev_dev
->cache_valid
|= VALID_IFINDEX
;
2282 netdev_dev
->ifindex
= ifindex
;
2284 *ifindexp
= netdev_dev
->ifindex
;
2289 get_etheraddr(const char *netdev_name
, uint8_t ea
[ETH_ADDR_LEN
])
2294 memset(&ifr
, 0, sizeof ifr
);
2295 strncpy(ifr
.ifr_name
, netdev_name
, sizeof ifr
.ifr_name
);
2296 COVERAGE_INC(netdev_get_hwaddr
);
2297 if (ioctl(af_inet_sock
, SIOCGIFHWADDR
, &ifr
) < 0) {
2298 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
2299 netdev_name
, strerror(errno
));
2302 hwaddr_family
= ifr
.ifr_hwaddr
.sa_family
;
2303 if (hwaddr_family
!= AF_UNSPEC
&& hwaddr_family
!= ARPHRD_ETHER
) {
2304 VLOG_WARN("%s device has unknown hardware address family %d",
2305 netdev_name
, hwaddr_family
);
2307 memcpy(ea
, ifr
.ifr_hwaddr
.sa_data
, ETH_ADDR_LEN
);
2312 set_etheraddr(const char *netdev_name
, int hwaddr_family
,
2313 const uint8_t mac
[ETH_ADDR_LEN
])
2317 memset(&ifr
, 0, sizeof ifr
);
2318 strncpy(ifr
.ifr_name
, netdev_name
, sizeof ifr
.ifr_name
);
2319 ifr
.ifr_hwaddr
.sa_family
= hwaddr_family
;
2320 memcpy(ifr
.ifr_hwaddr
.sa_data
, mac
, ETH_ADDR_LEN
);
2321 COVERAGE_INC(netdev_set_hwaddr
);
2322 if (ioctl(af_inet_sock
, SIOCSIFHWADDR
, &ifr
) < 0) {
2323 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
2324 netdev_name
, strerror(errno
));
2331 netdev_linux_do_ethtool(const char *name
, struct ethtool_cmd
*ecmd
,
2332 int cmd
, const char *cmd_name
)
2336 memset(&ifr
, 0, sizeof ifr
);
2337 strncpy(ifr
.ifr_name
, name
, sizeof ifr
.ifr_name
);
2338 ifr
.ifr_data
= (caddr_t
) ecmd
;
2341 COVERAGE_INC(netdev_ethtool
);
2342 if (ioctl(af_inet_sock
, SIOCETHTOOL
, &ifr
) == 0) {
2345 if (errno
!= EOPNOTSUPP
) {
2346 VLOG_WARN_RL(&rl
, "ethtool command %s on network device %s "
2347 "failed: %s", cmd_name
, name
, strerror(errno
));
2349 /* The device doesn't support this operation. That's pretty
2350 * common, so there's no point in logging anything. */
2357 netdev_linux_do_ioctl(const char *name
, struct ifreq
*ifr
, int cmd
,
2358 const char *cmd_name
)
2360 strncpy(ifr
->ifr_name
, name
, sizeof ifr
->ifr_name
);
2361 if (ioctl(af_inet_sock
, cmd
, ifr
) == -1) {
2362 VLOG_DBG_RL(&rl
, "%s: ioctl(%s) failed: %s", name
, cmd_name
,
2370 netdev_linux_get_ipv4(const struct netdev
*netdev
, struct in_addr
*ip
,
2371 int cmd
, const char *cmd_name
)
2376 ifr
.ifr_addr
.sa_family
= AF_INET
;
2377 error
= netdev_linux_do_ioctl(netdev_get_name(netdev
), &ifr
, cmd
, cmd_name
);
2379 const struct sockaddr_in
*sin
= (struct sockaddr_in
*) &ifr
.ifr_addr
;
2380 *ip
= sin
->sin_addr
;