赞
踩
上一节,我们了解如何,添加一个桥设备的大致步骤,现在我们看看如何在桥设备上添加一个端口。
我们先来看一个命令
brctrl addif br0 eth1
这个命令的作用是在br0上添加一个eth1的接口,内核实现的相关函数是br_add_if.
那么如何调用到这个接口呢?
上节我们在将添加一个桥设备的时候,有一个参数br_netdev_ops,我们当时说这里面有很多的注册函数,
调用add_del_if SIOCDEVPRIVATE,调用old_dev_ioctl函数,
其实我们再看源码时就会发现,old_dev_ioctl函数后面其实也调用了add_del_if。add_del_if,
最后在根据是添加还是删除接口的参数,进行相应的添加(SIOCBRADDIF),还是删除接口。
int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
struct net_bridge *br = netdev_priv(dev);
switch (cmd) {
case SIOCDEVPRIVATE:
return old_dev_ioctl(dev, rq, cmd);
case SIOCBRADDIF:
case SIOCBRDELIF:
return add_del_if(br, rq->ifr_ifindex, cmd == SIOCBRADDIF);
}
br_debug(br, "Bridge does not support ioctl 0x%x\n", cmd);
return -EOPNOTSUPP;
}
该函数注册在,桥设备添加时候dev->netdev_ops = &br_netdev_ops;
在br_netdev_ops有一个函数指针.ndo_do_ioctl= br_dev_ioctl
int br_add_if(struct net_bridge *br, struct net_device *dev)
{
struct net_bridge_port *p;
int err = 0;
unsigned br_hr, dev_hr;
bool changed_addr;
/* Don't allow bridging non-ethernet like devices, or DSA-enabled
* master network devices since the bridge layer rx_handler prevents
* the DSA fake ethertype handler to be invoked, so we do not strip off
* the DSA switch tag protocol header and the bridge layer just return
* RX_HANDLER_CONSUMED, stopping RX processing for these frames.
*/
if ((dev->flags & IFF_LOOPBACK) ||
dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
!is_valid_ether_addr(dev->dev_addr) ||
netdev_uses_dsa(dev))
return -EINVAL;
/* No bridging of bridges */
if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit)
return -ELOOP;
/* Device is already being bridged */
if (br_port_exists(dev))
return -EBUSY;
/* No bridging devices that dislike that (e.g. wireless) */
if (dev->priv_flags & IFF_DONT_BRIDGE)
return -EOPNOTSUPP;
/*分配一个新网桥端口并对其初始化*/
p = new_nbp(br, dev);
if (IS_ERR(p))
return PTR_ERR(p);
/*调用设备通知链,告诉网络有这样一个设备*/
call_netdevice_notifiers(NETDEV_JOIN, dev);
/**向设备添加或删除所有多播帧的接收。*/
err = dev_set_allmulti(dev, 1);
if (err)
goto put_back;
err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
SYSFS_BRIDGE_PORT_ATTR);
if (err)
goto err1;
/*把链路添加到sysfs*/
err = br_sysfs_addif(p);
if (err)
goto err2;
err = br_netpoll_enable(p);
if (err)
goto err3;
/*注册设备接收帧函数*/
err = netdev_rx_handler_register(dev, br_handle_frame, p);
if (err)
goto err4;
/*给该端口指派默认优先权*/
dev->priv_flags |= IFF_BRIDGE_PORT;
/*向上级设备添加主链路*/
err = netdev_master_upper_dev_link(dev, br->dev, NULL, NULL);
if (err)
goto err5;
/*禁用网络设备上的大型接收卸载(LRO)。
必须在RTNL下调用。
如果接收到的数据包可能转发到另一个接口,
则需要这样做。*/
dev_disable_lro(dev);
list_add_rcu(&p->list, &br->port_list);
/*更新桥上的端口数,如果有更新,再进一步将其设为混杂模式*/
nbp_update_port_count(br);
/* 重新计算dev->features并发送通知(如果已更改)。
应该调用驱动程序或硬件依赖条件可能会改变影响功能。*/
netdev_update_features(br->dev);
br_hr = br->dev->needed_headroom;
dev_hr = netdev_get_fwd_headroom(dev);
if (br_hr < dev_hr)
update_headroom(br, dev_hr);
else
netdev_set_rx_headroom(dev, br_hr);
/*把dev的mac添加到转发数据库中*/
if (br_fdb_insert(br, p, dev->dev_addr, 0))
netdev_err(dev, "failed insert local address bridge forwarding table\n");
/*初始化该桥端口的vlan*/
err = nbp_vlan_init(p);
if (err) {
netdev_err(dev, "failed to initialize vlan filtering on this port\n");
goto err6;
}
spin_lock_bh(&br->lock);
/*更新网桥id*/
changed_addr = br_stp_recalculate_bridge_id(br);
/*设备是否启动,桥是否启动,设备上是否有载波信号(网桥没有载波状态,因为网桥是虚拟设备)*/
if (netif_running(dev) && netif_oper_up(dev) &&
(br->dev->flags & IFF_UP))
/*启动网桥端口*/
br_stp_enable_port(p);
spin_unlock_bh(&br->lock);
br_ifinfo_notify(RTM_NEWLINK, p);
/*如果网桥的地址改变,则调用通知连相关的函数*/
if (changed_addr)
call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
/*更新网桥mtu*/
dev_set_mtu(br->dev, br_min_mtu(br));
br_set_gso_limits(br);
/*添加一个内核对象*/
kobject_uevent(&p->kobj, KOBJ_ADD);
return 0;
err6:
list_del_rcu(&p->list);
br_fdb_delete_by_port(br, p, 0, 1);
nbp_update_port_count(br);
netdev_upper_dev_unlink(dev, br->dev);
err5:
dev->priv_flags &= ~IFF_BRIDGE_PORT;
netdev_rx_handler_unregister(dev);
err4:
br_netpoll_disable(p);
err3:
sysfs_remove_link(br->ifobj, p->dev->name);
err2:
kobject_put(&p->kobj);
p = NULL; /* kobject_put frees */
err1:
dev_set_allmulti(dev, -1);
put_back:
dev_put(dev);
kfree(p);
return err;
}
在桥上添加接口的基本步骤,如上,删除桥端口,主要是把建立接口时所做的事情撤销,如添加接口出错时的一些处理。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。