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