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., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
32 #include <config/general.h>
33 #include <ipxe/if_ether.h>
34 #include <ipxe/iobuf.h>
35 #include <ipxe/tables.h>
36 #include <ipxe/process.h>
37 #include <ipxe/init.h>
38 #include <ipxe/malloc.h>
39 #include <ipxe/device.h>
40 #include <ipxe/errortab.h>
41 #include <ipxe/profile.h>
42 #include <ipxe/fault.h>
43 #include <ipxe/vlan.h>
44 #include <ipxe/netdevice.h>
48 * Network device management
52 /** List of network devices */
53 struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
55 /** List of open network devices, in reverse order of opening */
56 static struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
58 /** Network device index */
59 static unsigned int netdev_index = 0;
61 /** Network polling profiler */
62 static struct profiler net_poll_profiler __profiler = { .name = "net.poll" };
64 /** Network receive profiler */
65 static struct profiler net_rx_profiler __profiler = { .name = "net.rx" };
67 /** Network transmit profiler */
68 static struct profiler net_tx_profiler __profiler = { .name = "net.tx" };
70 /** Default unknown link status code */
71 #define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
72 #define EINFO_EUNKNOWN_LINK_STATUS \
73 __einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )
75 /** Default not-yet-attempted-configuration status code */
76 #define EUNUSED_CONFIG __einfo_error ( EINFO_EUNUSED_CONFIG )
77 #define EINFO_EUNUSED_CONFIG \
78 __einfo_uniqify ( EINFO_EINPROGRESS, 0x02, "Unused" )
80 /** Default configuration-in-progress status code */
81 #define EINPROGRESS_CONFIG __einfo_error ( EINFO_EINPROGRESS_CONFIG )
82 #define EINFO_EINPROGRESS_CONFIG \
83 __einfo_uniqify ( EINFO_EINPROGRESS, 0x03, "Incomplete" )
85 /** Default link-down status code */
86 #define ENOTCONN_LINK_DOWN __einfo_error ( EINFO_ENOTCONN_LINK_DOWN )
87 #define EINFO_ENOTCONN_LINK_DOWN \
88 __einfo_uniqify ( EINFO_ENOTCONN, 0x01, "Down" )
90 /** Human-readable message for the default link statuses */
91 struct errortab netdev_errors[] __errortab = {
92 __einfo_errortab ( EINFO_EUNKNOWN_LINK_STATUS ),
93 __einfo_errortab ( EINFO_ENOTCONN_LINK_DOWN ),
94 __einfo_errortab ( EINFO_EUNUSED_CONFIG ),
95 __einfo_errortab ( EINFO_EINPROGRESS_CONFIG ),
99 * Check whether or not network device has a link-layer address
101 * @v netdev Network device
102 * @ret has_ll_addr Network device has a link-layer address
104 static int netdev_has_ll_addr ( struct net_device *netdev ) {
105 uint8_t *ll_addr = netdev->ll_addr;
106 size_t remaining = sizeof ( netdev->ll_addr );
108 while ( remaining-- ) {
109 if ( *(ll_addr++) != 0 )
116 * Notify drivers of network device or link state change
118 * @v netdev Network device
120 static void netdev_notify ( struct net_device *netdev ) {
121 struct net_driver *driver;
123 for_each_table_entry ( driver, NET_DRIVERS ) {
124 if ( driver->notify )
125 driver->notify ( netdev );
130 * Freeze network device receive queue processing
132 * @v netdev Network device
134 void netdev_rx_freeze ( struct net_device *netdev ) {
136 /* Mark receive queue processing as frozen */
137 netdev->state |= NETDEV_RX_FROZEN;
139 /* Notify drivers of change */
140 netdev_notify ( netdev );
144 * Unfreeze network device receive queue processing
146 * @v netdev Network device
148 void netdev_rx_unfreeze ( struct net_device *netdev ) {
150 /* Mark receive queue processing as not frozen */
151 netdev->state &= ~NETDEV_RX_FROZEN;
153 /* Notify drivers of change */
154 netdev_notify ( netdev );
158 * Mark network device as having a specific link state
160 * @v netdev Network device
161 * @v rc Link status code
163 void netdev_link_err ( struct net_device *netdev, int rc ) {
165 /* Stop link block timer */
166 stop_timer ( &netdev->link_block );
168 /* Record link state */
169 netdev->link_rc = rc;
170 if ( netdev->link_rc == 0 ) {
171 DBGC ( netdev, "NETDEV %s link is up\n", netdev->name );
173 DBGC ( netdev, "NETDEV %s link is down: %s\n",
174 netdev->name, strerror ( netdev->link_rc ) );
177 /* Notify drivers of link state change */
178 netdev_notify ( netdev );
182 * Mark network device as having link down
184 * @v netdev Network device
186 void netdev_link_down ( struct net_device *netdev ) {
188 /* Avoid clobbering a more detailed link status code, if one
191 if ( ( netdev->link_rc == 0 ) ||
192 ( netdev->link_rc == -EUNKNOWN_LINK_STATUS ) ) {
193 netdev_link_err ( netdev, -ENOTCONN_LINK_DOWN );
198 * Mark network device link as being blocked
200 * @v netdev Network device
201 * @v timeout Timeout (in ticks)
203 void netdev_link_block ( struct net_device *netdev, unsigned long timeout ) {
205 /* Start link block timer */
206 if ( ! netdev_link_blocked ( netdev ) ) {
207 DBGC ( netdev, "NETDEV %s link blocked for %ld ticks\n",
208 netdev->name, timeout );
210 start_timer_fixed ( &netdev->link_block, timeout );
214 * Mark network device link as being unblocked
216 * @v netdev Network device
218 void netdev_link_unblock ( struct net_device *netdev ) {
220 /* Stop link block timer */
221 if ( netdev_link_blocked ( netdev ) )
222 DBGC ( netdev, "NETDEV %s link unblocked\n", netdev->name );
223 stop_timer ( &netdev->link_block );
227 * Handle network device link block timer expiry
229 * @v timer Link block timer
230 * @v fail Failure indicator
232 static void netdev_link_block_expired ( struct retry_timer *timer,
233 int fail __unused ) {
234 struct net_device *netdev =
235 container_of ( timer, struct net_device, link_block );
237 /* Assume link is no longer blocked */
238 DBGC ( netdev, "NETDEV %s link block expired\n", netdev->name );
242 * Record network device statistic
244 * @v stats Network device statistics
247 static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
248 struct net_device_error *error;
249 struct net_device_error *least_common_error;
252 /* If this is not an error, just update the good counter */
258 /* Update the bad counter */
261 /* Locate the appropriate error record */
262 least_common_error = &stats->errors[0];
263 for ( i = 0 ; i < ( sizeof ( stats->errors ) /
264 sizeof ( stats->errors[0] ) ) ; i++ ) {
265 error = &stats->errors[i];
266 /* Update matching record, if found */
267 if ( error->rc == rc ) {
271 if ( error->count < least_common_error->count )
272 least_common_error = error;
275 /* Overwrite the least common error record */
276 least_common_error->rc = rc;
277 least_common_error->count = 1;
281 * Transmit raw packet via network device
283 * @v netdev Network device
284 * @v iobuf I/O buffer
285 * @ret rc Return status code
287 * Transmits the packet via the specified network device. This
288 * function takes ownership of the I/O buffer.
290 int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
293 DBGC2 ( netdev, "NETDEV %s transmitting %p (%p+%zx)\n",
294 netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
295 profile_start ( &net_tx_profiler );
298 list_add_tail ( &iobuf->list, &netdev->tx_queue );
300 /* Avoid calling transmit() on unopened network devices */
301 if ( ! netdev_is_open ( netdev ) ) {
306 /* Discard packet (for test purposes) if applicable */
307 if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 )
310 /* Transmit packet */
311 if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
314 profile_stop ( &net_tx_profiler );
318 netdev_tx_complete_err ( netdev, iobuf, rc );
323 * Defer transmitted packet
325 * @v netdev Network device
326 * @v iobuf I/O buffer
328 * Drivers may call netdev_tx_defer() if there is insufficient space
329 * in the transmit descriptor ring. Any packets deferred in this way
330 * will be automatically retransmitted as soon as space becomes
331 * available (i.e. as soon as the driver calls netdev_tx_complete()).
333 * The packet must currently be in the network device's TX queue.
335 * Drivers utilising netdev_tx_defer() must ensure that space in the
336 * transmit descriptor ring is freed up @b before calling
337 * netdev_tx_complete(). For example, if the ring is modelled using a
338 * producer counter and a consumer counter, then the consumer counter
339 * must be incremented before the call to netdev_tx_complete().
340 * Failure to do this will cause the retransmitted packet to be
341 * immediately redeferred (which will result in out-of-order
342 * transmissions and other nastiness).
344 void netdev_tx_defer ( struct net_device *netdev, struct io_buffer *iobuf ) {
346 /* Catch data corruption as early as possible */
347 list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
349 /* Remove from transmit queue */
350 list_del ( &iobuf->list );
352 /* Add to deferred transmit queue */
353 list_add_tail ( &iobuf->list, &netdev->tx_deferred );
355 /* Record "out of space" statistic */
356 netdev_tx_err ( netdev, NULL, -ENOBUFS );
360 * Discard transmitted packet
362 * @v netdev Network device
363 * @v iobuf I/O buffer, or NULL
364 * @v rc Packet status code
366 * The packet is discarded and a TX error is recorded. This function
367 * takes ownership of the I/O buffer.
369 void netdev_tx_err ( struct net_device *netdev,
370 struct io_buffer *iobuf, int rc ) {
372 /* Update statistics counter */
373 netdev_record_stat ( &netdev->tx_stats, rc );
375 DBGC2 ( netdev, "NETDEV %s transmission %p complete\n",
376 netdev->name, iobuf );
378 DBGC ( netdev, "NETDEV %s transmission %p failed: %s\n",
379 netdev->name, iobuf, strerror ( rc ) );
387 * Complete network transmission
389 * @v netdev Network device
390 * @v iobuf I/O buffer
391 * @v rc Packet status code
393 * The packet must currently be in the network device's TX queue.
395 void netdev_tx_complete_err ( struct net_device *netdev,
396 struct io_buffer *iobuf, int rc ) {
398 /* Catch data corruption as early as possible */
399 list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
401 /* Dequeue and free I/O buffer */
402 list_del ( &iobuf->list );
403 netdev_tx_err ( netdev, iobuf, rc );
405 /* Transmit first pending packet, if any */
406 if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
407 struct io_buffer, list ) ) != NULL ) {
408 list_del ( &iobuf->list );
409 netdev_tx ( netdev, iobuf );
414 * Complete network transmission
416 * @v netdev Network device
417 * @v rc Packet status code
419 * Completes the oldest outstanding packet in the TX queue.
421 void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
422 struct io_buffer *iobuf;
424 if ( ( iobuf = list_first_entry ( &netdev->tx_queue, struct io_buffer,
426 netdev_tx_complete_err ( netdev, iobuf, rc );
431 * Flush device's transmit queue
433 * @v netdev Network device
435 static void netdev_tx_flush ( struct net_device *netdev ) {
437 /* Discard any packets in the TX queue. This will also cause
438 * any packets in the deferred TX queue to be discarded
441 while ( ! list_empty ( &netdev->tx_queue ) ) {
442 netdev_tx_complete_next_err ( netdev, -ECANCELED );
444 assert ( list_empty ( &netdev->tx_queue ) );
445 assert ( list_empty ( &netdev->tx_deferred ) );
449 * Add packet to receive queue
451 * @v netdev Network device
452 * @v iobuf I/O buffer, or NULL
454 * The packet is added to the network device's RX queue. This
455 * function takes ownership of the I/O buffer.
457 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
460 DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
461 netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
463 /* Discard packet (for test purposes) if applicable */
464 if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 ) {
465 netdev_rx_err ( netdev, iobuf, rc );
470 list_add_tail ( &iobuf->list, &netdev->rx_queue );
472 /* Update statistics counter */
473 netdev_record_stat ( &netdev->rx_stats, 0 );
477 * Discard received packet
479 * @v netdev Network device
480 * @v iobuf I/O buffer, or NULL
481 * @v rc Packet status code
483 * The packet is discarded and an RX error is recorded. This function
484 * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
485 * example, the net device wishes to report an error due to being
486 * unable to allocate an I/O buffer.
488 void netdev_rx_err ( struct net_device *netdev,
489 struct io_buffer *iobuf, int rc ) {
491 DBGC ( netdev, "NETDEV %s failed to receive %p: %s\n",
492 netdev->name, iobuf, strerror ( rc ) );
497 /* Update statistics counter */
498 netdev_record_stat ( &netdev->rx_stats, rc );
502 * Poll for completed and received packets on network device
504 * @v netdev Network device
506 * Polls the network device for completed transmissions and received
507 * packets. Any received packets will be added to the RX packet queue
510 void netdev_poll ( struct net_device *netdev ) {
512 if ( netdev_is_open ( netdev ) )
513 netdev->op->poll ( netdev );
517 * Remove packet from device's receive queue
519 * @v netdev Network device
520 * @ret iobuf I/O buffer, or NULL
522 * Removes the first packet from the device's RX queue and returns it.
523 * Ownership of the packet is transferred to the caller.
525 struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
526 struct io_buffer *iobuf;
528 iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
532 list_del ( &iobuf->list );
537 * Flush device's receive queue
539 * @v netdev Network device
541 static void netdev_rx_flush ( struct net_device *netdev ) {
542 struct io_buffer *iobuf;
544 /* Discard any packets in the RX queue */
545 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
546 netdev_rx_err ( netdev, iobuf, -ECANCELED );
551 * Finish network device configuration
553 * @v config Network device configuration
554 * @v rc Reason for completion
556 static void netdev_config_close ( struct net_device_configuration *config,
558 struct net_device_configurator *configurator = config->configurator;
559 struct net_device *netdev = config->netdev;
561 /* Restart interface */
562 intf_restart ( &config->job, rc );
564 /* Record configuration result */
567 DBGC ( netdev, "NETDEV %s configured via %s\n",
568 netdev->name, configurator->name );
570 DBGC ( netdev, "NETDEV %s configuration via %s failed: %s\n",
571 netdev->name, configurator->name, strerror ( rc ) );
575 /** Network device configuration interface operations */
576 static struct interface_operation netdev_config_ops[] = {
577 INTF_OP ( intf_close, struct net_device_configuration *,
578 netdev_config_close ),
581 /** Network device configuration interface descriptor */
582 static struct interface_descriptor netdev_config_desc =
583 INTF_DESC ( struct net_device_configuration, job, netdev_config_ops );
586 * Free network device
588 * @v refcnt Network device reference counter
590 static void free_netdev ( struct refcnt *refcnt ) {
591 struct net_device *netdev =
592 container_of ( refcnt, struct net_device, refcnt );
594 stop_timer ( &netdev->link_block );
595 netdev_tx_flush ( netdev );
596 netdev_rx_flush ( netdev );
597 clear_settings ( netdev_settings ( netdev ) );
602 * Allocate network device
604 * @v priv_len Length of private data area (net_device::priv)
605 * @ret netdev Network device, or NULL
607 * Allocates space for a network device and its private data area.
609 struct net_device * alloc_netdev ( size_t priv_len ) {
610 struct net_device *netdev;
611 struct net_device_configurator *configurator;
612 struct net_device_configuration *config;
613 unsigned int num_configs;
617 num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
618 confs_len = ( num_configs * sizeof ( netdev->configs[0] ) );
619 total_len = ( sizeof ( *netdev ) + confs_len + priv_len );
620 netdev = zalloc ( total_len );
622 ref_init ( &netdev->refcnt, free_netdev );
623 netdev->link_rc = -EUNKNOWN_LINK_STATUS;
624 timer_init ( &netdev->link_block, netdev_link_block_expired,
626 INIT_LIST_HEAD ( &netdev->tx_queue );
627 INIT_LIST_HEAD ( &netdev->tx_deferred );
628 INIT_LIST_HEAD ( &netdev->rx_queue );
629 netdev_settings_init ( netdev );
630 config = netdev->configs;
631 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ){
632 config->netdev = netdev;
633 config->configurator = configurator;
634 config->rc = -EUNUSED_CONFIG;
635 intf_init ( &config->job, &netdev_config_desc,
639 netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) +
646 * Register network device
648 * @v netdev Network device
649 * @ret rc Return status code
651 * Gives the network device a name and adds it to the list of network
654 int register_netdev ( struct net_device *netdev ) {
655 struct ll_protocol *ll_protocol = netdev->ll_protocol;
656 struct net_driver *driver;
657 struct net_device *duplicate;
661 /* Set initial link-layer address, if not already set */
662 if ( ! netdev_has_ll_addr ( netdev ) ) {
663 ll_protocol->init_addr ( netdev->hw_addr, netdev->ll_addr );
666 /* Reject network devices that are already available via a
667 * different hardware device.
669 duplicate = find_netdev_by_ll_addr ( ll_protocol, netdev->ll_addr );
670 if ( duplicate && ( duplicate->dev != netdev->dev ) ) {
671 DBGC ( netdev, "NETDEV rejecting duplicate (phys %s) of %s "
672 "(phys %s)\n", netdev->dev->name, duplicate->name,
673 duplicate->dev->name );
678 /* Record device index and create device name */
679 if ( netdev->name[0] == '\0' ) {
680 snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
683 netdev->index = ++netdev_index;
685 /* Use least significant bits of the link-layer address to
686 * improve the randomness of the (non-cryptographic) random
689 memcpy ( &seed, ( netdev->ll_addr + ll_protocol->ll_addr_len
690 - sizeof ( seed ) ), sizeof ( seed ) );
691 srand ( rand() ^ seed );
693 /* Add to device list */
694 netdev_get ( netdev );
695 list_add_tail ( &netdev->list, &net_devices );
696 DBGC ( netdev, "NETDEV %s registered (phys %s hwaddr %s)\n",
697 netdev->name, netdev->dev->name,
698 netdev_addr ( netdev ) );
700 /* Register per-netdev configuration settings */
701 if ( ( rc = register_settings ( netdev_settings ( netdev ),
702 NULL, netdev->name ) ) != 0 ) {
703 DBGC ( netdev, "NETDEV %s could not register settings: %s\n",
704 netdev->name, strerror ( rc ) );
705 goto err_register_settings;
709 for_each_table_entry ( driver, NET_DRIVERS ) {
710 if ( driver->probe && ( rc = driver->probe ( netdev ) ) != 0 ) {
711 DBGC ( netdev, "NETDEV %s could not add %s device: "
712 "%s\n", netdev->name, driver->name,
721 for_each_table_entry_continue_reverse ( driver, NET_DRIVERS ) {
722 if ( driver->remove )
723 driver->remove ( netdev );
725 clear_settings ( netdev_settings ( netdev ) );
726 unregister_settings ( netdev_settings ( netdev ) );
727 err_register_settings:
733 * Open network device
735 * @v netdev Network device
736 * @ret rc Return status code
738 int netdev_open ( struct net_device *netdev ) {
741 /* Do nothing if device is already open */
742 if ( netdev->state & NETDEV_OPEN )
745 DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
748 netdev->state |= NETDEV_OPEN;
750 /* Open the device */
751 if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
754 /* Add to head of open devices list */
755 list_add ( &netdev->open_list, &open_net_devices );
757 /* Notify drivers of device state change */
758 netdev_notify ( netdev );
763 netdev->state &= ~NETDEV_OPEN;
768 * Close network device
770 * @v netdev Network device
772 void netdev_close ( struct net_device *netdev ) {
773 unsigned int num_configs;
776 /* Do nothing if device is already closed */
777 if ( ! ( netdev->state & NETDEV_OPEN ) )
780 DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
782 /* Terminate any ongoing configurations. Use intf_close()
783 * rather than intf_restart() to allow the cancellation to be
784 * reported back to us if a configuration is actually in
787 num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
788 for ( i = 0 ; i < num_configs ; i++ )
789 intf_close ( &netdev->configs[i].job, -ECANCELED );
791 /* Remove from open devices list */
792 list_del ( &netdev->open_list );
795 netdev->state &= ~NETDEV_OPEN;
797 /* Notify drivers of device state change */
798 netdev_notify ( netdev );
800 /* Close the device */
801 netdev->op->close ( netdev );
803 /* Flush TX and RX queues */
804 netdev_tx_flush ( netdev );
805 netdev_rx_flush ( netdev );
809 * Unregister network device
811 * @v netdev Network device
813 * Removes the network device from the list of network devices.
815 void unregister_netdev ( struct net_device *netdev ) {
816 struct net_driver *driver;
818 /* Ensure device is closed */
819 netdev_close ( netdev );
822 for_each_table_entry_reverse ( driver, NET_DRIVERS ) {
823 if ( driver->remove )
824 driver->remove ( netdev );
827 /* Unregister per-netdev configuration settings */
828 clear_settings ( netdev_settings ( netdev ) );
829 unregister_settings ( netdev_settings ( netdev ) );
831 /* Remove from device list */
832 DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
833 list_del ( &netdev->list );
834 netdev_put ( netdev );
836 /* Reset network device index if no devices remain */
837 if ( list_empty ( &net_devices ) )
841 /** Enable or disable interrupts
843 * @v netdev Network device
844 * @v enable Interrupts should be enabled
846 void netdev_irq ( struct net_device *netdev, int enable ) {
848 /* Do nothing if device does not support interrupts */
849 if ( ! netdev_irq_supported ( netdev ) )
852 /* Enable or disable device interrupts */
853 netdev->op->irq ( netdev, enable );
855 /* Record interrupt enabled state */
856 netdev->state &= ~NETDEV_IRQ_ENABLED;
858 netdev->state |= NETDEV_IRQ_ENABLED;
862 * Get network device by name
864 * @v name Network device name
865 * @ret netdev Network device, or NULL
867 struct net_device * find_netdev ( const char *name ) {
868 struct net_device *netdev;
870 /* Allow "netX" shortcut */
871 if ( strcmp ( name, "netX" ) == 0 )
872 return last_opened_netdev();
874 /* Identify network device by name */
875 list_for_each_entry ( netdev, &net_devices, list ) {
876 if ( strcmp ( netdev->name, name ) == 0 )
884 * Get network device by index
886 * @v index Network device index
887 * @ret netdev Network device, or NULL
889 struct net_device * find_netdev_by_index ( unsigned int index ) {
890 struct net_device *netdev;
892 /* Identify network device by index */
893 list_for_each_entry ( netdev, &net_devices, list ) {
894 if ( netdev->index == index )
902 * Get network device by PCI bus:dev.fn address
904 * @v bus_type Bus type
905 * @v location Bus location
906 * @ret netdev Network device, or NULL
908 struct net_device * find_netdev_by_location ( unsigned int bus_type,
909 unsigned int location ) {
910 struct net_device *netdev;
912 list_for_each_entry ( netdev, &net_devices, list ) {
913 if ( ( netdev->dev->desc.bus_type == bus_type ) &&
914 ( netdev->dev->desc.location == location ) )
922 * Get network device by link-layer address
924 * @v ll_protocol Link-layer protocol
925 * @v ll_addr Link-layer address
926 * @ret netdev Network device, or NULL
928 struct net_device * find_netdev_by_ll_addr ( struct ll_protocol *ll_protocol,
929 const void *ll_addr ) {
930 struct net_device *netdev;
932 list_for_each_entry ( netdev, &net_devices, list ) {
933 if ( ( netdev->ll_protocol == ll_protocol ) &&
934 ( memcmp ( netdev->ll_addr, ll_addr,
935 ll_protocol->ll_addr_len ) == 0 ) )
943 * Get most recently opened network device
945 * @ret netdev Most recently opened network device, or NULL
947 struct net_device * last_opened_netdev ( void ) {
948 struct net_device *netdev;
950 netdev = list_first_entry ( &open_net_devices, struct net_device,
955 assert ( netdev_is_open ( netdev ) );
960 * Transmit network-layer packet
962 * @v iobuf I/O buffer
963 * @v netdev Network device
964 * @v net_protocol Network-layer protocol
965 * @v ll_dest Destination link-layer address
966 * @v ll_source Source link-layer address
967 * @ret rc Return status code
969 * Prepends link-layer headers to the I/O buffer and transmits the
970 * packet via the specified network device. This function takes
971 * ownership of the I/O buffer.
973 int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
974 struct net_protocol *net_protocol, const void *ll_dest,
975 const void *ll_source ) {
976 struct ll_protocol *ll_protocol = netdev->ll_protocol;
979 /* Add link-layer header */
980 if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, ll_source,
981 net_protocol->net_proto ) ) != 0 ) {
982 /* Record error for diagnosis */
983 netdev_tx_err ( netdev, iobuf, rc );
987 /* Transmit packet */
988 return netdev_tx ( netdev, iobuf );
992 * Process received network-layer packet
994 * @v iobuf I/O buffer
995 * @v netdev Network device
996 * @v net_proto Network-layer protocol, in network-byte order
997 * @v ll_dest Destination link-layer address
998 * @v ll_source Source link-layer address
999 * @v flags Packet flags
1000 * @ret rc Return status code
1002 int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
1003 uint16_t net_proto, const void *ll_dest, const void *ll_source,
1004 unsigned int flags ) {
1005 struct net_protocol *net_protocol;
1007 /* Hand off to network-layer protocol, if any */
1008 for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
1009 if ( net_protocol->net_proto == net_proto )
1010 return net_protocol->rx ( iobuf, netdev, ll_dest,
1014 DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
1015 netdev->name, ntohs ( net_proto ) );
1021 * Poll the network stack
1023 * This polls all interfaces for received packets, and processes
1024 * packets from the RX queue.
1026 void net_poll ( void ) {
1027 struct net_device *netdev;
1028 struct io_buffer *iobuf;
1029 struct ll_protocol *ll_protocol;
1030 const void *ll_dest;
1031 const void *ll_source;
1036 /* Poll and process each network device */
1037 list_for_each_entry ( netdev, &net_devices, list ) {
1039 /* Poll for new packets */
1040 profile_start ( &net_poll_profiler );
1041 netdev_poll ( netdev );
1042 profile_stop ( &net_poll_profiler );
1044 /* Leave received packets on the queue if receive
1045 * queue processing is currently frozen. This will
1046 * happen when the raw packets are to be manually
1047 * dequeued using netdev_rx_dequeue(), rather than
1048 * processed via the usual networking stack.
1050 if ( netdev_rx_frozen ( netdev ) )
1053 /* Process all received packets */
1054 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
1056 DBGC2 ( netdev, "NETDEV %s processing %p (%p+%zx)\n",
1057 netdev->name, iobuf, iobuf->data,
1058 iob_len ( iobuf ) );
1059 profile_start ( &net_rx_profiler );
1061 /* Remove link-layer header */
1062 ll_protocol = netdev->ll_protocol;
1063 if ( ( rc = ll_protocol->pull ( netdev, iobuf,
1064 &ll_dest, &ll_source,
1071 /* Hand packet to network layer */
1072 if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
1074 ll_source, flags ) ) != 0 ) {
1075 /* Record error for diagnosis */
1076 netdev_rx_err ( netdev, NULL, rc );
1078 profile_stop ( &net_rx_profiler );
1084 * Single-step the network stack
1086 * @v process Network stack process
1088 static void net_step ( struct process *process __unused ) {
1093 * Get the VLAN tag (when VLAN support is not present)
1095 * @v netdev Network device
1096 * @ret tag 0, indicating that device is not a VLAN device
1098 __weak unsigned int vlan_tag ( struct net_device *netdev __unused ) {
1103 * Identify VLAN device (when VLAN support is not present)
1105 * @v trunk Trunk network device
1107 * @ret netdev VLAN device, if any
1109 __weak struct net_device * vlan_find ( struct net_device *trunk __unused,
1110 unsigned int tag __unused ) {
1114 /** Networking stack process */
1115 PERMANENT_PROCESS ( net_process, net_step );
1118 * Discard some cached network device data
1120 * @ret discarded Number of cached items discarded
1122 static unsigned int net_discard ( void ) {
1123 struct net_device *netdev;
1124 struct io_buffer *iobuf;
1125 unsigned int discarded = 0;
1127 /* Try to drop one deferred TX packet from each network device */
1128 for_each_netdev ( netdev ) {
1129 if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
1131 list ) ) != NULL ) {
1133 /* Discard first deferred packet */
1134 list_del ( &iobuf->list );
1137 /* Report discard */
1145 /** Network device cache discarder */
1146 struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
1147 .discard = net_discard,
1151 * Find network device configurator
1154 * @ret configurator Network device configurator, or NULL
1156 struct net_device_configurator * find_netdev_configurator ( const char *name ) {
1157 struct net_device_configurator *configurator;
1159 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1160 if ( strcmp ( configurator->name, name ) == 0 )
1161 return configurator;
1167 * Start network device configuration
1169 * @v netdev Network device
1170 * @v configurator Network device configurator
1171 * @ret rc Return status code
1173 int netdev_configure ( struct net_device *netdev,
1174 struct net_device_configurator *configurator ) {
1175 struct net_device_configuration *config =
1176 netdev_configuration ( netdev, configurator );
1179 /* Check applicability of configurator */
1180 if ( ! netdev_configurator_applies ( netdev, configurator ) ) {
1181 DBGC ( netdev, "NETDEV %s does not support configuration via "
1182 "%s\n", netdev->name, configurator->name );
1186 /* Terminate any ongoing configuration */
1187 intf_restart ( &config->job, -ECANCELED );
1189 /* Mark configuration as being in progress */
1190 config->rc = -EINPROGRESS_CONFIG;
1192 DBGC ( netdev, "NETDEV %s starting configuration via %s\n",
1193 netdev->name, configurator->name );
1195 /* Start configuration */
1196 if ( ( rc = configurator->start ( &config->job, netdev ) ) != 0 ) {
1197 DBGC ( netdev, "NETDEV %s could not start configuration via "
1198 "%s: %s\n", netdev->name, configurator->name,
1208 * Start network device configuration via all supported configurators
1210 * @v netdev Network device
1211 * @ret rc Return status code
1213 int netdev_configure_all ( struct net_device *netdev ) {
1214 struct net_device_configurator *configurator;
1217 /* Start configuration for each configurator */
1218 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1220 /* Skip any inapplicable configurators */
1221 if ( ! netdev_configurator_applies ( netdev, configurator ) )
1224 /* Start configuration */
1225 if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 )
1233 * Check if network device has a configuration with a specified status code
1235 * @v netdev Network device
1237 * @ret has_rc Network device has a configuration with this status code
1239 static int netdev_has_configuration_rc ( struct net_device *netdev, int rc ) {
1240 unsigned int num_configs;
1243 num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
1244 for ( i = 0 ; i < num_configs ; i++ ) {
1245 if ( netdev->configs[i].rc == rc )
1252 * Check if network device configuration is in progress
1254 * @v netdev Network device
1255 * @ret is_in_progress Network device configuration is in progress
1257 int netdev_configuration_in_progress ( struct net_device *netdev ) {
1259 return netdev_has_configuration_rc ( netdev, -EINPROGRESS_CONFIG );
1263 * Check if network device has at least one successful configuration
1265 * @v netdev Network device
1266 * @v configurator Configurator
1267 * @ret rc Return status code
1269 int netdev_configuration_ok ( struct net_device *netdev ) {
1271 return netdev_has_configuration_rc ( netdev, 0 );