]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev-linux.c
netflow: Use macro for constant instead of static const int.
[mirror_ovs.git] / lib / netdev-linux.c
CommitLineData
e9e28be3 1/*
149f577a 2 * Copyright (c) 2009, 2010 Nicira Networks.
e9e28be3
BP
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <config.h>
8b61709d 18#include <assert.h>
e9e28be3 19#include <errno.h>
8b61709d
BP
20#include <fcntl.h>
21#include <arpa/inet.h>
22#include <inttypes.h>
23#include <linux/if_tun.h>
a740f0de 24#include <linux/ip.h>
8b61709d
BP
25#include <linux/types.h>
26#include <linux/ethtool.h>
6f42c8ea 27#include <linux/pkt_sched.h>
e9e28be3 28#include <linux/rtnetlink.h>
8b61709d
BP
29#include <linux/sockios.h>
30#include <linux/version.h>
31#include <sys/types.h>
32#include <sys/ioctl.h>
33#include <sys/socket.h>
34#include <netpacket/packet.h>
35#include <net/ethernet.h>
36#include <net/if.h>
a740f0de 37#include <linux/if_tunnel.h>
8b61709d
BP
38#include <net/if_arp.h>
39#include <net/if_packet.h>
40#include <net/route.h>
41#include <netinet/in.h>
e9e28be3 42#include <poll.h>
8b61709d
BP
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
e9e28be3
BP
46
47#include "coverage.h"
8b61709d
BP
48#include "dynamic-string.h"
49#include "fatal-signal.h"
50#include "netdev-provider.h"
e9e28be3
BP
51#include "netlink.h"
52#include "ofpbuf.h"
8b61709d 53#include "openflow/openflow.h"
8722022c
BP
54#include "openvswitch/internal_dev.h"
55#include "openvswitch/gre.h"
8b61709d
BP
56#include "packets.h"
57#include "poll-loop.h"
559843ed 58#include "rtnetlink.h"
8b61709d
BP
59#include "socket-util.h"
60#include "shash.h"
61#include "svec.h"
e9e28be3
BP
62
63#define THIS_MODULE VLM_netdev_linux
64#include "vlog.h"
8b61709d
BP
65\f
66/* These were introduced in Linux 2.6.14, so they might be missing if we have
67 * old headers. */
68#ifndef ADVERTISED_Pause
69#define ADVERTISED_Pause (1 << 13)
70#endif
71#ifndef ADVERTISED_Asym_Pause
72#define ADVERTISED_Asym_Pause (1 << 14)
73#endif
74
149f577a 75static struct rtnetlink_notifier netdev_linux_cache_notifier;
46415c90 76static int cache_notifier_refcount;
8b61709d
BP
77
78enum {
79 VALID_IFINDEX = 1 << 0,
80 VALID_ETHERADDR = 1 << 1,
81 VALID_IN4 = 1 << 2,
82 VALID_IN6 = 1 << 3,
83 VALID_MTU = 1 << 4,
fe6b0e03 84 VALID_CARRIER = 1 << 5,
80a86fbe
BP
85 VALID_IS_PSEUDO = 1 << 6, /* Represents is_internal and is_tap. */
86 VALID_POLICING = 1 << 7
8b61709d
BP
87};
88
149f577a
JG
89struct tap_state {
90 int fd;
91};
92
93struct netdev_dev_linux {
94 struct netdev_dev netdev_dev;
95
8b61709d 96 struct shash_node *shash_node;
149f577a 97 unsigned int cache_valid;
8b61709d 98
8722022c
BP
99 /* The following are figured out "on demand" only. They are only valid
100 * when the corresponding VALID_* bit in 'cache_valid' is set. */
8b61709d
BP
101 int ifindex;
102 uint8_t etheraddr[ETH_ADDR_LEN];
f1acd62b 103 struct in_addr address, netmask;
8b61709d
BP
104 struct in6_addr in6;
105 int mtu;
106 int carrier;
8722022c
BP
107 bool is_internal; /* Is this an openvswitch internal device? */
108 bool is_tap; /* Is this a tuntap device? */
80a86fbe
BP
109 uint32_t kbits_rate; /* Policing data. */
110 uint32_t kbits_burst;
149f577a
JG
111
112 union {
113 struct tap_state tap;
114 } state;
8b61709d
BP
115};
116
149f577a
JG
117struct netdev_linux {
118 struct netdev netdev;
5b7448ed 119 int fd;
149f577a 120};
8b61709d 121
8b61709d
BP
122/* An AF_INET socket (used for ioctl operations). */
123static int af_inet_sock = -1;
124
125struct netdev_linux_notifier {
126 struct netdev_notifier notifier;
127 struct list node;
128};
129
130static struct shash netdev_linux_notifiers =
131 SHASH_INITIALIZER(&netdev_linux_notifiers);
46097491 132static struct rtnetlink_notifier netdev_linux_poll_notifier;
8b61709d
BP
133
134/* This is set pretty low because we probably won't learn anything from the
135 * additional log messages. */
136static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
137
15b3596a 138static int netdev_linux_init(void);
6f643e49 139
0b0544d7 140static int netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *,
8b61709d 141 int cmd, const char *cmd_name);
149f577a
JG
142static int netdev_linux_do_ioctl(const char *name, struct ifreq *, int cmd,
143 const char *cmd_name);
f1acd62b
BP
144static int netdev_linux_get_ipv4(const struct netdev *, struct in_addr *,
145 int cmd, const char *cmd_name);
8b61709d
BP
146static int get_flags(const struct netdev *, int *flagsp);
147static int set_flags(struct netdev *, int flags);
148static int do_get_ifindex(const char *netdev_name);
149static int get_ifindex(const struct netdev *, int *ifindexp);
150static int do_set_addr(struct netdev *netdev,
151 int ioctl_nr, const char *ioctl_name,
152 struct in_addr addr);
153static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
154static int set_etheraddr(const char *netdev_name, int hwaddr_family,
155 const uint8_t[ETH_ADDR_LEN]);
156static int get_stats_via_netlink(int ifindex, struct netdev_stats *stats);
157static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats);
a5af30fb 158static int get_rtnl_sock(struct nl_sock **);
8b61709d 159
15b3596a
JG
160static bool
161is_netdev_linux_class(const struct netdev_class *netdev_class)
162{
163 return netdev_class->init == netdev_linux_init;
164}
165
149f577a
JG
166static struct netdev_dev_linux *
167netdev_dev_linux_cast(const struct netdev_dev *netdev_dev)
6c88d577 168{
15b3596a
JG
169 const struct netdev_class *netdev_class = netdev_dev_get_class(netdev_dev);
170 assert(is_netdev_linux_class(netdev_class));
171
149f577a 172 return CONTAINER_OF(netdev_dev, struct netdev_dev_linux, netdev_dev);
6c88d577
JP
173}
174
8b61709d
BP
175static struct netdev_linux *
176netdev_linux_cast(const struct netdev *netdev)
177{
15b3596a
JG
178 struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
179 const struct netdev_class *netdev_class = netdev_dev_get_class(netdev_dev);
180 assert(is_netdev_linux_class(netdev_class));
181
8b61709d
BP
182 return CONTAINER_OF(netdev, struct netdev_linux, netdev);
183}
184
185static int
186netdev_linux_init(void)
187{
188 static int status = -1;
189 if (status < 0) {
190 af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
191 status = af_inet_sock >= 0 ? 0 : errno;
192 if (status) {
193 VLOG_ERR("failed to create inet socket: %s", strerror(status));
194 }
195 }
196 return status;
197}
198
199static void
200netdev_linux_run(void)
201{
46097491 202 rtnetlink_notifier_run();
8b61709d
BP
203}
204
205static void
206netdev_linux_wait(void)
207{
46097491 208 rtnetlink_notifier_wait();
8b61709d
BP
209}
210
211static void
46097491 212netdev_linux_cache_cb(const struct rtnetlink_change *change,
67a4917b 213 void *aux OVS_UNUSED)
8b61709d 214{
149f577a 215 struct netdev_dev_linux *dev;
8b61709d 216 if (change) {
46415c90
JG
217 struct netdev_dev *base_dev = netdev_dev_from_name(change->ifname);
218 if (base_dev) {
15b3596a
JG
219 const struct netdev_class *netdev_class =
220 netdev_dev_get_class(base_dev);
221
222 if (is_netdev_linux_class(netdev_class)) {
223 dev = netdev_dev_linux_cast(base_dev);
224 dev->cache_valid = 0;
225 }
8b61709d
BP
226 }
227 } else {
46415c90 228 struct shash device_shash;
8b61709d 229 struct shash_node *node;
46415c90
JG
230
231 shash_init(&device_shash);
232 netdev_dev_get_devices(&netdev_linux_class, &device_shash);
233 SHASH_FOR_EACH (node, &device_shash) {
149f577a
JG
234 dev = node->data;
235 dev->cache_valid = 0;
8b61709d 236 }
46415c90 237 shash_destroy(&device_shash);
8b61709d
BP
238 }
239}
240
149f577a 241/* Creates the netdev device of 'type' with 'name'. */
8b61709d 242static int
c69ee87c 243netdev_linux_create_system(const char *name, const char *type OVS_UNUSED,
149f577a 244 const struct shash *args, struct netdev_dev **netdev_devp)
6c88d577 245{
149f577a
JG
246 struct netdev_dev_linux *netdev_dev;
247 int error;
6c88d577
JP
248
249 if (!shash_is_empty(args)) {
149f577a 250 VLOG_WARN("%s: arguments for system devices should be empty", name);
6c88d577
JP
251 }
252
46415c90 253 if (!cache_notifier_refcount) {
149f577a
JG
254 error = rtnetlink_notifier_register(&netdev_linux_cache_notifier,
255 netdev_linux_cache_cb, NULL);
256 if (error) {
257 return error;
258 }
259 }
46415c90 260 cache_notifier_refcount++;
6c88d577 261
149f577a 262 netdev_dev = xzalloc(sizeof *netdev_dev);
149f577a 263 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_linux_class);
46415c90 264
149f577a 265 *netdev_devp = &netdev_dev->netdev_dev;
a740f0de
JG
266 return 0;
267}
268
5b7448ed
JG
269/* For most types of netdevs we open the device for each call of
270 * netdev_open(). However, this is not the case with tap devices,
271 * since it is only possible to open the device once. In this
272 * situation we share a single file descriptor, and consequently
273 * buffers, across all readers. Therefore once data is read it will
274 * be unavailable to other reads for tap devices. */
a740f0de 275static int
c69ee87c 276netdev_linux_create_tap(const char *name, const char *type OVS_UNUSED,
149f577a 277 const struct shash *args, struct netdev_dev **netdev_devp)
a740f0de 278{
149f577a 279 struct netdev_dev_linux *netdev_dev;
a740f0de
JG
280 struct tap_state *state;
281 static const char tap_dev[] = "/dev/net/tun";
282 struct ifreq ifr;
283 int error;
284
285 if (!shash_is_empty(args)) {
149f577a 286 VLOG_WARN("%s: arguments for TAP devices should be empty", name);
6c88d577
JP
287 }
288
149f577a
JG
289 netdev_dev = xzalloc(sizeof *netdev_dev);
290 state = &netdev_dev->state.tap;
a740f0de 291
6c88d577 292 /* Open tap device. */
149f577a
JG
293 state->fd = open(tap_dev, O_RDWR);
294 if (state->fd < 0) {
6c88d577
JP
295 error = errno;
296 VLOG_WARN("opening \"%s\" failed: %s", tap_dev, strerror(error));
297 goto error;
298 }
299
300 /* Create tap device. */
301 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
302 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
149f577a 303 if (ioctl(state->fd, TUNSETIFF, &ifr) == -1) {
6c88d577
JP
304 VLOG_WARN("%s: creating tap device failed: %s", name,
305 strerror(errno));
306 error = errno;
307 goto error;
308 }
309
310 /* Make non-blocking. */
149f577a 311 error = set_nonblocking(state->fd);
a740f0de
JG
312 if (error) {
313 goto error;
314 }
315
149f577a
JG
316 netdev_dev_init(&netdev_dev->netdev_dev, name, &netdev_tap_class);
317 *netdev_devp = &netdev_dev->netdev_dev;
a740f0de
JG
318 return 0;
319
320error:
149f577a 321 free(netdev_dev);
a740f0de
JG
322 return error;
323}
324
a740f0de 325static void
149f577a 326destroy_tap(struct netdev_dev_linux *netdev_dev)
a740f0de 327{
149f577a
JG
328 struct tap_state *state = &netdev_dev->state.tap;
329
330 if (state->fd >= 0) {
331 close(state->fd);
a740f0de
JG
332 }
333}
334
149f577a 335/* Destroys the netdev device 'netdev_dev_'. */
6c88d577 336static void
149f577a 337netdev_linux_destroy(struct netdev_dev *netdev_dev_)
6c88d577 338{
149f577a
JG
339 struct netdev_dev_linux *netdev_dev = netdev_dev_linux_cast(netdev_dev_);
340 const char *type = netdev_dev_get_type(netdev_dev_);
6c88d577 341
149f577a 342 if (!strcmp(type, "system")) {
46415c90 343 cache_notifier_refcount--;
149f577a 344
46415c90 345 if (!cache_notifier_refcount) {
149f577a
JG
346 rtnetlink_notifier_unregister(&netdev_linux_cache_notifier);
347 }
348 } else if (!strcmp(type, "tap")) {
349 destroy_tap(netdev_dev);
6c88d577 350 }
149f577a 351
658797c8 352 free(netdev_dev);
6c88d577
JP
353}
354
8b61709d 355static int
5b7448ed 356netdev_linux_open(struct netdev_dev *netdev_dev_, int ethertype,
149f577a 357 struct netdev **netdevp)
8b61709d 358{
5b7448ed 359 struct netdev_dev_linux *netdev_dev = netdev_dev_linux_cast(netdev_dev_);
8b61709d
BP
360 struct netdev_linux *netdev;
361 enum netdev_flags flags;
362 int error;
363
364 /* Allocate network device. */
ec6fde61 365 netdev = xzalloc(sizeof *netdev);
49a6a163 366 netdev->fd = -1;
5b7448ed 367 netdev_init(&netdev->netdev, netdev_dev_);
8b61709d
BP
368
369 error = netdev_get_flags(&netdev->netdev, &flags);
370 if (error == ENODEV) {
371 goto error;
372 }
373
5b7448ed
JG
374 if (!strcmp(netdev_dev_get_type(netdev_dev_), "tap")) {
375 netdev->fd = netdev_dev->state.tap.fd;
5b7448ed 376 } else if (ethertype != NETDEV_ETH_TYPE_NONE) {
8b61709d
BP
377 struct sockaddr_ll sll;
378 int protocol;
379 int ifindex;
380
381 /* Create file descriptor. */
382 protocol = (ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
383 : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
384 : ethertype);
5b7448ed
JG
385 netdev->fd = socket(PF_PACKET, SOCK_RAW, htons(protocol));
386 if (netdev->fd < 0) {
8b61709d
BP
387 error = errno;
388 goto error;
389 }
8b61709d
BP
390
391 /* Set non-blocking mode. */
5b7448ed 392 error = set_nonblocking(netdev->fd);
8b61709d
BP
393 if (error) {
394 goto error;
395 }
396
397 /* Get ethernet device index. */
398 error = get_ifindex(&netdev->netdev, &ifindex);
399 if (error) {
400 goto error;
401 }
402
403 /* Bind to specific ethernet device. */
404 memset(&sll, 0, sizeof sll);
405 sll.sll_family = AF_PACKET;
406 sll.sll_ifindex = ifindex;
5b7448ed 407 if (bind(netdev->fd,
8b61709d
BP
408 (struct sockaddr *) &sll, sizeof sll) < 0) {
409 error = errno;
5b7448ed 410 VLOG_ERR("bind to %s failed: %s", netdev_dev_get_name(netdev_dev_),
149f577a 411 strerror(error));
8b61709d
BP
412 goto error;
413 }
414
415 /* Between the socket() and bind() calls above, the socket receives all
416 * packets of the requested type on all system interfaces. We do not
417 * want to receive that data, but there is no way to avoid it. So we
418 * must now drain out the receive queue. */
5b7448ed 419 error = drain_rcvbuf(netdev->fd);
8b61709d
BP
420 if (error) {
421 goto error;
422 }
423 }
424
425 *netdevp = &netdev->netdev;
426 return 0;
427
428error:
149f577a 429 netdev_uninit(&netdev->netdev, true);
8b61709d
BP
430 return error;
431}
432
433/* Closes and destroys 'netdev'. */
434static void
435netdev_linux_close(struct netdev *netdev_)
436{
437 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
438
49a6a163 439 if (netdev->fd > 0 && strcmp(netdev_get_type(netdev_), "tap")) {
5b7448ed 440 close(netdev->fd);
8b61709d
BP
441 }
442 free(netdev);
443}
e9e28be3 444
8b61709d
BP
445/* Initializes 'svec' with a list of the names of all known network devices. */
446static int
447netdev_linux_enumerate(struct svec *svec)
448{
449 struct if_nameindex *names;
450
451 names = if_nameindex();
452 if (names) {
453 size_t i;
454
455 for (i = 0; names[i].if_name != NULL; i++) {
456 svec_add(svec, names[i].if_name);
457 }
458 if_freenameindex(names);
459 return 0;
460 } else {
461 VLOG_WARN("could not obtain list of network device names: %s",
462 strerror(errno));
463 return errno;
464 }
465}
466
467static int
468netdev_linux_recv(struct netdev *netdev_, void *data, size_t size)
469{
470 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
471
5b7448ed 472 if (netdev->fd < 0) {
8b61709d 473 /* Device was opened with NETDEV_ETH_TYPE_NONE. */
c0e5f6ca 474 return -EAGAIN;
8b61709d
BP
475 }
476
477 for (;;) {
5b7448ed 478 ssize_t retval = read(netdev->fd, data, size);
8b61709d
BP
479 if (retval >= 0) {
480 return retval;
481 } else if (errno != EINTR) {
482 if (errno != EAGAIN) {
483 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
484 strerror(errno), netdev_get_name(netdev_));
485 }
c0e5f6ca 486 return -errno;
8b61709d
BP
487 }
488 }
489}
490
491/* Registers with the poll loop to wake up from the next call to poll_block()
492 * when a packet is ready to be received with netdev_recv() on 'netdev'. */
493static void
494netdev_linux_recv_wait(struct netdev *netdev_)
495{
496 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
5b7448ed
JG
497 if (netdev->fd >= 0) {
498 poll_fd_wait(netdev->fd, POLLIN);
8b61709d
BP
499 }
500}
501
502/* Discards all packets waiting to be received from 'netdev'. */
503static int
504netdev_linux_drain(struct netdev *netdev_)
505{
506 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
5b7448ed 507 if (netdev->fd < 0) {
8b61709d 508 return 0;
5b7448ed 509 } else if (!strcmp(netdev_get_type(netdev_), "tap")) {
8b61709d 510 struct ifreq ifr;
149f577a 511 int error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr,
8b61709d
BP
512 SIOCGIFTXQLEN, "SIOCGIFTXQLEN");
513 if (error) {
514 return error;
515 }
5b7448ed 516 drain_fd(netdev->fd, ifr.ifr_qlen);
8b61709d
BP
517 return 0;
518 } else {
5b7448ed 519 return drain_rcvbuf(netdev->fd);
8b61709d
BP
520 }
521}
522
523/* Sends 'buffer' on 'netdev'. Returns 0 if successful, otherwise a positive
524 * errno value. Returns EAGAIN without blocking if the packet cannot be queued
525 * immediately. Returns EMSGSIZE if a partial packet was transmitted or if
526 * the packet is too big or too small to transmit on the device.
527 *
528 * The caller retains ownership of 'buffer' in all cases.
529 *
530 * The kernel maintains a packet transmission queue, so the caller is not
531 * expected to do additional queuing of packets. */
532static int
533netdev_linux_send(struct netdev *netdev_, const void *data, size_t size)
534{
535 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
536
537 /* XXX should support sending even if 'ethertype' was NETDEV_ETH_TYPE_NONE.
538 */
5b7448ed 539 if (netdev->fd < 0) {
8b61709d
BP
540 return EPIPE;
541 }
542
543 for (;;) {
5b7448ed 544 ssize_t retval = write(netdev->fd, data, size);
8b61709d
BP
545 if (retval < 0) {
546 /* The Linux AF_PACKET implementation never blocks waiting for room
547 * for packets, instead returning ENOBUFS. Translate this into
548 * EAGAIN for the caller. */
549 if (errno == ENOBUFS) {
550 return EAGAIN;
551 } else if (errno == EINTR) {
552 continue;
553 } else if (errno != EAGAIN) {
554 VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
555 netdev_get_name(netdev_), strerror(errno));
556 }
557 return errno;
558 } else if (retval != size) {
559 VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
560 "%zu) on %s", retval, size, netdev_get_name(netdev_));
561 return EMSGSIZE;
562 } else {
563 return 0;
564 }
565 }
566}
567
568/* Registers with the poll loop to wake up from the next call to poll_block()
569 * when the packet transmission queue has sufficient room to transmit a packet
570 * with netdev_send().
571 *
572 * The kernel maintains a packet transmission queue, so the client is not
573 * expected to do additional queuing of packets. Thus, this function is
574 * unlikely to ever be used. It is included for completeness. */
575static void
576netdev_linux_send_wait(struct netdev *netdev_)
577{
578 struct netdev_linux *netdev = netdev_linux_cast(netdev_);
5b7448ed 579 if (netdev->fd < 0) {
8b61709d 580 /* Nothing to do. */
5b7448ed
JG
581 } else if (strcmp(netdev_get_type(netdev_), "tap")) {
582 poll_fd_wait(netdev->fd, POLLOUT);
8b61709d
BP
583 } else {
584 /* TAP device always accepts packets.*/
585 poll_immediate_wake();
586 }
587}
588
589/* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
590 * otherwise a positive errno value. */
591static int
592netdev_linux_set_etheraddr(struct netdev *netdev_,
593 const uint8_t mac[ETH_ADDR_LEN])
594{
149f577a
JG
595 struct netdev_dev_linux *netdev_dev =
596 netdev_dev_linux_cast(netdev_get_dev(netdev_));
eb395f2e
BP
597 int error;
598
149f577a
JG
599 if (!(netdev_dev->cache_valid & VALID_ETHERADDR)
600 || !eth_addr_equals(netdev_dev->etheraddr, mac)) {
eb395f2e
BP
601 error = set_etheraddr(netdev_get_name(netdev_), ARPHRD_ETHER, mac);
602 if (!error) {
149f577a
JG
603 netdev_dev->cache_valid |= VALID_ETHERADDR;
604 memcpy(netdev_dev->etheraddr, mac, ETH_ADDR_LEN);
eb395f2e
BP
605 }
606 } else {
607 error = 0;
8b61709d
BP
608 }
609 return error;
610}
611
612/* Returns a pointer to 'netdev''s MAC address. The caller must not modify or
613 * free the returned buffer. */
614static int
615netdev_linux_get_etheraddr(const struct netdev *netdev_,
616 uint8_t mac[ETH_ADDR_LEN])
617{
149f577a
JG
618 struct netdev_dev_linux *netdev_dev =
619 netdev_dev_linux_cast(netdev_get_dev(netdev_));
620 if (!(netdev_dev->cache_valid & VALID_ETHERADDR)) {
8b61709d 621 int error = get_etheraddr(netdev_get_name(netdev_),
149f577a 622 netdev_dev->etheraddr);
8b61709d
BP
623 if (error) {
624 return error;
625 }
149f577a 626 netdev_dev->cache_valid |= VALID_ETHERADDR;
8b61709d 627 }
149f577a 628 memcpy(mac, netdev_dev->etheraddr, ETH_ADDR_LEN);
8b61709d
BP
629 return 0;
630}
631
632/* Returns the maximum size of transmitted (and received) packets on 'netdev',
633 * in bytes, not including the hardware header; thus, this is typically 1500
634 * bytes for Ethernet devices. */
635static int
636netdev_linux_get_mtu(const struct netdev *netdev_, int *mtup)
637{
149f577a
JG
638 struct netdev_dev_linux *netdev_dev =
639 netdev_dev_linux_cast(netdev_get_dev(netdev_));
640 if (!(netdev_dev->cache_valid & VALID_MTU)) {
8b61709d
BP
641 struct ifreq ifr;
642 int error;
643
149f577a
JG
644 error = netdev_linux_do_ioctl(netdev_get_name(netdev_), &ifr,
645 SIOCGIFMTU, "SIOCGIFMTU");
8b61709d
BP
646 if (error) {
647 return error;
648 }
149f577a
JG
649 netdev_dev->mtu = ifr.ifr_mtu;
650 netdev_dev->cache_valid |= VALID_MTU;
8b61709d 651 }
149f577a 652 *mtup = netdev_dev->mtu;
8b61709d
BP
653 return 0;
654}
655
9ab3d9a3
BP
656/* Returns the ifindex of 'netdev', if successful, as a positive number.
657 * On failure, returns a negative errno value. */
658static int
659netdev_linux_get_ifindex(const struct netdev *netdev)
660{
661 int ifindex, error;
662
663 error = get_ifindex(netdev, &ifindex);
664 return error ? -error : ifindex;
665}
666
8b61709d
BP
667static int
668netdev_linux_get_carrier(const struct netdev *netdev_, bool *carrier)
669{
149f577a
JG
670 struct netdev_dev_linux *netdev_dev =
671 netdev_dev_linux_cast(netdev_get_dev(netdev_));
8b61709d
BP
672 int error = 0;
673 char *fn = NULL;
674 int fd = -1;
675
149f577a 676 if (!(netdev_dev->cache_valid & VALID_CARRIER)) {
8b61709d
BP
677 char line[8];
678 int retval;
679
149f577a
JG
680 fn = xasprintf("/sys/class/net/%s/carrier",
681 netdev_get_name(netdev_));
8b61709d
BP
682 fd = open(fn, O_RDONLY);
683 if (fd < 0) {
684 error = errno;
685 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(error));
686 goto exit;
687 }
688
689 retval = read(fd, line, sizeof line);
690 if (retval < 0) {
691 error = errno;
692 if (error == EINVAL) {
693 /* This is the normal return value when we try to check carrier
694 * if the network device is not up. */
695 } else {
696 VLOG_WARN_RL(&rl, "%s: read failed: %s", fn, strerror(error));
697 }
698 goto exit;
699 } else if (retval == 0) {
700 error = EPROTO;
701 VLOG_WARN_RL(&rl, "%s: unexpected end of file", fn);
702 goto exit;
703 }
704
705 if (line[0] != '0' && line[0] != '1') {
706 error = EPROTO;
707 VLOG_WARN_RL(&rl, "%s: value is %c (expected 0 or 1)",
708 fn, line[0]);
709 goto exit;
710 }
149f577a
JG
711 netdev_dev->carrier = line[0] != '0';
712 netdev_dev->cache_valid |= VALID_CARRIER;
8b61709d 713 }
149f577a 714 *carrier = netdev_dev->carrier;
8b61709d
BP
715 error = 0;
716
717exit:
718 if (fd >= 0) {
719 close(fd);
720 }
721 free(fn);
722 return error;
723}
724
725/* Check whether we can we use RTM_GETLINK to get network device statistics.
726 * In pre-2.6.19 kernels, this was only available if wireless extensions were
727 * enabled. */
728static bool
729check_for_working_netlink_stats(void)
730{
731 /* Decide on the netdev_get_stats() implementation to use. Netlink is
732 * preferable, so if that works, we'll use it. */
733 int ifindex = do_get_ifindex("lo");
734 if (ifindex < 0) {
735 VLOG_WARN("failed to get ifindex for lo, "
736 "obtaining netdev stats from proc");
737 return false;
738 } else {
739 struct netdev_stats stats;
740 int error = get_stats_via_netlink(ifindex, &stats);
741 if (!error) {
742 VLOG_DBG("obtaining netdev stats via rtnetlink");
743 return true;
744 } else {
745 VLOG_INFO("RTM_GETLINK failed (%s), obtaining netdev stats "
746 "via proc (you are probably running a pre-2.6.19 "
747 "kernel)", strerror(error));
748 return false;
749 }
750 }
751}
752
8722022c
BP
753/* Brings the 'is_internal' and 'is_tap' members of 'netdev_dev' up-to-date. */
754static void
755netdev_linux_update_is_pseudo(struct netdev_dev_linux *netdev_dev)
756{
757 if (!(netdev_dev->cache_valid & VALID_IS_PSEUDO)) {
758 const char *name = netdev_dev_get_name(&netdev_dev->netdev_dev);
759 const char *type = netdev_dev_get_type(&netdev_dev->netdev_dev);
760
761 netdev_dev->is_tap = !strcmp(type, "tap");
762 netdev_dev->is_internal = false;
763 if (!netdev_dev->is_tap) {
764 struct ethtool_drvinfo drvinfo;
765 int error;
766
767 memset(&drvinfo, 0, sizeof drvinfo);
768 error = netdev_linux_do_ethtool(name,
769 (struct ethtool_cmd *)&drvinfo,
770 ETHTOOL_GDRVINFO,
771 "ETHTOOL_GDRVINFO");
772
773 if (!error && !strcmp(drvinfo.driver, "openvswitch")) {
774 netdev_dev->is_internal = true;
775 }
776 }
777
778 netdev_dev->cache_valid |= VALID_IS_PSEUDO;
779 }
780}
781
8b61709d
BP
782/* Retrieves current device stats for 'netdev'.
783 *
784 * XXX All of the members of struct netdev_stats are 64 bits wide, but on
785 * 32-bit architectures the Linux network stats are only 32 bits. */
786static int
149f577a
JG
787netdev_linux_get_stats(const struct netdev *netdev_,
788 struct netdev_stats *stats)
8b61709d 789{
149f577a
JG
790 struct netdev_dev_linux *netdev_dev =
791 netdev_dev_linux_cast(netdev_get_dev(netdev_));
8b61709d
BP
792 static int use_netlink_stats = -1;
793 int error;
fe6b0e03
JG
794 struct netdev_stats raw_stats;
795 struct netdev_stats *collect_stats = stats;
8b61709d
BP
796
797 COVERAGE_INC(netdev_get_stats);
fe6b0e03 798
8722022c 799 netdev_linux_update_is_pseudo(netdev_dev);
149f577a 800 if (netdev_dev->is_internal) {
fe6b0e03
JG
801 collect_stats = &raw_stats;
802 }
803
8b61709d
BP
804 if (use_netlink_stats < 0) {
805 use_netlink_stats = check_for_working_netlink_stats();
806 }
807 if (use_netlink_stats) {
808 int ifindex;
809
149f577a 810 error = get_ifindex(netdev_, &ifindex);
8b61709d 811 if (!error) {
fe6b0e03 812 error = get_stats_via_netlink(ifindex, collect_stats);
8b61709d
BP
813 }
814 } else {
149f577a 815 error = get_stats_via_proc(netdev_get_name(netdev_), collect_stats);
8b61709d 816 }
fe6b0e03
JG
817
818 /* If this port is an internal port then the transmit and receive stats
819 * will appear to be swapped relative to the other ports since we are the
820 * one sending the data, not a remote computer. For consistency, we swap
821 * them back here. */
8722022c 822 if (!error && (netdev_dev->is_internal || netdev_dev->is_tap)) {
fe6b0e03
JG
823 stats->rx_packets = raw_stats.tx_packets;
824 stats->tx_packets = raw_stats.rx_packets;
825 stats->rx_bytes = raw_stats.tx_bytes;
826 stats->tx_bytes = raw_stats.rx_bytes;
827 stats->rx_errors = raw_stats.tx_errors;
828 stats->tx_errors = raw_stats.rx_errors;
829 stats->rx_dropped = raw_stats.tx_dropped;
830 stats->tx_dropped = raw_stats.rx_dropped;
831 stats->multicast = raw_stats.multicast;
832 stats->collisions = raw_stats.collisions;
833 stats->rx_length_errors = 0;
834 stats->rx_over_errors = 0;
835 stats->rx_crc_errors = 0;
836 stats->rx_frame_errors = 0;
837 stats->rx_fifo_errors = 0;
838 stats->rx_missed_errors = 0;
839 stats->tx_aborted_errors = 0;
840 stats->tx_carrier_errors = 0;
841 stats->tx_fifo_errors = 0;
842 stats->tx_heartbeat_errors = 0;
843 stats->tx_window_errors = 0;
844 }
845
8b61709d
BP
846 return error;
847}
848
8722022c
BP
849static int
850netdev_linux_set_stats(struct netdev *netdev,
851 const struct netdev_stats *stats)
852{
853 struct netdev_dev_linux *netdev_dev =
854 netdev_dev_linux_cast(netdev_get_dev(netdev));
855 struct internal_dev_stats dp_dev_stats;
856 struct ifreq ifr;
857
858 /* We must reject this call if 'netdev' is not an Open vSwitch internal
859 * port, because the ioctl that we are about to execute is in the "device
860 * private ioctls" range, which means that executing it on a device that
861 * is not the type we expect could do any random thing.
862 *
863 * (Amusingly, these ioctl numbers are commented "THESE IOCTLS ARE
864 * _DEPRECATED_ AND WILL DISAPPEAR IN 2.5.X" in linux/sockios.h. I guess
865 * DaveM is a little behind on that.) */
866 netdev_linux_update_is_pseudo(netdev_dev);
867 if (!netdev_dev->is_internal) {
868 return EOPNOTSUPP;
869 }
870
871 /* This actually only sets the *offset* that the dp_dev applies, but in our
872 * usage for fake bond devices the dp_dev never has any traffic of it own
873 * so it has the same effect. */
874 dp_dev_stats.rx_packets = stats->rx_packets;
875 dp_dev_stats.rx_bytes = stats->rx_bytes;
876 dp_dev_stats.tx_packets = stats->tx_packets;
877 dp_dev_stats.tx_bytes = stats->tx_bytes;
878 ifr.ifr_data = (void *) &dp_dev_stats;
879 return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr,
880 INTERNAL_DEV_SET_STATS,
881 "INTERNAL_DEV_SET_STATS");
882}
883
8b61709d
BP
884/* Stores the features supported by 'netdev' into each of '*current',
885 * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
886 * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
7671589a 887 * successful, otherwise a positive errno value. */
8b61709d
BP
888static int
889netdev_linux_get_features(struct netdev *netdev,
890 uint32_t *current, uint32_t *advertised,
891 uint32_t *supported, uint32_t *peer)
892{
893 struct ethtool_cmd ecmd;
894 int error;
895
896 memset(&ecmd, 0, sizeof ecmd);
0b0544d7 897 error = netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd,
8b61709d
BP
898 ETHTOOL_GSET, "ETHTOOL_GSET");
899 if (error) {
900 return error;
901 }
902
903 /* Supported features. */
904 *supported = 0;
905 if (ecmd.supported & SUPPORTED_10baseT_Half) {
906 *supported |= OFPPF_10MB_HD;
907 }
908 if (ecmd.supported & SUPPORTED_10baseT_Full) {
909 *supported |= OFPPF_10MB_FD;
910 }
911 if (ecmd.supported & SUPPORTED_100baseT_Half) {
912 *supported |= OFPPF_100MB_HD;
913 }
914 if (ecmd.supported & SUPPORTED_100baseT_Full) {
915 *supported |= OFPPF_100MB_FD;
916 }
917 if (ecmd.supported & SUPPORTED_1000baseT_Half) {
918 *supported |= OFPPF_1GB_HD;
919 }
920 if (ecmd.supported & SUPPORTED_1000baseT_Full) {
921 *supported |= OFPPF_1GB_FD;
922 }
923 if (ecmd.supported & SUPPORTED_10000baseT_Full) {
924 *supported |= OFPPF_10GB_FD;
925 }
926 if (ecmd.supported & SUPPORTED_TP) {
927 *supported |= OFPPF_COPPER;
928 }
929 if (ecmd.supported & SUPPORTED_FIBRE) {
930 *supported |= OFPPF_FIBER;
931 }
932 if (ecmd.supported & SUPPORTED_Autoneg) {
933 *supported |= OFPPF_AUTONEG;
934 }
935 if (ecmd.supported & SUPPORTED_Pause) {
936 *supported |= OFPPF_PAUSE;
937 }
938 if (ecmd.supported & SUPPORTED_Asym_Pause) {
939 *supported |= OFPPF_PAUSE_ASYM;
940 }
941
942 /* Advertised features. */
943 *advertised = 0;
944 if (ecmd.advertising & ADVERTISED_10baseT_Half) {
945 *advertised |= OFPPF_10MB_HD;
946 }
947 if (ecmd.advertising & ADVERTISED_10baseT_Full) {
948 *advertised |= OFPPF_10MB_FD;
949 }
950 if (ecmd.advertising & ADVERTISED_100baseT_Half) {
951 *advertised |= OFPPF_100MB_HD;
952 }
953 if (ecmd.advertising & ADVERTISED_100baseT_Full) {
954 *advertised |= OFPPF_100MB_FD;
955 }
956 if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
957 *advertised |= OFPPF_1GB_HD;
958 }
959 if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
960 *advertised |= OFPPF_1GB_FD;
961 }
962 if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
963 *advertised |= OFPPF_10GB_FD;
964 }
965 if (ecmd.advertising & ADVERTISED_TP) {
966 *advertised |= OFPPF_COPPER;
967 }
968 if (ecmd.advertising & ADVERTISED_FIBRE) {
969 *advertised |= OFPPF_FIBER;
970 }
971 if (ecmd.advertising & ADVERTISED_Autoneg) {
972 *advertised |= OFPPF_AUTONEG;
973 }
974 if (ecmd.advertising & ADVERTISED_Pause) {
975 *advertised |= OFPPF_PAUSE;
976 }
977 if (ecmd.advertising & ADVERTISED_Asym_Pause) {
978 *advertised |= OFPPF_PAUSE_ASYM;
979 }
980
981 /* Current settings. */
982 if (ecmd.speed == SPEED_10) {
983 *current = ecmd.duplex ? OFPPF_10MB_FD : OFPPF_10MB_HD;
984 } else if (ecmd.speed == SPEED_100) {
985 *current = ecmd.duplex ? OFPPF_100MB_FD : OFPPF_100MB_HD;
986 } else if (ecmd.speed == SPEED_1000) {
987 *current = ecmd.duplex ? OFPPF_1GB_FD : OFPPF_1GB_HD;
988 } else if (ecmd.speed == SPEED_10000) {
989 *current = OFPPF_10GB_FD;
990 } else {
991 *current = 0;
992 }
993
994 if (ecmd.port == PORT_TP) {
995 *current |= OFPPF_COPPER;
996 } else if (ecmd.port == PORT_FIBRE) {
997 *current |= OFPPF_FIBER;
998 }
999
1000 if (ecmd.autoneg) {
1001 *current |= OFPPF_AUTONEG;
1002 }
1003
1004 /* Peer advertisements. */
1005 *peer = 0; /* XXX */
1006
1007 return 0;
1008}
1009
1010/* Set the features advertised by 'netdev' to 'advertise'. */
1011static int
1012netdev_linux_set_advertisements(struct netdev *netdev, uint32_t advertise)
1013{
1014 struct ethtool_cmd ecmd;
1015 int error;
1016
1017 memset(&ecmd, 0, sizeof ecmd);
0b0544d7 1018 error = netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd,
8b61709d
BP
1019 ETHTOOL_GSET, "ETHTOOL_GSET");
1020 if (error) {
1021 return error;
1022 }
1023
1024 ecmd.advertising = 0;
1025 if (advertise & OFPPF_10MB_HD) {
1026 ecmd.advertising |= ADVERTISED_10baseT_Half;
1027 }
1028 if (advertise & OFPPF_10MB_FD) {
1029 ecmd.advertising |= ADVERTISED_10baseT_Full;
1030 }
1031 if (advertise & OFPPF_100MB_HD) {
1032 ecmd.advertising |= ADVERTISED_100baseT_Half;
1033 }
1034 if (advertise & OFPPF_100MB_FD) {
1035 ecmd.advertising |= ADVERTISED_100baseT_Full;
1036 }
1037 if (advertise & OFPPF_1GB_HD) {
1038 ecmd.advertising |= ADVERTISED_1000baseT_Half;
1039 }
1040 if (advertise & OFPPF_1GB_FD) {
1041 ecmd.advertising |= ADVERTISED_1000baseT_Full;
1042 }
1043 if (advertise & OFPPF_10GB_FD) {
1044 ecmd.advertising |= ADVERTISED_10000baseT_Full;
1045 }
1046 if (advertise & OFPPF_COPPER) {
1047 ecmd.advertising |= ADVERTISED_TP;
1048 }
1049 if (advertise & OFPPF_FIBER) {
1050 ecmd.advertising |= ADVERTISED_FIBRE;
1051 }
1052 if (advertise & OFPPF_AUTONEG) {
1053 ecmd.advertising |= ADVERTISED_Autoneg;
1054 }
1055 if (advertise & OFPPF_PAUSE) {
1056 ecmd.advertising |= ADVERTISED_Pause;
1057 }
1058 if (advertise & OFPPF_PAUSE_ASYM) {
1059 ecmd.advertising |= ADVERTISED_Asym_Pause;
1060 }
0b0544d7 1061 return netdev_linux_do_ethtool(netdev_get_name(netdev), &ecmd,
8b61709d
BP
1062 ETHTOOL_SSET, "ETHTOOL_SSET");
1063}
1064
1065/* If 'netdev_name' is the name of a VLAN network device (e.g. one created with
1066 * vconfig(8)), sets '*vlan_vid' to the VLAN VID associated with that device
1067 * and returns 0. Otherwise returns a errno value (specifically ENOENT if
1068 * 'netdev_name' is the name of a network device that is not a VLAN device) and
1069 * sets '*vlan_vid' to -1. */
1070static int
1071netdev_linux_get_vlan_vid(const struct netdev *netdev, int *vlan_vid)
1072{
1073 const char *netdev_name = netdev_get_name(netdev);
1074 struct ds line = DS_EMPTY_INITIALIZER;
1075 FILE *stream = NULL;
1076 int error;
1077 char *fn;
1078
1079 COVERAGE_INC(netdev_get_vlan_vid);
1080 fn = xasprintf("/proc/net/vlan/%s", netdev_name);
1081 stream = fopen(fn, "r");
1082 if (!stream) {
1083 error = errno;
1084 goto done;
1085 }
1086
1087 if (ds_get_line(&line, stream)) {
1088 if (ferror(stream)) {
1089 error = errno;
1090 VLOG_ERR_RL(&rl, "error reading \"%s\": %s", fn, strerror(errno));
1091 } else {
1092 error = EPROTO;
1093 VLOG_ERR_RL(&rl, "unexpected end of file reading \"%s\"", fn);
1094 }
1095 goto done;
1096 }
1097
1098 if (!sscanf(ds_cstr(&line), "%*s VID: %d", vlan_vid)) {
1099 error = EPROTO;
1100 VLOG_ERR_RL(&rl, "parse error reading \"%s\" line 1: \"%s\"",
1101 fn, ds_cstr(&line));
1102 goto done;
1103 }
1104
1105 error = 0;
1106
1107done:
1108 free(fn);
1109 if (stream) {
1110 fclose(stream);
1111 }
1112 ds_destroy(&line);
1113 if (error) {
1114 *vlan_vid = -1;
1115 }
1116 return error;
1117}
1118
1119#define POLICE_ADD_CMD "/sbin/tc qdisc add dev %s handle ffff: ingress"
1120#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"
8b61709d 1121
8e460221 1122/* Remove ingress policing from 'netdev'. Returns 0 if successful, otherwise a
6f42c8ea
BP
1123 * positive errno value.
1124 *
1125 * This function is equivalent to running
1126 * /sbin/tc qdisc del dev %s handle ffff: ingress
1127 * but it is much, much faster.
1128 */
8e460221
BP
1129static int
1130netdev_linux_remove_policing(struct netdev *netdev)
1131{
80a86fbe
BP
1132 struct netdev_dev_linux *netdev_dev =
1133 netdev_dev_linux_cast(netdev_get_dev(netdev));
8e460221 1134 const char *netdev_name = netdev_get_name(netdev);
8e460221 1135
6f42c8ea
BP
1136 struct ofpbuf request;
1137 struct ofpbuf *reply;
1138 struct tcmsg *tcmsg;
1139 struct nl_sock *rtnl_sock;
1140 int ifindex;
1141 int error;
1142
1143 error = get_ifindex(netdev, &ifindex);
1144 if (error) {
1145 return error;
8e460221 1146 }
6f42c8ea
BP
1147
1148 error = get_rtnl_sock(&rtnl_sock);
1149 if (error) {
1150 return error;
1151 }
1152
1153 ofpbuf_init(&request, 0);
1154 nl_msg_put_nlmsghdr(&request, rtnl_sock, sizeof *tcmsg,
1155 RTM_DELQDISC, NLM_F_REQUEST);
1156 tcmsg = ofpbuf_put_zeros(&request, sizeof *tcmsg);
1157 tcmsg->tcm_family = AF_UNSPEC;
1158 tcmsg->tcm_ifindex = ifindex;
1159 tcmsg->tcm_handle = 0xffff0000;
1160 tcmsg->tcm_parent = TC_H_INGRESS;
1161 nl_msg_put_string(&request, TCA_KIND, "ingress");
1162 nl_msg_put_unspec(&request, TCA_OPTIONS, NULL, 0);
1163 error = nl_sock_transact(rtnl_sock, &request, &reply);
1164 ofpbuf_uninit(&request);
1165 ofpbuf_delete(reply);
1166 if (error && error != ENOENT) {
1167 VLOG_WARN_RL(&rl, "%s: removing policing failed: %s",
1168 netdev_name, strerror(error));
1169 return error;
1170 }
1171
80a86fbe
BP
1172 netdev_dev->kbits_rate = 0;
1173 netdev_dev->kbits_burst = 0;
1174 netdev_dev->cache_valid |= VALID_POLICING;
8e460221
BP
1175 return 0;
1176}
1177
8b61709d
BP
1178/* Attempts to set input rate limiting (policing) policy. */
1179static int
1180netdev_linux_set_policing(struct netdev *netdev,
1181 uint32_t kbits_rate, uint32_t kbits_burst)
1182{
80a86fbe
BP
1183 struct netdev_dev_linux *netdev_dev =
1184 netdev_dev_linux_cast(netdev_get_dev(netdev));
8b61709d
BP
1185 const char *netdev_name = netdev_get_name(netdev);
1186 char command[1024];
1187
1188 COVERAGE_INC(netdev_set_policing);
8e460221 1189
80a86fbe
BP
1190 kbits_burst = (!kbits_rate ? 0 /* Force to 0 if no rate specified. */
1191 : !kbits_burst ? 1000 /* Default to 1000 kbits if 0. */
1192 : kbits_burst); /* Stick with user-specified value. */
1193
1194 if (netdev_dev->cache_valid & VALID_POLICING
1195 && netdev_dev->kbits_rate == kbits_rate
1196 && netdev_dev->kbits_burst == kbits_burst) {
1197 /* Assume that settings haven't changed since we last set them. */
1198 return 0;
1199 }
1200
8e460221 1201 netdev_linux_remove_policing(netdev);
8b61709d 1202 if (kbits_rate) {
8b61709d
BP
1203 snprintf(command, sizeof(command), POLICE_ADD_CMD, netdev_name);
1204 if (system(command) != 0) {
1205 VLOG_WARN_RL(&rl, "%s: problem adding policing", netdev_name);
1206 return -1;
1207 }
1208
1209 snprintf(command, sizeof(command), POLICE_CONFIG_CMD, netdev_name,
1210 kbits_rate, kbits_burst);
1211 if (system(command) != 0) {
1212 VLOG_WARN_RL(&rl, "%s: problem configuring policing",
1213 netdev_name);
1214 return -1;
1215 }
80a86fbe
BP
1216
1217 netdev_dev->kbits_rate = kbits_rate;
1218 netdev_dev->kbits_burst = kbits_burst;
1219 netdev_dev->cache_valid |= VALID_POLICING;
8b61709d
BP
1220 }
1221
1222 return 0;
1223}
1224
8b61709d 1225static int
f1acd62b
BP
1226netdev_linux_get_in4(const struct netdev *netdev_,
1227 struct in_addr *address, struct in_addr *netmask)
8b61709d 1228{
149f577a
JG
1229 struct netdev_dev_linux *netdev_dev =
1230 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1231
1232 if (!(netdev_dev->cache_valid & VALID_IN4)) {
8b61709d
BP
1233 int error;
1234
149f577a 1235 error = netdev_linux_get_ipv4(netdev_, &netdev_dev->address,
8b61709d
BP
1236 SIOCGIFADDR, "SIOCGIFADDR");
1237 if (error) {
1238 return error;
1239 }
1240
149f577a 1241 error = netdev_linux_get_ipv4(netdev_, &netdev_dev->netmask,
f1acd62b
BP
1242 SIOCGIFNETMASK, "SIOCGIFNETMASK");
1243 if (error) {
1244 return error;
1245 }
1246
149f577a 1247 netdev_dev->cache_valid |= VALID_IN4;
8b61709d 1248 }
149f577a
JG
1249 *address = netdev_dev->address;
1250 *netmask = netdev_dev->netmask;
f1acd62b 1251 return address->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
8b61709d
BP
1252}
1253
8b61709d 1254static int
f1acd62b
BP
1255netdev_linux_set_in4(struct netdev *netdev_, struct in_addr address,
1256 struct in_addr netmask)
8b61709d 1257{
149f577a
JG
1258 struct netdev_dev_linux *netdev_dev =
1259 netdev_dev_linux_cast(netdev_get_dev(netdev_));
8b61709d
BP
1260 int error;
1261
f1acd62b 1262 error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", address);
8b61709d 1263 if (!error) {
149f577a
JG
1264 netdev_dev->cache_valid |= VALID_IN4;
1265 netdev_dev->address = address;
1266 netdev_dev->netmask = netmask;
f1acd62b 1267 if (address.s_addr != INADDR_ANY) {
8b61709d 1268 error = do_set_addr(netdev_, SIOCSIFNETMASK,
f1acd62b 1269 "SIOCSIFNETMASK", netmask);
8b61709d
BP
1270 }
1271 }
1272 return error;
1273}
1274
1275static bool
1276parse_if_inet6_line(const char *line,
1277 struct in6_addr *in6, char ifname[16 + 1])
1278{
1279 uint8_t *s6 = in6->s6_addr;
1280#define X8 "%2"SCNx8
1281 return sscanf(line,
1282 " "X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8 X8
1283 "%*x %*x %*x %*x %16s\n",
1284 &s6[0], &s6[1], &s6[2], &s6[3],
1285 &s6[4], &s6[5], &s6[6], &s6[7],
1286 &s6[8], &s6[9], &s6[10], &s6[11],
1287 &s6[12], &s6[13], &s6[14], &s6[15],
1288 ifname) == 17;
1289}
1290
1291/* If 'netdev' has an assigned IPv6 address, sets '*in6' to that address (if
1292 * 'in6' is non-null) and returns true. Otherwise, returns false. */
1293static int
1294netdev_linux_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
1295{
149f577a
JG
1296 struct netdev_dev_linux *netdev_dev =
1297 netdev_dev_linux_cast(netdev_get_dev(netdev_));
1298 if (!(netdev_dev->cache_valid & VALID_IN6)) {
8b61709d
BP
1299 FILE *file;
1300 char line[128];
1301
149f577a 1302 netdev_dev->in6 = in6addr_any;
8b61709d
BP
1303
1304 file = fopen("/proc/net/if_inet6", "r");
1305 if (file != NULL) {
1306 const char *name = netdev_get_name(netdev_);
1307 while (fgets(line, sizeof line, file)) {
1308 struct in6_addr in6;
1309 char ifname[16 + 1];
1310 if (parse_if_inet6_line(line, &in6, ifname)
1311 && !strcmp(name, ifname))
1312 {
149f577a 1313 netdev_dev->in6 = in6;
8b61709d
BP
1314 break;
1315 }
1316 }
1317 fclose(file);
1318 }
149f577a 1319 netdev_dev->cache_valid |= VALID_IN6;
8b61709d 1320 }
149f577a 1321 *in6 = netdev_dev->in6;
8b61709d
BP
1322 return 0;
1323}
1324
1325static void
1326make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
1327{
1328 struct sockaddr_in sin;
1329 memset(&sin, 0, sizeof sin);
1330 sin.sin_family = AF_INET;
1331 sin.sin_addr = addr;
1332 sin.sin_port = 0;
1333
1334 memset(sa, 0, sizeof *sa);
1335 memcpy(sa, &sin, sizeof sin);
1336}
1337
1338static int
1339do_set_addr(struct netdev *netdev,
1340 int ioctl_nr, const char *ioctl_name, struct in_addr addr)
1341{
1342 struct ifreq ifr;
149f577a 1343 strncpy(ifr.ifr_name, netdev_get_name(netdev), sizeof ifr.ifr_name);
8b61709d 1344 make_in4_sockaddr(&ifr.ifr_addr, addr);
149f577a
JG
1345
1346 return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, ioctl_nr,
1347 ioctl_name);
8b61709d
BP
1348}
1349
1350/* Adds 'router' as a default IP gateway. */
1351static int
67a4917b 1352netdev_linux_add_router(struct netdev *netdev OVS_UNUSED, struct in_addr router)
8b61709d
BP
1353{
1354 struct in_addr any = { INADDR_ANY };
1355 struct rtentry rt;
1356 int error;
1357
1358 memset(&rt, 0, sizeof rt);
1359 make_in4_sockaddr(&rt.rt_dst, any);
1360 make_in4_sockaddr(&rt.rt_gateway, router);
1361 make_in4_sockaddr(&rt.rt_genmask, any);
1362 rt.rt_flags = RTF_UP | RTF_GATEWAY;
1363 COVERAGE_INC(netdev_add_router);
1364 error = ioctl(af_inet_sock, SIOCADDRT, &rt) < 0 ? errno : 0;
1365 if (error) {
1366 VLOG_WARN("ioctl(SIOCADDRT): %s", strerror(error));
1367 }
1368 return error;
1369}
1370
f1acd62b
BP
1371static int
1372netdev_linux_get_next_hop(const struct in_addr *host, struct in_addr *next_hop,
1373 char **netdev_name)
1374{
1375 static const char fn[] = "/proc/net/route";
1376 FILE *stream;
1377 char line[256];
1378 int ln;
1379
1380 *netdev_name = NULL;
1381 stream = fopen(fn, "r");
1382 if (stream == NULL) {
1383 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1384 return errno;
1385 }
1386
1387 ln = 0;
1388 while (fgets(line, sizeof line, stream)) {
1389 if (++ln >= 2) {
1390 char iface[17];
1391 uint32_t dest, gateway, mask;
1392 int refcnt, metric, mtu;
1393 unsigned int flags, use, window, irtt;
1394
1395 if (sscanf(line,
1396 "%16s %"SCNx32" %"SCNx32" %04X %d %u %d %"SCNx32
1397 " %d %u %u\n",
1398 iface, &dest, &gateway, &flags, &refcnt,
1399 &use, &metric, &mask, &mtu, &window, &irtt) != 11) {
1400
1401 VLOG_WARN_RL(&rl, "%s: could not parse line %d: %s",
1402 fn, ln, line);
1403 continue;
1404 }
1405 if (!(flags & RTF_UP)) {
1406 /* Skip routes that aren't up. */
1407 continue;
1408 }
1409
1410 /* The output of 'dest', 'mask', and 'gateway' were given in
1411 * network byte order, so we don't need need any endian
1412 * conversions here. */
1413 if ((dest & mask) == (host->s_addr & mask)) {
1414 if (!gateway) {
1415 /* The host is directly reachable. */
1416 next_hop->s_addr = 0;
1417 } else {
1418 /* To reach the host, we must go through a gateway. */
1419 next_hop->s_addr = gateway;
1420 }
1421 *netdev_name = xstrdup(iface);
1422 fclose(stream);
1423 return 0;
1424 }
1425 }
1426 }
1427
1428 fclose(stream);
1429 return ENXIO;
1430}
1431
8b61709d
BP
1432/* Looks up the ARP table entry for 'ip' on 'netdev'. If one exists and can be
1433 * successfully retrieved, it stores the corresponding MAC address in 'mac' and
1434 * returns 0. Otherwise, it returns a positive errno value; in particular,
1435 * ENXIO indicates that there is not ARP table entry for 'ip' on 'netdev'. */
1436static int
1437netdev_linux_arp_lookup(const struct netdev *netdev,
1438 uint32_t ip, uint8_t mac[ETH_ADDR_LEN])
1439{
1440 struct arpreq r;
c100e025 1441 struct sockaddr_in sin;
8b61709d
BP
1442 int retval;
1443
1444 memset(&r, 0, sizeof r);
c100e025
BP
1445 sin.sin_family = AF_INET;
1446 sin.sin_addr.s_addr = ip;
1447 sin.sin_port = 0;
1448 memcpy(&r.arp_pa, &sin, sizeof sin);
8b61709d
BP
1449 r.arp_ha.sa_family = ARPHRD_ETHER;
1450 r.arp_flags = 0;
149f577a 1451 strncpy(r.arp_dev, netdev_get_name(netdev), sizeof r.arp_dev);
8b61709d
BP
1452 COVERAGE_INC(netdev_arp_lookup);
1453 retval = ioctl(af_inet_sock, SIOCGARP, &r) < 0 ? errno : 0;
1454 if (!retval) {
1455 memcpy(mac, r.arp_ha.sa_data, ETH_ADDR_LEN);
1456 } else if (retval != ENXIO) {
1457 VLOG_WARN_RL(&rl, "%s: could not look up ARP entry for "IP_FMT": %s",
149f577a 1458 netdev_get_name(netdev), IP_ARGS(&ip), strerror(retval));
8b61709d
BP
1459 }
1460 return retval;
1461}
1462
1463static int
1464nd_to_iff_flags(enum netdev_flags nd)
1465{
1466 int iff = 0;
1467 if (nd & NETDEV_UP) {
1468 iff |= IFF_UP;
1469 }
1470 if (nd & NETDEV_PROMISC) {
1471 iff |= IFF_PROMISC;
1472 }
1473 return iff;
1474}
1475
1476static int
1477iff_to_nd_flags(int iff)
1478{
1479 enum netdev_flags nd = 0;
1480 if (iff & IFF_UP) {
1481 nd |= NETDEV_UP;
1482 }
1483 if (iff & IFF_PROMISC) {
1484 nd |= NETDEV_PROMISC;
1485 }
1486 return nd;
1487}
1488
1489static int
1490netdev_linux_update_flags(struct netdev *netdev, enum netdev_flags off,
1491 enum netdev_flags on, enum netdev_flags *old_flagsp)
1492{
1493 int old_flags, new_flags;
1494 int error;
1495
1496 error = get_flags(netdev, &old_flags);
1497 if (!error) {
1498 *old_flagsp = iff_to_nd_flags(old_flags);
1499 new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
1500 if (new_flags != old_flags) {
1501 error = set_flags(netdev, new_flags);
1502 }
1503 }
1504 return error;
1505}
1506
1507static void
1508poll_notify(struct list *list)
1509{
1510 struct netdev_linux_notifier *notifier;
1511 LIST_FOR_EACH (notifier, struct netdev_linux_notifier, node, list) {
1512 struct netdev_notifier *n = &notifier->notifier;
1513 n->cb(n);
1514 }
1515}
1516
1517static void
46097491 1518netdev_linux_poll_cb(const struct rtnetlink_change *change,
67a4917b 1519 void *aux OVS_UNUSED)
8b61709d
BP
1520{
1521 if (change) {
1522 struct list *list = shash_find_data(&netdev_linux_notifiers,
1523 change->ifname);
1524 if (list) {
1525 poll_notify(list);
1526 }
1527 } else {
1528 struct shash_node *node;
1529 SHASH_FOR_EACH (node, &netdev_linux_notifiers) {
1530 poll_notify(node->data);
1531 }
1532 }
1533}
1534
1535static int
1536netdev_linux_poll_add(struct netdev *netdev,
1537 void (*cb)(struct netdev_notifier *), void *aux,
1538 struct netdev_notifier **notifierp)
1539{
1540 const char *netdev_name = netdev_get_name(netdev);
1541 struct netdev_linux_notifier *notifier;
1542 struct list *list;
1543
1544 if (shash_is_empty(&netdev_linux_notifiers)) {
46097491 1545 int error = rtnetlink_notifier_register(&netdev_linux_poll_notifier,
8b61709d
BP
1546 netdev_linux_poll_cb, NULL);
1547 if (error) {
1548 return error;
1549 }
1550 }
1551
1552 list = shash_find_data(&netdev_linux_notifiers, netdev_name);
1553 if (!list) {
1554 list = xmalloc(sizeof *list);
1555 list_init(list);
1556 shash_add(&netdev_linux_notifiers, netdev_name, list);
1557 }
1558
1559 notifier = xmalloc(sizeof *notifier);
1560 netdev_notifier_init(&notifier->notifier, netdev, cb, aux);
1561 list_push_back(list, &notifier->node);
1562 *notifierp = &notifier->notifier;
1563 return 0;
1564}
1565
1566static void
1567netdev_linux_poll_remove(struct netdev_notifier *notifier_)
1568{
1569 struct netdev_linux_notifier *notifier =
1570 CONTAINER_OF(notifier_, struct netdev_linux_notifier, notifier);
1571 struct list *list;
1572
1573 /* Remove 'notifier' from its list. */
1574 list = list_remove(&notifier->node);
1575 if (list_is_empty(list)) {
1576 /* The list is now empty. Remove it from the hash and free it. */
1577 const char *netdev_name = netdev_get_name(notifier->notifier.netdev);
1578 shash_delete(&netdev_linux_notifiers,
1579 shash_find(&netdev_linux_notifiers, netdev_name));
1580 free(list);
1581 }
1582 free(notifier);
1583
1584 /* If that was the last notifier, unregister. */
1585 if (shash_is_empty(&netdev_linux_notifiers)) {
46097491 1586 rtnetlink_notifier_unregister(&netdev_linux_poll_notifier);
8b61709d
BP
1587 }
1588}
1589
1590const struct netdev_class netdev_linux_class = {
149f577a 1591 "system",
8b61709d
BP
1592
1593 netdev_linux_init,
1594 netdev_linux_run,
1595 netdev_linux_wait,
1596
a740f0de 1597 netdev_linux_create_system,
6c88d577
JP
1598 netdev_linux_destroy,
1599 NULL, /* reconfigure */
1600
8b61709d
BP
1601 netdev_linux_open,
1602 netdev_linux_close,
1603
1604 netdev_linux_enumerate,
1605
1606 netdev_linux_recv,
1607 netdev_linux_recv_wait,
1608 netdev_linux_drain,
1609
1610 netdev_linux_send,
1611 netdev_linux_send_wait,
1612
1613 netdev_linux_set_etheraddr,
1614 netdev_linux_get_etheraddr,
1615 netdev_linux_get_mtu,
9ab3d9a3 1616 netdev_linux_get_ifindex,
8b61709d
BP
1617 netdev_linux_get_carrier,
1618 netdev_linux_get_stats,
8722022c 1619 netdev_linux_set_stats,
8b61709d
BP
1620
1621 netdev_linux_get_features,
1622 netdev_linux_set_advertisements,
1623 netdev_linux_get_vlan_vid,
1624 netdev_linux_set_policing,
1625
1626 netdev_linux_get_in4,
1627 netdev_linux_set_in4,
1628 netdev_linux_get_in6,
1629 netdev_linux_add_router,
f1acd62b 1630 netdev_linux_get_next_hop,
8b61709d
BP
1631 netdev_linux_arp_lookup,
1632
1633 netdev_linux_update_flags,
1634
1635 netdev_linux_poll_add,
1636 netdev_linux_poll_remove,
1637};
1638
1639const struct netdev_class netdev_tap_class = {
149f577a 1640 "tap",
8b61709d
BP
1641
1642 netdev_linux_init,
149f577a
JG
1643 netdev_linux_run,
1644 netdev_linux_wait,
8b61709d 1645
a740f0de 1646 netdev_linux_create_tap,
6c88d577
JP
1647 netdev_linux_destroy,
1648 NULL, /* reconfigure */
1649
8b61709d
BP
1650 netdev_linux_open,
1651 netdev_linux_close,
1652
149f577a 1653 NULL, /* enumerate */
8b61709d
BP
1654
1655 netdev_linux_recv,
1656 netdev_linux_recv_wait,
1657 netdev_linux_drain,
1658
1659 netdev_linux_send,
1660 netdev_linux_send_wait,
1661
1662 netdev_linux_set_etheraddr,
1663 netdev_linux_get_etheraddr,
1664 netdev_linux_get_mtu,
9ab3d9a3 1665 netdev_linux_get_ifindex,
8b61709d
BP
1666 netdev_linux_get_carrier,
1667 netdev_linux_get_stats,
8722022c 1668 NULL, /* set_stats */
8b61709d
BP
1669
1670 netdev_linux_get_features,
1671 netdev_linux_set_advertisements,
a740f0de
JG
1672 netdev_linux_get_vlan_vid,
1673 netdev_linux_set_policing,
1674
1675 netdev_linux_get_in4,
1676 netdev_linux_set_in4,
1677 netdev_linux_get_in6,
1678 netdev_linux_add_router,
1679 netdev_linux_get_next_hop,
1680 netdev_linux_arp_lookup,
1681
1682 netdev_linux_update_flags,
1683
1684 netdev_linux_poll_add,
1685 netdev_linux_poll_remove,
1686};
6f643e49 1687
8b61709d
BP
1688\f
1689static int
1690get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
1691{
559843ed
BP
1692 /* Policy for RTNLGRP_LINK messages.
1693 *
1694 * There are *many* more fields in these messages, but currently we only
1695 * care about these fields. */
1696 static const struct nl_policy rtnlgrp_link_policy[] = {
1697 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
1698 [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
1699 .min_len = sizeof(struct rtnl_link_stats) },
1700 };
1701
a5af30fb 1702 struct nl_sock *rtnl_sock;
8b61709d
BP
1703 struct ofpbuf request;
1704 struct ofpbuf *reply;
1705 struct ifinfomsg *ifi;
1706 const struct rtnl_link_stats *rtnl_stats;
1707 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
1708 int error;
1709
a5af30fb
BP
1710 error = get_rtnl_sock(&rtnl_sock);
1711 if (error) {
1712 return error;
8b61709d
BP
1713 }
1714
1715 ofpbuf_init(&request, 0);
1716 nl_msg_put_nlmsghdr(&request, rtnl_sock, sizeof *ifi,
1717 RTM_GETLINK, NLM_F_REQUEST);
1718 ifi = ofpbuf_put_zeros(&request, sizeof *ifi);
1719 ifi->ifi_family = PF_UNSPEC;
1720 ifi->ifi_index = ifindex;
1721 error = nl_sock_transact(rtnl_sock, &request, &reply);
1722 ofpbuf_uninit(&request);
1723 if (error) {
1724 return error;
1725 }
1726
1727 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
1728 rtnlgrp_link_policy,
1729 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
1730 ofpbuf_delete(reply);
1731 return EPROTO;
1732 }
1733
1734 if (!attrs[IFLA_STATS]) {
1735 VLOG_WARN_RL(&rl, "RTM_GETLINK reply lacks stats");
576e26d7 1736 ofpbuf_delete(reply);
8b61709d
BP
1737 return EPROTO;
1738 }
1739
1740 rtnl_stats = nl_attr_get(attrs[IFLA_STATS]);
1741 stats->rx_packets = rtnl_stats->rx_packets;
1742 stats->tx_packets = rtnl_stats->tx_packets;
1743 stats->rx_bytes = rtnl_stats->rx_bytes;
1744 stats->tx_bytes = rtnl_stats->tx_bytes;
1745 stats->rx_errors = rtnl_stats->rx_errors;
1746 stats->tx_errors = rtnl_stats->tx_errors;
1747 stats->rx_dropped = rtnl_stats->rx_dropped;
1748 stats->tx_dropped = rtnl_stats->tx_dropped;
1749 stats->multicast = rtnl_stats->multicast;
1750 stats->collisions = rtnl_stats->collisions;
1751 stats->rx_length_errors = rtnl_stats->rx_length_errors;
1752 stats->rx_over_errors = rtnl_stats->rx_over_errors;
1753 stats->rx_crc_errors = rtnl_stats->rx_crc_errors;
1754 stats->rx_frame_errors = rtnl_stats->rx_frame_errors;
1755 stats->rx_fifo_errors = rtnl_stats->rx_fifo_errors;
1756 stats->rx_missed_errors = rtnl_stats->rx_missed_errors;
1757 stats->tx_aborted_errors = rtnl_stats->tx_aborted_errors;
1758 stats->tx_carrier_errors = rtnl_stats->tx_carrier_errors;
1759 stats->tx_fifo_errors = rtnl_stats->tx_fifo_errors;
1760 stats->tx_heartbeat_errors = rtnl_stats->tx_heartbeat_errors;
1761 stats->tx_window_errors = rtnl_stats->tx_window_errors;
1762
576e26d7
BP
1763 ofpbuf_delete(reply);
1764
8b61709d
BP
1765 return 0;
1766}
1767
1768static int
1769get_stats_via_proc(const char *netdev_name, struct netdev_stats *stats)
1770{
1771 static const char fn[] = "/proc/net/dev";
1772 char line[1024];
1773 FILE *stream;
1774 int ln;
1775
1776 stream = fopen(fn, "r");
1777 if (!stream) {
1778 VLOG_WARN_RL(&rl, "%s: open failed: %s", fn, strerror(errno));
1779 return errno;
1780 }
1781
1782 ln = 0;
1783 while (fgets(line, sizeof line, stream)) {
1784 if (++ln >= 3) {
1785 char devname[16];
1786#define X64 "%"SCNu64
1787 if (sscanf(line,
1788 " %15[^:]:"
1789 X64 X64 X64 X64 X64 X64 X64 "%*u"
1790 X64 X64 X64 X64 X64 X64 X64 "%*u",
1791 devname,
1792 &stats->rx_bytes,
1793 &stats->rx_packets,
1794 &stats->rx_errors,
1795 &stats->rx_dropped,
1796 &stats->rx_fifo_errors,
1797 &stats->rx_frame_errors,
1798 &stats->multicast,
1799 &stats->tx_bytes,
1800 &stats->tx_packets,
1801 &stats->tx_errors,
1802 &stats->tx_dropped,
1803 &stats->tx_fifo_errors,
1804 &stats->collisions,
1805 &stats->tx_carrier_errors) != 15) {
1806 VLOG_WARN_RL(&rl, "%s:%d: parse error", fn, ln);
1807 } else if (!strcmp(devname, netdev_name)) {
1808 stats->rx_length_errors = UINT64_MAX;
1809 stats->rx_over_errors = UINT64_MAX;
1810 stats->rx_crc_errors = UINT64_MAX;
1811 stats->rx_missed_errors = UINT64_MAX;
1812 stats->tx_aborted_errors = UINT64_MAX;
1813 stats->tx_heartbeat_errors = UINT64_MAX;
1814 stats->tx_window_errors = UINT64_MAX;
1815 fclose(stream);
1816 return 0;
1817 }
1818 }
1819 }
1820 VLOG_WARN_RL(&rl, "%s: no stats for %s", fn, netdev_name);
1821 fclose(stream);
1822 return ENODEV;
1823}
1824\f
1825static int
1826get_flags(const struct netdev *netdev, int *flags)
1827{
1828 struct ifreq ifr;
1829 int error;
1830
149f577a
JG
1831 error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, SIOCGIFFLAGS,
1832 "SIOCGIFFLAGS");
8b61709d
BP
1833 *flags = ifr.ifr_flags;
1834 return error;
1835}
1836
1837static int
1838set_flags(struct netdev *netdev, int flags)
1839{
1840 struct ifreq ifr;
1841
1842 ifr.ifr_flags = flags;
149f577a
JG
1843 return netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, SIOCSIFFLAGS,
1844 "SIOCSIFFLAGS");
8b61709d
BP
1845}
1846
1847static int
1848do_get_ifindex(const char *netdev_name)
1849{
1850 struct ifreq ifr;
1851
1852 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1853 COVERAGE_INC(netdev_get_ifindex);
1854 if (ioctl(af_inet_sock, SIOCGIFINDEX, &ifr) < 0) {
1855 VLOG_WARN_RL(&rl, "ioctl(SIOCGIFINDEX) on %s device failed: %s",
1856 netdev_name, strerror(errno));
1857 return -errno;
1858 }
1859 return ifr.ifr_ifindex;
1860}
1861
1862static int
1863get_ifindex(const struct netdev *netdev_, int *ifindexp)
1864{
149f577a
JG
1865 struct netdev_dev_linux *netdev_dev =
1866 netdev_dev_linux_cast(netdev_get_dev(netdev_));
8b61709d 1867 *ifindexp = 0;
149f577a 1868 if (!(netdev_dev->cache_valid & VALID_IFINDEX)) {
8b61709d
BP
1869 int ifindex = do_get_ifindex(netdev_get_name(netdev_));
1870 if (ifindex < 0) {
1871 return -ifindex;
1872 }
149f577a
JG
1873 netdev_dev->cache_valid |= VALID_IFINDEX;
1874 netdev_dev->ifindex = ifindex;
8b61709d 1875 }
149f577a 1876 *ifindexp = netdev_dev->ifindex;
8b61709d
BP
1877 return 0;
1878}
1879
1880static int
1881get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
1882{
1883 struct ifreq ifr;
1884 int hwaddr_family;
1885
1886 memset(&ifr, 0, sizeof ifr);
1887 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1888 COVERAGE_INC(netdev_get_hwaddr);
1889 if (ioctl(af_inet_sock, SIOCGIFHWADDR, &ifr) < 0) {
1890 VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
1891 netdev_name, strerror(errno));
1892 return errno;
1893 }
1894 hwaddr_family = ifr.ifr_hwaddr.sa_family;
1895 if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
1896 VLOG_WARN("%s device has unknown hardware address family %d",
1897 netdev_name, hwaddr_family);
1898 }
1899 memcpy(ea, ifr.ifr_hwaddr.sa_data, ETH_ADDR_LEN);
1900 return 0;
1901}
1902
1903static int
1904set_etheraddr(const char *netdev_name, int hwaddr_family,
1905 const uint8_t mac[ETH_ADDR_LEN])
1906{
1907 struct ifreq ifr;
1908
1909 memset(&ifr, 0, sizeof ifr);
1910 strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
1911 ifr.ifr_hwaddr.sa_family = hwaddr_family;
1912 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
1913 COVERAGE_INC(netdev_set_hwaddr);
1914 if (ioctl(af_inet_sock, SIOCSIFHWADDR, &ifr) < 0) {
1915 VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
1916 netdev_name, strerror(errno));
1917 return errno;
1918 }
1919 return 0;
1920}
1921
1922static int
0b0544d7 1923netdev_linux_do_ethtool(const char *name, struct ethtool_cmd *ecmd,
8b61709d
BP
1924 int cmd, const char *cmd_name)
1925{
1926 struct ifreq ifr;
1927
1928 memset(&ifr, 0, sizeof ifr);
0b0544d7 1929 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
8b61709d
BP
1930 ifr.ifr_data = (caddr_t) ecmd;
1931
1932 ecmd->cmd = cmd;
1933 COVERAGE_INC(netdev_ethtool);
1934 if (ioctl(af_inet_sock, SIOCETHTOOL, &ifr) == 0) {
1935 return 0;
1936 } else {
1937 if (errno != EOPNOTSUPP) {
1938 VLOG_WARN_RL(&rl, "ethtool command %s on network device %s "
0b0544d7 1939 "failed: %s", cmd_name, name, strerror(errno));
8b61709d
BP
1940 } else {
1941 /* The device doesn't support this operation. That's pretty
1942 * common, so there's no point in logging anything. */
1943 }
1944 return errno;
1945 }
1946}
1947
1948static int
149f577a
JG
1949netdev_linux_do_ioctl(const char *name, struct ifreq *ifr, int cmd,
1950 const char *cmd_name)
8b61709d 1951{
149f577a 1952 strncpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
8b61709d 1953 if (ioctl(af_inet_sock, cmd, ifr) == -1) {
149f577a
JG
1954 VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
1955 strerror(errno));
8b61709d
BP
1956 return errno;
1957 }
1958 return 0;
1959}
f1acd62b
BP
1960
1961static int
1962netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip,
1963 int cmd, const char *cmd_name)
1964{
1965 struct ifreq ifr;
1966 int error;
1967
1968 ifr.ifr_addr.sa_family = AF_INET;
149f577a 1969 error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name);
f1acd62b
BP
1970 if (!error) {
1971 const struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
1972 *ip = sin->sin_addr;
1973 }
1974 return error;
1975}
a5af30fb
BP
1976
1977/* Obtains a Netlink routing socket that is not subscribed to any multicast
1978 * groups. Returns 0 if successful, otherwise a positive errno value. Stores
1979 * the socket in '*rtnl_sockp' if successful, otherwise a null pointer. */
1980static int
1981get_rtnl_sock(struct nl_sock **rtnl_sockp)
1982{
1983 static struct nl_sock *sock;
1984 int error;
1985
1986 if (!sock) {
1987 error = nl_sock_create(NETLINK_ROUTE, 0, 0, 0, &sock);
1988 if (error) {
1989 VLOG_ERR_RL(&rl, "failed to create rtnetlink socket: %s",
1990 strerror(error));
1991 }
1992 } else {
1993 error = 0;
1994 }
1995
1996 *rtnl_sockp = sock;
1997 return error;
1998}