Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / net / netdevice.c
1 /*
2  * Copyright (C) 2006 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 <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <byteswap.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <config/general.h>
29 #include <ipxe/if_ether.h>
30 #include <ipxe/iobuf.h>
31 #include <ipxe/tables.h>
32 #include <ipxe/process.h>
33 #include <ipxe/init.h>
34 #include <ipxe/malloc.h>
35 #include <ipxe/device.h>
36 #include <ipxe/errortab.h>
37 #include <ipxe/profile.h>
38 #include <ipxe/vlan.h>
39 #include <ipxe/netdevice.h>
40
41 /** @file
42  *
43  * Network device management
44  *
45  */
46
47 /** List of network devices */
48 struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
49
50 /** List of open network devices, in reverse order of opening */
51 static struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
52
53 /** Network device index */
54 static unsigned int netdev_index = 0;
55
56 /** Network polling profiler */
57 static struct profiler net_poll_profiler __profiler = { .name = "net.poll" };
58
59 /** Network receive profiler */
60 static struct profiler net_rx_profiler __profiler = { .name = "net.rx" };
61
62 /** Network transmit profiler */
63 static struct profiler net_tx_profiler __profiler = { .name = "net.tx" };
64
65 /** Default unknown link status code */
66 #define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
67 #define EINFO_EUNKNOWN_LINK_STATUS \
68         __einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )
69
70 /** Default not-yet-attempted-configuration status code */
71 #define EUNUSED_CONFIG __einfo_error ( EINFO_EUNUSED_CONFIG )
72 #define EINFO_EUNUSED_CONFIG \
73         __einfo_uniqify ( EINFO_EINPROGRESS, 0x02, "Unused" )
74
75 /** Default configuration-in-progress status code */
76 #define EINPROGRESS_CONFIG __einfo_error ( EINFO_EINPROGRESS_CONFIG )
77 #define EINFO_EINPROGRESS_CONFIG \
78         __einfo_uniqify ( EINFO_EINPROGRESS, 0x03, "Incomplete" )
79
80 /** Default link-down status code */
81 #define ENOTCONN_LINK_DOWN __einfo_error ( EINFO_ENOTCONN_LINK_DOWN )
82 #define EINFO_ENOTCONN_LINK_DOWN \
83         __einfo_uniqify ( EINFO_ENOTCONN, 0x01, "Down" )
84
85 /** Human-readable message for the default link statuses */
86 struct errortab netdev_errors[] __errortab = {
87         __einfo_errortab ( EINFO_EUNKNOWN_LINK_STATUS ),
88         __einfo_errortab ( EINFO_ENOTCONN_LINK_DOWN ),
89         __einfo_errortab ( EINFO_EUNUSED_CONFIG ),
90         __einfo_errortab ( EINFO_EINPROGRESS_CONFIG ),
91 };
92
93 /**
94  * Check whether or not network device has a link-layer address
95  *
96  * @v netdev            Network device
97  * @ret has_ll_addr     Network device has a link-layer address
98  */
99 static int netdev_has_ll_addr ( struct net_device *netdev ) {
100         uint8_t *ll_addr = netdev->ll_addr;
101         size_t remaining = sizeof ( netdev->ll_addr );
102
103         while ( remaining-- ) {
104                 if ( *(ll_addr++) != 0 )
105                         return 1;
106         }
107         return 0;
108 }
109
110 /**
111  * Notify drivers of network device or link state change
112  *
113  * @v netdev            Network device
114  */
115 static void netdev_notify ( struct net_device *netdev ) {
116         struct net_driver *driver;
117
118         for_each_table_entry ( driver, NET_DRIVERS ) {
119                 if ( driver->notify )
120                         driver->notify ( netdev );
121         }
122 }
123
124 /**
125  * Freeze network device receive queue processing
126  *
127  * @v netdev            Network device
128  */
129 void netdev_rx_freeze ( struct net_device *netdev ) {
130
131         /* Mark receive queue processing as frozen */
132         netdev->state |= NETDEV_RX_FROZEN;
133
134         /* Notify drivers of change */
135         netdev_notify ( netdev );
136 }
137
138 /**
139  * Unfreeze network device receive queue processing
140  *
141  * @v netdev            Network device
142  */
143 void netdev_rx_unfreeze ( struct net_device *netdev ) {
144
145         /* Mark receive queue processing as not frozen */
146         netdev->state &= ~NETDEV_RX_FROZEN;
147
148         /* Notify drivers of change */
149         netdev_notify ( netdev );
150 }
151
152 /**
153  * Mark network device as having a specific link state
154  *
155  * @v netdev            Network device
156  * @v rc                Link status code
157  */
158 void netdev_link_err ( struct net_device *netdev, int rc ) {
159
160         /* Record link state */
161         netdev->link_rc = rc;
162         if ( netdev->link_rc == 0 ) {
163                 DBGC ( netdev, "NETDEV %s link is up\n", netdev->name );
164         } else {
165                 DBGC ( netdev, "NETDEV %s link is down: %s\n",
166                        netdev->name, strerror ( netdev->link_rc ) );
167         }
168
169         /* Notify drivers of link state change */
170         netdev_notify ( netdev );
171 }
172
173 /**
174  * Mark network device as having link down
175  *
176  * @v netdev            Network device
177  */
178 void netdev_link_down ( struct net_device *netdev ) {
179
180         /* Avoid clobbering a more detailed link status code, if one
181          * is already set.
182          */
183         if ( ( netdev->link_rc == 0 ) ||
184              ( netdev->link_rc == -EUNKNOWN_LINK_STATUS ) ) {
185                 netdev_link_err ( netdev, -ENOTCONN_LINK_DOWN );
186         }
187 }
188
189 /**
190  * Record network device statistic
191  *
192  * @v stats             Network device statistics
193  * @v rc                Status code
194  */
195 static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
196         struct net_device_error *error;
197         struct net_device_error *least_common_error;
198         unsigned int i;
199
200         /* If this is not an error, just update the good counter */
201         if ( rc == 0 ) {
202                 stats->good++;
203                 return;
204         }
205
206         /* Update the bad counter */
207         stats->bad++;
208
209         /* Locate the appropriate error record */
210         least_common_error = &stats->errors[0];
211         for ( i = 0 ; i < ( sizeof ( stats->errors ) /
212                             sizeof ( stats->errors[0] ) ) ; i++ ) {
213                 error = &stats->errors[i];
214                 /* Update matching record, if found */
215                 if ( error->rc == rc ) {
216                         error->count++;
217                         return;
218                 }
219                 if ( error->count < least_common_error->count )
220                         least_common_error = error;
221         }
222
223         /* Overwrite the least common error record */
224         least_common_error->rc = rc;
225         least_common_error->count = 1;
226 }
227
228 /**
229  * Transmit raw packet via network device
230  *
231  * @v netdev            Network device
232  * @v iobuf             I/O buffer
233  * @ret rc              Return status code
234  *
235  * Transmits the packet via the specified network device.  This
236  * function takes ownership of the I/O buffer.
237  */
238 int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
239         int rc;
240
241         DBGC2 ( netdev, "NETDEV %s transmitting %p (%p+%zx)\n",
242                 netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
243         profile_start ( &net_tx_profiler );
244
245         /* Enqueue packet */
246         list_add_tail ( &iobuf->list, &netdev->tx_queue );
247
248         /* Avoid calling transmit() on unopened network devices */
249         if ( ! netdev_is_open ( netdev ) ) {
250                 rc = -ENETUNREACH;
251                 goto err;
252         }
253
254         /* Discard packet (for test purposes) if applicable */
255         if ( ( NETDEV_DISCARD_RATE > 0 ) &&
256              ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
257                 rc = -EAGAIN;
258                 goto err;
259         }
260
261         /* Transmit packet */
262         if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
263                 goto err;
264
265         profile_stop ( &net_tx_profiler );
266         return 0;
267
268  err:
269         netdev_tx_complete_err ( netdev, iobuf, rc );
270         return rc;
271 }
272
273 /**
274  * Defer transmitted packet
275  *
276  * @v netdev            Network device
277  * @v iobuf             I/O buffer
278  *
279  * Drivers may call netdev_tx_defer() if there is insufficient space
280  * in the transmit descriptor ring.  Any packets deferred in this way
281  * will be automatically retransmitted as soon as space becomes
282  * available (i.e. as soon as the driver calls netdev_tx_complete()).
283  *
284  * The packet must currently be in the network device's TX queue.
285  *
286  * Drivers utilising netdev_tx_defer() must ensure that space in the
287  * transmit descriptor ring is freed up @b before calling
288  * netdev_tx_complete().  For example, if the ring is modelled using a
289  * producer counter and a consumer counter, then the consumer counter
290  * must be incremented before the call to netdev_tx_complete().
291  * Failure to do this will cause the retransmitted packet to be
292  * immediately redeferred (which will result in out-of-order
293  * transmissions and other nastiness).
294  */
295 void netdev_tx_defer ( struct net_device *netdev, struct io_buffer *iobuf ) {
296
297         /* Catch data corruption as early as possible */
298         list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
299
300         /* Remove from transmit queue */
301         list_del ( &iobuf->list );
302
303         /* Add to deferred transmit queue */
304         list_add_tail ( &iobuf->list, &netdev->tx_deferred );
305
306         /* Record "out of space" statistic */
307         netdev_tx_err ( netdev, NULL, -ENOBUFS );
308 }
309
310 /**
311  * Discard transmitted packet
312  *
313  * @v netdev            Network device
314  * @v iobuf             I/O buffer, or NULL
315  * @v rc                Packet status code
316  *
317  * The packet is discarded and a TX error is recorded.  This function
318  * takes ownership of the I/O buffer.
319  */
320 void netdev_tx_err ( struct net_device *netdev,
321                      struct io_buffer *iobuf, int rc ) {
322
323         /* Update statistics counter */
324         netdev_record_stat ( &netdev->tx_stats, rc );
325         if ( rc == 0 ) {
326                 DBGC2 ( netdev, "NETDEV %s transmission %p complete\n",
327                         netdev->name, iobuf );
328         } else {
329                 DBGC ( netdev, "NETDEV %s transmission %p failed: %s\n",
330                        netdev->name, iobuf, strerror ( rc ) );
331         }
332
333         /* Discard packet */
334         free_iob ( iobuf );
335 }
336
337 /**
338  * Complete network transmission
339  *
340  * @v netdev            Network device
341  * @v iobuf             I/O buffer
342  * @v rc                Packet status code
343  *
344  * The packet must currently be in the network device's TX queue.
345  */
346 void netdev_tx_complete_err ( struct net_device *netdev,
347                               struct io_buffer *iobuf, int rc ) {
348
349         /* Catch data corruption as early as possible */
350         list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
351
352         /* Dequeue and free I/O buffer */
353         list_del ( &iobuf->list );
354         netdev_tx_err ( netdev, iobuf, rc );
355
356         /* Transmit first pending packet, if any */
357         if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
358                                           struct io_buffer, list ) ) != NULL ) {
359                 list_del ( &iobuf->list );
360                 netdev_tx ( netdev, iobuf );
361         }
362 }
363
364 /**
365  * Complete network transmission
366  *
367  * @v netdev            Network device
368  * @v rc                Packet status code
369  *
370  * Completes the oldest outstanding packet in the TX queue.
371  */
372 void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
373         struct io_buffer *iobuf;
374
375         if ( ( iobuf = list_first_entry ( &netdev->tx_queue, struct io_buffer,
376                                           list ) ) != NULL ) {
377                 netdev_tx_complete_err ( netdev, iobuf, rc );
378         }
379 }
380
381 /**
382  * Flush device's transmit queue
383  *
384  * @v netdev            Network device
385  */
386 static void netdev_tx_flush ( struct net_device *netdev ) {
387
388         /* Discard any packets in the TX queue.  This will also cause
389          * any packets in the deferred TX queue to be discarded
390          * automatically.
391          */
392         while ( ! list_empty ( &netdev->tx_queue ) ) {
393                 netdev_tx_complete_next_err ( netdev, -ECANCELED );
394         }
395         assert ( list_empty ( &netdev->tx_queue ) );
396         assert ( list_empty ( &netdev->tx_deferred ) );
397 }
398
399 /**
400  * Add packet to receive queue
401  *
402  * @v netdev            Network device
403  * @v iobuf             I/O buffer, or NULL
404  *
405  * The packet is added to the network device's RX queue.  This
406  * function takes ownership of the I/O buffer.
407  */
408 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
409
410         DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
411                 netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
412
413         /* Discard packet (for test purposes) if applicable */
414         if ( ( NETDEV_DISCARD_RATE > 0 ) &&
415              ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
416                 netdev_rx_err ( netdev, iobuf, -EAGAIN );
417                 return;
418         }
419
420         /* Enqueue packet */
421         list_add_tail ( &iobuf->list, &netdev->rx_queue );
422
423         /* Update statistics counter */
424         netdev_record_stat ( &netdev->rx_stats, 0 );
425 }
426
427 /**
428  * Discard received packet
429  *
430  * @v netdev            Network device
431  * @v iobuf             I/O buffer, or NULL
432  * @v rc                Packet status code
433  *
434  * The packet is discarded and an RX error is recorded.  This function
435  * takes ownership of the I/O buffer.  @c iobuf may be NULL if, for
436  * example, the net device wishes to report an error due to being
437  * unable to allocate an I/O buffer.
438  */
439 void netdev_rx_err ( struct net_device *netdev,
440                      struct io_buffer *iobuf, int rc ) {
441
442         DBGC ( netdev, "NETDEV %s failed to receive %p: %s\n",
443                netdev->name, iobuf, strerror ( rc ) );
444
445         /* Discard packet */
446         free_iob ( iobuf );
447
448         /* Update statistics counter */
449         netdev_record_stat ( &netdev->rx_stats, rc );
450 }
451
452 /**
453  * Poll for completed and received packets on network device
454  *
455  * @v netdev            Network device
456  *
457  * Polls the network device for completed transmissions and received
458  * packets.  Any received packets will be added to the RX packet queue
459  * via netdev_rx().
460  */
461 void netdev_poll ( struct net_device *netdev ) {
462
463         if ( netdev_is_open ( netdev ) )
464                 netdev->op->poll ( netdev );
465 }
466
467 /**
468  * Remove packet from device's receive queue
469  *
470  * @v netdev            Network device
471  * @ret iobuf           I/O buffer, or NULL
472  *
473  * Removes the first packet from the device's RX queue and returns it.
474  * Ownership of the packet is transferred to the caller.
475  */
476 struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
477         struct io_buffer *iobuf;
478
479         iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
480         if ( ! iobuf )
481                 return NULL;
482
483         list_del ( &iobuf->list );
484         return iobuf;
485 }
486
487 /**
488  * Flush device's receive queue
489  *
490  * @v netdev            Network device
491  */
492 static void netdev_rx_flush ( struct net_device *netdev ) {
493         struct io_buffer *iobuf;
494
495         /* Discard any packets in the RX queue */
496         while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
497                 netdev_rx_err ( netdev, iobuf, -ECANCELED );
498         }
499 }
500
501 /**
502  * Finish network device configuration
503  *
504  * @v config            Network device configuration
505  * @v rc                Reason for completion
506  */
507 static void netdev_config_close ( struct net_device_configuration *config,
508                                   int rc ) {
509         struct net_device_configurator *configurator = config->configurator;
510         struct net_device *netdev = config->netdev;
511
512         /* Restart interface */
513         intf_restart ( &config->job, rc );
514
515         /* Record configuration result */
516         config->rc = rc;
517         if ( rc == 0 ) {
518                 DBGC ( netdev, "NETDEV %s configured via %s\n",
519                        netdev->name, configurator->name );
520         } else {
521                 DBGC ( netdev, "NETDEV %s configuration via %s failed: %s\n",
522                        netdev->name, configurator->name, strerror ( rc ) );
523         }
524 }
525
526 /** Network device configuration interface operations */
527 static struct interface_operation netdev_config_ops[] = {
528         INTF_OP ( intf_close, struct net_device_configuration *,
529                   netdev_config_close ),
530 };
531
532 /** Network device configuration interface descriptor */
533 static struct interface_descriptor netdev_config_desc =
534         INTF_DESC ( struct net_device_configuration, job, netdev_config_ops );
535
536 /**
537  * Free network device
538  *
539  * @v refcnt            Network device reference counter
540  */
541 static void free_netdev ( struct refcnt *refcnt ) {
542         struct net_device *netdev =
543                 container_of ( refcnt, struct net_device, refcnt );
544         
545         netdev_tx_flush ( netdev );
546         netdev_rx_flush ( netdev );
547         clear_settings ( netdev_settings ( netdev ) );
548         free ( netdev );
549 }
550
551 /**
552  * Allocate network device
553  *
554  * @v priv_len          Length of private data area (net_device::priv)
555  * @ret netdev          Network device, or NULL
556  *
557  * Allocates space for a network device and its private data area.
558  */
559 struct net_device * alloc_netdev ( size_t priv_len ) {
560         struct net_device *netdev;
561         struct net_device_configurator *configurator;
562         struct net_device_configuration *config;
563         unsigned int num_configs;
564         size_t confs_len;
565         size_t total_len;
566
567         num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
568         confs_len = ( num_configs * sizeof ( netdev->configs[0] ) );
569         total_len = ( sizeof ( *netdev ) + confs_len + priv_len );
570         netdev = zalloc ( total_len );
571         if ( netdev ) {
572                 ref_init ( &netdev->refcnt, free_netdev );
573                 netdev->link_rc = -EUNKNOWN_LINK_STATUS;
574                 INIT_LIST_HEAD ( &netdev->tx_queue );
575                 INIT_LIST_HEAD ( &netdev->tx_deferred );
576                 INIT_LIST_HEAD ( &netdev->rx_queue );
577                 netdev_settings_init ( netdev );
578                 config = netdev->configs;
579                 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ){
580                         config->netdev = netdev;
581                         config->configurator = configurator;
582                         config->rc = -EUNUSED_CONFIG;
583                         intf_init ( &config->job, &netdev_config_desc,
584                                     &netdev->refcnt );
585                         config++;
586                 }
587                 netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) +
588                                  confs_len );
589         }
590         return netdev;
591 }
592
593 /**
594  * Register network device
595  *
596  * @v netdev            Network device
597  * @ret rc              Return status code
598  *
599  * Gives the network device a name and adds it to the list of network
600  * devices.
601  */
602 int register_netdev ( struct net_device *netdev ) {
603         struct ll_protocol *ll_protocol = netdev->ll_protocol;
604         struct net_driver *driver;
605         struct net_device *duplicate;
606         uint32_t seed;
607         int rc;
608
609         /* Set initial link-layer address, if not already set */
610         if ( ! netdev_has_ll_addr ( netdev ) ) {
611                 ll_protocol->init_addr ( netdev->hw_addr, netdev->ll_addr );
612         }
613
614         /* Reject network devices that are already available via a
615          * different hardware device.
616          */
617         duplicate = find_netdev_by_ll_addr ( ll_protocol, netdev->ll_addr );
618         if ( duplicate && ( duplicate->dev != netdev->dev ) ) {
619                 DBGC ( netdev, "NETDEV rejecting duplicate (phys %s) of %s "
620                        "(phys %s)\n", netdev->dev->name, duplicate->name,
621                        duplicate->dev->name );
622                 rc = -EEXIST;
623                 goto err_duplicate;
624         }
625
626         /* Record device index and create device name */
627         netdev->index = netdev_index++;
628         if ( netdev->name[0] == '\0' ) {
629                 snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
630                            netdev->index );
631         }
632
633         /* Use least significant bits of the link-layer address to
634          * improve the randomness of the (non-cryptographic) random
635          * number generator.
636          */
637         memcpy ( &seed, ( netdev->ll_addr + ll_protocol->ll_addr_len
638                           - sizeof ( seed ) ), sizeof ( seed ) );
639         srand ( rand() ^ seed );
640
641         /* Add to device list */
642         netdev_get ( netdev );
643         list_add_tail ( &netdev->list, &net_devices );
644         DBGC ( netdev, "NETDEV %s registered (phys %s hwaddr %s)\n",
645                netdev->name, netdev->dev->name,
646                netdev_addr ( netdev ) );
647
648         /* Register per-netdev configuration settings */
649         if ( ( rc = register_settings ( netdev_settings ( netdev ),
650                                         NULL, netdev->name ) ) != 0 ) {
651                 DBGC ( netdev, "NETDEV %s could not register settings: %s\n",
652                        netdev->name, strerror ( rc ) );
653                 goto err_register_settings;
654         }
655
656         /* Probe device */
657         for_each_table_entry ( driver, NET_DRIVERS ) {
658                 if ( driver->probe && ( rc = driver->probe ( netdev ) ) != 0 ) {
659                         DBGC ( netdev, "NETDEV %s could not add %s device: "
660                                "%s\n", netdev->name, driver->name,
661                                strerror ( rc ) );
662                         goto err_probe;
663                 }
664         }
665
666         return 0;
667
668  err_probe:
669         for_each_table_entry_continue_reverse ( driver, NET_DRIVERS ) {
670                 if ( driver->remove )
671                         driver->remove ( netdev );
672         }
673         clear_settings ( netdev_settings ( netdev ) );
674         unregister_settings ( netdev_settings ( netdev ) );
675  err_register_settings:
676  err_duplicate:
677         return rc;
678 }
679
680 /**
681  * Open network device
682  *
683  * @v netdev            Network device
684  * @ret rc              Return status code
685  */
686 int netdev_open ( struct net_device *netdev ) {
687         int rc;
688
689         /* Do nothing if device is already open */
690         if ( netdev->state & NETDEV_OPEN )
691                 return 0;
692
693         DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
694
695         /* Mark as opened */
696         netdev->state |= NETDEV_OPEN;
697
698         /* Open the device */
699         if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
700                 goto err;
701
702         /* Add to head of open devices list */
703         list_add ( &netdev->open_list, &open_net_devices );
704
705         /* Notify drivers of device state change */
706         netdev_notify ( netdev );
707
708         return 0;
709
710  err:
711         netdev->state &= ~NETDEV_OPEN;
712         return rc;
713 }
714
715 /**
716  * Close network device
717  *
718  * @v netdev            Network device
719  */
720 void netdev_close ( struct net_device *netdev ) {
721         unsigned int num_configs;
722         unsigned int i;
723
724         /* Do nothing if device is already closed */
725         if ( ! ( netdev->state & NETDEV_OPEN ) )
726                 return;
727
728         DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
729
730         /* Terminate any ongoing configurations.  Use intf_close()
731          * rather than intf_restart() to allow the cancellation to be
732          * reported back to us if a configuration is actually in
733          * progress.
734          */
735         num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
736         for ( i = 0 ; i < num_configs ; i++ )
737                 intf_close ( &netdev->configs[i].job, -ECANCELED );
738
739         /* Remove from open devices list */
740         list_del ( &netdev->open_list );
741
742         /* Mark as closed */
743         netdev->state &= ~NETDEV_OPEN;
744
745         /* Notify drivers of device state change */
746         netdev_notify ( netdev );
747
748         /* Close the device */
749         netdev->op->close ( netdev );
750
751         /* Flush TX and RX queues */
752         netdev_tx_flush ( netdev );
753         netdev_rx_flush ( netdev );
754 }
755
756 /**
757  * Unregister network device
758  *
759  * @v netdev            Network device
760  *
761  * Removes the network device from the list of network devices.
762  */
763 void unregister_netdev ( struct net_device *netdev ) {
764         struct net_driver *driver;
765
766         /* Ensure device is closed */
767         netdev_close ( netdev );
768
769         /* Remove device */
770         for_each_table_entry_reverse ( driver, NET_DRIVERS ) {
771                 if ( driver->remove )
772                         driver->remove ( netdev );
773         }
774
775         /* Unregister per-netdev configuration settings */
776         clear_settings ( netdev_settings ( netdev ) );
777         unregister_settings ( netdev_settings ( netdev ) );
778
779         /* Remove from device list */
780         DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
781         list_del ( &netdev->list );
782         netdev_put ( netdev );
783
784         /* Reset network device index if no devices remain */
785         if ( list_empty ( &net_devices ) )
786                 netdev_index = 0;
787 }
788
789 /** Enable or disable interrupts
790  *
791  * @v netdev            Network device
792  * @v enable            Interrupts should be enabled
793  */
794 void netdev_irq ( struct net_device *netdev, int enable ) {
795
796         /* Do nothing if device does not support interrupts */
797         if ( ! netdev_irq_supported ( netdev ) )
798                 return;
799
800         /* Enable or disable device interrupts */
801         netdev->op->irq ( netdev, enable );
802
803         /* Record interrupt enabled state */
804         netdev->state &= ~NETDEV_IRQ_ENABLED;
805         if ( enable )
806                 netdev->state |= NETDEV_IRQ_ENABLED;
807 }
808
809 /**
810  * Get network device by name
811  *
812  * @v name              Network device name
813  * @ret netdev          Network device, or NULL
814  */
815 struct net_device * find_netdev ( const char *name ) {
816         struct net_device *netdev;
817
818         /* Allow "netX" shortcut */
819         if ( strcmp ( name, "netX" ) == 0 )
820                 return last_opened_netdev();
821
822         /* Identify network device by name */
823         list_for_each_entry ( netdev, &net_devices, list ) {
824                 if ( strcmp ( netdev->name, name ) == 0 )
825                         return netdev;
826         }
827
828         return NULL;
829 }
830
831 /**
832  * Get network device by index
833  *
834  * @v index             Network device index
835  * @ret netdev          Network device, or NULL
836  */
837 struct net_device * find_netdev_by_index ( unsigned int index ) {
838         struct net_device *netdev;
839
840         /* Identify network device by index */
841         list_for_each_entry ( netdev, &net_devices, list ) {
842                 if ( netdev->index == index )
843                         return netdev;
844         }
845
846         return NULL;
847 }
848
849 /**
850  * Get network device by PCI bus:dev.fn address
851  *
852  * @v bus_type          Bus type
853  * @v location          Bus location
854  * @ret netdev          Network device, or NULL
855  */
856 struct net_device * find_netdev_by_location ( unsigned int bus_type,
857                                               unsigned int location ) {
858         struct net_device *netdev;
859
860         list_for_each_entry ( netdev, &net_devices, list ) {
861                 if ( ( netdev->dev->desc.bus_type == bus_type ) &&
862                      ( netdev->dev->desc.location == location ) )
863                         return netdev;
864         }
865
866         return NULL;    
867 }
868
869 /**
870  * Get network device by link-layer address
871  *
872  * @v ll_protocol       Link-layer protocol
873  * @v ll_addr           Link-layer address
874  * @ret netdev          Network device, or NULL
875  */
876 struct net_device * find_netdev_by_ll_addr ( struct ll_protocol *ll_protocol,
877                                              const void *ll_addr ) {
878         struct net_device *netdev;
879
880         list_for_each_entry ( netdev, &net_devices, list ) {
881                 if ( ( netdev->ll_protocol == ll_protocol ) &&
882                      ( memcmp ( netdev->ll_addr, ll_addr,
883                                 ll_protocol->ll_addr_len ) == 0 ) )
884                         return netdev;
885         }
886
887         return NULL;
888 }
889
890 /**
891  * Get most recently opened network device
892  *
893  * @ret netdev          Most recently opened network device, or NULL
894  */
895 struct net_device * last_opened_netdev ( void ) {
896         struct net_device *netdev;
897
898         netdev = list_first_entry ( &open_net_devices, struct net_device,
899                                     open_list );
900         if ( ! netdev )
901                 return NULL;
902
903         assert ( netdev_is_open ( netdev ) );
904         return netdev;
905 }
906
907 /**
908  * Transmit network-layer packet
909  *
910  * @v iobuf             I/O buffer
911  * @v netdev            Network device
912  * @v net_protocol      Network-layer protocol
913  * @v ll_dest           Destination link-layer address
914  * @v ll_source         Source link-layer address
915  * @ret rc              Return status code
916  *
917  * Prepends link-layer headers to the I/O buffer and transmits the
918  * packet via the specified network device.  This function takes
919  * ownership of the I/O buffer.
920  */
921 int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
922              struct net_protocol *net_protocol, const void *ll_dest,
923              const void *ll_source ) {
924         struct ll_protocol *ll_protocol = netdev->ll_protocol;
925         int rc;
926
927         /* Add link-layer header */
928         if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, ll_source,
929                                         net_protocol->net_proto ) ) != 0 ) {
930                 /* Record error for diagnosis */
931                 netdev_tx_err ( netdev, iobuf, rc );
932                 return rc;
933         }
934
935         /* Transmit packet */
936         return netdev_tx ( netdev, iobuf );
937 }
938
939 /**
940  * Process received network-layer packet
941  *
942  * @v iobuf             I/O buffer
943  * @v netdev            Network device
944  * @v net_proto         Network-layer protocol, in network-byte order
945  * @v ll_dest           Destination link-layer address
946  * @v ll_source         Source link-layer address
947  * @v flags             Packet flags
948  * @ret rc              Return status code
949  */
950 int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
951              uint16_t net_proto, const void *ll_dest, const void *ll_source,
952              unsigned int flags ) {
953         struct net_protocol *net_protocol;
954
955         /* Hand off to network-layer protocol, if any */
956         for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
957                 if ( net_protocol->net_proto == net_proto )
958                         return net_protocol->rx ( iobuf, netdev, ll_dest,
959                                                   ll_source, flags );
960         }
961
962         DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
963                netdev->name, ntohs ( net_proto ) );
964         free_iob ( iobuf );
965         return -ENOTSUP;
966 }
967
968 /**
969  * Poll the network stack
970  *
971  * This polls all interfaces for received packets, and processes
972  * packets from the RX queue.
973  */
974 void net_poll ( void ) {
975         struct net_device *netdev;
976         struct io_buffer *iobuf;
977         struct ll_protocol *ll_protocol;
978         const void *ll_dest;
979         const void *ll_source;
980         uint16_t net_proto;
981         unsigned int flags;
982         int rc;
983
984         /* Poll and process each network device */
985         list_for_each_entry ( netdev, &net_devices, list ) {
986
987                 /* Poll for new packets */
988                 profile_start ( &net_poll_profiler );
989                 netdev_poll ( netdev );
990                 profile_stop ( &net_poll_profiler );
991
992                 /* Leave received packets on the queue if receive
993                  * queue processing is currently frozen.  This will
994                  * happen when the raw packets are to be manually
995                  * dequeued using netdev_rx_dequeue(), rather than
996                  * processed via the usual networking stack.
997                  */
998                 if ( netdev_rx_frozen ( netdev ) )
999                         continue;
1000
1001                 /* Process all received packets */
1002                 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
1003
1004                         DBGC2 ( netdev, "NETDEV %s processing %p (%p+%zx)\n",
1005                                 netdev->name, iobuf, iobuf->data,
1006                                 iob_len ( iobuf ) );
1007                         profile_start ( &net_rx_profiler );
1008
1009                         /* Remove link-layer header */
1010                         ll_protocol = netdev->ll_protocol;
1011                         if ( ( rc = ll_protocol->pull ( netdev, iobuf,
1012                                                         &ll_dest, &ll_source,
1013                                                         &net_proto,
1014                                                         &flags ) ) != 0 ) {
1015                                 free_iob ( iobuf );
1016                                 continue;
1017                         }
1018
1019                         /* Hand packet to network layer */
1020                         if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
1021                                              net_proto, ll_dest,
1022                                              ll_source, flags ) ) != 0 ) {
1023                                 /* Record error for diagnosis */
1024                                 netdev_rx_err ( netdev, NULL, rc );
1025                         }
1026                         profile_stop ( &net_rx_profiler );
1027                 }
1028         }
1029 }
1030
1031 /**
1032  * Single-step the network stack
1033  *
1034  * @v process           Network stack process
1035  */
1036 static void net_step ( struct process *process __unused ) {
1037         net_poll();
1038 }
1039
1040 /**
1041  * Get the VLAN tag (when VLAN support is not present)
1042  *
1043  * @v netdev            Network device
1044  * @ret tag             0, indicating that device is not a VLAN device
1045  */
1046 __weak unsigned int vlan_tag ( struct net_device *netdev __unused ) {
1047         return 0;
1048 }
1049
1050 /**
1051  * Identify VLAN device (when VLAN support is not present)
1052  *
1053  * @v trunk             Trunk network device
1054  * @v tag               VLAN tag
1055  * @ret netdev          VLAN device, if any
1056  */
1057 __weak struct net_device * vlan_find ( struct net_device *trunk __unused,
1058                                        unsigned int tag __unused ) {
1059         return NULL;
1060 }
1061
1062 /** Networking stack process */
1063 PERMANENT_PROCESS ( net_process, net_step );
1064
1065 /**
1066  * Discard some cached network device data
1067  *
1068  * @ret discarded       Number of cached items discarded
1069  */
1070 static unsigned int net_discard ( void ) {
1071         struct net_device *netdev;
1072         struct io_buffer *iobuf;
1073         unsigned int discarded = 0;
1074
1075         /* Try to drop one deferred TX packet from each network device */
1076         for_each_netdev ( netdev ) {
1077                 if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
1078                                                   struct io_buffer,
1079                                                   list ) ) != NULL ) {
1080
1081                         /* Discard first deferred packet */
1082                         list_del ( &iobuf->list );
1083                         free_iob ( iobuf );
1084
1085                         /* Report discard */
1086                         discarded++;
1087                 }
1088         }
1089
1090         return discarded;
1091 }
1092
1093 /** Network device cache discarder */
1094 struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
1095         .discard = net_discard,
1096 };
1097
1098 /**
1099  * Find network device configurator
1100  *
1101  * @v name              Name
1102  * @ret configurator    Network device configurator, or NULL
1103  */
1104 struct net_device_configurator * find_netdev_configurator ( const char *name ) {
1105         struct net_device_configurator *configurator;
1106
1107         for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1108                 if ( strcmp ( configurator->name, name ) == 0 )
1109                         return configurator;
1110         }
1111         return NULL;
1112 }
1113
1114 /**
1115  * Start network device configuration
1116  *
1117  * @v netdev            Network device
1118  * @v configurator      Network device configurator
1119  * @ret rc              Return status code
1120  */
1121 int netdev_configure ( struct net_device *netdev,
1122                        struct net_device_configurator *configurator ) {
1123         struct net_device_configuration *config =
1124                 netdev_configuration ( netdev, configurator );
1125         int rc;
1126
1127         /* Check applicability of configurator */
1128         if ( ! netdev_configurator_applies ( netdev, configurator ) ) {
1129                 DBGC ( netdev, "NETDEV %s does not support configuration via "
1130                        "%s\n", netdev->name, configurator->name );
1131                 return -ENOTSUP;
1132         }
1133
1134         /* Terminate any ongoing configuration */
1135         intf_restart ( &config->job, -ECANCELED );
1136
1137         /* Mark configuration as being in progress */
1138         config->rc = -EINPROGRESS_CONFIG;
1139
1140         DBGC ( netdev, "NETDEV %s starting configuration via %s\n",
1141                netdev->name, configurator->name );
1142
1143         /* Start configuration */
1144         if ( ( rc = configurator->start ( &config->job, netdev ) ) != 0 ) {
1145                 DBGC ( netdev, "NETDEV %s could not start configuration via "
1146                        "%s: %s\n", netdev->name, configurator->name,
1147                        strerror ( rc ) );
1148                 config->rc = rc;
1149                 return rc;
1150         }
1151
1152         return 0;
1153 }
1154
1155 /**
1156  * Start network device configuration via all supported configurators
1157  *
1158  * @v netdev            Network device
1159  * @ret rc              Return status code
1160  */
1161 int netdev_configure_all ( struct net_device *netdev ) {
1162         struct net_device_configurator *configurator;
1163         int rc;
1164
1165         /* Start configuration for each configurator */
1166         for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1167
1168                 /* Skip any inapplicable configurators */
1169                 if ( ! netdev_configurator_applies ( netdev, configurator ) )
1170                         continue;
1171
1172                 /* Start configuration */
1173                 if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 )
1174                         return rc;
1175         }
1176
1177         return 0;
1178 }
1179
1180 /**
1181  * Check if network device has a configuration with a specified status code
1182  *
1183  * @v netdev            Network device
1184  * @v rc                Status code
1185  * @ret has_rc          Network device has a configuration with this status code
1186  */
1187 static int netdev_has_configuration_rc ( struct net_device *netdev, int rc ) {
1188         unsigned int num_configs;
1189         unsigned int i;
1190
1191         num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
1192         for ( i = 0 ; i < num_configs ; i++ ) {
1193                 if ( netdev->configs[i].rc == rc )
1194                         return 1;
1195         }
1196         return 0;
1197 }
1198
1199 /**
1200  * Check if network device configuration is in progress
1201  *
1202  * @v netdev            Network device
1203  * @ret is_in_progress  Network device configuration is in progress
1204  */
1205 int netdev_configuration_in_progress ( struct net_device *netdev ) {
1206
1207         return netdev_has_configuration_rc ( netdev, -EINPROGRESS_CONFIG );
1208 }
1209
1210 /**
1211  * Check if network device has at least one successful configuration
1212  *
1213  * @v netdev            Network device
1214  * @v configurator      Configurator
1215  * @ret rc              Return status code
1216  */
1217 int netdev_configuration_ok ( struct net_device *netdev ) {
1218
1219         return netdev_has_configuration_rc ( netdev, 0 );
1220 }