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
20 FILE_LICENCE ( GPL2_OR_LATER );
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>
43 * Network device management
47 /** List of network devices */
48 struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
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 );
53 /** Network device index */
54 static unsigned int netdev_index = 0;
56 /** Network polling profiler */
57 static struct profiler net_poll_profiler __profiler = { .name = "net.poll" };
59 /** Network receive profiler */
60 static struct profiler net_rx_profiler __profiler = { .name = "net.rx" };
62 /** Network transmit profiler */
63 static struct profiler net_tx_profiler __profiler = { .name = "net.tx" };
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" )
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" )
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" )
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" )
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 ),
94 * Check whether or not network device has a link-layer address
96 * @v netdev Network device
97 * @ret has_ll_addr Network device has a link-layer address
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 );
103 while ( remaining-- ) {
104 if ( *(ll_addr++) != 0 )
111 * Notify drivers of network device or link state change
113 * @v netdev Network device
115 static void netdev_notify ( struct net_device *netdev ) {
116 struct net_driver *driver;
118 for_each_table_entry ( driver, NET_DRIVERS ) {
119 if ( driver->notify )
120 driver->notify ( netdev );
125 * Freeze network device receive queue processing
127 * @v netdev Network device
129 void netdev_rx_freeze ( struct net_device *netdev ) {
131 /* Mark receive queue processing as frozen */
132 netdev->state |= NETDEV_RX_FROZEN;
134 /* Notify drivers of change */
135 netdev_notify ( netdev );
139 * Unfreeze network device receive queue processing
141 * @v netdev Network device
143 void netdev_rx_unfreeze ( struct net_device *netdev ) {
145 /* Mark receive queue processing as not frozen */
146 netdev->state &= ~NETDEV_RX_FROZEN;
148 /* Notify drivers of change */
149 netdev_notify ( netdev );
153 * Mark network device as having a specific link state
155 * @v netdev Network device
156 * @v rc Link status code
158 void netdev_link_err ( struct net_device *netdev, int rc ) {
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 );
165 DBGC ( netdev, "NETDEV %s link is down: %s\n",
166 netdev->name, strerror ( netdev->link_rc ) );
169 /* Notify drivers of link state change */
170 netdev_notify ( netdev );
174 * Mark network device as having link down
176 * @v netdev Network device
178 void netdev_link_down ( struct net_device *netdev ) {
180 /* Avoid clobbering a more detailed link status code, if one
183 if ( ( netdev->link_rc == 0 ) ||
184 ( netdev->link_rc == -EUNKNOWN_LINK_STATUS ) ) {
185 netdev_link_err ( netdev, -ENOTCONN_LINK_DOWN );
190 * Record network device statistic
192 * @v stats Network device statistics
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;
200 /* If this is not an error, just update the good counter */
206 /* Update the bad counter */
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 ) {
219 if ( error->count < least_common_error->count )
220 least_common_error = error;
223 /* Overwrite the least common error record */
224 least_common_error->rc = rc;
225 least_common_error->count = 1;
229 * Transmit raw packet via network device
231 * @v netdev Network device
232 * @v iobuf I/O buffer
233 * @ret rc Return status code
235 * Transmits the packet via the specified network device. This
236 * function takes ownership of the I/O buffer.
238 int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
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 );
246 list_add_tail ( &iobuf->list, &netdev->tx_queue );
248 /* Avoid calling transmit() on unopened network devices */
249 if ( ! netdev_is_open ( netdev ) ) {
254 /* Discard packet (for test purposes) if applicable */
255 if ( ( NETDEV_DISCARD_RATE > 0 ) &&
256 ( ( random() % NETDEV_DISCARD_RATE ) == 0 ) ) {
261 /* Transmit packet */
262 if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
265 profile_stop ( &net_tx_profiler );
269 netdev_tx_complete_err ( netdev, iobuf, rc );
274 * Defer transmitted packet
276 * @v netdev Network device
277 * @v iobuf I/O buffer
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()).
284 * The packet must currently be in the network device's TX queue.
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).
295 void netdev_tx_defer ( struct net_device *netdev, struct io_buffer *iobuf ) {
297 /* Catch data corruption as early as possible */
298 list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
300 /* Remove from transmit queue */
301 list_del ( &iobuf->list );
303 /* Add to deferred transmit queue */
304 list_add_tail ( &iobuf->list, &netdev->tx_deferred );
306 /* Record "out of space" statistic */
307 netdev_tx_err ( netdev, NULL, -ENOBUFS );
311 * Discard transmitted packet
313 * @v netdev Network device
314 * @v iobuf I/O buffer, or NULL
315 * @v rc Packet status code
317 * The packet is discarded and a TX error is recorded. This function
318 * takes ownership of the I/O buffer.
320 void netdev_tx_err ( struct net_device *netdev,
321 struct io_buffer *iobuf, int rc ) {
323 /* Update statistics counter */
324 netdev_record_stat ( &netdev->tx_stats, rc );
326 DBGC2 ( netdev, "NETDEV %s transmission %p complete\n",
327 netdev->name, iobuf );
329 DBGC ( netdev, "NETDEV %s transmission %p failed: %s\n",
330 netdev->name, iobuf, strerror ( rc ) );
338 * Complete network transmission
340 * @v netdev Network device
341 * @v iobuf I/O buffer
342 * @v rc Packet status code
344 * The packet must currently be in the network device's TX queue.
346 void netdev_tx_complete_err ( struct net_device *netdev,
347 struct io_buffer *iobuf, int rc ) {
349 /* Catch data corruption as early as possible */
350 list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
352 /* Dequeue and free I/O buffer */
353 list_del ( &iobuf->list );
354 netdev_tx_err ( netdev, iobuf, rc );
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 );
365 * Complete network transmission
367 * @v netdev Network device
368 * @v rc Packet status code
370 * Completes the oldest outstanding packet in the TX queue.
372 void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
373 struct io_buffer *iobuf;
375 if ( ( iobuf = list_first_entry ( &netdev->tx_queue, struct io_buffer,
377 netdev_tx_complete_err ( netdev, iobuf, rc );
382 * Flush device's transmit queue
384 * @v netdev Network device
386 static void netdev_tx_flush ( struct net_device *netdev ) {
388 /* Discard any packets in the TX queue. This will also cause
389 * any packets in the deferred TX queue to be discarded
392 while ( ! list_empty ( &netdev->tx_queue ) ) {
393 netdev_tx_complete_next_err ( netdev, -ECANCELED );
395 assert ( list_empty ( &netdev->tx_queue ) );
396 assert ( list_empty ( &netdev->tx_deferred ) );
400 * Add packet to receive queue
402 * @v netdev Network device
403 * @v iobuf I/O buffer, or NULL
405 * The packet is added to the network device's RX queue. This
406 * function takes ownership of the I/O buffer.
408 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
410 DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
411 netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
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 );
421 list_add_tail ( &iobuf->list, &netdev->rx_queue );
423 /* Update statistics counter */
424 netdev_record_stat ( &netdev->rx_stats, 0 );
428 * Discard received packet
430 * @v netdev Network device
431 * @v iobuf I/O buffer, or NULL
432 * @v rc Packet status code
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.
439 void netdev_rx_err ( struct net_device *netdev,
440 struct io_buffer *iobuf, int rc ) {
442 DBGC ( netdev, "NETDEV %s failed to receive %p: %s\n",
443 netdev->name, iobuf, strerror ( rc ) );
448 /* Update statistics counter */
449 netdev_record_stat ( &netdev->rx_stats, rc );
453 * Poll for completed and received packets on network device
455 * @v netdev Network device
457 * Polls the network device for completed transmissions and received
458 * packets. Any received packets will be added to the RX packet queue
461 void netdev_poll ( struct net_device *netdev ) {
463 if ( netdev_is_open ( netdev ) )
464 netdev->op->poll ( netdev );
468 * Remove packet from device's receive queue
470 * @v netdev Network device
471 * @ret iobuf I/O buffer, or NULL
473 * Removes the first packet from the device's RX queue and returns it.
474 * Ownership of the packet is transferred to the caller.
476 struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
477 struct io_buffer *iobuf;
479 iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
483 list_del ( &iobuf->list );
488 * Flush device's receive queue
490 * @v netdev Network device
492 static void netdev_rx_flush ( struct net_device *netdev ) {
493 struct io_buffer *iobuf;
495 /* Discard any packets in the RX queue */
496 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
497 netdev_rx_err ( netdev, iobuf, -ECANCELED );
502 * Finish network device configuration
504 * @v config Network device configuration
505 * @v rc Reason for completion
507 static void netdev_config_close ( struct net_device_configuration *config,
509 struct net_device_configurator *configurator = config->configurator;
510 struct net_device *netdev = config->netdev;
512 /* Restart interface */
513 intf_restart ( &config->job, rc );
515 /* Record configuration result */
518 DBGC ( netdev, "NETDEV %s configured via %s\n",
519 netdev->name, configurator->name );
521 DBGC ( netdev, "NETDEV %s configuration via %s failed: %s\n",
522 netdev->name, configurator->name, strerror ( rc ) );
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 ),
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 );
537 * Free network device
539 * @v refcnt Network device reference counter
541 static void free_netdev ( struct refcnt *refcnt ) {
542 struct net_device *netdev =
543 container_of ( refcnt, struct net_device, refcnt );
545 netdev_tx_flush ( netdev );
546 netdev_rx_flush ( netdev );
547 clear_settings ( netdev_settings ( netdev ) );
552 * Allocate network device
554 * @v priv_len Length of private data area (net_device::priv)
555 * @ret netdev Network device, or NULL
557 * Allocates space for a network device and its private data area.
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;
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 );
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,
587 netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) +
594 * Register network device
596 * @v netdev Network device
597 * @ret rc Return status code
599 * Gives the network device a name and adds it to the list of network
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;
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 );
614 /* Reject network devices that are already available via a
615 * different hardware device.
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 );
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",
633 /* Use least significant bits of the link-layer address to
634 * improve the randomness of the (non-cryptographic) random
637 memcpy ( &seed, ( netdev->ll_addr + ll_protocol->ll_addr_len
638 - sizeof ( seed ) ), sizeof ( seed ) );
639 srand ( rand() ^ seed );
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 ) );
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;
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,
669 for_each_table_entry_continue_reverse ( driver, NET_DRIVERS ) {
670 if ( driver->remove )
671 driver->remove ( netdev );
673 clear_settings ( netdev_settings ( netdev ) );
674 unregister_settings ( netdev_settings ( netdev ) );
675 err_register_settings:
681 * Open network device
683 * @v netdev Network device
684 * @ret rc Return status code
686 int netdev_open ( struct net_device *netdev ) {
689 /* Do nothing if device is already open */
690 if ( netdev->state & NETDEV_OPEN )
693 DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
696 netdev->state |= NETDEV_OPEN;
698 /* Open the device */
699 if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
702 /* Add to head of open devices list */
703 list_add ( &netdev->open_list, &open_net_devices );
705 /* Notify drivers of device state change */
706 netdev_notify ( netdev );
711 netdev->state &= ~NETDEV_OPEN;
716 * Close network device
718 * @v netdev Network device
720 void netdev_close ( struct net_device *netdev ) {
721 unsigned int num_configs;
724 /* Do nothing if device is already closed */
725 if ( ! ( netdev->state & NETDEV_OPEN ) )
728 DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
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
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 );
739 /* Remove from open devices list */
740 list_del ( &netdev->open_list );
743 netdev->state &= ~NETDEV_OPEN;
745 /* Notify drivers of device state change */
746 netdev_notify ( netdev );
748 /* Close the device */
749 netdev->op->close ( netdev );
751 /* Flush TX and RX queues */
752 netdev_tx_flush ( netdev );
753 netdev_rx_flush ( netdev );
757 * Unregister network device
759 * @v netdev Network device
761 * Removes the network device from the list of network devices.
763 void unregister_netdev ( struct net_device *netdev ) {
764 struct net_driver *driver;
766 /* Ensure device is closed */
767 netdev_close ( netdev );
770 for_each_table_entry_reverse ( driver, NET_DRIVERS ) {
771 if ( driver->remove )
772 driver->remove ( netdev );
775 /* Unregister per-netdev configuration settings */
776 clear_settings ( netdev_settings ( netdev ) );
777 unregister_settings ( netdev_settings ( netdev ) );
779 /* Remove from device list */
780 DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
781 list_del ( &netdev->list );
782 netdev_put ( netdev );
784 /* Reset network device index if no devices remain */
785 if ( list_empty ( &net_devices ) )
789 /** Enable or disable interrupts
791 * @v netdev Network device
792 * @v enable Interrupts should be enabled
794 void netdev_irq ( struct net_device *netdev, int enable ) {
796 /* Do nothing if device does not support interrupts */
797 if ( ! netdev_irq_supported ( netdev ) )
800 /* Enable or disable device interrupts */
801 netdev->op->irq ( netdev, enable );
803 /* Record interrupt enabled state */
804 netdev->state &= ~NETDEV_IRQ_ENABLED;
806 netdev->state |= NETDEV_IRQ_ENABLED;
810 * Get network device by name
812 * @v name Network device name
813 * @ret netdev Network device, or NULL
815 struct net_device * find_netdev ( const char *name ) {
816 struct net_device *netdev;
818 /* Allow "netX" shortcut */
819 if ( strcmp ( name, "netX" ) == 0 )
820 return last_opened_netdev();
822 /* Identify network device by name */
823 list_for_each_entry ( netdev, &net_devices, list ) {
824 if ( strcmp ( netdev->name, name ) == 0 )
832 * Get network device by index
834 * @v index Network device index
835 * @ret netdev Network device, or NULL
837 struct net_device * find_netdev_by_index ( unsigned int index ) {
838 struct net_device *netdev;
840 /* Identify network device by index */
841 list_for_each_entry ( netdev, &net_devices, list ) {
842 if ( netdev->index == index )
850 * Get network device by PCI bus:dev.fn address
852 * @v bus_type Bus type
853 * @v location Bus location
854 * @ret netdev Network device, or NULL
856 struct net_device * find_netdev_by_location ( unsigned int bus_type,
857 unsigned int location ) {
858 struct net_device *netdev;
860 list_for_each_entry ( netdev, &net_devices, list ) {
861 if ( ( netdev->dev->desc.bus_type == bus_type ) &&
862 ( netdev->dev->desc.location == location ) )
870 * Get network device by link-layer address
872 * @v ll_protocol Link-layer protocol
873 * @v ll_addr Link-layer address
874 * @ret netdev Network device, or NULL
876 struct net_device * find_netdev_by_ll_addr ( struct ll_protocol *ll_protocol,
877 const void *ll_addr ) {
878 struct net_device *netdev;
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 ) )
891 * Get most recently opened network device
893 * @ret netdev Most recently opened network device, or NULL
895 struct net_device * last_opened_netdev ( void ) {
896 struct net_device *netdev;
898 netdev = list_first_entry ( &open_net_devices, struct net_device,
903 assert ( netdev_is_open ( netdev ) );
908 * Transmit network-layer packet
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
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.
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;
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 );
935 /* Transmit packet */
936 return netdev_tx ( netdev, iobuf );
940 * Process received network-layer packet
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
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;
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,
962 DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
963 netdev->name, ntohs ( net_proto ) );
969 * Poll the network stack
971 * This polls all interfaces for received packets, and processes
972 * packets from the RX queue.
974 void net_poll ( void ) {
975 struct net_device *netdev;
976 struct io_buffer *iobuf;
977 struct ll_protocol *ll_protocol;
979 const void *ll_source;
984 /* Poll and process each network device */
985 list_for_each_entry ( netdev, &net_devices, list ) {
987 /* Poll for new packets */
988 profile_start ( &net_poll_profiler );
989 netdev_poll ( netdev );
990 profile_stop ( &net_poll_profiler );
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.
998 if ( netdev_rx_frozen ( netdev ) )
1001 /* Process all received packets */
1002 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
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 );
1009 /* Remove link-layer header */
1010 ll_protocol = netdev->ll_protocol;
1011 if ( ( rc = ll_protocol->pull ( netdev, iobuf,
1012 &ll_dest, &ll_source,
1019 /* Hand packet to network layer */
1020 if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
1022 ll_source, flags ) ) != 0 ) {
1023 /* Record error for diagnosis */
1024 netdev_rx_err ( netdev, NULL, rc );
1026 profile_stop ( &net_rx_profiler );
1032 * Single-step the network stack
1034 * @v process Network stack process
1036 static void net_step ( struct process *process __unused ) {
1041 * Get the VLAN tag (when VLAN support is not present)
1043 * @v netdev Network device
1044 * @ret tag 0, indicating that device is not a VLAN device
1046 __weak unsigned int vlan_tag ( struct net_device *netdev __unused ) {
1051 * Identify VLAN device (when VLAN support is not present)
1053 * @v trunk Trunk network device
1055 * @ret netdev VLAN device, if any
1057 __weak struct net_device * vlan_find ( struct net_device *trunk __unused,
1058 unsigned int tag __unused ) {
1062 /** Networking stack process */
1063 PERMANENT_PROCESS ( net_process, net_step );
1066 * Discard some cached network device data
1068 * @ret discarded Number of cached items discarded
1070 static unsigned int net_discard ( void ) {
1071 struct net_device *netdev;
1072 struct io_buffer *iobuf;
1073 unsigned int discarded = 0;
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,
1079 list ) ) != NULL ) {
1081 /* Discard first deferred packet */
1082 list_del ( &iobuf->list );
1085 /* Report discard */
1093 /** Network device cache discarder */
1094 struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
1095 .discard = net_discard,
1099 * Find network device configurator
1102 * @ret configurator Network device configurator, or NULL
1104 struct net_device_configurator * find_netdev_configurator ( const char *name ) {
1105 struct net_device_configurator *configurator;
1107 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1108 if ( strcmp ( configurator->name, name ) == 0 )
1109 return configurator;
1115 * Start network device configuration
1117 * @v netdev Network device
1118 * @v configurator Network device configurator
1119 * @ret rc Return status code
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 );
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 );
1134 /* Terminate any ongoing configuration */
1135 intf_restart ( &config->job, -ECANCELED );
1137 /* Mark configuration as being in progress */
1138 config->rc = -EINPROGRESS_CONFIG;
1140 DBGC ( netdev, "NETDEV %s starting configuration via %s\n",
1141 netdev->name, configurator->name );
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,
1156 * Start network device configuration via all supported configurators
1158 * @v netdev Network device
1159 * @ret rc Return status code
1161 int netdev_configure_all ( struct net_device *netdev ) {
1162 struct net_device_configurator *configurator;
1165 /* Start configuration for each configurator */
1166 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1168 /* Skip any inapplicable configurators */
1169 if ( ! netdev_configurator_applies ( netdev, configurator ) )
1172 /* Start configuration */
1173 if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 )
1181 * Check if network device has a configuration with a specified status code
1183 * @v netdev Network device
1185 * @ret has_rc Network device has a configuration with this status code
1187 static int netdev_has_configuration_rc ( struct net_device *netdev, int rc ) {
1188 unsigned int num_configs;
1191 num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
1192 for ( i = 0 ; i < num_configs ; i++ ) {
1193 if ( netdev->configs[i].rc == rc )
1200 * Check if network device configuration is in progress
1202 * @v netdev Network device
1203 * @ret is_in_progress Network device configuration is in progress
1205 int netdev_configuration_in_progress ( struct net_device *netdev ) {
1207 return netdev_has_configuration_rc ( netdev, -EINPROGRESS_CONFIG );
1211 * Check if network device has at least one successful configuration
1213 * @v netdev Network device
1214 * @v configurator Configurator
1215 * @ret rc Return status code
1217 int netdev_configuration_ok ( struct net_device *netdev ) {
1219 return netdev_has_configuration_rc ( netdev, 0 );