summaryrefslogtreecommitdiffstats
path: root/src/net/netdevice.c
blob: 2733d237d28e146dfa9f324a0e6c7ebb45e77eeb (plain) (blame)
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
/*
 * Copyright (C) 2006 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdint.h>
#include <stdlib.h>
#include <byteswap.h>
#include <string.h>
#include <errno.h>
#include <vsprintf.h>
#include <gpxe/if_ether.h>
#include <gpxe/pkbuff.h>
#include <gpxe/tables.h>
#include <gpxe/process.h>
#include <gpxe/init.h>
#include <gpxe/device.h>
#include <gpxe/netdevice.h>

/** @file
 *
 * Network device management
 *
 */

/** Registered network-layer protocols */
static struct net_protocol net_protocols[0]
	__table_start ( struct net_protocol, net_protocols );
static struct net_protocol net_protocols_end[0]
	__table_end ( struct net_protocol, net_protocols );

/** List of network devices */
struct list_head net_devices = LIST_HEAD_INIT ( net_devices );

/**
 * Transmit raw packet via network device
 *
 * @v netdev		Network device
 * @v pkb		Packet buffer
 * @ret rc		Return status code
 *
 * Transmits the packet via the specified network device.  This
 * function takes ownership of the packet buffer.
 */
int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb ) {
	int rc;

	DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
	       netdev, pkb, pkb->data, pkb_len ( pkb ) );

	list_add_tail ( &pkb->list, &netdev->tx_queue );

	if ( ! ( netdev->state & NETDEV_OPEN ) ) {
		rc = -ENETUNREACH;
		goto err;
	}
		
	if ( ( rc = netdev->transmit ( netdev, pkb ) ) != 0 )
		goto err;

	return 0;

 err:
	DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
	       netdev, pkb, strerror ( rc ) );
	netdev_tx_complete ( netdev, pkb );
	return rc;
}

/**
 * Complete network transmission
 *
 * @v netdev		Network device
 * @v pkb		Packet buffer
 *
 * The packet must currently be in the network device's TX queue.
 */
void netdev_tx_complete ( struct net_device *netdev, struct pk_buff *pkb ) {
	DBGC ( netdev, "NETDEV %p transmission %p complete\n", netdev, pkb );

	/* Catch data corruption as early as possible */
	assert ( pkb->list.next != NULL );
	assert ( pkb->list.prev != NULL );

	list_del ( &pkb->list );
	free_pkb ( pkb );
}

/**
 * Complete network transmission
 *
 * @v netdev		Network device
 *
 * Completes the oldest outstanding packet in the TX queue.
 */
void netdev_tx_complete_next ( struct net_device *netdev ) {
	struct pk_buff *pkb;

	list_for_each_entry ( pkb, &netdev->tx_queue, list ) {
		netdev_tx_complete ( netdev, pkb );
		return;
	}
}

/**
 * Add packet to receive queue
 *
 * @v netdev		Network device
 * @v pkb		Packet buffer
 *
 * The packet is added to the network device's RX queue.  This
 * function takes ownership of the packet buffer.
 */
void netdev_rx ( struct net_device *netdev, struct pk_buff *pkb ) {
	DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
	       netdev, pkb, pkb->data, pkb_len ( pkb ) );
	list_add_tail ( &pkb->list, &netdev->rx_queue );
}

/**
 * Poll for packet on network device
 *
 * @v netdev		Network device
 * @v rx_quota		Maximum number of packets to receive
 * @ret True		There are packets present in the receive queue
 * @ret False		There are no packets present in the receive queue
 *
 * Polls the network device for received packets.  Any received
 * packets will be added to the RX packet queue via netdev_rx().
 */
int netdev_poll ( struct net_device *netdev, unsigned int rx_quota ) {

	if ( netdev->state & NETDEV_OPEN )
		netdev->poll ( netdev, rx_quota );

	return ( ! list_empty ( &netdev->rx_queue ) );
}

/**
 * Remove packet from device's receive queue
 *
 * @v netdev		Network device
 * @ret pkb		Packet buffer, or NULL
 *
 * Removes the first packet from the device's RX queue and returns it.
 * Ownership of the packet is transferred to the caller.
 */
struct pk_buff * netdev_rx_dequeue ( struct net_device *netdev ) {
	struct pk_buff *pkb;

	list_for_each_entry ( pkb, &netdev->rx_queue, list ) {
		list_del ( &pkb->list );
		return pkb;
	}
	return NULL;
}

/**
 * Allocate network device
 *
 * @v priv_size		Size of private data area (net_device::priv)
 * @ret netdev		Network device, or NULL
 *
 * Allocates space for a network device and its private data area.
 */
struct net_device * alloc_netdev ( size_t priv_size ) {
	struct net_device *netdev;
	size_t total_len;

	total_len = ( sizeof ( *netdev ) + priv_size );
	netdev = malloc ( total_len );
	if ( netdev ) {
		memset ( netdev, 0, total_len );
		INIT_LIST_HEAD ( &netdev->references );
		INIT_LIST_HEAD ( &netdev->tx_queue );
		INIT_LIST_HEAD ( &netdev->rx_queue );
		netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
	}
	return netdev;
}

/**
 * Register network device
 *
 * @v netdev		Network device
 * @ret rc		Return status code
 *
 * Gives the network device a name and adds it to the list of network
 * devices.
 */
int register_netdev ( struct net_device *netdev ) {
	static unsigned int ifindex = 0;

	/* Create device name */
	snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
		   ifindex++ );

	/* Add to device list */
	list_add_tail ( &netdev->list, &net_devices );
	DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
	       netdev, netdev->name, netdev->dev->name,
	       netdev_hwaddr ( netdev ) );

	return 0;
}

/**
 * Open network device
 *
 * @v netdev		Network device
 * @ret rc		Return status code
 */
int netdev_open ( struct net_device *netdev ) {
	int rc;

	/* Do nothing if device is already open */
	if ( netdev->state & NETDEV_OPEN )
		return 0;

	DBGC ( netdev, "NETDEV %p opening\n", netdev );

	/* Open the device */
	if ( ( rc = netdev->open ( netdev ) ) != 0 )
		return rc;

	/* Mark as opened */
	netdev->state |= NETDEV_OPEN;
	return 0;
}

/**
 * Close network device
 *
 * @v netdev		Network device
 */
void netdev_close ( struct net_device *netdev ) {
	struct pk_buff *pkb;

	/* Do nothing if device is already closed */
	if ( ! ( netdev->state & NETDEV_OPEN ) )
		return;

	DBGC ( netdev, "NETDEV %p closing\n", netdev );

	/* Close the device */
	netdev->close ( netdev );

	/* Discard any packets in the TX queue */
	while ( ! list_empty ( &netdev->tx_queue ) ) {
		netdev_tx_complete_next ( netdev );
	}

	/* Discard any packets in the RX queue */
	while ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) {
		DBGC ( netdev, "NETDEV %p discarding received %p\n",
		       netdev, pkb );
		free_pkb ( pkb );
	}

	/* Mark as closed */
	netdev->state &= ~NETDEV_OPEN;
}

/**
 * Unregister network device
 *
 * @v netdev		Network device
 *
 * Removes the network device from the list of network devices.
 */
void unregister_netdev ( struct net_device *netdev ) {

	/* Ensure device is closed */
	netdev_close ( netdev );

	/* Kill off any persistent references to this device */
	forget_references ( &netdev->references );

	/* Remove from device list */
	list_del ( &netdev->list );
	DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
}

/**
 * Free network device
 *
 * @v netdev		Network device
 */
void free_netdev ( struct net_device *netdev ) {
	free ( netdev );
}

/**
 * Get network device by name
 *
 * @v name		Network device name
 * @ret netdev		Network device, or NULL
 */
struct net_device * find_netdev ( const char *name ) {
	struct net_device *netdev;

	list_for_each_entry ( netdev, &net_devices, list ) {
		if ( strcmp ( netdev->name, name ) == 0 )
			return netdev;
	}

	return NULL;
}

/**
 * Get network device by PCI bus:dev.fn address
 *
 * @v busdevfn		PCI bus:dev.fn address
 * @ret netdev		Network device, or NULL
 */
struct net_device * find_pci_netdev ( unsigned int busdevfn ) {
	struct net_device *netdev;

	list_for_each_entry ( netdev, &net_devices, list ) {
		if ( ( netdev->dev->desc.bus_type == BUS_TYPE_PCI ) &&
		     ( netdev->dev->desc.pci.busdevfn == busdevfn ) )
			return netdev;
	}

	return NULL;	
}

/**
 * Transmit network-layer packet
 *
 * @v pkb		Packet buffer
 * @v netdev		Network device
 * @v net_protocol	Network-layer protocol
 * @v ll_dest		Destination link-layer address
 * @ret rc		Return status code
 *
 * Prepends link-layer headers to the packet buffer and transmits the
 * packet via the specified network device.  This function takes
 * ownership of the packet buffer.
 */
int net_tx ( struct pk_buff *pkb, struct net_device *netdev,
	     struct net_protocol *net_protocol, const void *ll_dest ) {
	return netdev->ll_protocol->tx ( pkb, netdev, net_protocol, ll_dest );
}

/**
 * Process received network-layer packet
 *
 * @v pkb		Packet buffer
 * @v netdev		Network device
 * @v net_proto		Network-layer protocol, in network-byte order
 * @v ll_source		Source link-layer address
 * @ret rc		Return status code
 */
int net_rx ( struct pk_buff *pkb, struct net_device *netdev,
	     uint16_t net_proto, const void *ll_source ) {
	struct net_protocol *net_protocol;

	/* Hand off to network-layer protocol, if any */
	for ( net_protocol = net_protocols ; net_protocol < net_protocols_end ;
	      net_protocol++ ) {
		if ( net_protocol->net_proto == net_proto ) {
			return net_protocol->rx ( pkb, netdev, ll_source );
		}
	}
	free_pkb ( pkb );
	return 0;
}

/**
 * Single-step the network stack
 *
 * @v process		Network stack process
 *
 * This polls all interfaces for received packets, and processes
 * packets from the RX queue.
 */
static void net_step ( struct process *process ) {
	struct net_device *netdev;
	struct pk_buff *pkb;

	/* Poll and process each network device */
	list_for_each_entry ( netdev, &net_devices, list ) {

		/* Poll for new packets */
		netdev_poll ( netdev, -1U );

		/* Process received packets */
		while ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) {
			DBGC ( netdev, "NETDEV %p processing %p\n",
			       netdev, pkb );
			netdev->ll_protocol->rx ( pkb, netdev );
		}
	}

	/* Re-schedule ourself */
	schedule ( process );
}

/** Networking stack process */
static struct process net_process = {
	.step = net_step,
};

/** Initialise the networking stack process */
static void init_net ( void ) {
	schedule ( &net_process );
}

INIT_FN ( INIT_PROCESS, init_net, NULL, NULL );