STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
test_etharp.c
Go to the documentation of this file.
1 #include "test_etharp.h"
2 
3 #include "lwip/udp.h"
4 #include "netif/etharp.h"
5 #include "lwip/stats.h"
6 
7 #if !LWIP_STATS || !UDP_STATS || !MEMP_STATS || !ETHARP_STATS
8 #error "This tests needs UDP-, MEMP- and ETHARP-statistics enabled"
9 #endif
10 #if !ETHARP_SUPPORT_STATIC_ENTRIES
11 #error "This test needs ETHARP_SUPPORT_STATIC_ENTRIES enabled"
12 #endif
13 
14 static struct netif test_netif;
15 static ip4_addr_t test_ipaddr, test_netmask, test_gw;
16 struct eth_addr test_ethaddr = {{1,1,1,1,1,1}};
17 struct eth_addr test_ethaddr2 = {{1,1,1,1,1,2}};
18 struct eth_addr test_ethaddr3 = {{1,1,1,1,1,3}};
19 struct eth_addr test_ethaddr4 = {{1,1,1,1,1,4}};
20 static int linkoutput_ctr;
21 
22 /* Helper functions */
23 static void
24 etharp_remove_all(void)
25 {
26  int i;
27  /* call etharp_tmr often enough to have all entries cleaned */
28  for(i = 0; i < 0xff; i++) {
29  etharp_tmr();
30  }
31 }
32 
33 static err_t
34 default_netif_linkoutput(struct netif *netif, struct pbuf *p)
35 {
36  fail_unless(netif == &test_netif);
37  fail_unless(p != NULL);
38  linkoutput_ctr++;
39  return ERR_OK;
40 }
41 
42 static err_t
43 default_netif_init(struct netif *netif)
44 {
45  fail_unless(netif != NULL);
46  netif->linkoutput = default_netif_linkoutput;
47  netif->output = etharp_output;
48  netif->mtu = 1500;
50  netif->hwaddr_len = ETHARP_HWADDR_LEN;
51  return ERR_OK;
52 }
53 
54 static void
55 default_netif_add(void)
56 {
57  IP4_ADDR(&test_gw, 192,168,0,1);
58  IP4_ADDR(&test_ipaddr, 192,168,0,1);
59  IP4_ADDR(&test_netmask, 255,255,0,0);
60 
61  fail_unless(netif_default == NULL);
62  netif_set_default(netif_add(&test_netif, &test_ipaddr, &test_netmask,
63  &test_gw, NULL, default_netif_init, NULL));
64  netif_set_up(&test_netif);
65 }
66 
67 static void
68 default_netif_remove(void)
69 {
70  fail_unless(netif_default == &test_netif);
71  netif_remove(&test_netif);
72 }
73 
74 static void
75 create_arp_response(ip4_addr_t *adr)
76 {
77  int k;
78  struct eth_hdr *ethhdr;
79  struct etharp_hdr *etharphdr;
80  struct pbuf *p = pbuf_alloc(PBUF_RAW, sizeof(struct eth_hdr) + sizeof(struct etharp_hdr), PBUF_RAM);
81  if(p == NULL) {
82  FAIL_RET();
83  }
84  ethhdr = (struct eth_hdr*)p->payload;
85  etharphdr = (struct etharp_hdr*)(ethhdr + 1);
86 
87  ethhdr->dest = test_ethaddr;
88  ethhdr->src = test_ethaddr2;
89  ethhdr->type = htons(ETHTYPE_ARP);
90 
91  etharphdr->hwtype = htons(/*HWTYPE_ETHERNET*/ 1);
92  etharphdr->proto = htons(ETHTYPE_IP);
93  etharphdr->hwlen = ETHARP_HWADDR_LEN;
94  etharphdr->protolen = sizeof(ip4_addr_t);
95  etharphdr->opcode = htons(ARP_REPLY);
96 
97  SMEMCPY(&etharphdr->sipaddr, adr, sizeof(ip4_addr_t));
98  SMEMCPY(&etharphdr->dipaddr, &test_ipaddr, sizeof(ip4_addr_t));
99 
100  k = 6;
101  while(k > 0) {
102  k--;
103  /* Write the ARP MAC-Addresses */
104  etharphdr->shwaddr.addr[k] = test_ethaddr2.addr[k];
105  etharphdr->dhwaddr.addr[k] = test_ethaddr.addr[k];
106  /* Write the Ethernet MAC-Addresses */
107  ethhdr->dest.addr[k] = test_ethaddr.addr[k];
108  ethhdr->src.addr[k] = test_ethaddr2.addr[k];
109  }
110 
111  ethernet_input(p, &test_netif);
112 }
113 
114 /* Setups/teardown functions */
115 
116 static void
117 etharp_setup(void)
118 {
119  etharp_remove_all();
120  default_netif_add();
121 }
122 
123 static void
124 etharp_teardown(void)
125 {
126  etharp_remove_all();
127  default_netif_remove();
128 }
129 
130 
131 /* Test functions */
132 
133 START_TEST(test_etharp_table)
134 {
135 #if ETHARP_SUPPORT_STATIC_ENTRIES
136  err_t err;
137 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
138  s8_t idx;
139  const ip4_addr_t *unused_ipaddr;
140  struct eth_addr *unused_ethaddr;
141  struct udp_pcb* pcb;
142  LWIP_UNUSED_ARG(_i);
143 
144  if (netif_default != &test_netif) {
145  fail("This test needs a default netif");
146  }
147 
148  linkoutput_ctr = 0;
149 
150  pcb = udp_new();
151  fail_unless(pcb != NULL);
152  if (pcb != NULL) {
153  ip4_addr_t adrs[ARP_TABLE_SIZE + 2];
154  int i;
155  for(i = 0; i < ARP_TABLE_SIZE + 2; i++) {
156  IP4_ADDR(&adrs[i], 192,168,0,i+2);
157  }
158  /* fill ARP-table with dynamic entries */
159  for(i = 0; i < ARP_TABLE_SIZE; i++) {
160  struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
161  fail_unless(p != NULL);
162  if (p != NULL) {
163  err_t err;
164  ip_addr_t dst;
165  ip_addr_copy_from_ip4(dst, adrs[i]);
166  err = udp_sendto(pcb, p, &dst, 123);
167  fail_unless(err == ERR_OK);
168  /* etharp request sent? */
169  fail_unless(linkoutput_ctr == (2*i) + 1);
170  pbuf_free(p);
171 
172  /* create an ARP response */
173  create_arp_response(&adrs[i]);
174  /* queued UDP packet sent? */
175  fail_unless(linkoutput_ctr == (2*i) + 2);
176 
177  idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
178  fail_unless(idx == i);
179  etharp_tmr();
180  }
181  }
182  linkoutput_ctr = 0;
183 #if ETHARP_SUPPORT_STATIC_ENTRIES
184  /* create one static entry */
185  err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE], &test_ethaddr3);
186  fail_unless(err == ERR_OK);
187  idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
188  fail_unless(idx == 0);
189  fail_unless(linkoutput_ctr == 0);
190 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
191 
192  linkoutput_ctr = 0;
193  /* fill ARP-table with dynamic entries */
194  for(i = 0; i < ARP_TABLE_SIZE; i++) {
195  struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
196  fail_unless(p != NULL);
197  if (p != NULL) {
198  err_t err;
199  ip_addr_t dst;
200  ip_addr_copy_from_ip4(dst, adrs[i]);
201  err = udp_sendto(pcb, p, &dst, 123);
202  fail_unless(err == ERR_OK);
203  /* etharp request sent? */
204  fail_unless(linkoutput_ctr == (2*i) + 1);
205  pbuf_free(p);
206 
207  /* create an ARP response */
208  create_arp_response(&adrs[i]);
209  /* queued UDP packet sent? */
210  fail_unless(linkoutput_ctr == (2*i) + 2);
211 
212  idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
213  if (i < ARP_TABLE_SIZE - 1) {
214  fail_unless(idx == i+1);
215  } else {
216  /* the last entry must not overwrite the static entry! */
217  fail_unless(idx == 1);
218  }
219  etharp_tmr();
220  }
221  }
222 #if ETHARP_SUPPORT_STATIC_ENTRIES
223  /* create a second static entry */
224  err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE+1], &test_ethaddr4);
225  fail_unless(err == ERR_OK);
226  idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
227  fail_unless(idx == 0);
228  idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
229  fail_unless(idx == 2);
230  /* and remove it again */
231  err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE+1]);
232  fail_unless(err == ERR_OK);
233  idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
234  fail_unless(idx == 0);
235  idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
236  fail_unless(idx == -1);
237 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
238 
239  /* check that static entries don't time out */
240  etharp_remove_all();
241  idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
242  fail_unless(idx == 0);
243 
244 #if ETHARP_SUPPORT_STATIC_ENTRIES
245  /* remove the first static entry */
246  err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE]);
247  fail_unless(err == ERR_OK);
248  idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
249  fail_unless(idx == -1);
250  idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
251  fail_unless(idx == -1);
252 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
253 
254  udp_remove(pcb);
255  }
256 }
257 END_TEST
258 
259 
261 Suite *
263 {
264  testfunc tests[] = {
265  TESTFUNC(test_etharp_table)
266  };
267  return create_suite("ETHARP", tests, sizeof(tests)/sizeof(testfunc), etharp_setup, etharp_teardown);
268 }
u8_t flags
Definition: netif.h:269
#define TESTFUNC(x)
Definition: lwip_check.h:21
struct netif * netif_default
Definition: netif.c:85
uint32_t idx
Definition: lcd_log.c:247
signed char s8_t
Definition: cc.h:39
struct eth_addr test_ethaddr2
Definition: test_etharp.c:17
#define NETIF_FLAG_LINK_UP
Definition: netif.h:86
u16_t mtu
Definition: netif.h:263
#define htons(x)
Definition: def.h:86
void netif_set_default(struct netif *netif)
Definition: netif.c:511
#define SMEMCPY(dst, src, len)
Definition: opt.h:92
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:199
Definition: pbuf.h:77
void netif_set_up(struct netif *netif)
Definition: netif.c:530
netif_linkoutput_fn linkoutput
Definition: netif.h:211
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
Definition: pbuf.h:68
#define NETIF_FLAG_ETHARP
Definition: netif.h:90
#define ARP_TABLE_SIZE
Definition: opt.h:451
struct eth_addr test_ethaddr4
Definition: test_etharp.c:19
Suite * create_suite(const char *name, testfunc *tests, size_t num_tests, SFun setup, SFun teardown)
#define ERR_OK
Definition: err.h:52
Definition: pbuf.h:108
#define NETIF_FLAG_BROADCAST
Definition: netif.h:80
s8_t err_t
Definition: err.h:47
Definition: netif.h:182
void netif_remove(struct netif *netif)
Definition: netif.c:319
u8_t hwaddr_len
Definition: netif.h:265
struct eth_addr test_ethaddr3
Definition: test_etharp.c:18
ip6_addr_t ip_addr_t
Definition: ip_addr.h:194
struct netif * netif_add(struct netif *netif, void *state, netif_init_fn init, netif_input_fn input)
Definition: netif.c:184
END_TEST Suite * etharp_suite(void)
Definition: test_etharp.c:262
struct eth_addr test_ethaddr
Definition: test_etharp.c:16
START_TEST(test_etharp_table)
Definition: test_etharp.c:133
#define FAIL_RET()
Definition: lwip_check.h:10
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
void * payload
Definition: pbuf.h:113