OpenCores
URL https://opencores.org/ocsvn/1g_ethernet_dpi/1g_ethernet_dpi/trunk

Subversion Repositories 1g_ethernet_dpi

[/] [1g_ethernet_dpi/] [trunk/] [sw/] [dev/] [test_bfm/] [ether/] [ether_tx.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 kuzmi4
//////////////////////////////////////////////////////////////////////////////////
2
#include <stdlib.h>
3
#include <stdio.h>
4
 
5
#include "ether.h" // ETH_PKT_MAC_ADDR_LEN, ..
6
#include "eth_host_bfm.h" // eth_frm_write_wait, ..
7
 
8
//////////////////////////////////////////////////////////////////////////////////
9
// IPv4: name+id LIST
10
typedef struct {
11
    char id;
12
    char name[20];
13
} ipv4_prot_map_t;
14
const ipv4_prot_map_t ipv4_prot_map[] = {
15
    {IPv4_ICMP,         "IPv4_ICMP"},
16
    {IPv4_IGMP,         "IPv4_IGMP"},
17
    {IPv4_TCP,          "IPv4_TCP"},
18
    {IPv4_UDP,          "IPv4_UDP"},
19
    {IPv4_RDP,          "IPv4_RDP"},
20
    {IPv4_IRTP,         "IPv4_IRTP"},
21
    {IPv4_IPv6Encaps,   "IPv4_IPv6Encaps"},
22
    {IPv4_IPv6Route,    "IPv4_IPv6Route"},
23
    {IPv4_IPv6Frag,     "IPv4_IPv6Frag"},
24
    {IPv4_QNX,          "IPv4_QNX"},
25
    {IPv4_SMP,          "IPv4_SMP"},
26
    {IPv4_SCTP,         "IPv4_SCTP"},
27
    {IPv4_UDPLite,      "IPv4_UDPLite"}
28
};
29
//////////////////////////////////////////////////////////////////////////////////
30
// 
31
extern unsigned short crc_ip(void * ptr, unsigned count);
32
extern long crc_ether(char *buf, int len);
33
 
34
// 
35
void ether_tx_raw(char *ibuff, int ilen);
36
void ether_tx_ipv4(char *ibuff, int ilen);
37
 
38
//////////////////////////////////////////////////////////////////////////////////
39
//
40
// Ethernet TX routine
41
//
42
void ether_tx(char *ibuff, int ilen)
43
{
44
    // 
45
    unsigned short pkt_type = *((unsigned short *)((ibuff)+ETH_PKT_MAC_ADDR_LEN*2));
46
    switch(pkt_type) {
47
        case ETH_PKT_ARP : {
48
            printf("ether_tx: ETH_PKT_ARP, len=%04x\n", ilen);
49
            ether_tx_raw(ibuff, ilen);
50
            break;
51
        }
52
        case ETH_PKT_IPv4 : {
53
            ip_hdr_t *ptr_ipv4_hdr = (ip_hdr_t *)(ibuff+ETH_HDR_LEN);
54
            int i, prot=0;
55
            for (i = 0; i < (sizeof(ipv4_prot_map))/(sizeof(ipv4_prot_map_t)); i++) {
56
                if (ipv4_prot_map[i].id == ptr_ipv4_hdr->ip_prot) {
57
                    printf("ether_tx: %s, len=%04x\n", ipv4_prot_map[i].name, ilen);
58
                    prot++;
59
                }
60
            }
61
            if (prot == 0) { // not found in list
62
                printf("ether_tx: ETH_PKT_IPv4: IP protocol number = %x, len=%04x\n", ptr_ipv4_hdr->ip_prot, ilen);
63
            }
64
 
65
            /*
66
            switch(ptr_ipv4_hdr->ip_prot) {
67
                case IPv4_ICMP : {
68
                    printf("ether_tx: ETH_PKT_IPv4-ICMP, len=%04x\n", ilen);
69
                    break;
70
                }
71
                case IPv4_UDP : {
72
                    printf("ether_tx: ETH_PKT_IPv4-UDP, len=%04x\n", ilen);
73
                    break;
74
                }
75
                default : {
76
                    printf("ether_tx: ETH_PKT_IPv4: IP protocol number = %x, len=%04x\n", ptr_ipv4_hdr->ip_prot, ilen);
77
                    break;
78
                }
79
            }
80
            */
81
            ether_tx_ipv4(ibuff, ilen);
82
            break;
83
        }/*
84
        case ETH_PKT_IPv6 : {
85
            printf("ether_tx: ETH_PKT_IPv6, len=%04x\n", ilen);
86
            ether_tx_raw(ibuff, ilen);
87
        }*/
88
        default : {
89
            /*printf("ether_tx: pkt-type=%04x, len=%04x\n", ntohs(pkt_type), ilen);
90
            ether_tx_raw(ibuff, ilen);*/
91
            break;
92
        }
93
    }
94
}
95
//////////////////////////////////////////////////////////////////////////////////
96
//
97
// ARP+others
98
//
99
void ether_tx_raw(char *ibuff, int ilen)
100
{
101
    int i;
102
    // apr-len == 42 in case if {apr-len < ETH_MIN_TU} -> we will have rejection inside MAC
103
    int len_raw = (ilen < ETH_MIN_TU)? ETH_MIN_TU : ilen;
104
    char *ptr_raw = (char *)calloc(len_raw, sizeof(char)); // <- MEM-alloc
105
    if (ptr_raw == NULL) { return; }
106
    for (i = 0; i < ETH_MIN_TU; i++) { *(ptr_raw+i) = 0; }
107
    // data-copy
108
    for (i = 0; i < ilen; i++) {
109
        *(ptr_raw+i) = *(ibuff+i);
110
    }
111
    // upld
112
    for (i = 0; i < len_raw; i++) {
113
        eth_frm_write(*(ptr_raw+i), i);
114
    }
115
    // fcs
116
    int fcs = crc_ether(ptr_raw, len_raw);
117
    eth_frm_write(((fcs >>  0) & 0xFF), len_raw+0);
118
    eth_frm_write(((fcs >>  8) & 0xFF), len_raw+1);
119
    eth_frm_write(((fcs >> 16) & 0xFF), len_raw+2);
120
    eth_frm_write(((fcs >> 24) & 0xFF), len_raw+3);
121
    // len + STA
122
    eth_frm_write_len(len_raw+4); //+ FCS
123
    // 
124
    free(ptr_raw); // -> MEM-free
125
}
126
//////////////////////////////////////////////////////////////////////////////////
127
//
128
// IPv4-UDP
129
//
130
void ether_tx_ipv4(char *ibuff, int ilen)
131
{
132
    // check len
133
    char *ptr_ipv4;
134
    int i, tx_len;
135
 
136
    if (ilen < ETH_MIN_TU){
137
        ptr_ipv4 = (char *)calloc(ETH_MIN_TU, sizeof(char)); // <- MEM-alloc
138
        if (ptr_ipv4 == NULL) { return; }
139
        for (i = 0; i < ETH_MIN_TU; i++) { *(ptr_ipv4+i) = 0; }
140
        for (i = 0; i < ilen; i++) { *(ptr_ipv4+i) = *(ibuff+i); }
141
        tx_len = ETH_MIN_TU;
142
    } else {
143
        ptr_ipv4 = ibuff;
144
        tx_len = ilen;
145
    }
146
 
147
    // ipv4-csum
148
    ip_hdr_t *ptr_ipv4_hdr = (ip_hdr_t *)(ibuff+ETH_HDR_LEN);
149
    ptr_ipv4_hdr->ip_chksum = 0;
150
    unsigned short ipv4_xsum = ~crc_ip((char *)ptr_ipv4_hdr, ETH_IPv4_HDR_LEN >> 1);
151
    ptr_ipv4_hdr->ip_chksum = ipv4_xsum;
152
    // udp-csum
153
    udp_hdr_t *ptr_udp_hdr = (udp_hdr_t *)(ibuff+ETH_HDR_LEN+ETH_IPv4_HDR_LEN);
154
    ptr_udp_hdr->ud_cksum = 0;
155
 
156
    // upld
157
    for (i = 0; i < tx_len; i++) {
158
        eth_frm_write(*(ptr_ipv4+i), i);
159
    }
160
    // fcs
161
    int fcs = crc_ether(ptr_ipv4, tx_len);
162
    eth_frm_write(((fcs >>  0) & 0xFF), tx_len+0);
163
    eth_frm_write(((fcs >>  8) & 0xFF), tx_len+1);
164
    eth_frm_write(((fcs >> 16) & 0xFF), tx_len+2);
165
    eth_frm_write(((fcs >> 24) & 0xFF), tx_len+3);
166
    // len + STA
167
    eth_frm_write_len(tx_len+4); //+ FCS
168
    // 
169
    if (ilen < ETH_MIN_TU){
170
        free(ptr_ipv4); // -> MEM-free
171
    }
172
}
173
//////////////////////////////////////////////////////////////////////////////////

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.