driver: ipvlan: Define common functions to decrease duplicated codes used to add or del IP address
There are some duplicated codes in ipvlan_add_addr6/4 and ipvlan_del_addr6/4. Now define two common functions ipvlan_add_addr and ipvlan_del_addr to decrease the duplicated codes. It could be helful to maintain the codes. Signed-off-by: Gao Feng <fgao@ikuai8.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
parent
c4907c6ebb
commit
8667398277
@ -674,23 +674,22 @@ static int ipvlan_device_event(struct notifier_block *unused,
|
|||||||
return NOTIFY_DONE;
|
return NOTIFY_DONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
|
static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
|
||||||
{
|
{
|
||||||
struct ipvl_addr *addr;
|
struct ipvl_addr *addr;
|
||||||
|
|
||||||
if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
|
|
||||||
netif_err(ipvlan, ifup, ipvlan->dev,
|
|
||||||
"Failed to add IPv6=%pI6c addr for %s intf\n",
|
|
||||||
ip6_addr, ipvlan->dev->name);
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
|
addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
|
||||||
if (!addr)
|
if (!addr)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
addr->master = ipvlan;
|
addr->master = ipvlan;
|
||||||
memcpy(&addr->ip6addr, ip6_addr, sizeof(struct in6_addr));
|
if (is_v6) {
|
||||||
addr->atype = IPVL_IPV6;
|
memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
|
||||||
|
addr->atype = IPVL_IPV6;
|
||||||
|
} else {
|
||||||
|
memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
|
||||||
|
addr->atype = IPVL_IPV4;
|
||||||
|
}
|
||||||
list_add_tail(&addr->anode, &ipvlan->addrs);
|
list_add_tail(&addr->anode, &ipvlan->addrs);
|
||||||
|
|
||||||
/* If the interface is not up, the address will be added to the hash
|
/* If the interface is not up, the address will be added to the hash
|
||||||
@ -702,11 +701,11 @@ static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
|
static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
|
||||||
{
|
{
|
||||||
struct ipvl_addr *addr;
|
struct ipvl_addr *addr;
|
||||||
|
|
||||||
addr = ipvlan_find_addr(ipvlan, ip6_addr, true);
|
addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
|
||||||
if (!addr)
|
if (!addr)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -717,6 +716,23 @@ static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
|
||||||
|
{
|
||||||
|
if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
|
||||||
|
netif_err(ipvlan, ifup, ipvlan->dev,
|
||||||
|
"Failed to add IPv6=%pI6c addr for %s intf\n",
|
||||||
|
ip6_addr, ipvlan->dev->name);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipvlan_add_addr(ipvlan, ip6_addr, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
|
||||||
|
{
|
||||||
|
return ipvlan_del_addr(ipvlan, ip6_addr, true);
|
||||||
|
}
|
||||||
|
|
||||||
static int ipvlan_addr6_event(struct notifier_block *unused,
|
static int ipvlan_addr6_event(struct notifier_block *unused,
|
||||||
unsigned long event, void *ptr)
|
unsigned long event, void *ptr)
|
||||||
{
|
{
|
||||||
@ -750,45 +766,19 @@ static int ipvlan_addr6_event(struct notifier_block *unused,
|
|||||||
|
|
||||||
static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
|
static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
|
||||||
{
|
{
|
||||||
struct ipvl_addr *addr;
|
|
||||||
|
|
||||||
if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
|
if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
|
||||||
netif_err(ipvlan, ifup, ipvlan->dev,
|
netif_err(ipvlan, ifup, ipvlan->dev,
|
||||||
"Failed to add IPv4=%pI4 on %s intf.\n",
|
"Failed to add IPv4=%pI4 on %s intf.\n",
|
||||||
ip4_addr, ipvlan->dev->name);
|
ip4_addr, ipvlan->dev->name);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
addr = kzalloc(sizeof(struct ipvl_addr), GFP_KERNEL);
|
|
||||||
if (!addr)
|
|
||||||
return -ENOMEM;
|
|
||||||
|
|
||||||
addr->master = ipvlan;
|
return ipvlan_add_addr(ipvlan, ip4_addr, false);
|
||||||
memcpy(&addr->ip4addr, ip4_addr, sizeof(struct in_addr));
|
|
||||||
addr->atype = IPVL_IPV4;
|
|
||||||
list_add_tail(&addr->anode, &ipvlan->addrs);
|
|
||||||
|
|
||||||
/* If the interface is not up, the address will be added to the hash
|
|
||||||
* list by ipvlan_open.
|
|
||||||
*/
|
|
||||||
if (netif_running(ipvlan->dev))
|
|
||||||
ipvlan_ht_addr_add(ipvlan, addr);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
|
static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
|
||||||
{
|
{
|
||||||
struct ipvl_addr *addr;
|
return ipvlan_del_addr(ipvlan, ip4_addr, false);
|
||||||
|
|
||||||
addr = ipvlan_find_addr(ipvlan, ip4_addr, false);
|
|
||||||
if (!addr)
|
|
||||||
return;
|
|
||||||
|
|
||||||
ipvlan_ht_addr_del(addr);
|
|
||||||
list_del(&addr->anode);
|
|
||||||
kfree_rcu(addr, rcu);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ipvlan_addr4_event(struct notifier_block *unused,
|
static int ipvlan_addr4_event(struct notifier_block *unused,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user