#include <linux/module.h> #include <linux/inetdevice.h> #include <linux/netdevice.h> #include <linux/types.h> #include <linux/if_ether.h> #include <linux/string.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/in.h> #include <linux/inet.h> #include <linux/socket.h> #include <linux/netfilter.h> #include <linux/netfilter_ipv4.h> #include <net/netfilter/nf_conntrack.h> #include <linux/netfilter/nf_conntrack_tuple_common.h> #include <linux/netfilter/nf_conntrack_common.h> #include <linux/version.h> #include <net/sock.h> #include <net/route.h> #include <net/flow.h>和#include <linux/module.h> #include <linux/inetdevice.h> #include <linux/netdevice.h> #include <linux/types.h> #include <linux/if_ether.h> #include <linux/string.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/in.h> #include <linux/inet.h> #include <linux/socket.h> #include <linux/version.h> #include <linux/netfilter.h> #include <net/sock.h> #include <net/route.h> #include <net/flow.h> #include <net/ip.h>对应的在vpp中的头文件
时间: 2023-08-14 18:06:41 浏览: 226
在vpp中,这些头文件对应的可能是:
- <vnet/vnet.h>:包含了许多与网络相关的数据类型、函数和宏定义。
- <vnet/ip/ip.h>:包含了IP协议相关的数据类型、函数和宏定义。
- <vnet/tcp/tcp.h>:包含了TCP协议相关的数据类型、函数和宏定义。
- <vnet/udp/udp.h>:包含了UDP协议相关的数据类型、函数和宏定义。
- <vnet/ethernet/ethernet.h>:包含了以太网协议相关的数据类型、函数和宏定义。
- <vnet/ip/ip4.h>:包含了IPv4协议相关的数据类型、函数和宏定义。
- <vnet/ip/ip6.h>:包含了IPv6协议相关的数据类型、函数和宏定义。
- <vnet/tcp/tcp_header.h>:包含了TCP报文头相关的数据类型、函数和宏定义。
- <vnet/udp/udp_header.h>:包含了UDP报文头相关的数据类型、函数和宏定义。
相关问题
linux mac802.11,linux wireless 基础知识 MAC80211 CFG80211(示例代码)
Linux中的MAC80211和CFG80211是用于无线网络的子系统。它们提供了一组API,使开发人员能够在Linux内核中实现无线网络设备驱动程序。
MAC80211是一个实现IEEE 802.11标准的软件模块,它负责管理Linux内核中的无线网络设备。它为网络设备提供了一组接口,使它们能够与其他网络设备进行通信。MAC80211还负责处理无线帧和管理无线网络的连接。
CFG80211是一个用于配置802.11设备的API。它负责管理无线网络设备的配置,例如频率、信道和加密设置等。它还提供了一组接口,使用户空间应用程序能够与无线网络设备进行通信。
示例代码:
以下代码展示了如何使用CFG80211 API在Linux内核中配置无线网络设备。
```
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/wireless.h>
#include <net/cfg80211.h>
static struct cfg80211_ops my_cfg_ops = {
.change_beacon = NULL,
};
static struct cfg80211_device my_cfg_device = {
.ops = &my_cfg_ops,
};
static int __init my_init(void)
{
int ret;
struct wireless_dev *wdev;
wdev = kzalloc(sizeof(*wdev), GFP_KERNEL);
if (!wdev)
return -ENOMEM;
wdev->wiphy = wiphy_new(&my_cfg_ops, sizeof(*wdev));
if (!wdev->wiphy) {
kfree(wdev);
return -ENOMEM;
}
wdev->wiphy->privid++;
wdev->wiphy->dev.parent = NULL;
wdev->wiphy->dev.release = NULL;
wdev->wiphy->dev.groups = NULL;
wdev->wiphy->dev.dma_mask = NULL;
wdev->wiphy->dev.coherent_dma_mask = ~0;
ret = wiphy_register(wdev->wiphy);
if (ret) {
wiphy_free(wdev->wiphy);
kfree(wdev);
return ret;
}
wdev->wiphy->dev.parent = wiphy_dev(wdev->wiphy);
wdev->netdev = alloc_netdev_mqs(sizeof(struct net_device *), "my_dev",
NET_NAME_UNKNOWN, ether_setup,
1, 1);
if (!wdev->netdev) {
wiphy_unregister(wdev->wiphy);
wiphy_free(wdev->wiphy);
kfree(wdev);
return -ENOMEM;
}
wdev->wiphy->privid++;
wdev->netdev->ieee80211_ptr = wdev;
wdev->netdev->ieee80211_ptr->iftype = NL80211_IFTYPE_STATION;
wdev->netdev->ieee80211_ptr->flags |= IEEE80211_STA_CONNECTION_POLL;
ret = register_netdev(wdev->netdev);
if (ret) {
free_netdev(wdev->netdev);
wiphy_unregister(wdev->wiphy);
wiphy_free(wdev->wiphy);
kfree(wdev);
return ret;
}
my_cfg_device.wiphy = wdev->wiphy;
ret = cfg80211_register_device(&my_cfg_device);
if (ret) {
unregister_netdev(wdev->netdev);
free_netdev(wdev->netdev);
wiphy_unregister(wdev->wiphy);
wiphy_free(wdev->wiphy);
kfree(wdev);
return ret;
}
return 0;
}
static void __exit my_exit(void)
{
cfg80211_unregister_device(&my_cfg_device);
unregister_netdev(wdev->netdev);
free_netdev(wdev->netdev);
wiphy_unregister(wdev->wiphy);
wiphy_free(wdev->wiphy);
kfree(wdev);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
```
这段代码首先创建了一个无线设备和一个无线网络接口。然后,它将无线设备注册到CFG80211子系统中,并将无线网络接口注册到Linux内核中。最后,它将无线设备和无线网络接口添加到一个数据结构中,并将该数据结构注册到CFG80211子系统中。
lan8720 linux驱动
LAN8720是一款常用的以太网PHY芯片,它可以通过RMII或MII接口与Linux系统连接。下面是一个可能的LAN8720 Linux驱动程序的示例代码:
```
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/i2c.h>
#include <linux/netdevice.h>
#include <linux/ethtool.h>
#include <linux/of.h>
#include <linux/of_device.h>
/* LAN8720 PHY registers */
#define LAN8720_REG_BCR 0x00 /* Basic Control Register */
#define LAN8720_REG_BSR 0x01 /* Basic Status Register */
#define LAN8720_REG_PHYID1 0x02 /* PHY Identifier 1 */
#define LAN8720_REG_PHYID2 0x03 /* PHY Identifier 2 */
#define LAN8720_REG_ANAR 0x04 /* Auto-Negotiation Advertisement Register */
#define LAN8720_REG_ANLPAR 0x05 /* Auto-Negotiation Link Partner Ability Register */
#define LAN8720_REG_ANER 0x06 /* Auto-Negotiation Expansion Register */
#define LAN8720_REG_DSCR 0x10 /* PCS/TX Descriptor Register */
#define LAN8720_REG_DSCSR 0x11 /* PCS/TX Descriptor and Status Register */
#define LAN8720_REG_PHYCR 0x19 /* PHY Control Register */
#define LAN8720_PHY_RESET_DELAY_MS 100
struct lan8720_priv {
struct device *dev;
struct net_device *netdev;
void __iomem *regs;
};
static int lan8720_mdio_read(struct mii_bus *bus, int phy_addr, int reg_addr)
{
struct lan8720_priv *priv = bus->priv;
void __iomem *regs = priv->regs;
int val;
writel(0x80000000 | (phy_addr << 23) | (reg_addr << 18), regs + LAN8720_REG_DSCR);
while (!(readl(regs + LAN8720_REG_DSCSR) & 0x80000000));
val = readl(regs + LAN8720_REG_DSCR) & 0xffff;
return val;
}
static int lan8720_mdio_write(struct mii_bus *bus, int phy_addr, int reg_addr, u16 val)
{
struct lan8720_priv *priv = bus->priv;
void __iomem *regs = priv->regs;
writel(0xc0000000 | (phy_addr << 23) | (reg_addr << 18) | val, regs + LAN8720_REG_DSCR);
while (!(readl(regs + LAN8720_REG_DSCSR) & 0x80000000));
return 0;
}
static int lan8720_phy_reset(struct lan8720_priv *priv)
{
u16 val;
/* Reset PHY */
lan8720_mdio_write(priv->netdev->mdio_bus, priv->netdev->phydev->addr, LAN8720_REG_BCR, 0x8000);
msleep(LAN8720_PHY_RESET_DELAY_MS);
/* Wait for PHY to come out of reset */
val = lan8720_mdio_read(priv->netdev->mdio_bus, priv->netdev->phydev->addr, LAN8720_REG_BCR);
if (val & 0x8000)
return -EBUSY;
return 0;
}
static int lan8720_phy_init(struct lan8720_priv *priv)
{
u16 val;
/* Reset PHY */
if (lan8720_phy_reset(priv))
return -EBUSY;
/* Enable auto-negotiation */
lan8720_mdio_write(priv->netdev->mdio_bus, priv->netdev->phydev->addr, LAN8720_REG_BCR, 0x1000);
/* Wait for auto-negotiation to complete */
do {
val = lan8720_mdio_read(priv->netdev->mdio_bus, priv->netdev->phydev->addr, LAN8720_REG_BSR);
} while (!(val & 0x0020));
/* Enable RX/TX */
lan8720_mdio_write(priv->netdev->mdio_bus, priv->netdev->phydev->addr, LAN8720_REG_BCR, 0x2000);
return 0;
}
static int lan8720_probe(struct platform_device *pdev)
{
struct lan8720_priv *priv;
struct net_device *netdev;
struct device_node *np = pdev->dev.of_node;
struct resource *res;
int ret;
/* Allocate private data */
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
/* Allocate netdev */
netdev = alloc_etherdev(sizeof(*priv));
if (!netdev)
return -ENOMEM;
/* Set netdev MAC address */
of_property_read_u8_array(np, "local-mac-address", netdev->dev_addr, ETH_ALEN);
/* Initialize netdev */
netdev->netdev_ops = &lan8720_netdev_ops;
netdev->ethtool_ops = &lan8720_ethtool_ops;
netdev->needs_free_netdev = true;
netif_napi_add(netdev, &priv->napi, lan8720_rx_napi, NAPI_POLL_WEIGHT);
/* Allocate MDIO bus */
netdev->phydev = mdiobus_alloc();
if (!netdev->phydev) {
free_netdev(netdev);
return -ENOMEM;
}
/* Set MDIO bus parameters */
netdev->phydev->bus = priv->netdev->mdio_bus;
netdev->phydev->mdio_read = lan8720_mdio_read;
netdev->phydev->mdio_write = lan8720_mdio_write;
netdev->phydev->priv = priv;
/* Initialize PHY */
ret = lan8720_phy_init(priv);
if (ret) {
mdiobus_free(netdev->phydev->bus);
free_netdev(netdev);
return ret;
}
/* Set netdev device and add it to network interface */
priv->dev = &pdev->dev;
priv->netdev = netdev;
platform_set_drvdata(pdev, priv);
ret = register_netdev(netdev);
if (ret) {
mdiobus_free(netdev->phydev->bus);
free_netdev(netdev);
return ret;
}
return 0;
}
static int lan8720_remove(struct platform_device *pdev)
{
struct lan8720_priv *priv = platform_get_drvdata(pdev);
unregister_netdev(priv->netdev);
mdiobus_free(priv->netdev->phydev->bus);
free_netdev(priv->netdev);
return 0;
}
static const struct of_device_id lan8720_of_match[] = {
{ .compatible = "smsc,lan8720" },
{ },
};
MODULE_DEVICE_TABLE(of, lan8720_of_match);
static struct platform_driver lan8720_driver = {
.probe = lan8720_probe,
.remove = lan8720_remove,
.driver = {
.name = "lan8720",
.of_match_table = lan8720_of_match,
},
};
module_platform_driver(lan8720_driver);
MODULE_AUTHOR("Your Name Here");
MODULE_DESCRIPTION("LAN8720 driver");
MODULE_LICENSE("GPL");
```
需要注意的是,以上示例代码只是一个框架,实际的驱动程序需要根据硬件的具体特性进行实现。另外,该驱动程序还包含了一个NAPI驱动程序,以便在高速网络环境下进行数据接收。
阅读全文