Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418009
  • 博文数量: 99
  • 博客积分: 65
  • 博客等级: 民兵
  • 技术积分: 1012
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-20 16:30
个人简介

linux kernel 工程师

文章分类

全部博文(99)

文章存档

2018年(5)

2017年(12)

2016年(27)

2015年(10)

2014年(43)

2012年(2)

我的朋友

分类: LINUX

2014-02-11 17:10:29

dev_add_pack用来实现项协议栈增加协议handler

struct packet_type {
 __be16   type; /* This is really htons(ether_type). */    //用来与skb->protocol进行匹配
 struct net_device *dev; /* NULL is wildcarded here      */
 //协议的handler函数
 int   (*func) (struct sk_buff *,
      struct net_device *,
      struct packet_type *,
      struct net_device *);
 struct sk_buff  *(*gso_segment)(struct sk_buff *skb,
      netdev_features_t features);
 int   (*gso_send_check)(struct sk_buff *skb);
 struct sk_buff  **(*gro_receive)(struct sk_buff **head,
            struct sk_buff *skb);
 int   (*gro_complete)(struct sk_buff *skb);
 bool   (*id_match)(struct packet_type *ptype,
         struct sock *sk);
 void   *af_packet_priv;
 struct list_head list;
};

static inline struct list_head *ptype_head(const struct packet_type *pt)
{
 if (pt->type == htons(ETH_P_ALL))
  return &ptype_all;
 else
  return &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
}

void dev_add_pack(struct packet_type *pt)
{
 struct list_head *head = ptype_head(pt);

 spin_lock(&ptype_lock);
 list_add_rcu(&pt->list, head);
 spin_unlock(&ptype_lock);
}


---------------------------------
下面是两个handler的例子,分别处理arp(0x0806)和ip(0x800)
1.  ip处理
static struct packet_type ip_packet_type __read_mostly = {
 .type = cpu_to_be16(ETH_P_IP),
 .func = ip_rcv,
 .gso_send_check = inet_gso_send_check,
 .gso_segment = inet_gso_segment,
 .gro_receive = inet_gro_receive,
 .gro_complete = inet_gro_complete,
};

static int __init inet_init(void)
{
...
 dev_add_pack(&ip_packet_type);
....
}

2. arp 处理

static struct packet_type arp_packet_type __read_mostly = {
 .type = cpu_to_be16(ETH_P_ARP),
 .func = arp_rcv,
};

static int arp_proc_init(void);

void __init arp_init(void)
{
          ....
 dev_add_pack(&arp_packet_type);
 ...
}

阅读(1972) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~