2 * Copyright (C) 2006 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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <gpxe/if_ether.h>
25 #include <gpxe/pkbuff.h>
26 #include <gpxe/tables.h>
27 #include <gpxe/process.h>
28 #include <gpxe/init.h>
29 #include <gpxe/netdevice.h>
33 * Network device management
37 /** Registered network-layer protocols */
38 static struct net_protocol net_protocols[0] __table_start ( net_protocols );
39 static struct net_protocol net_protocols_end[0] __table_end ( net_protocols );
41 /** List of network devices */
42 static LIST_HEAD ( net_devices );
45 * Transmit raw packet via network device
47 * @v netdev Network device
48 * @v pkb Packet buffer
49 * @ret rc Return status code
51 * Transmits the packet via the specified network device. This
52 * function takes ownership of the packet buffer.
54 int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb ) {
55 DBG ( "%s transmitting %p+%zx\n", netdev_name ( netdev ),
56 pkb->data, pkb_len ( pkb ) );
57 return netdev->transmit ( netdev, pkb );
61 * Add packet to receive queue
63 * @v netdev Network device
64 * @v pkb Packet buffer
66 * The packet is added to the network device's RX queue. This
67 * function takes ownership of the packet buffer.
69 void netdev_rx ( struct net_device *netdev, struct pk_buff *pkb ) {
70 DBG ( "%s received %p+%zx\n", netdev_name ( netdev ),
71 pkb->data, pkb_len ( pkb ) );
72 list_add_tail ( &pkb->list, &netdev->rx_queue );
76 * Transmit network-layer packet
78 * @v pkb Packet buffer
79 * @v netdev Network device
80 * @v net_protocol Network-layer protocol
81 * @v ll_dest Destination link-layer address
82 * @ret rc Return status code
84 * Prepends link-layer headers to the packet buffer and transmits the
85 * packet via the specified network device. This function takes
86 * ownership of the packet buffer.
88 int net_tx ( struct pk_buff *pkb, struct net_device *netdev,
89 struct net_protocol *net_protocol, const void *ll_dest ) {
90 return netdev->ll_protocol->tx ( pkb, netdev, net_protocol, ll_dest );
94 * Process received network-layer packet
96 * @v pkb Packet buffer
97 * @v netdev Network device
98 * @v net_proto Network-layer protocol, in network-byte order
99 * @v ll_source Source link-layer address
101 void net_rx ( struct pk_buff *pkb, struct net_device *netdev,
102 uint16_t net_proto, const void *ll_source ) {
103 struct net_protocol *net_protocol;
105 /* Hand off to network-layer protocol, if any */
106 for ( net_protocol = net_protocols ; net_protocol < net_protocols_end ;
108 if ( net_protocol->net_proto == net_proto ) {
109 net_protocol->rx ( pkb, netdev, ll_source );
116 * Poll for packet on network device
118 * @v netdev Network device
119 * @ret True There are packets present in the receive queue
120 * @ret False There are no packets present in the receive queue
122 * Polls the network device for received packets. Any received
123 * packets will be added to the RX packet queue via netdev_rx().
125 int netdev_poll ( struct net_device *netdev ) {
126 netdev->poll ( netdev );
127 return ( ! list_empty ( &netdev->rx_queue ) );
131 * Remove packet from device's receive queue
133 * @v netdev Network device
134 * @ret pkb Packet buffer, or NULL
136 * Removes the first packet from the device's RX queue and returns it.
137 * Ownership of the packet is transferred to the caller.
139 struct pk_buff * netdev_rx_dequeue ( struct net_device *netdev ) {
142 list_for_each_entry ( pkb, &netdev->rx_queue, list ) {
143 list_del ( &pkb->list );
150 * Allocate network device
152 * @v priv_size Size of private data area (net_device::priv)
153 * @ret netdev Network device, or NULL
155 * Allocates space for a network device and its private data area.
157 struct net_device * alloc_netdev ( size_t priv_size ) {
158 struct net_device *netdev;
160 netdev = calloc ( 1, sizeof ( *netdev ) + priv_size );
162 INIT_LIST_HEAD ( &netdev->rx_queue );
163 netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
169 * Register network device
171 * @v netdev Network device
172 * @ret rc Return status code
174 * Adds the network device to the list of network devices.
176 int register_netdev ( struct net_device *netdev ) {
178 /* Add to device list */
179 list_add_tail ( &netdev->list, &net_devices );
180 DBG ( "%s registered\n", netdev_name ( netdev ) );
186 * Unregister network device
188 * @v netdev Network device
190 * Removes the network device from the list of network devices.
192 void unregister_netdev ( struct net_device *netdev ) {
195 /* Discard any packets in the RX queue */
196 while ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) {
197 DBG ( "%s discarding %p+%zx\n", netdev_name ( netdev ),
198 pkb->data, pkb_len ( pkb ) );
202 /* Remove from device list */
203 list_del ( &netdev->list );
204 DBG ( "%s unregistered\n", netdev_name ( netdev ) );
208 * Free network device
210 * @v netdev Network device
212 void free_netdev ( struct net_device *netdev ) {
217 * Iterate through network devices
219 * @ret netdev Network device, or NULL
221 * This returns the registered network devices in the order of
222 * registration. If no network devices are registered, it will return
225 struct net_device * next_netdev ( void ) {
226 struct net_device *netdev;
228 list_for_each_entry ( netdev, &net_devices, list ) {
229 list_del ( &netdev->list );
230 list_add_tail ( &netdev->list, &net_devices );
237 * Single-step the network stack
239 * @v process Network stack process
241 * This polls all interfaces for any received packets, and processes
242 * at most one packet from the RX queue.
244 * We avoid processing all received packets, because processing the
245 * received packet can trigger transmission of a new packet (e.g. an
246 * ARP response). Since TX completions will be processed as part of
247 * the poll operation, it is easy to overflow small TX queues if
248 * multiple packets are processed per poll.
250 static void net_step ( struct process *process ) {
251 struct net_device *netdev;
254 /* Poll and process each network device */
255 list_for_each_entry ( netdev, &net_devices, list ) {
257 /* Poll for new packets */
258 netdev_poll ( netdev );
260 /* Handle at most one received packet per poll */
261 if ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) {
262 DBG ( "%s processing %p+%zx\n", netdev_name ( netdev ),
263 pkb->data, pkb_len ( pkb ) );
264 netdev->ll_protocol->rx ( pkb, netdev );
268 /* Re-schedule ourself */
269 schedule ( process );
272 /** Networking stack process */
273 static struct process net_process = {
277 /** Initialise the networking stack process */
278 static void init_net ( void ) {
279 schedule ( &net_process );
282 INIT_FN ( INIT_PROCESS, init_net, NULL, NULL );