2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/iobuf.h>
27 #include <ipxe/retry.h>
28 #include <ipxe/timer.h>
29 #include <ipxe/malloc.h>
30 #include <ipxe/neighbour.h>
36 * This file implements the abstract functions of neighbour discovery,
37 * independent of the underlying network protocol (e.g. ARP or NDP).
41 /** Neighbour discovery minimum timeout */
42 #define NEIGHBOUR_MIN_TIMEOUT ( TICKS_PER_SEC / 8 )
44 /** Neighbour discovery maximum timeout */
45 #define NEIGHBOUR_MAX_TIMEOUT ( TICKS_PER_SEC * 3 )
47 /** The neighbour cache */
48 struct list_head neighbours = LIST_HEAD_INIT ( neighbours );
50 static void neighbour_expired ( struct retry_timer *timer, int over );
53 * Free neighbour cache entry
55 * @v refcnt Reference count
57 static void neighbour_free ( struct refcnt *refcnt ) {
58 struct neighbour *neighbour =
59 container_of ( refcnt, struct neighbour, refcnt );
62 assert ( list_empty ( &neighbour->tx_queue ) );
64 /* Drop reference to network device */
65 netdev_put ( neighbour->netdev );
72 * Create neighbour cache entry
74 * @v netdev Network device
75 * @v net_protocol Network-layer protocol
76 * @v net_dest Destination network-layer address
77 * @ret neighbour Neighbour cache entry, or NULL if allocation failed
79 static struct neighbour * neighbour_create ( struct net_device *netdev,
80 struct net_protocol *net_protocol,
81 const void *net_dest ) {
82 struct neighbour *neighbour;
84 /* Allocate and initialise entry */
85 neighbour = zalloc ( sizeof ( *neighbour ) );
88 ref_init ( &neighbour->refcnt, neighbour_free );
89 neighbour->netdev = netdev_get ( netdev );
90 neighbour->net_protocol = net_protocol;
91 memcpy ( neighbour->net_dest, net_dest,
92 net_protocol->net_addr_len );
93 timer_init ( &neighbour->timer, neighbour_expired, &neighbour->refcnt );
94 neighbour->timer.min_timeout = NEIGHBOUR_MIN_TIMEOUT;
95 neighbour->timer.max_timeout = NEIGHBOUR_MAX_TIMEOUT;
96 INIT_LIST_HEAD ( &neighbour->tx_queue );
98 /* Transfer ownership to cache */
99 list_add ( &neighbour->list, &neighbours );
101 DBGC ( neighbour, "NEIGHBOUR %s %s %s created\n", netdev->name,
102 net_protocol->name, net_protocol->ntoa ( net_dest ) );
107 * Find neighbour cache entry
109 * @v netdev Network device
110 * @v net_protocol Network-layer protocol
111 * @v net_dest Destination network-layer address
112 * @ret neighbour Neighbour cache entry, or NULL if not found
114 static struct neighbour * neighbour_find ( struct net_device *netdev,
115 struct net_protocol *net_protocol,
116 const void *net_dest ) {
117 struct neighbour *neighbour;
119 list_for_each_entry ( neighbour, &neighbours, list ) {
120 if ( ( neighbour->netdev == netdev ) &&
121 ( neighbour->net_protocol == net_protocol ) &&
122 ( memcmp ( neighbour->net_dest, net_dest,
123 net_protocol->net_addr_len ) == 0 ) ) {
125 /* Move to start of cache */
126 list_del ( &neighbour->list );
127 list_add ( &neighbour->list, &neighbours );
136 * Start neighbour discovery
138 * @v neighbour Neighbour cache entry
139 * @v discovery Neighbour discovery protocol
140 * @v net_source Source network-layer address
142 static void neighbour_discover ( struct neighbour *neighbour,
143 struct neighbour_discovery *discovery,
144 const void *net_source ) {
145 struct net_device *netdev = neighbour->netdev;
146 struct net_protocol *net_protocol = neighbour->net_protocol;
148 /* Record discovery protocol and source network-layer address */
149 neighbour->discovery = discovery;
150 memcpy ( neighbour->net_source, net_source,
151 net_protocol->net_addr_len );
153 /* Start timer to trigger neighbour discovery */
154 start_timer_nodelay ( &neighbour->timer );
156 DBGC ( neighbour, "NEIGHBOUR %s %s %s discovering via %s\n",
157 netdev->name, net_protocol->name,
158 net_protocol->ntoa ( neighbour->net_dest ),
159 neighbour->discovery->name );
163 * Complete neighbour discovery
165 * @v neighbour Neighbour cache entry
166 * @v ll_dest Destination link-layer address
168 static void neighbour_discovered ( struct neighbour *neighbour,
169 const void *ll_dest ) {
170 struct net_device *netdev = neighbour->netdev;
171 struct ll_protocol *ll_protocol = netdev->ll_protocol;
172 struct net_protocol *net_protocol = neighbour->net_protocol;
173 struct io_buffer *iobuf;
176 /* Fill in link-layer address */
177 memcpy ( neighbour->ll_dest, ll_dest, ll_protocol->ll_addr_len );
178 DBGC ( neighbour, "NEIGHBOUR %s %s %s is %s %s\n", netdev->name,
179 net_protocol->name, net_protocol->ntoa ( neighbour->net_dest ),
180 ll_protocol->name, ll_protocol->ntoa ( neighbour->ll_dest ) );
182 /* Stop retransmission timer */
183 stop_timer ( &neighbour->timer );
185 /* Transmit any packets in queue. Take out a temporary
186 * reference on the entry to prevent it from going out of
187 * scope during the call to net_tx().
189 ref_get ( &neighbour->refcnt );
190 while ( ( iobuf = list_first_entry ( &neighbour->tx_queue,
191 struct io_buffer, list )) != NULL){
192 DBGC2 ( neighbour, "NEIGHBOUR %s %s %s transmitting deferred "
193 "packet\n", netdev->name, net_protocol->name,
194 net_protocol->ntoa ( neighbour->net_dest ) );
195 list_del ( &iobuf->list );
196 if ( ( rc = net_tx ( iobuf, netdev, net_protocol, ll_dest,
197 netdev->ll_addr ) ) != 0 ) {
198 DBGC ( neighbour, "NEIGHBOUR %s %s %s could not "
199 "transmit deferred packet: %s\n",
200 netdev->name, net_protocol->name,
201 net_protocol->ntoa ( neighbour->net_dest ),
203 /* Ignore error and continue */
206 ref_put ( &neighbour->refcnt );
210 * Destroy neighbour cache entry
212 * @v neighbour Neighbour cache entry
213 * @v rc Reason for destruction
215 static void neighbour_destroy ( struct neighbour *neighbour, int rc ) {
216 struct net_device *netdev = neighbour->netdev;
217 struct net_protocol *net_protocol = neighbour->net_protocol;
218 struct io_buffer *iobuf;
220 /* Take ownership from cache */
221 list_del ( &neighbour->list );
224 stop_timer ( &neighbour->timer );
226 /* Discard any outstanding I/O buffers */
227 while ( ( iobuf = list_first_entry ( &neighbour->tx_queue,
228 struct io_buffer, list )) != NULL){
229 DBGC2 ( neighbour, "NEIGHBOUR %s %s %s discarding deferred "
230 "packet: %s\n", netdev->name, net_protocol->name,
231 net_protocol->ntoa ( neighbour->net_dest ),
233 list_del ( &iobuf->list );
234 netdev_tx_err ( neighbour->netdev, iobuf, rc );
237 DBGC ( neighbour, "NEIGHBOUR %s %s %s destroyed: %s\n", netdev->name,
238 net_protocol->name, net_protocol->ntoa ( neighbour->net_dest ),
241 /* Drop remaining reference */
242 ref_put ( &neighbour->refcnt );
246 * Handle neighbour timer expiry
248 * @v timer Retry timer
249 * @v fail Failure indicator
251 static void neighbour_expired ( struct retry_timer *timer, int fail ) {
252 struct neighbour *neighbour =
253 container_of ( timer, struct neighbour, timer );
254 struct net_device *netdev = neighbour->netdev;
255 struct net_protocol *net_protocol = neighbour->net_protocol;
256 struct neighbour_discovery *discovery =
257 neighbour->discovery;
258 const void *net_dest = neighbour->net_dest;
259 const void *net_source = neighbour->net_source;
262 /* If we have failed, destroy the cache entry */
264 neighbour_destroy ( neighbour, -ETIMEDOUT );
268 /* Restart the timer */
269 start_timer ( &neighbour->timer );
271 /* Transmit neighbour request */
272 if ( ( rc = discovery->tx_request ( netdev, net_protocol, net_dest,
273 net_source ) ) != 0 ) {
274 DBGC ( neighbour, "NEIGHBOUR %s %s %s could not transmit %s "
275 "request: %s\n", netdev->name, net_protocol->name,
276 net_protocol->ntoa ( neighbour->net_dest ),
277 neighbour->discovery->name, strerror ( rc ) );
278 /* Retransmit when timer expires */
284 * Transmit packet, determining link-layer address via neighbour discovery
286 * @v iobuf I/O buffer
287 * @v netdev Network device
288 * @v discovery Neighbour discovery protocol
289 * @v net_protocol Network-layer protocol
290 * @v net_dest Destination network-layer address
291 * @v net_source Source network-layer address
292 * @v ll_source Source link-layer address
293 * @ret rc Return status code
295 int neighbour_tx ( struct io_buffer *iobuf, struct net_device *netdev,
296 struct net_protocol *net_protocol, const void *net_dest,
297 struct neighbour_discovery *discovery,
298 const void *net_source, const void *ll_source ) {
299 struct neighbour *neighbour;
301 /* Find or create neighbour cache entry */
302 neighbour = neighbour_find ( netdev, net_protocol, net_dest );
304 neighbour = neighbour_create ( netdev, net_protocol, net_dest );
307 neighbour_discover ( neighbour, discovery, net_source );
310 /* If a link-layer address is available then transmit
311 * immediately, otherwise queue for later transmission.
313 if ( neighbour_has_ll_dest ( neighbour ) ) {
314 return net_tx ( iobuf, netdev, net_protocol, neighbour->ll_dest,
317 DBGC2 ( neighbour, "NEIGHBOUR %s %s %s deferring packet\n",
318 netdev->name, net_protocol->name,
319 net_protocol->ntoa ( net_dest ) );
320 list_add_tail ( &iobuf->list, &neighbour->tx_queue );
326 * Update existing neighbour cache entry
328 * @v netdev Network device
329 * @v net_protocol Network-layer protocol
330 * @v net_dest Destination network-layer address
331 * @v ll_dest Destination link-layer address
332 * @ret rc Return status code
334 int neighbour_update ( struct net_device *netdev,
335 struct net_protocol *net_protocol,
336 const void *net_dest, const void *ll_dest ) {
337 struct neighbour *neighbour;
339 /* Find neighbour cache entry */
340 neighbour = neighbour_find ( netdev, net_protocol, net_dest );
344 /* Set destination address */
345 neighbour_discovered ( neighbour, ll_dest );
351 * Define neighbour cache entry
353 * @v netdev Network device
354 * @v net_protocol Network-layer protocol
355 * @v net_dest Destination network-layer address
356 * @v ll_dest Destination link-layer address, if known
357 * @ret rc Return status code
359 int neighbour_define ( struct net_device *netdev,
360 struct net_protocol *net_protocol,
361 const void *net_dest, const void *ll_dest ) {
362 struct neighbour *neighbour;
364 /* Find or create neighbour cache entry */
365 neighbour = neighbour_find ( netdev, net_protocol, net_dest );
367 neighbour = neighbour_create ( netdev, net_protocol, net_dest );
372 /* Set destination address */
373 neighbour_discovered ( neighbour, ll_dest );
379 * Update neighbour cache on network device state change or removal
381 * @v netdev Network device
383 static void neighbour_flush ( struct net_device *netdev ) {
384 struct neighbour *neighbour;
385 struct neighbour *tmp;
387 /* Remove all neighbour cache entries when a network device is closed */
388 if ( ! netdev_is_open ( netdev ) ) {
389 list_for_each_entry_safe ( neighbour, tmp, &neighbours, list )
390 neighbour_destroy ( neighbour, -ENODEV );
394 /** Neighbour driver (for net device notifications) */
395 struct net_driver neighbour_net_driver __net_driver = {
397 .notify = neighbour_flush,
398 .remove = neighbour_flush,
402 * Discard some cached neighbour entries
404 * @ret discarded Number of cached items discarded
406 static unsigned int neighbour_discard ( void ) {
407 struct neighbour *neighbour;
409 /* Drop oldest cache entry, if any */
410 neighbour = list_last_entry ( &neighbours, struct neighbour, list );
412 neighbour_destroy ( neighbour, -ENOBUFS );
420 * Neighbour cache discarder
422 * Neighbour cache entries are deemed to have a high replacement cost,
423 * since flushing an active neighbour cache entry midway through a TCP
424 * transfer will cause substantial disruption.
426 struct cache_discarder neighbour_discarder __cache_discarder (CACHE_EXPENSIVE)={
427 .discard = neighbour_discard,