- netdev = xcalloc(1, sizeof *netdev);
- netdev_init(&netdev->netdev, name, &netdev_linux_class);
- netdev->netdev_fd = -1;
- netdev->tap_fd = -1;
- netdev->cache = shash_find_data(&cache_map, name);
- if (!netdev->cache) {
- if (shash_is_empty(&cache_map)) {
- int error = rtnetlink_notifier_register(
- &netdev_linux_cache_notifier, netdev_linux_cache_cb, NULL);
- if (error) {
- netdev_close(&netdev->netdev);
- return error;
- }
- }
- netdev->cache = xmalloc(sizeof *netdev->cache);
- netdev->cache->shash_node = shash_add(&cache_map, name,
- netdev->cache);
- netdev->cache->valid = 0;
- netdev->cache->ref_cnt = 0;
- }
- netdev->cache->ref_cnt++;
-
- if (!strcmp(netdev_get_type(&netdev->netdev), "tap")) {
- static const char tap_dev[] = "/dev/net/tun";
- struct ifreq ifr;
-
- /* Open tap device. */
- netdev->tap_fd = open(tap_dev, O_RDWR);
- if (netdev->tap_fd < 0) {
- error = errno;
- VLOG_WARN("opening \"%s\" failed: %s", tap_dev, strerror(error));
- goto error;
- }
-
- /* Create tap device. */
- ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
- strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
- if (ioctl(netdev->tap_fd, TUNSETIFF, &ifr) == -1) {
- VLOG_WARN("%s: creating tap device failed: %s", name,
- strerror(errno));
- error = errno;
- goto error;
- }
-
- /* Make non-blocking. */
- error = set_nonblocking(netdev->tap_fd);
- if (error) {
- goto error;
- }
- }