]>
Commit | Line | Data |
---|---|---|
f6eb6b20 | 1 | /* |
e5e4b47c | 2 | * Copyright (c) 2011, 2013, 2014 Gaetano Catalli. |
a0cbddc0 | 3 | * Copyright (c) 2013, 2014 YAMAMOTO Takashi. |
f6eb6b20 GL |
4 | * |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at: | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
97d0619c | 18 | #if !defined(__MACH__) |
f6eb6b20 GL |
19 | #include <config.h> |
20 | ||
796223f5 | 21 | #include "netdev-provider.h" |
f6eb6b20 | 22 | #include <stdlib.h> |
f6eb6b20 GL |
23 | #include <errno.h> |
24 | #include <fcntl.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/time.h> | |
27 | #include <sys/ioctl.h> | |
28 | #include <sys/socket.h> | |
29 | #include <sys/sockio.h> | |
ccf7b34e | 30 | #include <net/bpf.h> |
f6eb6b20 GL |
31 | #include <ifaddrs.h> |
32 | #include <pcap/pcap.h> | |
33 | #include <net/if.h> | |
34 | #include <net/if_dl.h> | |
35 | #include <net/if_media.h> | |
36 | #include <net/if_tap.h> | |
37 | #include <netinet/in.h> | |
666afb55 | 38 | #ifdef HAVE_NET_IF_MIB_H |
f6eb6b20 | 39 | #include <net/if_mib.h> |
666afb55 | 40 | #endif |
f6eb6b20 GL |
41 | #include <poll.h> |
42 | #include <string.h> | |
43 | #include <unistd.h> | |
44 | #include <sys/sysctl.h> | |
d00409c5 YT |
45 | #if defined(__NetBSD__) |
46 | #include <net/route.h> | |
7b6d4b2a | 47 | #include <netinet/if_inarp.h> |
d00409c5 | 48 | #endif |
f6eb6b20 GL |
49 | |
50 | #include "rtbsd.h" | |
51 | #include "coverage.h" | |
e14deea0 | 52 | #include "dp-packet.h" |
df1e5a3b | 53 | #include "dpif-netdev.h" |
3e8a2ad1 | 54 | #include "openvswitch/dynamic-string.h" |
f6eb6b20 | 55 | #include "fatal-signal.h" |
f6eb6b20 | 56 | #include "openflow/openflow.h" |
996e5ae9 | 57 | #include "ovs-thread.h" |
f6eb6b20 GL |
58 | #include "packets.h" |
59 | #include "poll-loop.h" | |
ee89ea7b | 60 | #include "openvswitch/shash.h" |
da4a6191 | 61 | #include "socket-util.h" |
f6eb6b20 | 62 | #include "svec.h" |
cb22974d | 63 | #include "util.h" |
e6211adc | 64 | #include "openvswitch/vlog.h" |
f6eb6b20 GL |
65 | |
66 | VLOG_DEFINE_THIS_MODULE(netdev_bsd); | |
67 | ||
68 | \f | |
a0cbddc0 YT |
69 | struct netdev_rxq_bsd { |
70 | struct netdev_rxq up; | |
f6eb6b20 | 71 | |
796223f5 BP |
72 | /* Packet capture descriptor for a system network device. |
73 | * For a tap device this is NULL. */ | |
74 | pcap_t *pcap_handle; | |
f6eb6b20 | 75 | |
796223f5 BP |
76 | /* Selectable file descriptor for the network device. |
77 | * This descriptor will be used for polling operations. */ | |
78 | int fd; | |
f6eb6b20 GL |
79 | }; |
80 | ||
b5d57fc8 BP |
81 | struct netdev_bsd { |
82 | struct netdev up; | |
86383816 BP |
83 | |
84 | /* Never changes after initialization. */ | |
85 | char *kernel_name; | |
86 | ||
87 | /* Protects all members below. */ | |
88 | struct ovs_mutex mutex; | |
89 | ||
f6eb6b20 | 90 | unsigned int cache_valid; |
f6eb6b20 GL |
91 | |
92 | int ifindex; | |
74ff3298 | 93 | struct eth_addr etheraddr; |
f6eb6b20 GL |
94 | int mtu; |
95 | int carrier; | |
96 | ||
796223f5 BP |
97 | int tap_fd; /* TAP character device, if any, otherwise -1. */ |
98 | ||
99 | /* Used for sending packets on non-tap devices. */ | |
100 | pcap_t *pcap; | |
101 | int fd; | |
f6eb6b20 GL |
102 | }; |
103 | ||
104 | ||
105 | enum { | |
6b6e1329 | 106 | VALID_IFINDEX = 1 << 0, |
f6eb6b20 | 107 | VALID_ETHERADDR = 1 << 1, |
6b6e1329 PS |
108 | VALID_IN = 1 << 2, |
109 | VALID_MTU = 1 << 3, | |
110 | VALID_CARRIER = 1 << 4 | |
f6eb6b20 GL |
111 | }; |
112 | ||
f6eb6b20 GL |
113 | #define PCAP_SNAPLEN 2048 |
114 | ||
115 | ||
116 | /* | |
117 | * Notifier used to invalidate device informations in case of status change. | |
118 | * | |
119 | * It will be registered with a 'rtbsd_notifier_register()' when the first | |
120 | * device will be created with the call of either 'netdev_bsd_tap_create()' or | |
121 | * 'netdev_bsd_system_create()'. | |
122 | * | |
123 | * The callback associated with this notifier ('netdev_bsd_cache_cb()') will | |
124 | * invalidate cached information about the device. | |
125 | */ | |
126 | static struct rtbsd_notifier netdev_bsd_cache_notifier; | |
127 | static int cache_notifier_refcount; | |
128 | ||
129 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); | |
130 | ||
f6eb6b20 | 131 | static void destroy_tap(int fd, const char *name); |
b5d57fc8 | 132 | static int get_flags(const struct netdev *, int *flagsp); |
4b609110 | 133 | static int set_flags(const char *, int flags); |
f6eb6b20 | 134 | static int do_set_addr(struct netdev *netdev, |
b72d8dc2 | 135 | unsigned long ioctl_nr, const char *ioctl_name, |
f6eb6b20 | 136 | struct in_addr addr); |
74ff3298 | 137 | static int get_etheraddr(const char *netdev_name, struct eth_addr *ea); |
f6eb6b20 | 138 | static int set_etheraddr(const char *netdev_name, int hwaddr_family, |
74ff3298 | 139 | int hwaddr_len, const struct eth_addr); |
f6eb6b20 GL |
140 | static int get_ifindex(const struct netdev *, int *ifindexp); |
141 | ||
666afb55 YT |
142 | static int ifr_get_flags(const struct ifreq *); |
143 | static void ifr_set_flags(struct ifreq *, int flags); | |
144 | ||
996e5ae9 | 145 | #ifdef __NetBSD__ |
b72d8dc2 | 146 | static int af_link_ioctl(unsigned long command, const void *arg); |
996e5ae9 BP |
147 | #endif |
148 | ||
1c33f0c3 | 149 | static void netdev_bsd_run(const struct netdev_class *); |
df1e5a3b | 150 | static int netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup); |
f6eb6b20 GL |
151 | |
152 | static bool | |
153 | is_netdev_bsd_class(const struct netdev_class *netdev_class) | |
154 | { | |
996e5ae9 | 155 | return netdev_class->run == netdev_bsd_run; |
f6eb6b20 GL |
156 | } |
157 | ||
158 | static struct netdev_bsd * | |
159 | netdev_bsd_cast(const struct netdev *netdev) | |
160 | { | |
b5d57fc8 | 161 | ovs_assert(is_netdev_bsd_class(netdev_get_class(netdev))); |
180c6d0b | 162 | return CONTAINER_OF(netdev, struct netdev_bsd, up); |
f6eb6b20 GL |
163 | } |
164 | ||
a0cbddc0 YT |
165 | static struct netdev_rxq_bsd * |
166 | netdev_rxq_bsd_cast(const struct netdev_rxq *rxq) | |
796223f5 | 167 | { |
a0cbddc0 YT |
168 | ovs_assert(is_netdev_bsd_class(netdev_get_class(rxq->netdev))); |
169 | return CONTAINER_OF(rxq, struct netdev_rxq_bsd, up); | |
796223f5 BP |
170 | } |
171 | ||
87ad1c07 YT |
172 | static const char * |
173 | netdev_get_kernel_name(const struct netdev *netdev) | |
174 | { | |
175 | return netdev_bsd_cast(netdev)->kernel_name; | |
176 | } | |
177 | ||
f6eb6b20 GL |
178 | /* |
179 | * Perform periodic work needed by netdev. In BSD netdevs it checks for any | |
180 | * interface status changes, and eventually calls all the user callbacks. | |
181 | */ | |
182 | static void | |
1c33f0c3 | 183 | netdev_bsd_run(const struct netdev_class *netdev_class OVS_UNUSED) |
f6eb6b20 GL |
184 | { |
185 | rtbsd_notifier_run(); | |
186 | } | |
187 | ||
188 | /* | |
189 | * Arranges for poll_block() to wake up if the "run" member function needs to | |
190 | * be called. | |
191 | */ | |
192 | static void | |
1c33f0c3 | 193 | netdev_bsd_wait(const struct netdev_class *netdev_class OVS_UNUSED) |
f6eb6b20 GL |
194 | { |
195 | rtbsd_notifier_wait(); | |
196 | } | |
197 | ||
f6eb6b20 GL |
198 | /* Invalidate cache in case of interface status change. */ |
199 | static void | |
200 | netdev_bsd_cache_cb(const struct rtbsd_change *change, | |
bb79b46b | 201 | void *aux OVS_UNUSED) |
f6eb6b20 | 202 | { |
b5d57fc8 | 203 | struct netdev_bsd *dev; |
f6eb6b20 GL |
204 | |
205 | if (change) { | |
b5d57fc8 | 206 | struct netdev *base_dev = netdev_from_name(change->if_name); |
f6eb6b20 GL |
207 | |
208 | if (base_dev) { | |
209 | const struct netdev_class *netdev_class = | |
b5d57fc8 | 210 | netdev_get_class(base_dev); |
f6eb6b20 GL |
211 | |
212 | if (is_netdev_bsd_class(netdev_class)) { | |
b5d57fc8 | 213 | dev = netdev_bsd_cast(base_dev); |
f6eb6b20 | 214 | dev->cache_valid = 0; |
3e912ffc | 215 | netdev_change_seq_changed(base_dev); |
f6eb6b20 | 216 | } |
2f980d74 | 217 | netdev_close(base_dev); |
f6eb6b20 GL |
218 | } |
219 | } else { | |
220 | /* | |
221 | * XXX the API is lacking, we should be able to iterate on the list of | |
222 | * netdevs without having to store the info in a temp shash. | |
223 | */ | |
224 | struct shash device_shash; | |
225 | struct shash_node *node; | |
226 | ||
227 | shash_init(&device_shash); | |
b5d57fc8 | 228 | netdev_get_devices(&netdev_bsd_class, &device_shash); |
f6eb6b20 | 229 | SHASH_FOR_EACH (node, &device_shash) { |
9da7ae12 BP |
230 | struct netdev *netdev = node->data; |
231 | dev = netdev_bsd_cast(netdev); | |
f6eb6b20 | 232 | dev->cache_valid = 0; |
3e912ffc | 233 | netdev_change_seq_changed(netdev); |
2f980d74 | 234 | netdev_close(netdev); |
f6eb6b20 GL |
235 | } |
236 | shash_destroy(&device_shash); | |
237 | } | |
238 | } | |
239 | ||
240 | static int | |
241 | cache_notifier_ref(void) | |
242 | { | |
243 | int ret = 0; | |
244 | ||
245 | if (!cache_notifier_refcount) { | |
246 | ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier, | |
5ae76b41 | 247 | netdev_bsd_cache_cb, NULL); |
f6eb6b20 GL |
248 | if (ret) { |
249 | return ret; | |
250 | } | |
251 | } | |
252 | cache_notifier_refcount++; | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static int | |
257 | cache_notifier_unref(void) | |
258 | { | |
259 | cache_notifier_refcount--; | |
260 | if (cache_notifier_refcount == 0) { | |
261 | rtbsd_notifier_unregister(&netdev_bsd_cache_notifier); | |
262 | } | |
263 | return 0; | |
264 | } | |
265 | ||
9dc63482 BP |
266 | static struct netdev * |
267 | netdev_bsd_alloc(void) | |
268 | { | |
269 | struct netdev_bsd *netdev = xzalloc(sizeof *netdev); | |
270 | return &netdev->up; | |
271 | } | |
272 | ||
f6eb6b20 | 273 | static int |
9dc63482 | 274 | netdev_bsd_construct_system(struct netdev *netdev_) |
f6eb6b20 | 275 | { |
9dc63482 | 276 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
b5d57fc8 | 277 | enum netdev_flags flags; |
f6eb6b20 GL |
278 | int error; |
279 | ||
280 | error = cache_notifier_ref(); | |
281 | if (error) { | |
282 | return error; | |
283 | } | |
284 | ||
834d6caf | 285 | ovs_mutex_init(&netdev->mutex); |
b5d57fc8 | 286 | netdev->tap_fd = -1; |
9dc63482 | 287 | netdev->kernel_name = xstrdup(netdev_->name); |
f6eb6b20 | 288 | |
b5d57fc8 | 289 | /* Verify that the netdev really exists by attempting to read its flags */ |
9dc63482 | 290 | error = netdev_get_flags(netdev_, &flags); |
b5d57fc8 | 291 | if (error == ENXIO) { |
23e29ebb | 292 | free(netdev->kernel_name); |
b5d57fc8 | 293 | cache_notifier_unref(); |
ed7f2174 | 294 | ovs_mutex_destroy(&netdev->mutex); |
b5d57fc8 BP |
295 | return error; |
296 | } | |
297 | ||
f6eb6b20 GL |
298 | return 0; |
299 | } | |
300 | ||
f6eb6b20 | 301 | static int |
9dc63482 | 302 | netdev_bsd_construct_tap(struct netdev *netdev_) |
f6eb6b20 | 303 | { |
9dc63482 BP |
304 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
305 | const char *name = netdev_->name; | |
f6eb6b20 GL |
306 | int error = 0; |
307 | struct ifreq ifr; | |
87ad1c07 | 308 | char *kernel_name = NULL; |
f6eb6b20 GL |
309 | |
310 | error = cache_notifier_ref(); | |
311 | if (error) { | |
312 | goto error; | |
313 | } | |
314 | ||
f6eb6b20 GL |
315 | memset(&ifr, 0, sizeof(ifr)); |
316 | ||
317 | /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used | |
318 | * to retrieve the name of the tap device. */ | |
834d6caf | 319 | ovs_mutex_init(&netdev->mutex); |
b5d57fc8 | 320 | netdev->tap_fd = open("/dev/tap", O_RDWR); |
b5d57fc8 | 321 | if (netdev->tap_fd < 0) { |
f6eb6b20 | 322 | error = errno; |
10a89ef0 | 323 | VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error)); |
6de8e4b2 | 324 | goto error_unref_notifier; |
f6eb6b20 GL |
325 | } |
326 | ||
327 | /* Retrieve tap name (e.g. tap0) */ | |
b5d57fc8 | 328 | if (ioctl(netdev->tap_fd, TAPGIFNAME, &ifr) == -1) { |
f6eb6b20 GL |
329 | /* XXX Need to destroy the device? */ |
330 | error = errno; | |
a5440992 | 331 | close(netdev->tap_fd); |
6de8e4b2 | 332 | goto error_unref_notifier; |
f6eb6b20 GL |
333 | } |
334 | ||
335 | /* Change the name of the tap device */ | |
666afb55 | 336 | #if defined(SIOCSIFNAME) |
f6eb6b20 | 337 | ifr.ifr_data = (void *)name; |
259e0b1a BP |
338 | error = af_inet_ioctl(SIOCSIFNAME, &ifr); |
339 | if (error) { | |
b5d57fc8 | 340 | destroy_tap(netdev->tap_fd, ifr.ifr_name); |
6de8e4b2 | 341 | goto error_unref_notifier; |
f6eb6b20 | 342 | } |
87ad1c07 | 343 | kernel_name = xstrdup(name); |
666afb55 YT |
344 | #else |
345 | /* | |
666afb55 YT |
346 | * NetBSD doesn't support inteface renaming. |
347 | */ | |
348 | VLOG_INFO("tap %s is created for bridge %s", ifr.ifr_name, name); | |
87ad1c07 | 349 | kernel_name = xstrdup(ifr.ifr_name); |
666afb55 | 350 | #endif |
f6eb6b20 GL |
351 | |
352 | /* set non-blocking. */ | |
b5d57fc8 | 353 | error = set_nonblocking(netdev->tap_fd); |
f6eb6b20 | 354 | if (error) { |
87ad1c07 | 355 | destroy_tap(netdev->tap_fd, kernel_name); |
6de8e4b2 | 356 | goto error_unref_notifier; |
f6eb6b20 GL |
357 | } |
358 | ||
359 | /* Turn device UP */ | |
666afb55 | 360 | ifr_set_flags(&ifr, IFF_UP); |
8742957c | 361 | ovs_strlcpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name); |
259e0b1a BP |
362 | error = af_inet_ioctl(SIOCSIFFLAGS, &ifr); |
363 | if (error) { | |
87ad1c07 | 364 | destroy_tap(netdev->tap_fd, kernel_name); |
6de8e4b2 | 365 | goto error_unref_notifier; |
f6eb6b20 GL |
366 | } |
367 | ||
87ad1c07 | 368 | netdev->kernel_name = kernel_name; |
f6eb6b20 GL |
369 | |
370 | return 0; | |
371 | ||
6de8e4b2 | 372 | error_unref_notifier: |
86383816 | 373 | ovs_mutex_destroy(&netdev->mutex); |
f6eb6b20 GL |
374 | cache_notifier_unref(); |
375 | error: | |
87ad1c07 | 376 | free(kernel_name); |
f6eb6b20 GL |
377 | return error; |
378 | } | |
379 | ||
380 | static void | |
9dc63482 | 381 | netdev_bsd_destruct(struct netdev *netdev_) |
f6eb6b20 | 382 | { |
b5d57fc8 | 383 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
f6eb6b20 GL |
384 | |
385 | cache_notifier_unref(); | |
386 | ||
b5d57fc8 | 387 | if (netdev->tap_fd >= 0) { |
87ad1c07 | 388 | destroy_tap(netdev->tap_fd, netdev_get_kernel_name(netdev_)); |
f6eb6b20 | 389 | } |
b5d57fc8 BP |
390 | if (netdev->pcap) { |
391 | pcap_close(netdev->pcap); | |
796223f5 | 392 | } |
87ad1c07 | 393 | free(netdev->kernel_name); |
86383816 | 394 | ovs_mutex_destroy(&netdev->mutex); |
9dc63482 BP |
395 | } |
396 | ||
397 | static void | |
398 | netdev_bsd_dealloc(struct netdev *netdev_) | |
399 | { | |
400 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); | |
401 | ||
f6eb6b20 GL |
402 | free(netdev); |
403 | } | |
404 | ||
405 | static int | |
796223f5 | 406 | netdev_bsd_open_pcap(const char *name, pcap_t **pcapp, int *fdp) |
f6eb6b20 | 407 | { |
f6eb6b20 | 408 | char errbuf[PCAP_ERRBUF_SIZE]; |
796223f5 | 409 | pcap_t *pcap = NULL; |
f6eb6b20 | 410 | int one = 1; |
796223f5 BP |
411 | int error; |
412 | int fd; | |
f6eb6b20 | 413 | |
796223f5 | 414 | /* Open the pcap device. The device is opened in non-promiscuous mode |
f6eb6b20 GL |
415 | * because the interface flags are manually set by the caller. */ |
416 | errbuf[0] = '\0'; | |
796223f5 BP |
417 | pcap = pcap_open_live(name, PCAP_SNAPLEN, 0, 1000, errbuf); |
418 | if (!pcap) { | |
419 | VLOG_ERR_RL(&rl, "%s: pcap_open_live failed: %s", name, errbuf); | |
f6eb6b20 GL |
420 | error = EIO; |
421 | goto error; | |
796223f5 BP |
422 | } |
423 | if (errbuf[0] != '\0') { | |
424 | VLOG_WARN_RL(&rl, "%s: pcap_open_live: %s", name, errbuf); | |
f6eb6b20 GL |
425 | } |
426 | ||
796223f5 BP |
427 | /* Get the underlying fd. */ |
428 | fd = pcap_get_selectable_fd(pcap); | |
f6eb6b20 | 429 | if (fd == -1) { |
796223f5 | 430 | VLOG_WARN_RL(&rl, "%s: no selectable file descriptor", name); |
f6eb6b20 GL |
431 | error = errno; |
432 | goto error; | |
433 | } | |
434 | ||
435 | /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called | |
436 | * on the file descriptor returned by pcap_get_selectable_fd to achieve | |
437 | * a real non-blocking behaviour.*/ | |
796223f5 | 438 | error = pcap_setnonblock(pcap, 1, errbuf); |
f6eb6b20 GL |
439 | if (error == -1) { |
440 | error = errno; | |
441 | goto error; | |
442 | } | |
443 | ||
796223f5 BP |
444 | /* This call assure that reads return immediately upon packet |
445 | * reception. Otherwise, a read will block until either the kernel | |
446 | * buffer becomes full or a timeout occurs. */ | |
447 | if (ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) { | |
448 | VLOG_ERR_RL(&rl, "ioctl(BIOCIMMEDIATE) on %s device failed: %s", | |
10a89ef0 | 449 | name, ovs_strerror(errno)); |
f6eb6b20 GL |
450 | error = errno; |
451 | goto error; | |
452 | } | |
453 | ||
796223f5 BP |
454 | /* Capture only incoming packets. */ |
455 | error = pcap_setdirection(pcap, PCAP_D_IN); | |
f6eb6b20 GL |
456 | if (error == -1) { |
457 | error = errno; | |
458 | goto error; | |
459 | } | |
460 | ||
796223f5 BP |
461 | *pcapp = pcap; |
462 | *fdp = fd; | |
f6eb6b20 GL |
463 | return 0; |
464 | ||
465 | error: | |
796223f5 BP |
466 | if (pcap) { |
467 | pcap_close(pcap); | |
f6eb6b20 | 468 | } |
796223f5 BP |
469 | *pcapp = NULL; |
470 | *fdp = -1; | |
f6eb6b20 GL |
471 | return error; |
472 | } | |
473 | ||
a0cbddc0 YT |
474 | static struct netdev_rxq * |
475 | netdev_bsd_rxq_alloc(void) | |
9dc63482 | 476 | { |
a0cbddc0 YT |
477 | struct netdev_rxq_bsd *rxq = xzalloc(sizeof *rxq); |
478 | return &rxq->up; | |
9dc63482 BP |
479 | } |
480 | ||
796223f5 | 481 | static int |
a0cbddc0 | 482 | netdev_bsd_rxq_construct(struct netdev_rxq *rxq_) |
796223f5 | 483 | { |
a0cbddc0 YT |
484 | struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); |
485 | struct netdev *netdev_ = rxq->up.netdev; | |
b5d57fc8 | 486 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
86383816 | 487 | int error; |
796223f5 | 488 | |
796223f5 | 489 | if (!strcmp(netdev_get_type(netdev_), "tap")) { |
a0cbddc0 YT |
490 | rxq->pcap_handle = NULL; |
491 | rxq->fd = netdev->tap_fd; | |
86383816 | 492 | error = 0; |
796223f5 | 493 | } else { |
86383816 BP |
494 | ovs_mutex_lock(&netdev->mutex); |
495 | error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_), | |
a0cbddc0 | 496 | &rxq->pcap_handle, &rxq->fd); |
86383816 | 497 | ovs_mutex_unlock(&netdev->mutex); |
796223f5 BP |
498 | } |
499 | ||
86383816 | 500 | return error; |
796223f5 BP |
501 | } |
502 | ||
503 | static void | |
a0cbddc0 | 504 | netdev_bsd_rxq_destruct(struct netdev_rxq *rxq_) |
796223f5 | 505 | { |
a0cbddc0 | 506 | struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); |
796223f5 | 507 | |
a0cbddc0 YT |
508 | if (rxq->pcap_handle) { |
509 | pcap_close(rxq->pcap_handle); | |
796223f5 | 510 | } |
9dc63482 BP |
511 | } |
512 | ||
513 | static void | |
a0cbddc0 | 514 | netdev_bsd_rxq_dealloc(struct netdev_rxq *rxq_) |
9dc63482 | 515 | { |
a0cbddc0 | 516 | struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); |
9dc63482 | 517 | |
a0cbddc0 | 518 | free(rxq); |
796223f5 | 519 | } |
f6eb6b20 GL |
520 | |
521 | /* The recv callback of the netdev class returns the number of bytes of the | |
522 | * received packet. | |
523 | * | |
524 | * This can be done by the pcap_next() function. Unfortunately pcap_next() does | |
525 | * not make difference between a missing packet on the capture interface and | |
526 | * an error during the file capture. We can use the pcap_dispatch() function | |
527 | * instead, which is able to distinguish between errors and null packet. | |
528 | * | |
529 | * To make pcap_dispatch() returns the number of bytes read from the interface | |
530 | * we need to define the following callback and argument. | |
531 | */ | |
532 | struct pcap_arg { | |
533 | void *data; | |
534 | int size; | |
535 | int retval; | |
536 | }; | |
537 | ||
538 | /* | |
539 | * This callback will be executed on every captured packet. | |
540 | * | |
541 | * If the packet captured by pcap_dispatch() does not fit the pcap buffer, | |
542 | * pcap returns a truncated packet and we follow this behavior. | |
543 | * | |
544 | * The argument args->retval is the packet size in bytes. | |
545 | */ | |
546 | static void | |
547 | proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet) | |
548 | { | |
e4b997e3 | 549 | struct pcap_arg *args = ALIGNED_CAST(struct pcap_arg *, args_); |
f6eb6b20 GL |
550 | |
551 | if (args->size < hdr->len) { | |
552 | VLOG_WARN_RL(&rl, "packet truncated"); | |
553 | args->retval = args->size; | |
554 | } else { | |
555 | args->retval = hdr->len; | |
556 | } | |
557 | ||
558 | /* copy the packet to our buffer */ | |
559 | memcpy(args->data, packet, args->retval); | |
560 | } | |
561 | ||
562 | /* | |
563 | * This function attempts to receive a packet from the specified network | |
564 | * device. It is assumed that the network device is a system device or a tap | |
565 | * device opened as a system one. In this case the read operation is performed | |
a0cbddc0 | 566 | * from rxq->pcap. |
f6eb6b20 GL |
567 | */ |
568 | static int | |
cf62fa4c | 569 | netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, struct dp_packet *buffer) |
f6eb6b20 GL |
570 | { |
571 | struct pcap_arg arg; | |
572 | int ret; | |
573 | ||
f6eb6b20 | 574 | /* prepare the pcap argument to store the packet */ |
cf62fa4c PS |
575 | arg.size = dp_packet_tailroom(buffer); |
576 | arg.data = dp_packet_data(buffer); | |
f6eb6b20 GL |
577 | |
578 | for (;;) { | |
a0cbddc0 | 579 | ret = pcap_dispatch(rxq->pcap_handle, 1, proc_pkt, (u_char *) &arg); |
f6eb6b20 GL |
580 | |
581 | if (ret > 0) { | |
cf62fa4c | 582 | dp_packet_set_size(buffer, dp_packet_size(buffer) + arg.retval); |
bfd3367b | 583 | return 0; |
f6eb6b20 GL |
584 | } |
585 | if (ret == -1) { | |
586 | if (errno == EINTR) { | |
587 | continue; | |
588 | } | |
589 | } | |
590 | ||
bfd3367b | 591 | return EAGAIN; |
f6eb6b20 GL |
592 | } |
593 | } | |
594 | ||
595 | /* | |
596 | * This function attempts to receive a packet from the specified network | |
796223f5 | 597 | * device. It is assumed that the network device is a tap device and |
a0cbddc0 | 598 | * 'rxq->fd' is initialized with the tap file descriptor. |
f6eb6b20 GL |
599 | */ |
600 | static int | |
cf62fa4c | 601 | netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct dp_packet *buffer) |
f6eb6b20 | 602 | { |
cf62fa4c | 603 | size_t size = dp_packet_tailroom(buffer); |
bfd3367b | 604 | |
f6eb6b20 | 605 | for (;;) { |
cf62fa4c | 606 | ssize_t retval = read(rxq->fd, dp_packet_data(buffer), size); |
f6eb6b20 | 607 | if (retval >= 0) { |
cf62fa4c | 608 | dp_packet_set_size(buffer, dp_packet_size(buffer) + retval); |
bfd3367b | 609 | return 0; |
f6eb6b20 GL |
610 | } else if (errno != EINTR) { |
611 | if (errno != EAGAIN) { | |
612 | VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s", | |
a0cbddc0 | 613 | ovs_strerror(errno), netdev_rxq_get_name(&rxq->up)); |
f6eb6b20 | 614 | } |
bfd3367b | 615 | return errno; |
f6eb6b20 GL |
616 | } |
617 | } | |
618 | } | |
619 | ||
f6eb6b20 | 620 | static int |
64839cf4 | 621 | netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet_batch *batch) |
f6eb6b20 | 622 | { |
a0cbddc0 YT |
623 | struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); |
624 | struct netdev *netdev = rxq->up.netdev; | |
e14deea0 | 625 | struct dp_packet *packet; |
df1e5a3b PS |
626 | ssize_t retval; |
627 | int mtu; | |
628 | ||
a0cbddc0 | 629 | if (netdev_bsd_get_mtu(netdev, &mtu)) { |
df1e5a3b PS |
630 | mtu = ETH_PAYLOAD_MAX; |
631 | } | |
632 | ||
2482b0b0 | 633 | /* Assume Ethernet port. No need to set packet_type. */ |
e14deea0 | 634 | packet = dp_packet_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu, |
91088554 | 635 | DP_NETDEV_HEADROOM); |
a0cbddc0 | 636 | retval = (rxq->pcap_handle |
cf62fa4c PS |
637 | ? netdev_rxq_bsd_recv_pcap(rxq, packet) |
638 | : netdev_rxq_bsd_recv_tap(rxq, packet)); | |
df1e5a3b PS |
639 | |
640 | if (retval) { | |
e14deea0 | 641 | dp_packet_delete(packet); |
df1e5a3b | 642 | } else { |
64839cf4 WT |
643 | batch->packets[0] = packet; |
644 | batch->count = 1; | |
df1e5a3b PS |
645 | } |
646 | return retval; | |
f6eb6b20 GL |
647 | } |
648 | ||
f6eb6b20 GL |
649 | /* |
650 | * Registers with the poll loop to wake up from the next call to poll_block() | |
a0cbddc0 | 651 | * when a packet is ready to be received with netdev_rxq_recv() on 'rxq'. |
f6eb6b20 GL |
652 | */ |
653 | static void | |
a0cbddc0 | 654 | netdev_bsd_rxq_wait(struct netdev_rxq *rxq_) |
f6eb6b20 | 655 | { |
a0cbddc0 | 656 | struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); |
f6eb6b20 | 657 | |
a0cbddc0 | 658 | poll_fd_wait(rxq->fd, POLLIN); |
f6eb6b20 GL |
659 | } |
660 | ||
a0cbddc0 | 661 | /* Discards all packets waiting to be received from 'rxq'. */ |
f6eb6b20 | 662 | static int |
a0cbddc0 | 663 | netdev_bsd_rxq_drain(struct netdev_rxq *rxq_) |
f6eb6b20 GL |
664 | { |
665 | struct ifreq ifr; | |
a0cbddc0 | 666 | struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_); |
f6eb6b20 | 667 | |
a0cbddc0 YT |
668 | strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rxq_get_netdev(rxq_))); |
669 | if (ioctl(rxq->fd, BIOCFLUSH, &ifr) == -1) { | |
f6eb6b20 | 670 | VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s", |
a0cbddc0 | 671 | netdev_rxq_get_name(rxq_), ovs_strerror(errno)); |
f6eb6b20 GL |
672 | return errno; |
673 | } | |
674 | return 0; | |
675 | } | |
676 | ||
677 | /* | |
678 | * Send a packet on the specified network device. The device could be either a | |
679 | * system or a tap device. | |
680 | */ | |
681 | static int | |
f00fa8cb | 682 | netdev_bsd_send(struct netdev *netdev_, int qid OVS_UNUSED, |
324c8374 IM |
683 | struct dp_packet_batch *batch, bool may_steal, |
684 | bool concurrent_txq OVS_UNUSED) | |
f6eb6b20 | 685 | { |
b5d57fc8 | 686 | struct netdev_bsd *dev = netdev_bsd_cast(netdev_); |
796223f5 | 687 | const char *name = netdev_get_name(netdev_); |
86383816 | 688 | int error; |
f4fd623c | 689 | int i; |
f6eb6b20 | 690 | |
86383816 | 691 | ovs_mutex_lock(&dev->mutex); |
796223f5 | 692 | if (dev->tap_fd < 0 && !dev->pcap) { |
86383816 BP |
693 | error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd); |
694 | } else { | |
695 | error = 0; | |
f6eb6b20 GL |
696 | } |
697 | ||
64839cf4 WT |
698 | for (i = 0; i < batch->count; i++) { |
699 | const void *data = dp_packet_data(batch->packets[i]); | |
700 | size_t size = dp_packet_size(batch->packets[i]); | |
f4fd623c | 701 | |
aaca4fe0 | 702 | /* Truncate the packet if it is configured. */ |
64839cf4 | 703 | size -= dp_packet_get_cutlen(batch->packets[i]); |
aaca4fe0 | 704 | |
f4fd623c DDP |
705 | while (!error) { |
706 | ssize_t retval; | |
707 | if (dev->tap_fd >= 0) { | |
708 | retval = write(dev->tap_fd, data, size); | |
86383816 | 709 | } else { |
f4fd623c DDP |
710 | retval = pcap_inject(dev->pcap, data, size); |
711 | } | |
712 | if (retval < 0) { | |
713 | if (errno == EINTR) { | |
714 | continue; | |
715 | } else { | |
716 | error = errno; | |
717 | if (error != EAGAIN) { | |
718 | VLOG_WARN_RL(&rl, "error sending Ethernet packet on" | |
719 | " %s: %s", name, ovs_strerror(error)); | |
720 | } | |
86383816 | 721 | } |
f4fd623c DDP |
722 | } else if (retval != size) { |
723 | VLOG_WARN_RL(&rl, "sent partial Ethernet packet " | |
724 | "(%"PRIuSIZE" bytes of " | |
725 | "%"PRIuSIZE") on %s", retval, size, name); | |
726 | error = EMSGSIZE; | |
727 | } else { | |
728 | break; | |
f6eb6b20 | 729 | } |
f6eb6b20 GL |
730 | } |
731 | } | |
86383816 BP |
732 | |
733 | ovs_mutex_unlock(&dev->mutex); | |
64839cf4 | 734 | dp_packet_delete_batch(batch, may_steal); |
40d26f04 | 735 | |
86383816 | 736 | return error; |
f6eb6b20 GL |
737 | } |
738 | ||
739 | /* | |
740 | * Registers with the poll loop to wake up from the next call to poll_block() | |
741 | * when the packet transmission queue has sufficient room to transmit a packet | |
742 | * with netdev_send(). | |
743 | */ | |
744 | static void | |
f00fa8cb | 745 | netdev_bsd_send_wait(struct netdev *netdev_, int qid OVS_UNUSED) |
f6eb6b20 | 746 | { |
b5d57fc8 | 747 | struct netdev_bsd *dev = netdev_bsd_cast(netdev_); |
f6eb6b20 | 748 | |
86383816 | 749 | ovs_mutex_lock(&dev->mutex); |
796223f5 | 750 | if (dev->tap_fd >= 0) { |
f6eb6b20 GL |
751 | /* TAP device always accepts packets. */ |
752 | poll_immediate_wake(); | |
796223f5 BP |
753 | } else if (dev->pcap) { |
754 | poll_fd_wait(dev->fd, POLLOUT); | |
755 | } else { | |
756 | /* We haven't even tried to send a packet yet. */ | |
757 | poll_immediate_wake(); | |
f6eb6b20 | 758 | } |
86383816 | 759 | ovs_mutex_unlock(&dev->mutex); |
f6eb6b20 GL |
760 | } |
761 | ||
762 | /* | |
763 | * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful, | |
764 | * otherwise a positive errno value. | |
765 | */ | |
766 | static int | |
767 | netdev_bsd_set_etheraddr(struct netdev *netdev_, | |
74ff3298 | 768 | const struct eth_addr mac) |
f6eb6b20 | 769 | { |
b5d57fc8 | 770 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
86383816 | 771 | int error = 0; |
f6eb6b20 | 772 | |
86383816 | 773 | ovs_mutex_lock(&netdev->mutex); |
b5d57fc8 BP |
774 | if (!(netdev->cache_valid & VALID_ETHERADDR) |
775 | || !eth_addr_equals(netdev->etheraddr, mac)) { | |
87ad1c07 YT |
776 | error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK, |
777 | ETH_ADDR_LEN, mac); | |
f6eb6b20 | 778 | if (!error) { |
b5d57fc8 | 779 | netdev->cache_valid |= VALID_ETHERADDR; |
74ff3298 | 780 | netdev->etheraddr = mac; |
3e912ffc | 781 | netdev_change_seq_changed(netdev_); |
f6eb6b20 | 782 | } |
f6eb6b20 | 783 | } |
86383816 BP |
784 | ovs_mutex_unlock(&netdev->mutex); |
785 | ||
f6eb6b20 GL |
786 | return error; |
787 | } | |
788 | ||
789 | /* | |
790 | * Returns a pointer to 'netdev''s MAC address. The caller must not modify or | |
791 | * free the returned buffer. | |
792 | */ | |
793 | static int | |
74ff3298 | 794 | netdev_bsd_get_etheraddr(const struct netdev *netdev_, struct eth_addr *mac) |
f6eb6b20 | 795 | { |
b5d57fc8 | 796 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
86383816 | 797 | int error = 0; |
f6eb6b20 | 798 | |
86383816 | 799 | ovs_mutex_lock(&netdev->mutex); |
b5d57fc8 | 800 | if (!(netdev->cache_valid & VALID_ETHERADDR)) { |
86383816 | 801 | error = get_etheraddr(netdev_get_kernel_name(netdev_), |
74ff3298 | 802 | &netdev->etheraddr); |
86383816 BP |
803 | if (!error) { |
804 | netdev->cache_valid |= VALID_ETHERADDR; | |
f6eb6b20 | 805 | } |
f6eb6b20 | 806 | } |
86383816 | 807 | if (!error) { |
74ff3298 | 808 | *mac = netdev->etheraddr; |
86383816 BP |
809 | } |
810 | ovs_mutex_unlock(&netdev->mutex); | |
f6eb6b20 | 811 | |
86383816 | 812 | return error; |
f6eb6b20 GL |
813 | } |
814 | ||
815 | /* | |
816 | * Returns the maximum size of transmitted (and received) packets on 'netdev', | |
817 | * in bytes, not including the hardware header; thus, this is typically 1500 | |
818 | * bytes for Ethernet devices. | |
819 | */ | |
820 | static int | |
821 | netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup) | |
822 | { | |
b5d57fc8 | 823 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
86383816 | 824 | int error = 0; |
f6eb6b20 | 825 | |
86383816 | 826 | ovs_mutex_lock(&netdev->mutex); |
b5d57fc8 | 827 | if (!(netdev->cache_valid & VALID_MTU)) { |
f6eb6b20 | 828 | struct ifreq ifr; |
f6eb6b20 | 829 | |
259e0b1a | 830 | error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr, |
87ad1c07 | 831 | SIOCGIFMTU, "SIOCGIFMTU"); |
86383816 BP |
832 | if (!error) { |
833 | netdev->mtu = ifr.ifr_mtu; | |
834 | netdev->cache_valid |= VALID_MTU; | |
f6eb6b20 | 835 | } |
f6eb6b20 | 836 | } |
86383816 BP |
837 | if (!error) { |
838 | *mtup = netdev->mtu; | |
839 | } | |
840 | ovs_mutex_unlock(&netdev->mutex); | |
f6eb6b20 | 841 | |
e7d63486 | 842 | return error; |
f6eb6b20 GL |
843 | } |
844 | ||
845 | static int | |
86383816 | 846 | netdev_bsd_get_ifindex(const struct netdev *netdev_) |
f6eb6b20 | 847 | { |
86383816 | 848 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
f6eb6b20 GL |
849 | int ifindex, error; |
850 | ||
86383816 BP |
851 | ovs_mutex_lock(&netdev->mutex); |
852 | error = get_ifindex(netdev_, &ifindex); | |
853 | ovs_mutex_unlock(&netdev->mutex); | |
854 | ||
f6eb6b20 GL |
855 | return error ? -error : ifindex; |
856 | } | |
857 | ||
858 | static int | |
859 | netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier) | |
860 | { | |
b5d57fc8 | 861 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
86383816 | 862 | int error = 0; |
f6eb6b20 | 863 | |
86383816 | 864 | ovs_mutex_lock(&netdev->mutex); |
b5d57fc8 | 865 | if (!(netdev->cache_valid & VALID_CARRIER)) { |
f6eb6b20 GL |
866 | struct ifmediareq ifmr; |
867 | ||
868 | memset(&ifmr, 0, sizeof(ifmr)); | |
8742957c BP |
869 | ovs_strlcpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_), |
870 | sizeof ifmr.ifm_name); | |
f6eb6b20 | 871 | |
259e0b1a | 872 | error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr); |
86383816 BP |
873 | if (!error) { |
874 | netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE; | |
875 | netdev->cache_valid |= VALID_CARRIER; | |
876 | ||
877 | /* If the interface doesn't report whether the media is active, | |
878 | * just assume it is active. */ | |
879 | if ((ifmr.ifm_status & IFM_AVALID) == 0) { | |
880 | netdev->carrier = true; | |
881 | } | |
882 | } else { | |
f6eb6b20 | 883 | VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s", |
259e0b1a | 884 | netdev_get_name(netdev_), ovs_strerror(error)); |
f6eb6b20 GL |
885 | } |
886 | } | |
86383816 BP |
887 | if (!error) { |
888 | *carrier = netdev->carrier; | |
889 | } | |
890 | ovs_mutex_unlock(&netdev->mutex); | |
f6eb6b20 | 891 | |
86383816 | 892 | return error; |
f6eb6b20 GL |
893 | } |
894 | ||
3aacfbb3 | 895 | static void |
7f542c09 | 896 | convert_stats_system(struct netdev_stats *stats, const struct if_data *ifd) |
3aacfbb3 YT |
897 | { |
898 | /* | |
899 | * note: UINT64_MAX means unsupported | |
900 | */ | |
901 | stats->rx_packets = ifd->ifi_ipackets; | |
902 | stats->tx_packets = ifd->ifi_opackets; | |
903 | stats->rx_bytes = ifd->ifi_obytes; | |
904 | stats->tx_bytes = ifd->ifi_ibytes; | |
905 | stats->rx_errors = ifd->ifi_ierrors; | |
906 | stats->tx_errors = ifd->ifi_oerrors; | |
907 | stats->rx_dropped = ifd->ifi_iqdrops; | |
908 | stats->tx_dropped = UINT64_MAX; | |
909 | stats->multicast = ifd->ifi_imcasts; | |
910 | stats->collisions = ifd->ifi_collisions; | |
911 | stats->rx_length_errors = UINT64_MAX; | |
912 | stats->rx_over_errors = UINT64_MAX; | |
913 | stats->rx_crc_errors = UINT64_MAX; | |
914 | stats->rx_frame_errors = UINT64_MAX; | |
915 | stats->rx_fifo_errors = UINT64_MAX; | |
916 | stats->rx_missed_errors = UINT64_MAX; | |
917 | stats->tx_aborted_errors = UINT64_MAX; | |
918 | stats->tx_carrier_errors = UINT64_MAX; | |
919 | stats->tx_fifo_errors = UINT64_MAX; | |
920 | stats->tx_heartbeat_errors = UINT64_MAX; | |
921 | stats->tx_window_errors = UINT64_MAX; | |
922 | } | |
923 | ||
7f542c09 YT |
924 | static void |
925 | convert_stats_tap(struct netdev_stats *stats, const struct if_data *ifd) | |
926 | { | |
927 | /* | |
a0cbddc0 | 928 | * Similar to convert_stats_system but swapping rxq and tx |
7f542c09 YT |
929 | * because 'ifd' is stats for the network interface side of the |
930 | * tap device and what the caller wants is one for the character | |
931 | * device side. | |
932 | * | |
933 | * note: UINT64_MAX means unsupported | |
934 | */ | |
935 | stats->rx_packets = ifd->ifi_opackets; | |
936 | stats->tx_packets = ifd->ifi_ipackets; | |
937 | stats->rx_bytes = ifd->ifi_ibytes; | |
938 | stats->tx_bytes = ifd->ifi_obytes; | |
939 | stats->rx_errors = ifd->ifi_oerrors; | |
940 | stats->tx_errors = ifd->ifi_ierrors; | |
941 | stats->rx_dropped = UINT64_MAX; | |
942 | stats->tx_dropped = ifd->ifi_iqdrops; | |
943 | stats->multicast = ifd->ifi_omcasts; | |
944 | stats->collisions = UINT64_MAX; | |
945 | stats->rx_length_errors = UINT64_MAX; | |
946 | stats->rx_over_errors = UINT64_MAX; | |
947 | stats->rx_crc_errors = UINT64_MAX; | |
948 | stats->rx_frame_errors = UINT64_MAX; | |
949 | stats->rx_fifo_errors = UINT64_MAX; | |
950 | stats->rx_missed_errors = UINT64_MAX; | |
951 | stats->tx_aborted_errors = UINT64_MAX; | |
952 | stats->tx_carrier_errors = UINT64_MAX; | |
953 | stats->tx_fifo_errors = UINT64_MAX; | |
954 | stats->tx_heartbeat_errors = UINT64_MAX; | |
955 | stats->tx_window_errors = UINT64_MAX; | |
956 | } | |
957 | ||
958 | static void | |
959 | convert_stats(const struct netdev *netdev, struct netdev_stats *stats, | |
960 | const struct if_data *ifd) | |
961 | { | |
962 | if (netdev_bsd_cast(netdev)->tap_fd == -1) { | |
963 | convert_stats_system(stats, ifd); | |
964 | } else { | |
965 | convert_stats_tap(stats, ifd); | |
966 | } | |
967 | } | |
968 | ||
f6eb6b20 GL |
969 | /* Retrieves current device stats for 'netdev'. */ |
970 | static int | |
bf8377a7 | 971 | netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats) |
f6eb6b20 | 972 | { |
666afb55 | 973 | #if defined(__FreeBSD__) |
f6eb6b20 GL |
974 | int if_count, i; |
975 | int mib[6]; | |
976 | size_t len; | |
977 | struct ifmibdata ifmd; | |
978 | ||
979 | ||
980 | mib[0] = CTL_NET; | |
981 | mib[1] = PF_LINK; | |
982 | mib[2] = NETLINK_GENERIC; | |
983 | mib[3] = IFMIB_SYSTEM; | |
984 | mib[4] = IFMIB_IFCOUNT; | |
985 | ||
986 | len = sizeof(if_count); | |
987 | ||
988 | if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) { | |
989 | VLOG_DBG_RL(&rl, "%s: sysctl failed: %s", | |
10a89ef0 | 990 | netdev_get_name(netdev_), ovs_strerror(errno)); |
f6eb6b20 GL |
991 | return errno; |
992 | } | |
993 | ||
994 | mib[5] = IFDATA_GENERAL; | |
995 | mib[3] = IFMIB_IFDATA; | |
996 | len = sizeof(ifmd); | |
997 | for (i = 1; i <= if_count; i++) { | |
998 | mib[4] = i; //row | |
999 | if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) { | |
1000 | VLOG_DBG_RL(&rl, "%s: sysctl failed: %s", | |
10a89ef0 | 1001 | netdev_get_name(netdev_), ovs_strerror(errno)); |
f6eb6b20 GL |
1002 | return errno; |
1003 | } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) { | |
a48fd8eb | 1004 | convert_stats(netdev_, stats, &ifmd.ifmd_data); |
f6eb6b20 GL |
1005 | break; |
1006 | } | |
1007 | } | |
1008 | ||
1009 | return 0; | |
bf8377a7 YT |
1010 | #elif defined(__NetBSD__) |
1011 | struct ifdatareq ifdr; | |
996e5ae9 | 1012 | int error; |
bf8377a7 YT |
1013 | |
1014 | memset(&ifdr, 0, sizeof(ifdr)); | |
8742957c BP |
1015 | ovs_strlcpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_), |
1016 | sizeof(ifdr.ifdr_name)); | |
996e5ae9 BP |
1017 | error = af_link_ioctl(SIOCGIFDATA, &ifdr); |
1018 | if (!error) { | |
7f542c09 | 1019 | convert_stats(netdev_, stats, &ifdr.ifdr_data); |
bf8377a7 | 1020 | } |
996e5ae9 | 1021 | return error; |
bf8377a7 YT |
1022 | #else |
1023 | #error not implemented | |
666afb55 | 1024 | #endif |
f6eb6b20 GL |
1025 | } |
1026 | ||
1027 | static uint32_t | |
1028 | netdev_bsd_parse_media(int media) | |
1029 | { | |
1030 | uint32_t supported = 0; | |
1031 | bool half_duplex = media & IFM_HDX ? true : false; | |
1032 | ||
1033 | switch (IFM_SUBTYPE(media)) { | |
1034 | case IFM_10_2: | |
1035 | case IFM_10_5: | |
1036 | case IFM_10_STP: | |
1037 | case IFM_10_T: | |
1038 | supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD; | |
1039 | supported |= NETDEV_F_COPPER; | |
1040 | break; | |
1041 | ||
1042 | case IFM_10_FL: | |
1043 | supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD; | |
1044 | supported |= NETDEV_F_FIBER; | |
1045 | break; | |
1046 | ||
1047 | case IFM_100_T2: | |
1048 | case IFM_100_T4: | |
1049 | case IFM_100_TX: | |
1050 | case IFM_100_VG: | |
1051 | supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD; | |
1052 | supported |= NETDEV_F_COPPER; | |
1053 | break; | |
1054 | ||
1055 | case IFM_100_FX: | |
1056 | supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD; | |
1057 | supported |= NETDEV_F_FIBER; | |
1058 | break; | |
1059 | ||
1060 | case IFM_1000_CX: | |
1061 | case IFM_1000_T: | |
1062 | supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD; | |
1063 | supported |= NETDEV_F_COPPER; | |
1064 | break; | |
1065 | ||
1066 | case IFM_1000_LX: | |
1067 | case IFM_1000_SX: | |
1068 | supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD; | |
1069 | supported |= NETDEV_F_FIBER; | |
1070 | break; | |
1071 | ||
1072 | case IFM_10G_CX4: | |
1073 | supported |= NETDEV_F_10GB_FD; | |
1074 | supported |= NETDEV_F_COPPER; | |
1075 | break; | |
1076 | ||
1077 | case IFM_10G_LR: | |
1078 | case IFM_10G_SR: | |
1079 | supported |= NETDEV_F_10GB_FD; | |
1080 | supported |= NETDEV_F_FIBER; | |
1081 | break; | |
1082 | ||
1083 | default: | |
1084 | return 0; | |
1085 | } | |
1086 | ||
1087 | if (IFM_SUBTYPE(media) == IFM_AUTO) { | |
1088 | supported |= NETDEV_F_AUTONEG; | |
1089 | } | |
1090 | /* | |
1091 | if (media & IFM_ETH_FMASK) { | |
1092 | supported |= NETDEV_F_PAUSE; | |
1093 | } | |
1094 | */ | |
1095 | ||
1096 | return supported; | |
1097 | } | |
1098 | ||
1099 | /* | |
1100 | * Stores the features supported by 'netdev' into each of '*current', | |
1101 | * '*advertised', '*supported', and '*peer' that are non-null. Each value is a | |
1102 | * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if | |
1103 | * successful, otherwise a positive errno value. On failure, all of the | |
1104 | * passed-in values are set to 0. | |
1105 | */ | |
1106 | static int | |
1107 | netdev_bsd_get_features(const struct netdev *netdev, | |
bb79b46b EM |
1108 | enum netdev_features *current, uint32_t *advertised, |
1109 | enum netdev_features *supported, uint32_t *peer) | |
f6eb6b20 GL |
1110 | { |
1111 | struct ifmediareq ifmr; | |
1112 | int *media_list; | |
1113 | int i; | |
1114 | int error; | |
1115 | ||
1116 | ||
1117 | /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */ | |
1118 | ||
1119 | memset(&ifmr, 0, sizeof(ifmr)); | |
8742957c | 1120 | ovs_strlcpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name); |
f6eb6b20 GL |
1121 | |
1122 | /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the | |
1123 | * number of supported modes, and a second with a buffer to retrieve | |
1124 | * them. */ | |
259e0b1a BP |
1125 | error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr); |
1126 | if (error) { | |
f6eb6b20 | 1127 | VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s", |
259e0b1a BP |
1128 | netdev_get_name(netdev), ovs_strerror(error)); |
1129 | return error; | |
f6eb6b20 GL |
1130 | } |
1131 | ||
1132 | media_list = xcalloc(ifmr.ifm_count, sizeof(int)); | |
1133 | ifmr.ifm_ulist = media_list; | |
1134 | ||
bf1aa7a0 | 1135 | if (IFM_TYPE(ifmr.ifm_current) != IFM_ETHER) { |
f6eb6b20 GL |
1136 | VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet", |
1137 | netdev_get_name(netdev)); | |
1138 | error = EINVAL; | |
1139 | goto cleanup; | |
1140 | } | |
1141 | ||
259e0b1a BP |
1142 | error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr); |
1143 | if (error) { | |
f6eb6b20 | 1144 | VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s", |
259e0b1a | 1145 | netdev_get_name(netdev), ovs_strerror(error)); |
f6eb6b20 GL |
1146 | goto cleanup; |
1147 | } | |
1148 | ||
1149 | /* Current settings. */ | |
1150 | *current = netdev_bsd_parse_media(ifmr.ifm_active); | |
1151 | ||
1152 | /* Advertised features. */ | |
1153 | *advertised = netdev_bsd_parse_media(ifmr.ifm_current); | |
1154 | ||
1155 | /* Supported features. */ | |
1156 | *supported = 0; | |
1157 | for (i = 0; i < ifmr.ifm_count; i++) { | |
1158 | *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]); | |
1159 | } | |
1160 | ||
1161 | /* Peer advertisements. */ | |
1162 | *peer = 0; /* XXX */ | |
1163 | ||
1164 | error = 0; | |
1165 | cleanup: | |
1166 | free(media_list); | |
1167 | return error; | |
1168 | } | |
1169 | ||
f6eb6b20 GL |
1170 | /* |
1171 | * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If | |
1172 | * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a | |
1173 | * positive errno value. | |
1174 | */ | |
1175 | static int | |
1176 | netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr, | |
bb79b46b | 1177 | struct in_addr mask) |
f6eb6b20 | 1178 | { |
b5d57fc8 | 1179 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
f6eb6b20 GL |
1180 | int error; |
1181 | ||
86383816 | 1182 | ovs_mutex_lock(&netdev->mutex); |
f6eb6b20 GL |
1183 | error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr); |
1184 | if (!error) { | |
f6eb6b20 GL |
1185 | if (addr.s_addr != INADDR_ANY) { |
1186 | error = do_set_addr(netdev_, SIOCSIFNETMASK, | |
1187 | "SIOCSIFNETMASK", mask); | |
1188 | } | |
3e912ffc | 1189 | netdev_change_seq_changed(netdev_); |
f6eb6b20 | 1190 | } |
86383816 BP |
1191 | ovs_mutex_unlock(&netdev->mutex); |
1192 | ||
f6eb6b20 GL |
1193 | return error; |
1194 | } | |
1195 | ||
1196 | static int | |
a8704b50 PS |
1197 | netdev_bsd_get_addr_list(const struct netdev *netdev_, |
1198 | struct in6_addr **addr, struct in6_addr **mask, int *n_cnt) | |
f6eb6b20 | 1199 | { |
b5d57fc8 | 1200 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
a8704b50 | 1201 | int error; |
f6eb6b20 | 1202 | |
6b6e1329 | 1203 | if (!(netdev->cache_valid & VALID_IN)) { |
a8704b50 | 1204 | netdev_get_addrs_list_flush(); |
f6eb6b20 | 1205 | } |
a8704b50 PS |
1206 | error = netdev_get_addrs(netdev_get_name(netdev_), addr, mask, n_cnt); |
1207 | if (!error) { | |
6b6e1329 | 1208 | netdev->cache_valid |= VALID_IN; |
a8704b50 PS |
1209 | } |
1210 | return error; | |
f6eb6b20 GL |
1211 | } |
1212 | ||
d00409c5 | 1213 | #if defined(__NetBSD__) |
2f980d74 BP |
1214 | static char * |
1215 | netdev_bsd_kernel_name_to_ovs_name(const char *kernel_name) | |
d00409c5 | 1216 | { |
2f980d74 | 1217 | char *ovs_name = NULL; |
d00409c5 YT |
1218 | struct shash device_shash; |
1219 | struct shash_node *node; | |
1220 | ||
1221 | shash_init(&device_shash); | |
1222 | netdev_get_devices(&netdev_tap_class, &device_shash); | |
1223 | SHASH_FOR_EACH(node, &device_shash) { | |
9da7ae12 BP |
1224 | struct netdev *netdev = node->data; |
1225 | struct netdev_bsd * const dev = netdev_bsd_cast(netdev); | |
d00409c5 YT |
1226 | |
1227 | if (!strcmp(dev->kernel_name, kernel_name)) { | |
2f980d74 BP |
1228 | free(ovs_name); |
1229 | ovs_name = xstrdup(netdev_get_name(&dev->up)); | |
d00409c5 | 1230 | } |
2f980d74 | 1231 | netdev_close(netdev); |
d00409c5 YT |
1232 | } |
1233 | shash_destroy(&device_shash); | |
d00409c5 | 1234 | |
2f980d74 | 1235 | return ovs_name ? ovs_name : xstrdup(kernel_name); |
d00409c5 YT |
1236 | } |
1237 | #endif | |
1238 | ||
1239 | static int | |
ee9e7b17 EM |
1240 | netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED, |
1241 | struct in_addr *next_hop OVS_UNUSED, | |
1242 | char **netdev_name OVS_UNUSED) | |
d00409c5 YT |
1243 | { |
1244 | #if defined(__NetBSD__) | |
1245 | static int seq = 0; | |
1246 | struct sockaddr_in sin; | |
1247 | struct sockaddr_dl sdl; | |
1248 | int s; | |
1249 | int i; | |
1250 | struct { | |
1251 | struct rt_msghdr h; | |
1252 | char space[512]; | |
1253 | } buf; | |
1254 | struct rt_msghdr *rtm = &buf.h; | |
1255 | const pid_t pid = getpid(); | |
1256 | char *cp; | |
1257 | ssize_t ssz; | |
1258 | bool gateway = false; | |
1259 | char *ifname = NULL; | |
1260 | int saved_errno; | |
1261 | ||
1262 | memset(next_hop, 0, sizeof(*next_hop)); | |
1263 | *netdev_name = NULL; | |
1264 | ||
1265 | memset(&sin, 0, sizeof(sin)); | |
1266 | sin.sin_len = sizeof(sin); | |
1267 | sin.sin_family = AF_INET; | |
1268 | sin.sin_port = 0; | |
1269 | sin.sin_addr = *host; | |
1270 | ||
1271 | memset(&sdl, 0, sizeof(sdl)); | |
1272 | sdl.sdl_len = sizeof(sdl); | |
1273 | sdl.sdl_family = AF_LINK; | |
1274 | ||
1275 | s = socket(PF_ROUTE, SOCK_RAW, 0); | |
1276 | memset(&buf, 0, sizeof(buf)); | |
1277 | rtm->rtm_flags = RTF_HOST|RTF_UP; | |
1278 | rtm->rtm_version = RTM_VERSION; | |
1279 | rtm->rtm_addrs = RTA_DST|RTA_IFP; | |
1280 | cp = (void *)&buf.space; | |
1281 | memcpy(cp, &sin, sizeof(sin)); | |
1282 | RT_ADVANCE(cp, (struct sockaddr *)(void *)&sin); | |
1283 | memcpy(cp, &sdl, sizeof(sdl)); | |
1284 | RT_ADVANCE(cp, (struct sockaddr *)(void *)&sdl); | |
1285 | rtm->rtm_msglen = cp - (char *)(void *)rtm; | |
1286 | rtm->rtm_seq = ++seq; | |
1287 | rtm->rtm_type = RTM_GET; | |
1288 | rtm->rtm_pid = pid; | |
1289 | write(s, rtm, rtm->rtm_msglen); | |
1290 | memset(&buf, 0, sizeof(buf)); | |
1291 | do { | |
1292 | ssz = read(s, &buf, sizeof(buf)); | |
1293 | } while (ssz > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); | |
1294 | saved_errno = errno; | |
1295 | close(s); | |
1296 | if (ssz <= 0) { | |
1297 | if (ssz < 0) { | |
1298 | return saved_errno; | |
1299 | } | |
1300 | return EPIPE; /* XXX */ | |
1301 | } | |
1302 | cp = (void *)&buf.space; | |
1303 | for (i = 1; i; i <<= 1) { | |
1304 | if ((rtm->rtm_addrs & i) != 0) { | |
1305 | const struct sockaddr *sa = (const void *)cp; | |
1306 | ||
1307 | if ((i == RTA_GATEWAY) && sa->sa_family == AF_INET) { | |
1308 | const struct sockaddr_in * const sin = | |
e4b997e3 | 1309 | ALIGNED_CAST(const struct sockaddr_in *, sa); |
d00409c5 YT |
1310 | |
1311 | *next_hop = sin->sin_addr; | |
1312 | gateway = true; | |
1313 | } | |
1314 | if ((i == RTA_IFP) && sa->sa_family == AF_LINK) { | |
1315 | const struct sockaddr_dl * const sdl = | |
e4b997e3 | 1316 | ALIGNED_CAST(const struct sockaddr_dl *, sa); |
35bacb7f | 1317 | char *kernel_name; |
d00409c5 | 1318 | |
35bacb7f | 1319 | kernel_name = xmemdup0(sdl->sdl_data, sdl->sdl_nlen); |
2f980d74 | 1320 | ifname = netdev_bsd_kernel_name_to_ovs_name(kernel_name); |
d00409c5 YT |
1321 | free(kernel_name); |
1322 | } | |
1323 | RT_ADVANCE(cp, sa); | |
1324 | } | |
1325 | } | |
1326 | if (ifname == NULL) { | |
1327 | return ENXIO; | |
1328 | } | |
1329 | if (!gateway) { | |
1330 | *next_hop = *host; | |
1331 | } | |
1332 | *netdev_name = ifname; | |
1333 | VLOG_DBG("host " IP_FMT " next-hop " IP_FMT " if %s", | |
1334 | IP_ARGS(host->s_addr), IP_ARGS(next_hop->s_addr), *netdev_name); | |
1335 | return 0; | |
1336 | #else | |
1337 | return EOPNOTSUPP; | |
1338 | #endif | |
1339 | } | |
1340 | ||
7b6d4b2a YT |
1341 | static int |
1342 | netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED, | |
1343 | ovs_be32 ip OVS_UNUSED, | |
74ff3298 | 1344 | struct eth_addr *mac OVS_UNUSED) |
7b6d4b2a YT |
1345 | { |
1346 | #if defined(__NetBSD__) | |
1347 | const struct rt_msghdr *rtm; | |
1348 | size_t needed; | |
1349 | char *buf; | |
1350 | const char *cp; | |
1351 | const char *ep; | |
1352 | int mib[6]; | |
1353 | int error; | |
1354 | ||
1355 | buf = NULL; | |
1356 | mib[0] = CTL_NET; | |
1357 | mib[1] = PF_ROUTE; | |
1358 | mib[2] = 0; | |
1359 | mib[3] = AF_INET; | |
1360 | mib[4] = NET_RT_FLAGS; | |
c8c300b8 | 1361 | #ifdef RTF_LLINFO |
7b6d4b2a | 1362 | mib[5] = RTF_LLINFO; |
c8c300b8 HK |
1363 | #else |
1364 | mib[5] = 0; | |
1365 | #endif | |
7b6d4b2a YT |
1366 | if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) { |
1367 | error = errno; | |
1368 | goto error; | |
1369 | } | |
1370 | buf = xmalloc(needed); | |
1371 | if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) { | |
1372 | error = errno; | |
1373 | goto error; | |
1374 | } | |
1375 | ep = buf + needed; | |
1376 | for (cp = buf; cp < ep; cp += rtm->rtm_msglen) { | |
1377 | const struct sockaddr_inarp *sina; | |
1378 | const struct sockaddr_dl *sdl; | |
1379 | ||
1380 | rtm = (const void *)cp; | |
1381 | sina = (const void *)(rtm + 1); | |
1382 | if (ip != sina->sin_addr.s_addr) { | |
1383 | continue; | |
1384 | } | |
1385 | sdl = (const void *) | |
1386 | ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len)); | |
1387 | if (sdl->sdl_alen == ETH_ADDR_LEN) { | |
1388 | memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN); | |
1389 | error = 0; | |
1390 | goto error; | |
1391 | } | |
1392 | } | |
1393 | error = ENXIO; | |
1394 | error: | |
1395 | free(buf); | |
1396 | return error; | |
1397 | #else | |
1398 | return EOPNOTSUPP; | |
1399 | #endif | |
1400 | } | |
1401 | ||
f6eb6b20 GL |
1402 | static void |
1403 | make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr) | |
1404 | { | |
1405 | struct sockaddr_in sin; | |
1406 | memset(&sin, 0, sizeof sin); | |
1407 | sin.sin_family = AF_INET; | |
1408 | sin.sin_addr = addr; | |
1409 | sin.sin_port = 0; | |
1410 | ||
1411 | memset(sa, 0, sizeof *sa); | |
1412 | memcpy(sa, &sin, sizeof sin); | |
1413 | } | |
1414 | ||
1415 | static int | |
1416 | do_set_addr(struct netdev *netdev, | |
b72d8dc2 YT |
1417 | unsigned long ioctl_nr, const char *ioctl_name, |
1418 | struct in_addr addr) | |
f6eb6b20 GL |
1419 | { |
1420 | struct ifreq ifr; | |
1421 | make_in4_sockaddr(&ifr.ifr_addr, addr); | |
259e0b1a | 1422 | return af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, ioctl_nr, |
87ad1c07 | 1423 | ioctl_name); |
f6eb6b20 GL |
1424 | } |
1425 | ||
1426 | static int | |
1427 | nd_to_iff_flags(enum netdev_flags nd) | |
1428 | { | |
1429 | int iff = 0; | |
1430 | if (nd & NETDEV_UP) { | |
1431 | iff |= IFF_UP; | |
1432 | } | |
1433 | if (nd & NETDEV_PROMISC) { | |
1434 | iff |= IFF_PROMISC; | |
666afb55 | 1435 | #if defined(IFF_PPROMISC) |
f6eb6b20 | 1436 | iff |= IFF_PPROMISC; |
666afb55 | 1437 | #endif |
f6eb6b20 | 1438 | } |
7ba19d41 AC |
1439 | if (nd & NETDEV_LOOPBACK) { |
1440 | iff |= IFF_LOOPBACK; | |
1441 | } | |
f6eb6b20 GL |
1442 | return iff; |
1443 | } | |
1444 | ||
1445 | static int | |
1446 | iff_to_nd_flags(int iff) | |
1447 | { | |
1448 | enum netdev_flags nd = 0; | |
1449 | if (iff & IFF_UP) { | |
1450 | nd |= NETDEV_UP; | |
1451 | } | |
1452 | if (iff & IFF_PROMISC) { | |
1453 | nd |= NETDEV_PROMISC; | |
1454 | } | |
7ba19d41 AC |
1455 | if (iff & IFF_LOOPBACK) { |
1456 | nd |= NETDEV_LOOPBACK; | |
1457 | } | |
f6eb6b20 GL |
1458 | return nd; |
1459 | } | |
1460 | ||
1461 | static int | |
b5d57fc8 | 1462 | netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off, |
bb79b46b | 1463 | enum netdev_flags on, enum netdev_flags *old_flagsp) |
f6eb6b20 GL |
1464 | { |
1465 | int old_flags, new_flags; | |
1466 | int error; | |
1467 | ||
b5d57fc8 | 1468 | error = get_flags(netdev_, &old_flags); |
f6eb6b20 GL |
1469 | if (!error) { |
1470 | *old_flagsp = iff_to_nd_flags(old_flags); | |
1471 | new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on); | |
1472 | if (new_flags != old_flags) { | |
87ad1c07 | 1473 | error = set_flags(netdev_get_kernel_name(netdev_), new_flags); |
3e912ffc | 1474 | netdev_change_seq_changed(netdev_); |
f6eb6b20 GL |
1475 | } |
1476 | } | |
1477 | return error; | |
1478 | } | |
1479 | ||
b681f93f DDP |
1480 | /* Linux has also different GET_STATS, SET_STATS, |
1481 | * GET_STATUS) | |
1482 | */ | |
1483 | #define NETDEV_BSD_CLASS(NAME, CONSTRUCT, \ | |
1484 | GET_FEATURES) \ | |
1485 | { \ | |
1486 | NAME, \ | |
118c77b1 | 1487 | false, /* is_pmd */ \ |
b681f93f DDP |
1488 | \ |
1489 | NULL, /* init */ \ | |
1490 | netdev_bsd_run, \ | |
1491 | netdev_bsd_wait, \ | |
1492 | netdev_bsd_alloc, \ | |
1493 | CONSTRUCT, \ | |
1494 | netdev_bsd_destruct, \ | |
1495 | netdev_bsd_dealloc, \ | |
1496 | NULL, /* get_config */ \ | |
1497 | NULL, /* set_config */ \ | |
1498 | NULL, /* get_tunnel_config */ \ | |
a36de779 PS |
1499 | NULL, /* build header */ \ |
1500 | NULL, /* push header */ \ | |
1501 | NULL, /* pop header */ \ | |
7dec44fe | 1502 | NULL, /* get_numa_id */ \ |
050c60bf | 1503 | NULL, /* set_tx_multiq */ \ |
b681f93f DDP |
1504 | \ |
1505 | netdev_bsd_send, \ | |
1506 | netdev_bsd_send_wait, \ | |
1507 | \ | |
1508 | netdev_bsd_set_etheraddr, \ | |
1509 | netdev_bsd_get_etheraddr, \ | |
1510 | netdev_bsd_get_mtu, \ | |
1511 | NULL, /* set_mtu */ \ | |
1512 | netdev_bsd_get_ifindex, \ | |
1513 | netdev_bsd_get_carrier, \ | |
1514 | NULL, /* get_carrier_resets */ \ | |
1515 | NULL, /* set_miimon_interval */ \ | |
1516 | netdev_bsd_get_stats, \ | |
b681f93f DDP |
1517 | \ |
1518 | GET_FEATURES, \ | |
1519 | NULL, /* set_advertisement */ \ | |
875ab130 | 1520 | NULL, /* get_pt_mode */ \ |
b681f93f DDP |
1521 | NULL, /* set_policing */ \ |
1522 | NULL, /* get_qos_type */ \ | |
1523 | NULL, /* get_qos_capabilities */ \ | |
1524 | NULL, /* get_qos */ \ | |
1525 | NULL, /* set_qos */ \ | |
1526 | NULL, /* get_queue */ \ | |
1527 | NULL, /* set_queue */ \ | |
1528 | NULL, /* delete_queue */ \ | |
1529 | NULL, /* get_queue_stats */ \ | |
1530 | NULL, /* queue_dump_start */ \ | |
1531 | NULL, /* queue_dump_next */ \ | |
1532 | NULL, /* queue_dump_done */ \ | |
1533 | NULL, /* dump_queue_stats */ \ | |
1534 | \ | |
b681f93f | 1535 | netdev_bsd_set_in4, \ |
a8704b50 | 1536 | netdev_bsd_get_addr_list, \ |
b681f93f DDP |
1537 | NULL, /* add_router */ \ |
1538 | netdev_bsd_get_next_hop, \ | |
1539 | NULL, /* get_status */ \ | |
1540 | netdev_bsd_arp_lookup, /* arp_lookup */ \ | |
1541 | \ | |
1542 | netdev_bsd_update_flags, \ | |
790fb3b7 | 1543 | NULL, /* reconfigure */ \ |
b681f93f | 1544 | \ |
a0cbddc0 YT |
1545 | netdev_bsd_rxq_alloc, \ |
1546 | netdev_bsd_rxq_construct, \ | |
1547 | netdev_bsd_rxq_destruct, \ | |
1548 | netdev_bsd_rxq_dealloc, \ | |
1549 | netdev_bsd_rxq_recv, \ | |
1550 | netdev_bsd_rxq_wait, \ | |
1551 | netdev_bsd_rxq_drain, \ | |
18ebd48c PB |
1552 | \ |
1553 | NO_OFFLOAD_API \ | |
b681f93f | 1554 | } |
f6eb6b20 | 1555 | |
b681f93f DDP |
1556 | const struct netdev_class netdev_bsd_class = |
1557 | NETDEV_BSD_CLASS( | |
1558 | "system", | |
1559 | netdev_bsd_construct_system, | |
1560 | netdev_bsd_get_features); | |
1561 | ||
1562 | const struct netdev_class netdev_tap_class = | |
1563 | NETDEV_BSD_CLASS( | |
1564 | "tap", | |
1565 | netdev_bsd_construct_tap, | |
1566 | netdev_bsd_get_features); | |
f6eb6b20 GL |
1567 | \f |
1568 | ||
1569 | static void | |
1570 | destroy_tap(int fd, const char *name) | |
1571 | { | |
1572 | struct ifreq ifr; | |
1573 | ||
1574 | close(fd); | |
1575 | strcpy(ifr.ifr_name, name); | |
1576 | /* XXX What to do if this call fails? */ | |
259e0b1a | 1577 | af_inet_ioctl(SIOCIFDESTROY, &ifr); |
f6eb6b20 GL |
1578 | } |
1579 | ||
1580 | static int | |
b5d57fc8 | 1581 | get_flags(const struct netdev *netdev, int *flags) |
f6eb6b20 GL |
1582 | { |
1583 | struct ifreq ifr; | |
1584 | int error; | |
1585 | ||
259e0b1a | 1586 | error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev), &ifr, |
b5d57fc8 | 1587 | SIOCGIFFLAGS, "SIOCGIFFLAGS"); |
f6eb6b20 | 1588 | |
666afb55 | 1589 | *flags = ifr_get_flags(&ifr); |
f6eb6b20 GL |
1590 | |
1591 | return error; | |
1592 | } | |
1593 | ||
1594 | static int | |
4b609110 | 1595 | set_flags(const char *name, int flags) |
f6eb6b20 GL |
1596 | { |
1597 | struct ifreq ifr; | |
1598 | ||
666afb55 | 1599 | ifr_set_flags(&ifr, flags); |
f6eb6b20 | 1600 | |
259e0b1a | 1601 | return af_inet_ifreq_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS"); |
f6eb6b20 GL |
1602 | } |
1603 | ||
1604 | static int | |
1605 | get_ifindex(const struct netdev *netdev_, int *ifindexp) | |
1606 | { | |
b5d57fc8 | 1607 | struct netdev_bsd *netdev = netdev_bsd_cast(netdev_); |
f6eb6b20 | 1608 | *ifindexp = 0; |
b5d57fc8 | 1609 | if (!(netdev->cache_valid & VALID_IFINDEX)) { |
f6eb6b20 GL |
1610 | int ifindex = if_nametoindex(netdev_get_name(netdev_)); |
1611 | if (ifindex <= 0) { | |
1612 | return errno; | |
1613 | } | |
b5d57fc8 BP |
1614 | netdev->cache_valid |= VALID_IFINDEX; |
1615 | netdev->ifindex = ifindex; | |
f6eb6b20 | 1616 | } |
b5d57fc8 | 1617 | *ifindexp = netdev->ifindex; |
f6eb6b20 GL |
1618 | return 0; |
1619 | } | |
1620 | ||
1621 | static int | |
74ff3298 | 1622 | get_etheraddr(const char *netdev_name, struct eth_addr *ea) |
f6eb6b20 GL |
1623 | { |
1624 | struct ifaddrs *head; | |
1625 | struct ifaddrs *ifa; | |
1626 | struct sockaddr_dl *sdl; | |
1627 | ||
1628 | if (getifaddrs(&head) != 0) { | |
1629 | VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name, | |
10a89ef0 | 1630 | ovs_strerror(errno)); |
f6eb6b20 GL |
1631 | return errno; |
1632 | } | |
1633 | ||
1634 | for (ifa = head; ifa; ifa = ifa->ifa_next) { | |
1635 | if (ifa->ifa_addr->sa_family == AF_LINK) { | |
1636 | if (!strcmp(ifa->ifa_name, netdev_name)) { | |
e4b997e3 | 1637 | sdl = ALIGNED_CAST(struct sockaddr_dl *, ifa->ifa_addr); |
f6eb6b20 GL |
1638 | if (sdl) { |
1639 | memcpy(ea, LLADDR(sdl), sdl->sdl_alen); | |
1640 | freeifaddrs(head); | |
1641 | return 0; | |
1642 | } | |
1643 | } | |
1644 | } | |
1645 | } | |
1646 | ||
1647 | VLOG_ERR("could not find ethernet address for %s device", netdev_name); | |
1648 | freeifaddrs(head); | |
1649 | return ENODEV; | |
1650 | } | |
1651 | ||
1652 | static int | |
666afb55 YT |
1653 | set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED, |
1654 | int hwaddr_len OVS_UNUSED, | |
74ff3298 | 1655 | const struct eth_addr mac OVS_UNUSED) |
f6eb6b20 | 1656 | { |
8cb67eb5 | 1657 | #if defined(__FreeBSD__) |
f6eb6b20 | 1658 | struct ifreq ifr; |
259e0b1a | 1659 | int error; |
f6eb6b20 GL |
1660 | |
1661 | memset(&ifr, 0, sizeof ifr); | |
8742957c | 1662 | ovs_strlcpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name); |
f6eb6b20 GL |
1663 | ifr.ifr_addr.sa_family = hwaddr_family; |
1664 | ifr.ifr_addr.sa_len = hwaddr_len; | |
74ff3298 | 1665 | memcpy(ifr.ifr_addr.sa_data, &mac, hwaddr_len); |
259e0b1a BP |
1666 | error = af_inet_ioctl(SIOCSIFLLADDR, &ifr); |
1667 | if (error) { | |
f6eb6b20 | 1668 | VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s", |
259e0b1a BP |
1669 | netdev_name, ovs_strerror(error)); |
1670 | return error; | |
f6eb6b20 GL |
1671 | } |
1672 | return 0; | |
8cb67eb5 YT |
1673 | #elif defined(__NetBSD__) |
1674 | struct if_laddrreq req; | |
1675 | struct sockaddr_dl *sdl; | |
1676 | struct sockaddr_storage oldaddr; | |
996e5ae9 | 1677 | int error; |
8cb67eb5 YT |
1678 | |
1679 | /* | |
1680 | * get the old address, add new one, and then remove old one. | |
1681 | */ | |
1682 | ||
1683 | if (hwaddr_len != ETH_ADDR_LEN) { | |
1684 | /* just to be safe about sockaddr storage size */ | |
1685 | return EOPNOTSUPP; | |
1686 | } | |
1687 | memset(&req, 0, sizeof(req)); | |
8742957c | 1688 | ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name)); |
8cb67eb5 YT |
1689 | req.addr.ss_len = sizeof(req.addr); |
1690 | req.addr.ss_family = hwaddr_family; | |
1691 | sdl = (struct sockaddr_dl *)&req.addr; | |
1692 | sdl->sdl_alen = hwaddr_len; | |
996e5ae9 BP |
1693 | |
1694 | error = af_link_ioctl(SIOCGLIFADDR, &req); | |
1695 | if (error) { | |
1696 | return error; | |
8cb67eb5 | 1697 | } |
1e450da5 | 1698 | if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], &mac, hwaddr_len)) { |
8cb67eb5 YT |
1699 | return 0; |
1700 | } | |
1701 | oldaddr = req.addr; | |
1702 | ||
1703 | memset(&req, 0, sizeof(req)); | |
8742957c | 1704 | ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name)); |
8cb67eb5 YT |
1705 | req.flags = IFLR_ACTIVE; |
1706 | sdl = (struct sockaddr_dl *)&req.addr; | |
1707 | sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len; | |
1708 | sdl->sdl_alen = hwaddr_len; | |
1709 | sdl->sdl_family = hwaddr_family; | |
1e450da5 | 1710 | memcpy(sdl->sdl_data, &mac, hwaddr_len); |
996e5ae9 BP |
1711 | error = af_link_ioctl(SIOCALIFADDR, &req); |
1712 | if (error) { | |
1713 | return error; | |
8cb67eb5 YT |
1714 | } |
1715 | ||
1716 | memset(&req, 0, sizeof(req)); | |
8742957c | 1717 | ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name)); |
8cb67eb5 | 1718 | req.addr = oldaddr; |
996e5ae9 | 1719 | return af_link_ioctl(SIOCDLIFADDR, &req); |
8cb67eb5 YT |
1720 | #else |
1721 | #error not implemented | |
666afb55 | 1722 | #endif |
f6eb6b20 GL |
1723 | } |
1724 | ||
666afb55 YT |
1725 | static int |
1726 | ifr_get_flags(const struct ifreq *ifr) | |
1727 | { | |
1728 | #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH | |
1547aff6 | 1729 | return (ifr->ifr_flagshigh << 16) | (ifr->ifr_flags & 0xffff); |
666afb55 | 1730 | #else |
c61e1b73 | 1731 | return ifr->ifr_flags; |
666afb55 YT |
1732 | #endif |
1733 | } | |
1734 | ||
1735 | static void | |
1736 | ifr_set_flags(struct ifreq *ifr, int flags) | |
1737 | { | |
666afb55 | 1738 | #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH |
1547aff6 | 1739 | ifr->ifr_flags = flags & 0xffff; |
666afb55 | 1740 | ifr->ifr_flagshigh = flags >> 16; |
1547aff6 KL |
1741 | #else |
1742 | ifr->ifr_flags = flags; | |
666afb55 YT |
1743 | #endif |
1744 | } | |
996e5ae9 | 1745 | |
a48fd8eb | 1746 | #if defined(__NetBSD__) |
996e5ae9 BP |
1747 | /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and |
1748 | * 'arg'. Returns 0 if successful, otherwise a positive errno value. */ | |
1749 | int | |
b72d8dc2 | 1750 | af_link_ioctl(unsigned long command, const void *arg) |
996e5ae9 BP |
1751 | { |
1752 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; | |
1753 | static int sock; | |
1754 | ||
1755 | if (ovsthread_once_start(&once)) { | |
1756 | sock = socket(AF_LINK, SOCK_DGRAM, 0); | |
1757 | if (sock < 0) { | |
1758 | sock = -errno; | |
1759 | VLOG_ERR("failed to create link socket: %s", ovs_strerror(errno)); | |
1760 | } | |
1761 | ovsthread_once_done(&once); | |
1762 | } | |
1763 | ||
1764 | return (sock < 0 ? -sock | |
1765 | : ioctl(sock, command, arg) == -1 ? errno | |
1766 | : 0); | |
1767 | } | |
a48fd8eb | 1768 | #endif |
97d0619c | 1769 | #endif /* !defined(__MACH__) */ |