Skip to content

Commit 37ccbd3

Browse files
committed
[neighbour] Add nstat() function to print out neighbour table
Signed-off-by: Michael Brown <mcb30@ipxe.org>
1 parent cba22d3 commit 37ccbd3

File tree

4 files changed

+115
-41
lines changed

4 files changed

+115
-41
lines changed

src/include/ipxe/neighbour.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
FILE_LICENCE ( GPL2_OR_LATER );
1111

12+
#include <stdint.h>
13+
#include <ipxe/refcnt.h>
14+
#include <ipxe/list.h>
1215
#include <ipxe/netdevice.h>
16+
#include <ipxe/retry.h>
1317

1418
/** A neighbour discovery protocol */
1519
struct neighbour_discovery {
@@ -29,6 +33,46 @@ struct neighbour_discovery {
2933
const void *net_dest, const void *net_source );
3034
};
3135

36+
/** A neighbour cache entry */
37+
struct neighbour {
38+
/** Reference count */
39+
struct refcnt refcnt;
40+
/** List of neighbour cache entries */
41+
struct list_head list;
42+
43+
/** Network device */
44+
struct net_device *netdev;
45+
/** Network-layer protocol */
46+
struct net_protocol *net_protocol;
47+
/** Network-layer destination address */
48+
uint8_t net_dest[MAX_NET_ADDR_LEN];
49+
/** Link-layer destination address */
50+
uint8_t ll_dest[MAX_LL_ADDR_LEN];
51+
52+
/** Neighbour discovery protocol (if any) */
53+
struct neighbour_discovery *discovery;
54+
/** Network-layer source address (if any) */
55+
uint8_t net_source[MAX_NET_ADDR_LEN];
56+
/** Retransmission timer */
57+
struct retry_timer timer;
58+
59+
/** Pending I/O buffers */
60+
struct list_head tx_queue;
61+
};
62+
63+
/**
64+
* Test if neighbour cache entry has a valid link-layer address
65+
*
66+
* @v neighbour Neighbour cache entry
67+
* @ret has_ll_dest Neighbour cache entry has a valid link-layer address
68+
*/
69+
static inline __attribute__ (( always_inline )) int
70+
neighbour_has_ll_dest ( struct neighbour *neighbour ) {
71+
return ( ! timer_running ( &neighbour->timer ) );
72+
}
73+
74+
extern struct list_head neighbours;
75+
3276
extern int neighbour_tx ( struct io_buffer *iobuf, struct net_device *netdev,
3377
struct net_protocol *net_protocol,
3478
const void *net_dest,

src/include/usr/neighmgmt.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef _USR_NEIGHMGMT_H
2+
#define _USR_NEIGHMGMT_H
3+
4+
/** @file
5+
*
6+
* Neighbour management
7+
*
8+
*/
9+
10+
FILE_LICENCE ( GPL2_OR_LATER );
11+
12+
extern void nstat ( void );
13+
14+
#endif /* _USR_NEIGHMGMT_H */

src/net/neighbour.c

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
2323
#include <stdlib.h>
2424
#include <string.h>
2525
#include <errno.h>
26-
#include <ipxe/refcnt.h>
27-
#include <ipxe/list.h>
2826
#include <ipxe/iobuf.h>
2927
#include <ipxe/retry.h>
3028
#include <ipxe/timer.h>
@@ -40,41 +38,14 @@ FILE_LICENCE ( GPL2_OR_LATER );
4038
*
4139
*/
4240

43-
/** A neighbour cache entry */
44-
struct neighbour {
45-
/** Reference count */
46-
struct refcnt refcnt;
47-
/** List of neighbour cache entries */
48-
struct list_head list;
49-
50-
/** Network device */
51-
struct net_device *netdev;
52-
/** Network-layer protocol */
53-
struct net_protocol *net_protocol;
54-
/** Network-layer destination address */
55-
uint8_t net_dest[MAX_NET_ADDR_LEN];
56-
/** Link-layer destination address */
57-
uint8_t ll_dest[MAX_LL_ADDR_LEN];
58-
59-
/** Neighbour discovery protocol (if any) */
60-
struct neighbour_discovery *discovery;
61-
/** Network-layer source address (if any) */
62-
uint8_t net_source[MAX_NET_ADDR_LEN];
63-
/** Retransmission timer */
64-
struct retry_timer timer;
65-
66-
/** Pending I/O buffers */
67-
struct list_head tx_queue;
68-
};
69-
7041
/** Neighbour discovery minimum timeout */
7142
#define NEIGHBOUR_MIN_TIMEOUT ( TICKS_PER_SEC / 8 )
7243

7344
/** Neighbour discovery maximum timeout */
7445
#define NEIGHBOUR_MAX_TIMEOUT ( TICKS_PER_SEC * 3 )
7546

7647
/** The neighbour cache */
77-
static LIST_HEAD ( neighbours );
48+
struct list_head neighbours = LIST_HEAD_INIT ( neighbours );
7849

7950
static void neighbour_expired ( struct retry_timer *timer, int over );
8051

@@ -97,17 +68,6 @@ static void neighbour_free ( struct refcnt *refcnt ) {
9768
free ( neighbour );
9869
}
9970

100-
/**
101-
* Test if neighbour cache entry has a valid link-layer address
102-
*
103-
* @v neighbour Neighbour cache entry
104-
* @ret has_ll_dest Neighbour cache entry has a valid link-layer address
105-
*/
106-
static inline __attribute__ (( always_inline )) int
107-
neighbour_has_ll_dest ( struct neighbour *neighbour ) {
108-
return ( ! timer_running ( &neighbour->timer ) );
109-
}
110-
11171
/**
11272
* Create neighbour cache entry
11373
*

src/usr/neighmgmt.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3+
*
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.
8+
*
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.
13+
*
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
17+
* 02110-1301, USA.
18+
*/
19+
20+
FILE_LICENCE ( GPL2_OR_LATER );
21+
22+
#include <stdio.h>
23+
#include <ipxe/neighbour.h>
24+
#include <usr/neighmgmt.h>
25+
26+
/** @file
27+
*
28+
* Neighbour management
29+
*
30+
*/
31+
32+
/**
33+
* Print neighbour table
34+
*
35+
*/
36+
void nstat ( void ) {
37+
struct neighbour *neighbour;
38+
struct net_device *netdev;
39+
struct ll_protocol *ll_protocol;
40+
struct net_protocol *net_protocol;
41+
42+
list_for_each_entry ( neighbour, &neighbours, list ) {
43+
netdev = neighbour->netdev;
44+
ll_protocol = netdev->ll_protocol;
45+
net_protocol = neighbour->net_protocol;
46+
printf ( "%s %s %s is %s %s", netdev->name, net_protocol->name,
47+
net_protocol->ntoa ( neighbour->net_dest ),
48+
ll_protocol->name,
49+
( neighbour_has_ll_dest ( neighbour ) ?
50+
ll_protocol->ntoa ( neighbour->ll_dest ) :
51+
"(incomplete)" ) );
52+
if ( neighbour->discovery )
53+
printf ( " (%s)", neighbour->discovery->name );
54+
printf ( "\n" );
55+
}
56+
}

0 commit comments

Comments
 (0)