Linux DSA Net Switch驱动开发
參考
Atheros QCA8337交換芯片驅動開發
Linux虛擬網絡設備之bridge(橋)
phy 驅動與 switch 驅動
ALinux網橋的實現分析與使用
DSA switch configuration from userspace
源碼分析
基于xilinx petalinux2015.2.1,kernel3.19,QCA8337驅動開發。
設備樹初始化,關注dsa,mii-bus,dsa,ethernet,reg三個節點,
設備初始化,
//*\net\dsa\dsa.c line702 static int dsa_probe(struct platform_device *pdev) {struct dsa_platform_data *pd = pdev->dev.platform_data;struct net_device *dev;struct dsa_switch_tree *dst;int i, ret; ...if (pdev->dev.of_node) {ret = dsa_of_probe(pdev);if (ret)return ret;pd = pdev->dev.platform_data;} ...for (i = 0; i < pd->nr_chips; i++) {struct dsa_switch *ds;ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev);if (IS_ERR(ds)) {netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",i, PTR_ERR(ds));continue;}dst->ds[i] = ds;if (ds->drv->poll_link != NULL)dst->link_poll_needed = 1;} ...if (dst->link_poll_needed) {INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);init_timer(&dst->link_poll_timer);dst->link_poll_timer.data = (unsigned long)dst;dst->link_poll_timer.function = dsa_link_poll_timer;dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);add_timer(&dst->link_poll_timer);}return 0;out:dsa_of_remove(pdev);return ret; }重點函數dsa_switch_setup,需要在*\net\dsa目錄下添加qca的CONFIG_NET_DSA_TAG_QCA的實現,這一層實現的原因是,交換芯片會給接收和發送的包打上或者去除一個包頭header,用來控制包轉發packet forwarding,dsa_switch_setup也調用了很多驅動的接口函數probe,setup,set_addr,實現這幾個函數,則qca8377的驅動就開發出來了,
//*\net\dsa\dsa.c line178 static struct dsa_switch * dsa_switch_setup(struct dsa_switch_tree *dst, int index,struct device *parent, struct device *host_dev) {struct dsa_chip_data *pd = dst->pd->chip + index;struct dsa_switch_driver *drv;struct dsa_switch *ds;int ret;char *name;int i;bool valid_name_found = false; ...drv = dsa_switch_probe(host_dev, pd->sw_addr, &name); ...for (i = 0; i < DSA_MAX_PORTS; i++) {char *name;name = pd->port_names[i];if (name == NULL)continue;if (!strcmp(name, "cpu")) {if (dst->cpu_switch != -1) {netdev_err(dst->master_netdev,"multiple cpu ports?!\n");ret = -EINVAL;goto out;}dst->cpu_switch = index;dst->cpu_port = i;} else if (!strcmp(name, "dsa")) {ds->dsa_port_mask |= 1 << i;} else {ds->phys_port_mask |= 1 << i;}valid_name_found = true;}if (!valid_name_found && i == DSA_MAX_PORTS) {ret = -EINVAL;goto out;}/* Make the built-in MII bus mask match the number of ports,* switch drivers can override this later*/ds->phys_mii_mask = ds->phys_port_mask;/** If the CPU connects to this switch, set the switch tree* tagging protocol to the preferred tagging format of this* switch.*/if (dst->cpu_switch == index) {switch (drv->tag_protocol) { #ifdef CONFIG_NET_DSA_TAG_DSAcase DSA_TAG_PROTO_DSA:dst->rcv = dsa_netdev_ops.rcv;break; #endif #ifdef CONFIG_NET_DSA_TAG_EDSAcase DSA_TAG_PROTO_EDSA:dst->rcv = edsa_netdev_ops.rcv;break; #endif #ifdef CONFIG_NET_DSA_TAG_TRAILERcase DSA_TAG_PROTO_TRAILER:dst->rcv = trailer_netdev_ops.rcv;break; #endif #ifdef CONFIG_NET_DSA_TAG_BRCMcase DSA_TAG_PROTO_BRCM:dst->rcv = brcm_netdev_ops.rcv;break; #endif #ifdef CONFIG_NET_DSA_TAG_QCA /*add by xxx*/case DSA_TAG_PROTO_QCA:dst->rcv = qca_netdev_ops.rcv;break; #endifcase DSA_TAG_PROTO_NONE:break;default:ret = -ENOPROTOOPT;goto out;}dst->tag_protocol = drv->tag_protocol;}/** Do basic register setup.*/ret = drv->setup(ds);if (ret < 0)goto out;ret = drv->set_addr(ds, dst->master_netdev->dev_addr);if (ret < 0)goto out;ds->slave_mii_bus = mdiobus_alloc();if (ds->slave_mii_bus == NULL) {ret = -ENOMEM;goto out;}dsa_slave_mii_bus_init(ds);ret = mdiobus_register(ds->slave_mii_bus);if (ret < 0)goto out_free;/** Create network devices for physical switch ports.*/for (i = 0; i < DSA_MAX_PORTS; i++) {struct net_device *slave_dev;if (!(ds->phys_port_mask & (1 << i)))continue;slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);if (slave_dev == NULL) {netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",index, i, pd->port_names[i]);continue;}ds->ports[i] = slave_dev;} ... }dsa_slave_create函數,其中dsa_slave_phy_setup配置phy。
struct net_device * dsa_slave_create(struct dsa_switch *ds, struct device *parent,int port, char *name) { ...ret = dsa_slave_phy_setup(p, slave_dev);if (ret) {free_netdev(slave_dev);return NULL;}ret = register_netdev(slave_dev);if (ret) {netdev_err(master, "error %d registering interface %s\n",ret, slave_dev->name);phy_disconnect(p->phy);free_netdev(slave_dev);return NULL;}netif_carrier_off(slave_dev);if (p->phy != NULL) {if (ds->drv->get_phy_flags)p->phy->dev_flags |= ds->drv->get_phy_flags(ds, port);phy_attach(slave_dev, dev_name(&p->phy->dev),PHY_INTERFACE_MODE_GMII);p->phy->autoneg = AUTONEG_ENABLE;p->phy->speed = 0;p->phy->duplex = 0;p->phy->advertising = p->phy->supported | ADVERTISED_Autoneg;}return slave_dev; }需要升級以下文件,
*/net/dsa/dsa.c */net/dsa/tag_qca.c */net/dsa/Kconfig */net/dsa/Makefile */include/net/dsa.h */drivers/net/dsa/qca8k.c */drivers/net/dsa/qca8k.h */drivers/net/dsa/Kconfig */drivers/net/dsa/Makefile使用
Configuration with tagging support
The tagging based configuration is desired and supported by the majority of DSA switches. These switches are capable to tag incoming and outgoing traffic without using a VLAN based configuration.
single port
# configure each interface ip addr add 192.0.2.1/30 dev lan1 ip addr add 192.0.2.5/30 dev lan2 ip addr add 192.0.2.9/30 dev lan3# The master interface needs to be brought up before the slave ports. ip link set eth0 up# bring up the slave interfaces ip link set lan1 up ip link set lan2 up ip link set lan3 upbridge
# The master interface needs to be brought up before the slave ports. ip link set eth0 up# bring up the slave interfaces ip link set lan1 up ip link set lan2 up ip link set lan3 up# create bridge ip link add name br0 type bridge# add ports to bridge ip link set dev lan1 master br0 ip link set dev lan2 master br0 ip link set dev lan3 master br0# configure the bridge ip addr add 192.0.2.129/25 dev br0# bring up the bridge ip link set dev br0 upgateway
# The master interface needs to be brought up before the slave ports. ip link set eth0 up# bring up the slave interfaces ip link set wan up ip link set lan1 up ip link set lan2 up# configure the upstream port ip addr add 192.0.2.1/30 dev wan# create bridge ip link add name br0 type bridge# add ports to bridge ip link set dev lan1 master br0 ip link set dev lan2 master br0# configure the bridge ip addr add 192.0.2.129/25 dev br0# bring up the bridge ip link set dev br0 upConfiguration without tagging support
A minority of switches are not capable to use a taging protocol (DSA_TAG_PROTO_NONE). These switches can be configured by a VLAN based configuration.
single port
The configuration can only be set up via VLAN tagging and bridge setup.
# tag traffic on CPU port ip link add link eth0 name eth0.1 type vlan id 1 ip link add link eth0 name eth0.2 type vlan id 2 ip link add link eth0 name eth0.3 type vlan id 3# The master interface needs to be brought up before the slave ports. ip link set eth0 up ip link set eth0.1 up ip link set eth0.2 up ip link set eth0.3 up# bring up the slave interfaces ip link set lan1 up ip link set lan2 up ip link set lan3 up# create bridge ip link add name br0 type bridge# activate VLAN filtering ip link set dev br0 type bridge vlan_filtering 1# add ports to bridges ip link set dev lan1 master br0 ip link set dev lan2 master br0 ip link set dev lan3 master br0# tag traffic on ports bridge vlan add dev lan1 vid 1 pvid untagged bridge vlan add dev lan2 vid 2 pvid untagged bridge vlan add dev lan3 vid 3 pvid untagged# configure the VLANs ip addr add 192.0.2.1/30 dev eth0.1 ip addr add 192.0.2.5/30 dev eth0.2 ip addr add 192.0.2.9/30 dev eth0.3# bring up the bridge devices ip link set br0 upbridge
# tag traffic on CPU port ip link add link eth0 name eth0.1 type vlan id 1# The master interface needs to be brought up before the slave ports. ip link set eth0 up ip link set eth0.1 up# bring up the slave interfaces ip link set lan1 up ip link set lan2 up ip link set lan3 up# create bridge ip link add name br0 type bridge# activate VLAN filtering ip link set dev br0 type bridge vlan_filtering 1# add ports to bridge ip link set dev lan1 master br0 ip link set dev lan2 master br0 ip link set dev lan3 master br0 ip link set eth0.1 master br0# tag traffic on ports bridge vlan add dev lan1 vid 1 pvid untagged bridge vlan add dev lan2 vid 1 pvid untagged bridge vlan add dev lan3 vid 1 pvid untagged# configure the bridge ip addr add 192.0.2.129/25 dev br0# bring up the bridge ip link set dev br0 upgateway
# tag traffic on CPU port ip link add link eth0 name eth0.1 type vlan id 1 ip link add link eth0 name eth0.2 type vlan id 2# The master interface needs to be brought up before the slave ports. ip link set eth0 up ip link set eth0.1 up ip link set eth0.2 up# bring up the slave interfaces ip link set wan up ip link set lan1 up ip link set lan2 up# create bridge ip link add name br0 type bridge# activate VLAN filtering ip link set dev br0 type bridge vlan_filtering 1# add ports to bridges ip link set dev wan master br0 ip link set eth0.1 master br0 ip link set dev lan1 master br0 ip link set dev lan2 master br0# tag traffic on ports bridge vlan add dev lan1 vid 1 pvid untagged bridge vlan add dev lan2 vid 1 pvid untagged bridge vlan add dev wan vid 2 pvid untagged# configure the VLANs ip addr add 192.0.2.1/30 dev eth0.2 ip addr add 192.0.2.129/25 dev br0# bring up the bridge devices ip link set br0 up總結
以上是生活随笔為你收集整理的Linux DSA Net Switch驱动开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021-08-22 tbpy帮助文档
- 下一篇: linux命令里的xz是干嘛的,Ubun