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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/*
* Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
FILE_LICENCE ( GPL2_OR_LATER );
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ipxe/iobuf.h>
#include <ipxe/retry.h>
#include <ipxe/timer.h>
#include <ipxe/malloc.h>
#include <ipxe/neighbour.h>
/** @file
*
* Neighbour discovery
*
* This file implements the abstract functions of neighbour discovery,
* independent of the underlying network protocol (e.g. ARP or NDP).
*
*/
/** Neighbour discovery minimum timeout */
#define NEIGHBOUR_MIN_TIMEOUT ( TICKS_PER_SEC / 8 )
/** Neighbour discovery maximum timeout */
#define NEIGHBOUR_MAX_TIMEOUT ( TICKS_PER_SEC * 3 )
/** The neighbour cache */
struct list_head neighbours = LIST_HEAD_INIT ( neighbours );
static void neighbour_expired ( struct retry_timer *timer, int over );
/**
* Free neighbour cache entry
*
* @v refcnt Reference count
*/
static void neighbour_free ( struct refcnt *refcnt ) {
struct neighbour *neighbour =
container_of ( refcnt, struct neighbour, refcnt );
/* Sanity check */
assert ( list_empty ( &neighbour->tx_queue ) );
/* Drop reference to network device */
netdev_put ( neighbour->netdev );
/* Free neighbour */
free ( neighbour );
}
/**
* Create neighbour cache entry
*
* @v netdev Network device
* @v net_protocol Network-layer protocol
* @v net_dest Destination network-layer address
* @ret neighbour Neighbour cache entry, or NULL if allocation failed
*/
static struct neighbour * neighbour_create ( struct net_device *netdev,
struct net_protocol *net_protocol,
const void *net_dest ) {
struct neighbour *neighbour;
/* Allocate and initialise entry */
neighbour = zalloc ( sizeof ( *neighbour ) );
if ( ! neighbour )
return NULL;
ref_init ( &neighbour->refcnt, neighbour_free );
neighbour->netdev = netdev_get ( netdev );
neighbour->net_protocol = net_protocol;
memcpy ( neighbour->net_dest, net_dest,
net_protocol->net_addr_len );
timer_init ( &neighbour->timer, neighbour_expired, &neighbour->refcnt );
neighbour->timer.min_timeout = NEIGHBOUR_MIN_TIMEOUT;
neighbour->timer.max_timeout = NEIGHBOUR_MAX_TIMEOUT;
INIT_LIST_HEAD ( &neighbour->tx_queue );
/* Transfer ownership to cache */
list_add ( &neighbour->list, &neighbours );
DBGC ( neighbour, "NEIGHBOUR %s %s %s created\n", netdev->name,
net_protocol->name, net_protocol->ntoa ( net_dest ) );
return neighbour;
}
/**
* Find neighbour cache entry
*
* @v netdev Network device
* @v net_protocol Network-layer protocol
* @v net_dest Destination network-layer address
* @ret neighbour Neighbour cache entry, or NULL if not found
*/
static struct neighbour * neighbour_find ( struct net_device *netdev,
struct net_protocol *net_protocol,
const void *net_dest ) {
struct neighbour *neighbour;
list_for_each_entry ( neighbour, &neighbours, list ) {
if ( ( neighbour->netdev == netdev ) &&
( neighbour->net_protocol == net_protocol ) &&
( memcmp ( neighbour->net_dest, net_dest,
net_protocol->net_addr_len ) == 0 ) ) {
/* Move to start of cache */
list_del ( &neighbour->list );
list_add ( &neighbour->list, &neighbours );
return neighbour;
}
}
return NULL;
}
/**
* Start neighbour discovery
*
* @v neighbour Neighbour cache entry
* @v discovery Neighbour discovery protocol
* @v net_source Source network-layer address
*/
static void neighbour_discover ( struct neighbour *neighbour,
struct neighbour_discovery *discovery,
const void *net_source ) {
struct net_device *netdev = neighbour->netdev;
struct net_protocol *net_protocol = neighbour->net_protocol;
/* Record discovery protocol and source network-layer address */
neighbour->discovery = discovery;
memcpy ( neighbour->net_source, net_source,
net_protocol->net_addr_len );
/* Start timer to trigger neighbour discovery */
start_timer_nodelay ( &neighbour->timer );
DBGC ( neighbour, "NEIGHBOUR %s %s %s discovering via %s\n",
netdev->name, net_protocol->name,
net_protocol->ntoa ( neighbour->net_dest ),
neighbour->discovery->name );
}
/**
* Complete neighbour discovery
*
* @v neighbour Neighbour cache entry
* @v ll_dest Destination link-layer address
*/
static void neighbour_discovered ( struct neighbour *neighbour,
const void *ll_dest ) {
struct net_device *netdev = neighbour->netdev;
struct ll_protocol *ll_protocol = netdev->ll_protocol;
struct net_protocol *net_protocol = neighbour->net_protocol;
struct io_buffer *iobuf;
int rc;
/* Fill in link-layer address */
memcpy ( neighbour->ll_dest, ll_dest, ll_protocol->ll_addr_len );
DBGC ( neighbour, "NEIGHBOUR %s %s %s is %s %s\n", netdev->name,
net_protocol->name, net_protocol->ntoa ( neighbour->net_dest ),
ll_protocol->name, ll_protocol->ntoa ( neighbour->ll_dest ) );
/* Stop retransmission timer */
stop_timer ( &neighbour->timer );
/* Transmit any packets in queue. Take out a temporary
* reference on the entry to prevent it from going out of
* scope during the call to net_tx().
*/
ref_get ( &neighbour->refcnt );
while ( ( iobuf = list_first_entry ( &neighbour->tx_queue,
struct io_buffer, list )) != NULL){
DBGC2 ( neighbour, "NEIGHBOUR %s %s %s transmitting deferred "
"packet\n", netdev->name, net_protocol->name,
net_protocol->ntoa ( neighbour->net_dest ) );
list_del ( &iobuf->list );
if ( ( rc = net_tx ( iobuf, netdev, net_protocol, ll_dest,
netdev->ll_addr ) ) != 0 ) {
DBGC ( neighbour, "NEIGHBOUR %s %s %s could not "
"transmit deferred packet: %s\n",
netdev->name, net_protocol->name,
net_protocol->ntoa ( neighbour->net_dest ),
strerror ( rc ) );
/* Ignore error and continue */
}
}
ref_put ( &neighbour->refcnt );
}
/**
* Destroy neighbour cache entry
*
* @v neighbour Neighbour cache entry
* @v rc Reason for destruction
*/
static void neighbour_destroy ( struct neighbour *neighbour, int rc ) {
struct net_device *netdev = neighbour->netdev;
struct net_protocol *net_protocol = neighbour->net_protocol;
struct io_buffer *iobuf;
/* Take ownership from cache */
list_del ( &neighbour->list );
/* Stop timer */
stop_timer ( &neighbour->timer );
/* Discard any outstanding I/O buffers */
while ( ( iobuf = list_first_entry ( &neighbour->tx_queue,
struct io_buffer, list )) != NULL){
DBGC2 ( neighbour, "NEIGHBOUR %s %s %s discarding deferred "
"packet: %s\n", netdev->name, net_protocol->name,
net_protocol->ntoa ( neighbour->net_dest ),
strerror ( rc ) );
list_del ( &iobuf->list );
netdev_tx_err ( neighbour->netdev, iobuf, rc );
}
DBGC ( neighbour, "NEIGHBOUR %s %s %s destroyed: %s\n", netdev->name,
net_protocol->name, net_protocol->ntoa ( neighbour->net_dest ),
strerror ( rc ) );
/* Drop remaining reference */
ref_put ( &neighbour->refcnt );
}
/**
* Handle neighbour timer expiry
*
* @v timer Retry timer
* @v fail Failure indicator
*/
static void neighbour_expired ( struct retry_timer *timer, int fail ) {
struct neighbour *neighbour =
container_of ( timer, struct neighbour, timer );
struct net_device *netdev = neighbour->netdev;
struct net_protocol *net_protocol = neighbour->net_protocol;
struct neighbour_discovery *discovery =
neighbour->discovery;
const void *net_dest = neighbour->net_dest;
const void *net_source = neighbour->net_source;
int rc;
/* If we have failed, destroy the cache entry */
if ( fail ) {
neighbour_destroy ( neighbour, -ETIMEDOUT );
return;
}
/* Restart the timer */
start_timer ( &neighbour->timer );
/* Transmit neighbour request */
if ( ( rc = discovery->tx_request ( netdev, net_protocol, net_dest,
net_source ) ) != 0 ) {
DBGC ( neighbour, "NEIGHBOUR %s %s %s could not transmit %s "
"request: %s\n", netdev->name, net_protocol->name,
net_protocol->ntoa ( neighbour->net_dest ),
neighbour->discovery->name, strerror ( rc ) );
/* Retransmit when timer expires */
return;
}
}
/**
* Transmit packet, determining link-layer address via neighbour discovery
*
* @v iobuf I/O buffer
* @v netdev Network device
* @v discovery Neighbour discovery protocol
* @v net_protocol Network-layer protocol
* @v net_dest Destination network-layer address
* @v net_source Source network-layer address
* @v ll_source Source link-layer address
* @ret rc Return status code
*/
int neighbour_tx ( struct io_buffer *iobuf, struct net_device *netdev,
struct net_protocol *net_protocol, const void *net_dest,
struct neighbour_discovery *discovery,
const void *net_source, const void *ll_source ) {
struct neighbour *neighbour;
/* Find or create neighbour cache entry */
neighbour = neighbour_find ( netdev, net_protocol, net_dest );
if ( ! neighbour ) {
neighbour = neighbour_create ( netdev, net_protocol, net_dest );
if ( ! neighbour )
return -ENOMEM;
neighbour_discover ( neighbour, discovery, net_source );
}
/* If a link-layer address is available then transmit
* immediately, otherwise queue for later transmission.
*/
if ( neighbour_has_ll_dest ( neighbour ) ) {
return net_tx ( iobuf, netdev, net_protocol, neighbour->ll_dest,
ll_source );
} else {
DBGC2 ( neighbour, "NEIGHBOUR %s %s %s deferring packet\n",
netdev->name, net_protocol->name,
net_protocol->ntoa ( net_dest ) );
list_add_tail ( &iobuf->list, &neighbour->tx_queue );
return -EAGAIN;
}
}
/**
* Update existing neighbour cache entry
*
* @v netdev Network device
* @v net_protocol Network-layer protocol
* @v net_dest Destination network-layer address
* @v ll_dest Destination link-layer address
* @ret rc Return status code
*/
int neighbour_update ( struct net_device *netdev,
struct net_protocol *net_protocol,
const void *net_dest, const void *ll_dest ) {
struct neighbour *neighbour;
/* Find neighbour cache entry */
neighbour = neighbour_find ( netdev, net_protocol, net_dest );
if ( ! neighbour )
return -ENOENT;
/* Set destination address */
neighbour_discovered ( neighbour, ll_dest );
return 0;
}
/**
* Define neighbour cache entry
*
* @v netdev Network device
* @v net_protocol Network-layer protocol
* @v net_dest Destination network-layer address
* @v ll_dest Destination link-layer address, if known
* @ret rc Return status code
*/
int neighbour_define ( struct net_device *netdev,
struct net_protocol *net_protocol,
const void *net_dest, const void *ll_dest ) {
struct neighbour *neighbour;
/* Find or create neighbour cache entry */
neighbour = neighbour_find ( netdev, net_protocol, net_dest );
if ( ! neighbour ) {
neighbour = neighbour_create ( netdev, net_protocol, net_dest );
if ( ! neighbour )
return -ENOMEM;
}
/* Set destination address */
neighbour_discovered ( neighbour, ll_dest );
return 0;
}
/**
* Update neighbour cache on network device state change or removal
*
* @v netdev Network device
*/
static void neighbour_flush ( struct net_device *netdev ) {
struct neighbour *neighbour;
struct neighbour *tmp;
/* Remove all neighbour cache entries when a network device is closed */
if ( ! netdev_is_open ( netdev ) ) {
list_for_each_entry_safe ( neighbour, tmp, &neighbours, list )
neighbour_destroy ( neighbour, -ENODEV );
}
}
/** Neighbour driver (for net device notifications) */
struct net_driver neighbour_net_driver __net_driver = {
.name = "Neighbour",
.notify = neighbour_flush,
.remove = neighbour_flush,
};
/**
* Discard some cached neighbour entries
*
* @ret discarded Number of cached items discarded
*/
static unsigned int neighbour_discard ( void ) {
struct neighbour *neighbour;
/* Drop oldest cache entry, if any */
neighbour = list_last_entry ( &neighbours, struct neighbour, list );
if ( neighbour ) {
neighbour_destroy ( neighbour, -ENOBUFS );
return 1;
} else {
return 0;
}
}
/**
* Neighbour cache discarder
*
* Neighbour cache entries are deemed to have a high replacement cost,
* since flushing an active neighbour cache entry midway through a TCP
* transfer will cause substantial disruption.
*/
struct cache_discarder neighbour_discarder __cache_discarder (CACHE_EXPENSIVE)={
.discard = neighbour_discard,
};
|