-
Notifications
You must be signed in to change notification settings - Fork 1
/
mch_main.c
267 lines (222 loc) · 6.55 KB
/
mch_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* mch_main.c
*
* Created on: Jul 2, 2014
* Author: yonch
*/
#include "lwip/inet.h"
#include "lwip/tcp.h"
#include "lwip/ip_frag.h"
#include "lwip/netif.h"
#include "lwip/init.h"
#include "lwip/stats.h"
#include "netif/etharp.h"
#include "lwip/timers.h"
#include "lwip/tcp_impl.h"
#include "lwip/udp.h"
#include <time.h>
#include <assert.h>
#define NUM_INTERFACES 2
char *MCH_IP_ADDRS[NUM_INTERFACES] = {"192.168.1.11", "192.168.1.22"};
struct ip_addr mch_myip_addr[NUM_INTERFACES];
static struct netif mchdrv_netif[NUM_INTERFACES];
#define MCH_ARP_TIMER_INTERVAL (ARP_TMR_INTERVAL * 1000)
#define MCH_TCP_TIMER_INTERVAL (TCP_TMR_INTERVAL * 1000)
#define MCH_IPREASS_TIMER_INTERVAL (IP_TMR_INTERVAL * 1000)
#define MCH_IPADDR_GW "192.168.1.254"
#define MCH_IPADDR_NETMASK "255.255.255.0"
typedef struct timespec mch_timestamp;
void mch_timestamp_get(mch_timestamp *ts) {
clock_gettime(CLOCK_MONOTONIC, ts);
}
s32_t mch_timestamp_diff(mch_timestamp *a, mch_timestamp *b) {
return 1000 * (b->tv_sec - a->tv_sec) + (b->tv_nsec - a->tv_nsec) / (1000 * 1000);
}
void mch_timestamp_init() {
return;
}
u32_t sys_now() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000 + (ts.tv_nsec / (1000 * 1000));
}
static mch_timestamp ts_etharp;
static mch_timestamp ts_tcp;
static mch_timestamp ts_ipreass;
err_t mchdrv_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr)
{
struct pbuf *dest_pbuf;
struct netif *dest_netif;
err_t err;
/* verbose print */
printf("%s: on interface %d, len %d\n", __func__, (int)(uint64_t)netif->state,
p->tot_len);
/* we copy p to a new destination pbuf, because the tcp stack could change
* buffers on retransmits (according to the wiki) */
dest_pbuf = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
if (dest_pbuf == NULL) {
printf("%s: could not allocate destination pbuf\n", __func__);
return ERR_MEM;
}
err = pbuf_copy(dest_pbuf, p);
if (err != ERR_OK) {
printf("%s: buffer copy returned %d\n", __func__, err);
pbuf_free(dest_pbuf);
return err;
}
/* input the packet to the other netif */
assert((uint64_t)netif->state <= 1);
dest_netif = &mchdrv_netif[1 - (u8_t)(uint64_t)netif->state];
err = dest_netif->input(dest_pbuf, dest_netif);
if (err != ERR_OK)
printf("%s: dest_netif->input returned error %d", __func__, err);
return ERR_OK;
}
err_t mchdrv_init(struct netif *netif)
{
/* we're not using hwaddr */
netif->hwaddr_len = 0;
netif->mtu = 1500;
netif->name[0] = 'i';
netif->name[1] = 'p';
netif->num = (u8_t)(uint64_t)netif->state;
netif->output = mchdrv_output;
return ERR_OK;
}
int mch_net_init(void)
{
struct ip_addr gw_addr, netmask;
struct mch_pci_dev * mchdrv_pcidev;
void * mchdrvnet_priv;
uint8_t mac_addr[6];
int err = -1;
int i;
// Hard-coded IP for my address, gateway and netmask
for (i = 0; i < NUM_INTERFACES; i++) {
if (mch_net_aton(MCH_IP_ADDRS[i], &mch_myip_addr[i]))
return -1;
}
if (mch_net_aton(MCH_IPADDR_GW, &gw_addr))
return -1;
if (mch_net_aton(MCH_IPADDR_NETMASK, &netmask))
return -1;
// Initialize LWIP
lwip_init();
// Add our netif to LWIP (netif_add calls our driver initialization function)
for (i = 0; i < NUM_INTERFACES; i++) {
if (netif_add(&mchdrv_netif[i], &mch_myip_addr[i], &netmask, &gw_addr,
(void *)(uint64_t)i, mchdrv_init, ip_input) == NULL) {
mch_printf("mch_net_init (%d): netif_add (mchdrv_init) failed\n", i);
return -1;
}
}
netif_set_default(&mchdrv_netif[0]);
netif_set_up(&mchdrv_netif[0]);
netif_set_up(&mchdrv_netif[1]);
// Initialize timer values
mch_timestamp_get(&ts_etharp);
mch_timestamp_get(&ts_tcp);
mch_timestamp_get(&ts_ipreass);
return 0;
}
//
// Regular polling mechanism. This should be called each time through
// the main application loop (after each interrupt, regardless of source).
//
// It handles any received packets, permits NIC device driver house-keeping
// and invokes timer-based TCP/IP functions (TCP retransmissions, delayed
// acks, IP reassembly timeouts, ARP timeouts, etc.)
//
void mch_net_poll(void)
{
mch_timestamp now;
// Call network interface to process incoming packets and do housekeeping
//mchdrv_poll(&mchdrv_netif);
// Process lwip network-related timers.
mch_timestamp_get(&now);
if (mch_timestamp_diff(&ts_etharp, &now) >= MCH_ARP_TIMER_INTERVAL) {
etharp_tmr();
ts_etharp = now;
}
if (mch_timestamp_diff(&ts_tcp, &now) >= MCH_TCP_TIMER_INTERVAL) {
tcp_tmr();
ts_tcp = now;
}
if (mch_timestamp_diff(&ts_ipreass, &now) >= MCH_IPREASS_TIMER_INTERVAL) {
ip_reass_tmr();
ts_ipreass = now;
}
}
//
// Convert address from string to internal format.
// Return 0 on success; else non-zero
//
int mch_net_aton(char * str_addr, struct ip_addr * net_addr)
{
struct in_addr a;
int i = inet_aton(str_addr, &net_addr->addr);
if (!i)
return -1;
return 0;
}
void test_udp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p,
ip_addr_t *addr, u16_t port)
{
printf("%s: len %d, from %s port %d\n", __func__, p->tot_len,
ipaddr_ntoa(addr), port);
/* it's our responsibility to dealloc the pbuf */
pbuf_free(p);
}
void test_udp()
{
struct udp_pcb *udp_src;
struct udp_pcb *udp_dst;
struct pbuf *p;
err_t err;
/* note: we don't bother freeing memory on failure */
printf("testing udp.\n");
printf("\tallocating pcbs.\n");
udp_src = udp_new();
if (udp_src == NULL) {
printf("%s: couldn't allocate src pcb\n", __func__);
return;
}
udp_dst = udp_new();
if (udp_dst == NULL) {
printf("%s: couldn't allocate dst pcb\n", __func__);
return;
}
/* bind the server side */
err = udp_bind(udp_dst, &mch_myip_addr[1], 2222);
if (err != ERR_OK) {
printf("%s: binding returned error %d\n", __func__, err);
return;
}
udp_recv(udp_dst, test_udp_recv, NULL);
/* make test pbuf */
p = pbuf_alloc(PBUF_TRANSPORT, 15, PBUF_RAM);
if (p == NULL) {
printf("%s: couldn't allocate pbuf\n", __func__);
return;
}
/* send the pbuf */
err = udp_sendto_if(udp_src, p, &mch_myip_addr[1], 2222, &mchdrv_netif[0]);
if (err != ERR_OK) {
printf("%s: udp_sendto_if returned %d\n", __func__, err);
return;
}
}
//
// Main entry point
//
int main(void)
{
mch_timestamp_init(); // Initialize timestamp generator
mch_net_init();
test_udp();
// while (1) {
// [snip other non-lwip functions]
// mch_wait_for_interrupt(); // Awakened by network, timer or other interrupt
// mch_net_poll(); // Poll network stack
// }
}