From 82377f4d4ab6de76318f649190d0301f117b270e Mon Sep 17 00:00:00 2001 From: Giuseppe Lettieri Date: Thu, 15 Aug 2013 20:28:31 +0200 Subject: [PATCH] netdev_pltap,netdev_tunnel: moved to new API --- lib/netdev-pltap.c | 118 ++++++++++++++++++++++++-------------------- lib/netdev-tunnel.c | 99 ++++++++++++++++++++----------------- 2 files changed, 120 insertions(+), 97 deletions(-) diff --git a/lib/netdev-pltap.c b/lib/netdev-pltap.c index 8f4a67ac0..9d93b9e96 100644 --- a/lib/netdev-pltap.c +++ b/lib/netdev-pltap.c @@ -61,8 +61,6 @@ struct netdev_pltap { unsigned int change_seq; }; -static const struct netdev_rx_class netdev_rx_pltap_class; - static struct list sync_list; struct netdev_rx_pltap { @@ -74,8 +72,7 @@ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); static struct shash pltap_netdevs = SHASH_INITIALIZER(&pltap_netdevs); -static int netdev_pltap_create(const struct netdev_class *, const char *, - struct netdev **); +static int netdev_pltap_construct(struct netdev *netdev_); static void netdev_pltap_update_seq(struct netdev_pltap *); static int get_flags(struct netdev_pltap *dev, enum netdev_flags *flags); @@ -89,7 +86,7 @@ netdev_pltap_finalized(struct netdev_pltap *dev) static bool is_netdev_pltap_class(const struct netdev_class *class) { - return class->create == netdev_pltap_create; + return class->construct == netdev_pltap_construct; } static struct netdev_pltap * @@ -102,7 +99,7 @@ netdev_pltap_cast(const struct netdev *netdev) static struct netdev_rx_pltap* netdev_rx_pltap_cast(const struct netdev_rx *rx) { - netdev_rx_assert_class(rx, &netdev_rx_pltap_class); + ovs_assert(is_netdev_pltap_class(netdev_get_class(rx->netdev))); return CONTAINER_OF(rx, struct netdev_rx_pltap, up); } @@ -125,21 +122,26 @@ static void sync_done(struct netdev_pltap *dev) dev->sync_flags_needed = false; } +static struct netdev * +netdev_pltap_alloc(void) +{ + struct netdev_pltap *netdev = xzalloc(sizeof *netdev); + return &netdev->up; +} + static int -netdev_pltap_create(const struct netdev_class *class OVS_UNUSED, const char *name, - struct netdev **netdevp) +netdev_pltap_construct(struct netdev *netdev_) { - struct netdev_pltap *netdev; + struct netdev_pltap *netdev = netdev_pltap_cast(netdev_); int error; - netdev = xzalloc(sizeof *netdev); - netdev->real_name = xzalloc(IFNAMSIZ + 1); memset(&netdev->local_addr, 0, sizeof(netdev->local_addr)); netdev->valid_local_ip = false; netdev->valid_local_netmask = false; netdev->flags = 0; netdev->sync_flags_needed = false; + netdev->change_seq = 1; list_init(&netdev->sync_list); @@ -147,29 +149,24 @@ netdev_pltap_create(const struct netdev_class *class OVS_UNUSED, const char *nam netdev->fd = tun_alloc(IFF_TAP, netdev->real_name); if (netdev->fd < 0) { error = errno; - VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s", name, ovs_strerror(error)); - goto cleanup; + VLOG_WARN("tun_alloc(IFF_TAP, %s) failed: %s", + netdev_get_name(netdev_), ovs_strerror(error)); + return error; } VLOG_DBG("real_name = %s", netdev->real_name); /* Make non-blocking. */ error = set_nonblocking(netdev->fd); if (error) { - goto cleanup; + return error; } - netdev_init(&netdev->up, name, &netdev_pltap_class); - shash_add(&pltap_netdevs, name, netdev); - *netdevp = &netdev->up; + shash_add(&pltap_netdevs, netdev_get_name(netdev_), netdev); return 0; - -cleanup: - free(netdev); - return error; } static void -netdev_pltap_destroy(struct netdev *netdev_) +netdev_pltap_destruct(struct netdev *netdev_) { struct netdev_pltap *netdev = netdev_pltap_cast(netdev_); @@ -180,38 +177,53 @@ netdev_pltap_destroy(struct netdev *netdev_) shash_find_and_delete(&pltap_netdevs, netdev_get_name(netdev_)); +} + +static void +netdev_pltap_dealloc(struct netdev *netdev_) +{ + struct netdev_pltap *netdev = netdev_pltap_cast(netdev_); free(netdev); } static int netdev_pltap_up(struct netdev_pltap *dev); +static struct netdev_rx * +netdev_pltap_rx_alloc(void) +{ + struct netdev_rx_pltap *rx = xzalloc(sizeof *rx); + return &rx->up; +} + static int -netdev_pltap_rx_open(struct netdev *netdev_, struct netdev_rx **rxp) +netdev_pltap_rx_construct(struct netdev_rx *rx_) { + struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_); + struct netdev *netdev_ = rx->up.netdev; struct netdev_pltap *netdev = netdev_pltap_cast(netdev_); - struct netdev_rx_pltap *rx; - int err; + int error; - rx = xmalloc(sizeof *rx); - netdev_rx_init(&rx->up, netdev_, &netdev_rx_pltap_class); rx->fd = netdev->fd; - *rxp = &rx->up; if (!netdev_pltap_finalized(netdev)) return 0; - err = netdev_pltap_up(netdev); - if (err) { - free(rx); - *rxp = NULL; - return err; + error = netdev_pltap_up(netdev); + if (error) { + return error; } return 0; } static void -netdev_rx_pltap_destroy(struct netdev_rx *rx_) +netdev_pltap_rx_destruct(struct netdev_rx *rx_ OVS_UNUSED) +{ +} + +static void +netdev_pltap_rx_dealloc(struct netdev_rx *rx_) { struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_); + free(rx); } @@ -451,7 +463,7 @@ netdev_pltap_set_config(struct netdev *dev_, const struct smap *args) } static int -netdev_rx_pltap_recv(struct netdev_rx *rx_, void *buffer, size_t size) +netdev_pltap_rx_recv(struct netdev_rx *rx_, void *buffer, size_t size) { struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_); struct tun_pi pi; @@ -479,7 +491,7 @@ netdev_rx_pltap_recv(struct netdev_rx *rx_, void *buffer, size_t size) } static void -netdev_rx_pltap_wait(struct netdev_rx *rx_) +netdev_pltap_rx_wait(struct netdev_rx *rx_) { struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_); struct netdev_pltap *netdev = @@ -531,7 +543,7 @@ netdev_pltap_send_wait(struct netdev *netdev_) } static int -netdev_rx_pltap_drain(struct netdev_rx *rx_) +netdev_pltap_rx_drain(struct netdev_rx *rx_) { struct netdev_rx_pltap *rx = netdev_rx_pltap_cast(rx_); char buffer[128]; @@ -724,23 +736,23 @@ const struct netdev_class netdev_pltap_class = { netdev_pltap_run, netdev_pltap_wait, - netdev_pltap_create, - netdev_pltap_destroy, + netdev_pltap_alloc, + netdev_pltap_construct, + netdev_pltap_destruct, + netdev_pltap_dealloc, netdev_pltap_get_config, netdev_pltap_set_config, - NULL, /* get_tunnel_config */ - - netdev_pltap_rx_open, + NULL, /* get_tunnel_config */ netdev_pltap_send, netdev_pltap_send_wait, netdev_pltap_set_etheraddr, netdev_pltap_get_etheraddr, - NULL, /* get_mtu */ - NULL, /* set_mtu */ + NULL, /* get_mtu */ + NULL, /* set_mtu */ NULL, /* get_ifindex */ - NULL, /* get_carrier */ + NULL, /* get_carrier */ NULL, /* get_carrier_resets */ NULL, /* get_miimon */ netdev_pltap_get_stats, @@ -771,13 +783,13 @@ const struct netdev_class netdev_pltap_class = { netdev_pltap_update_flags, - netdev_pltap_change_seq -}; - -static const struct netdev_rx_class netdev_rx_pltap_class = { - netdev_rx_pltap_destroy, - netdev_rx_pltap_recv, - netdev_rx_pltap_wait, - netdev_rx_pltap_drain, + netdev_pltap_change_seq, + netdev_pltap_rx_alloc, + netdev_pltap_rx_construct, + netdev_pltap_rx_destruct, + netdev_pltap_rx_dealloc, + netdev_pltap_rx_recv, + netdev_pltap_rx_wait, + netdev_pltap_rx_drain, }; diff --git a/lib/netdev-tunnel.c b/lib/netdev-tunnel.c index f8bc74bb1..d9c113668 100644 --- a/lib/netdev-tunnel.c +++ b/lib/netdev-tunnel.c @@ -57,20 +57,17 @@ struct netdev_rx_tunnel { int fd; }; -static const struct netdev_rx_class netdev_rx_tunnel_class; - static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20); static struct shash tunnel_netdevs = SHASH_INITIALIZER(&tunnel_netdevs); -static int netdev_tunnel_create(const struct netdev_class *, const char *, - struct netdev **); +static int netdev_tunnel_construct(struct netdev *netdevp_); static void netdev_tunnel_update_seq(struct netdev_tunnel *); static bool is_netdev_tunnel_class(const struct netdev_class *class) { - return class->create == netdev_tunnel_create; + return class->construct == netdev_tunnel_construct; } static struct netdev_tunnel * @@ -83,20 +80,23 @@ netdev_tunnel_cast(const struct netdev *netdev) static struct netdev_rx_tunnel * netdev_rx_tunnel_cast(const struct netdev_rx *rx) { - netdev_rx_assert_class(rx, &netdev_rx_tunnel_class); + ovs_assert(is_netdev_tunnel_class(netdev_get_class(rx->netdev))); return CONTAINER_OF(rx, struct netdev_rx_tunnel, up); } +static struct netdev * +netdev_tunnel_alloc(void) +{ + struct netdev_tunnel *netdev = xzalloc(sizeof *netdev); + return &netdev->up; +} + static int -netdev_tunnel_create(const struct netdev_class *class, const char *name, - struct netdev **netdevp) +netdev_tunnel_construct(struct netdev *netdev_) { static unsigned int n = 0; - struct netdev_tunnel *netdev; - int error; + struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_); - netdev = xzalloc(sizeof *netdev); - netdev_init(&netdev->up, name, class); netdev->hwaddr[0] = 0xfe; netdev->hwaddr[1] = 0xff; netdev->hwaddr[2] = 0xff; @@ -113,28 +113,23 @@ netdev_tunnel_create(const struct netdev_class *class, const char *name, netdev->sockfd = inet_open_passive(SOCK_DGRAM, "", 0, &netdev->local_addr, 0); if (netdev->sockfd < 0) { - error = netdev->sockfd; - goto error; + return netdev->sockfd; } - shash_add(&tunnel_netdevs, name, netdev); + shash_add(&tunnel_netdevs, netdev_get_name(netdev_), netdev); n++; - *netdevp = &netdev->up; - - VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", name, netdev->sockfd, netdev->local_addr.sin_port); + VLOG_DBG("tunnel_create: name=%s, fd=%d, port=%d", + netdev_get_name(netdev_), netdev->sockfd, netdev->local_addr.sin_port); return 0; -error: - free(netdev); - return error; } static void -netdev_tunnel_destroy(struct netdev *netdev_) +netdev_tunnel_destruct(struct netdev *netdev_) { struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_); @@ -143,6 +138,12 @@ netdev_tunnel_destroy(struct netdev *netdev_) shash_find_and_delete(&tunnel_netdevs, netdev_get_name(netdev_)); +} + +static void +netdev_tunnel_dealloc(struct netdev *netdev_) +{ + struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_); free(netdev); } @@ -207,28 +208,39 @@ netdev_tunnel_set_config(struct netdev *dev_, const struct smap *args) return netdev_tunnel_connect(netdev); } +static struct netdev_rx * +netdev_tunnel_rx_alloc(void) +{ + struct netdev_rx_tunnel *rx = xzalloc(sizeof *rx); + return &rx->up; +} + static int -netdev_tunnel_rx_open(struct netdev *netdev_, struct netdev_rx **rxp) +netdev_tunnel_rx_construct(struct netdev_rx *rx_) { + struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_); + struct netdev *netdev_ = rx->up.netdev; struct netdev_tunnel *netdev = netdev_tunnel_cast(netdev_); - struct netdev_rx_tunnel *rx; - rx = xmalloc(sizeof *rx); - netdev_rx_init(&rx->up, netdev_, &netdev_rx_tunnel_class); rx->fd = netdev->sockfd; - *rxp = &rx->up; return 0; } static void -netdev_rx_tunnel_destroy(struct netdev_rx *rx_) +netdev_tunnel_rx_destruct(struct netdev_rx *rx_ OVS_UNUSED) +{ +} + +static void +netdev_tunnel_rx_dealloc(struct netdev_rx *rx_) { struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_); + free(rx); } static int -netdev_rx_tunnel_recv(struct netdev_rx *rx_, void *buffer, size_t size) +netdev_tunnel_rx_recv(struct netdev_rx *rx_, void *buffer, size_t size) { struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_); struct netdev_tunnel *netdev = @@ -262,7 +274,7 @@ netdev_rx_tunnel_recv(struct netdev_rx *rx_, void *buffer, size_t size) } static void -netdev_rx_tunnel_wait(struct netdev_rx *rx_) +netdev_tunnel_rx_wait(struct netdev_rx *rx_) { struct netdev_rx_tunnel *rx = netdev_rx_tunnel_cast(rx_); @@ -313,7 +325,7 @@ netdev_tunnel_send_wait(struct netdev *netdev_) } static int -netdev_rx_tunnel_drain(struct netdev_rx *rx_) +netdev_tunnel_rx_drain(struct netdev_rx *rx_) { struct netdev_tunnel *netdev = netdev_tunnel_cast(rx_->netdev); @@ -488,14 +500,14 @@ const struct netdev_class netdev_tunnel_class = { NULL, /* run */ NULL, /* wait */ - netdev_tunnel_create, - netdev_tunnel_destroy, + netdev_tunnel_alloc, + netdev_tunnel_construct, + netdev_tunnel_destruct, + netdev_tunnel_dealloc, netdev_tunnel_get_config, netdev_tunnel_set_config, NULL, /* get_tunnel_config */ - netdev_tunnel_rx_open, - netdev_tunnel_send, netdev_tunnel_send_wait, @@ -535,14 +547,13 @@ const struct netdev_class netdev_tunnel_class = { netdev_tunnel_update_flags, - netdev_tunnel_change_seq -}; - + netdev_tunnel_change_seq, -static const struct netdev_rx_class netdev_rx_tunnel_class = { - netdev_rx_tunnel_destroy, - netdev_rx_tunnel_recv, - netdev_rx_tunnel_wait, - netdev_rx_tunnel_drain, + netdev_tunnel_rx_alloc, + netdev_tunnel_rx_construct, + netdev_tunnel_rx_destruct, + netdev_tunnel_rx_dealloc, + netdev_tunnel_rx_recv, + netdev_tunnel_rx_wait, + netdev_tunnel_rx_drain, }; - -- 2.43.0