Objects Relationship


socket object

userspace藉由system call API: sys_socket()產生,結構中有一個 sock object

struct socket {
    socket_state state;
    kmemcheck_bitfield_begin(type);
    short type;
    kmemcheck_bitfield_end(type);
    unsigned long flags;
    . . .
    struct file *file;
    struct sock *sk;
    const struct proto_ops *ops;
};
(include/linux/net.h)

sock object

L3的抽象代表層(for scoket object),會擁有很多個sk_buff(也就是很多封包,用linked-list存)

struct sock {
    struct sk_buff_head sk_receive_queue;
    int sk_rcvbuf;
    unsigned long sk_flags;
    int sk_sndbuf;
    struct sk_buff_head sk_write_queue;
    . . .
    unsigned int sk_shutdown : 2,
    sk_no_check : 2,
    sk_protocol : 8,
    sk_type : 16;
    . . .
    void (*sk_data_ready)(struct sock *sk, int bytes);
    void (*sk_write_space)(struct sock *sk);
};

sk_type: socket的類型,像是:SOCK_STREAM, SOCK_RAW等等。(include/linux/net.h) sk_write_queue: 這就是retransmission queue


sk_buff_head object

這就是sk_buff的linked-list

sk_buff(skb)

  1. 當封包抵達device,net device driver就會allocate skb
  2. 有net_device,查詢硬體資訊,比方說MTU等等
  3. 當packet只是forward用的時候,sk=NULL(sock object)。

    struct sk_buff {
     . . .
     struct sock *sk;
     struct net_device *dev;  // 下面有補充
     . . .
     __u8 pkt_type:3,   // L2決定:broadcast, muticast...
     . . .
     __be16 protocol;
     . . .
     sk_buff_data_t tail;
     sk_buff_data_t end;
     unsigned char *head,
     *data;
     sk_buff_data_t transport_header;
     sk_buff_data_t network_header;
     sk_buff_data_t mac_header;
     . . .
    };
    (include/linux/skbuff.h)
    
  4. 一個skb包含L2,L3,L4的 header & payload。

  5. skb的操作方式:

    • data: 正在處理的payload
    • tail: end of payload
    • len: tail - data

L2 -> L3


net_device

struct net_device {
    unsigned int irq; /* device IRQ number */
    . . .
    const struct net_device_ops *netdev_ops;
    . . .
    unsigned int mtu;
    . . .
    unsigned int promiscuity;
    . . .
    unsigned char *dev_addr;
    . . .
};
(include/linux/netdevice.h)

results matching ""

    No results matching ""