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