TCP/UDP协议中用到的各种header, SystemVerilog版

整理了TCP/IP协议中用到的各种header, 方便大家使用

package type_defs;
typedef logic [3:0]  u4_t;
typedef logic [7:0]  u8_t;
typedef logic [15:0] u16_t;
typedef logic [31:0] u32_t;
typedef logic [63:0] u64_t;


typedef struct packed {	//MAC头
u16_t padding;
logic [47:0] dst_addr;
logic [47:0] src_addr;
u16_t mac_type;
} mac_header_t;

typedef struct packed { //ARP头
    u16_t hardware_type;
    u16_t protocol_type;
    u8_t hardware_addr_len;
    u8_t protocol_addr_len;
    u16_t operation_type;
    logic [47:0] src_addr;
    u32_t src_ip_addr;
    logic [47:0] dst_addr;
    u32_t dst_ip_addr;
} arp_header_t;

parameter PROTO_ICMP = 8'd1,
PROTO_TCP = 8'd6,
PROTO_UDP = 8'd17;

typedef struct packed{ //IP头
    u4_t version;
    u4_t header_len;
    u8_t tos;
    u16_t len;
    u16_t ipid;
    logic reserved;
    logic b_no_fragment;
    logic [5:0] reserved1;
    u8_t ipoffset;
    u8_t ttl;
    u8_t proto;
    u16_t checksum;
    u32_t src_ipaddr;
    u32_t dst_ipaddr;
} ipv4_header_t;

typedef struct packed{ //ICMP头
    u8_t icmp_type;
    u8_t code;
    u16_t checksum;
    u16_t id;
    u16_t seqno;
}icmp_headr_t;  


typedef struct packed{ //伪首部,用于计算checksum
u32_t src_addr;  
u32_t dst_addr;     
u8_t mbz; 
u8_t protocal;   
u16_t len;
}psdheader_t;

typedef struct packed{ //udp头
    u16_t srcport;
    u16_t destport;
    u16_t len;
    u16_t checksum;
}udp_header_t;


typedef enum logic[2:0] { //tcp状态机
    TCP_LISTEN = 3'd0,
    TCP_SYN_RCVD,
    TCP_ESTABLISHED,
    TCP_CLOSE_WAIT,
    TCP_LAST_ACK
}E_TCP_STATE;

typedef struct packed { //tcp头
    u16_t srcport;
    u16_t destport;
    u32_t seqno;
    u32_t ackno;
    u4_t header_len;
    logic [3:0] reserved;
    struct packed {
        logic ECE;
        logic CWR;
        logic URG;
        logic ACK;
        logic PSH;
        logic RST;
        logic SYN;
        logic FIN;
    }flags;
    u16_t window_size;
    u16_t checksum;
    u16_t urgp;
}tcp_header_t;

typedef struct packed{ //tcp选项头,这里举了一个例子, 可以改成别的
    u8_t kind_mss;
    u8_t mass_len;
    u16_t mss;
    u8_t kind_window_scale;
    u8_t window_scale_len;
    u8_t window_scale;
    u8_t end_of_opt;
}tcp_opts_header_t; 

typedef struct packed{ //描述tcp连接的各种变量
    struct packed {
        logic ECE;
        logic CWR;
        logic URG;
        logic ACK;
        logic PSH;
        logic RST;
        logic SYN;
        logic FIN;
    }rflags;
    u32_t ackno;
    u32_t seqno;
    u32_t dup_ackno;
    logic [2:0] dup_ack_count;
    logic new_rx_data;
    logic has_data_to_send;
    logic need_resend_data;
    logic ack_recved;
    logic ackno_correct;
    logic seqno_correct;
    logic window_size_update;

    u32_t first_seqno;
    u32_t second_seqno;
    u16_t rlen;
    u16_t llen;          /**< Length of the data that was previously sent. */
    u16_t rwindow_size;
    u16_t lwindow_size;
    
    u32_t ripaddr;      /**< The IP address of the remote host. */
    u16_t rport;        /**< The local remote TCP port.*/
    u16_t lport;        /**< The local TCP port. */
    u16_t rmss;
    u16_t lmss;          /**< Current maximum segment size for the connection. */
    u32_t rcv_nxt;      /**< The sequence number that we expect to receive next. */
    u32_t snd_nxt;      /**< The sequence number that was last sent by us. */
    E_TCP_STATE tcp_state;      /**< TCP state. */

    u16_t sa;            /**< Retransmission time-out calculation variable. */
    u16_t sv;            /**< Retransmission time-out calculation variable. */
    u16_t rto;           /**< Retransmission time-out. */
    u16_t tcpstateflags; /**< TCP state and flags. */
    u16_t timer;         /**< The retransmission timer. */
    logic timer_adder_c7;
    u8_t num_retrans;   /**< The number of retransmissions for the last segment sent. */
    u32_t ackno_rto;
    logic rto_calc_started;
    logic recv_correct;
    logic [19:0] ripid;
    logic [2:0]  unacked_num;
    logic [5:0] cwnd;
    logic [5:0] ssthresh;
} tcp_conn_t;


endpackage



你可能感兴趣的:(TCP/UDP,FPGA)