iPXE
netdevice.c
Go to the documentation of this file.
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  * 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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <byteswap.h>
30 #include <string.h>
31 #include <errno.h>
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>
45 
46 /** @file
47  *
48  * Network device management
49  *
50  */
51 
52 /** List of network devices */
54 
55 /** List of open network devices, in reverse order of opening */
57 
58 /** Network polling profiler */
59 static struct profiler net_poll_profiler __profiler = { .name = "net.poll" };
60 
61 /** Network receive profiler */
62 static struct profiler net_rx_profiler __profiler = { .name = "net.rx" };
63 
64 /** Network transmit profiler */
65 static struct profiler net_tx_profiler __profiler = { .name = "net.tx" };
66 
67 /** Default unknown link status code */
68 #define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
69 #define EINFO_EUNKNOWN_LINK_STATUS \
70  __einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )
71 
72 /** Default not-yet-attempted-configuration status code */
73 #define EUNUSED_CONFIG __einfo_error ( EINFO_EUNUSED_CONFIG )
74 #define EINFO_EUNUSED_CONFIG \
75  __einfo_uniqify ( EINFO_EINPROGRESS, 0x02, "Unused" )
76 
77 /** Default configuration-in-progress status code */
78 #define EINPROGRESS_CONFIG __einfo_error ( EINFO_EINPROGRESS_CONFIG )
79 #define EINFO_EINPROGRESS_CONFIG \
80  __einfo_uniqify ( EINFO_EINPROGRESS, 0x03, "Incomplete" )
81 
82 /** Default link-down status code */
83 #define ENOTCONN_LINK_DOWN __einfo_error ( EINFO_ENOTCONN_LINK_DOWN )
84 #define EINFO_ENOTCONN_LINK_DOWN \
85  __einfo_uniqify ( EINFO_ENOTCONN, 0x01, "Down" )
86 
87 /** Human-readable message for the default link statuses */
88 struct errortab netdev_errors[] __errortab = {
93 };
94 
95 /**
96  * Check whether or not network device has a link-layer address
97  *
98  * @v netdev Network device
99  * @ret has_ll_addr Network device has a link-layer address
100  */
101 static int netdev_has_ll_addr ( struct net_device *netdev ) {
102  uint8_t *ll_addr = netdev->ll_addr;
103  size_t remaining = sizeof ( netdev->ll_addr );
104 
105  while ( remaining-- ) {
106  if ( *(ll_addr++) != 0 )
107  return 1;
108  }
109  return 0;
110 }
111 
112 /**
113  * Notify drivers of network device or link state change
114  *
115  * @v netdev Network device
116  */
117 static void netdev_notify ( struct net_device *netdev ) {
118  struct net_driver *driver;
119 
120  for_each_table_entry ( driver, NET_DRIVERS ) {
121  if ( driver->notify )
122  driver->notify ( netdev );
123  }
124 }
125 
126 /**
127  * Freeze network device receive queue processing
128  *
129  * @v netdev Network device
130  */
132 
133  /* Mark receive queue processing as frozen */
135 
136  /* Notify drivers of change */
137  netdev_notify ( netdev );
138 }
139 
140 /**
141  * Unfreeze network device receive queue processing
142  *
143  * @v netdev Network device
144  */
146 
147  /* Mark receive queue processing as not frozen */
149 
150  /* Notify drivers of change */
151  netdev_notify ( netdev );
152 }
153 
154 /**
155  * Mark network device as having a specific link state
156  *
157  * @v netdev Network device
158  * @v rc Link status code
159  */
160 void netdev_link_err ( struct net_device *netdev, int rc ) {
161 
162  /* Stop link block timer */
164 
165  /* Record link state */
166  netdev->link_rc = rc;
167  if ( netdev->link_rc == 0 ) {
168  DBGC ( netdev, "NETDEV %s link is up\n", netdev->name );
169  } else {
170  DBGC ( netdev, "NETDEV %s link is down: %s\n",
171  netdev->name, strerror ( netdev->link_rc ) );
172  }
173 
174  /* Notify drivers of link state change */
175  netdev_notify ( netdev );
176 }
177 
178 /**
179  * Mark network device as having link down
180  *
181  * @v netdev Network device
182  */
184 
185  /* Avoid clobbering a more detailed link status code, if one
186  * is already set.
187  */
188  if ( ( netdev->link_rc == 0 ) ||
189  ( netdev->link_rc == -EUNKNOWN_LINK_STATUS ) ) {
191  }
192 }
193 
194 /**
195  * Mark network device link as being blocked
196  *
197  * @v netdev Network device
198  * @v timeout Timeout (in ticks)
199  */
200 void netdev_link_block ( struct net_device *netdev, unsigned long timeout ) {
201 
202  /* Start link block timer */
203  if ( ! netdev_link_blocked ( netdev ) ) {
204  DBGC ( netdev, "NETDEV %s link blocked for %ld ticks\n",
205  netdev->name, timeout );
206  }
208 }
209 
210 /**
211  * Mark network device link as being unblocked
212  *
213  * @v netdev Network device
214  */
216 
217  /* Stop link block timer */
218  if ( netdev_link_blocked ( netdev ) )
219  DBGC ( netdev, "NETDEV %s link unblocked\n", netdev->name );
221 }
222 
223 /**
224  * Handle network device link block timer expiry
225  *
226  * @v timer Link block timer
227  * @v fail Failure indicator
228  */
230  int fail __unused ) {
231  struct net_device *netdev =
233 
234  /* Assume link is no longer blocked */
235  DBGC ( netdev, "NETDEV %s link block expired\n", netdev->name );
236 }
237 
238 /**
239  * Record network device statistic
240  *
241  * @v stats Network device statistics
242  * @v rc Status code
243  */
244 static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
245  struct net_device_error *error;
246  struct net_device_error *least_common_error;
247  unsigned int i;
248 
249  /* If this is not an error, just update the good counter */
250  if ( rc == 0 ) {
251  stats->good++;
252  return;
253  }
254 
255  /* Update the bad counter */
256  stats->bad++;
257 
258  /* Locate the appropriate error record */
259  least_common_error = &stats->errors[0];
260  for ( i = 0 ; i < ( sizeof ( stats->errors ) /
261  sizeof ( stats->errors[0] ) ) ; i++ ) {
262  error = &stats->errors[i];
263  /* Update matching record, if found */
264  if ( error->rc == rc ) {
265  error->count++;
266  return;
267  }
268  if ( error->count < least_common_error->count )
269  least_common_error = error;
270  }
271 
272  /* Overwrite the least common error record */
273  least_common_error->rc = rc;
274  least_common_error->count = 1;
275 }
276 
277 /**
278  * Transmit raw packet via network device
279  *
280  * @v netdev Network device
281  * @v iobuf I/O buffer
282  * @ret rc Return status code
283  *
284  * Transmits the packet via the specified network device. This
285  * function takes ownership of the I/O buffer.
286  */
287 int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
288  int rc;
289 
290  DBGC2 ( netdev, "NETDEV %s transmitting %p (%p+%zx)\n",
291  netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
292  profile_start ( &net_tx_profiler );
293 
294  /* Enqueue packet */
295  list_add_tail ( &iobuf->list, &netdev->tx_queue );
296 
297  /* Guard against re-entry */
299  rc = -EBUSY;
300  goto err_busy;
301  }
303 
304  /* Avoid calling transmit() on unopened network devices */
305  if ( ! netdev_is_open ( netdev ) ) {
306  rc = -ENETUNREACH;
307  goto err_closed;
308  }
309 
310  /* Discard packet (for test purposes) if applicable */
311  if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 )
312  goto err_fault;
313 
314  /* Map for DMA, if required */
315  if ( netdev->dma && ( ! dma_mapped ( &iobuf->map ) ) ) {
316  if ( ( rc = iob_map_tx ( iobuf, netdev->dma ) ) != 0 )
317  goto err_map;
318  }
319 
320  /* Transmit packet */
321  if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
322  goto err_transmit;
323 
324  /* Clear in-progress flag */
326 
327  profile_stop ( &net_tx_profiler );
328  return 0;
329 
330  err_transmit:
331  err_map:
332  err_fault:
333  err_closed:
335  err_busy:
336  netdev_tx_complete_err ( netdev, iobuf, rc );
337  return rc;
338 }
339 
340 /**
341  * Defer transmitted packet
342  *
343  * @v netdev Network device
344  * @v iobuf I/O buffer
345  *
346  * Drivers may call netdev_tx_defer() if there is insufficient space
347  * in the transmit descriptor ring. Any packets deferred in this way
348  * will be automatically retransmitted as soon as space becomes
349  * available (i.e. as soon as the driver calls netdev_tx_complete()).
350  *
351  * The packet must currently be in the network device's TX queue.
352  *
353  * Drivers utilising netdev_tx_defer() must ensure that space in the
354  * transmit descriptor ring is freed up @b before calling
355  * netdev_tx_complete(). For example, if the ring is modelled using a
356  * producer counter and a consumer counter, then the consumer counter
357  * must be incremented before the call to netdev_tx_complete().
358  * Failure to do this will cause the retransmitted packet to be
359  * immediately redeferred (which will result in out-of-order
360  * transmissions and other nastiness).
361  *
362  * I/O buffers that have been mapped for DMA will remain mapped while
363  * present in the deferred transmit queue.
364  */
365 void netdev_tx_defer ( struct net_device *netdev, struct io_buffer *iobuf ) {
366 
367  /* Catch data corruption as early as possible */
368  list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
369 
370  /* Remove from transmit queue */
371  list_del ( &iobuf->list );
372 
373  /* Add to deferred transmit queue */
374  list_add_tail ( &iobuf->list, &netdev->tx_deferred );
375 
376  /* Record "out of space" statistic */
378 }
379 
380 /**
381  * Discard transmitted packet
382  *
383  * @v netdev Network device
384  * @v iobuf I/O buffer, or NULL
385  * @v rc Packet status code
386  *
387  * The packet is discarded and a TX error is recorded. This function
388  * takes ownership of the I/O buffer.
389  *
390  * The I/O buffer will be automatically unmapped for DMA, if
391  * applicable.
392  */
394  struct io_buffer *iobuf, int rc ) {
395 
396  /* Update statistics counter */
398  if ( rc == 0 ) {
399  DBGC2 ( netdev, "NETDEV %s transmission %p complete\n",
400  netdev->name, iobuf );
401  } else {
402  DBGC ( netdev, "NETDEV %s transmission %p failed: %s\n",
403  netdev->name, iobuf, strerror ( rc ) );
404  }
405 
406  /* Unmap I/O buffer, if required */
407  if ( iobuf && dma_mapped ( &iobuf->map ) )
408  iob_unmap ( iobuf );
409 
410  /* Discard packet */
411  free_iob ( iobuf );
412 }
413 
414 /**
415  * Complete network transmission
416  *
417  * @v netdev Network device
418  * @v iobuf I/O buffer
419  * @v rc Packet status code
420  *
421  * The packet must currently be in the network device's TX queue.
422  */
424  struct io_buffer *iobuf, int rc ) {
425 
426  /* Catch data corruption as early as possible */
427  list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
428 
429  /* Dequeue and free I/O buffer */
430  list_del ( &iobuf->list );
431  netdev_tx_err ( netdev, iobuf, rc );
432 
433  /* Handle pending transmit queue */
434  while ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
435  struct io_buffer, list ) ) ) {
436 
437  /* Remove from pending transmit queue */
438  list_del ( &iobuf->list );
439 
440  /* When any transmit completion fails, cancel all
441  * pending transmissions.
442  */
443  if ( rc != 0 ) {
444  netdev_tx_err ( netdev, iobuf, -ECANCELED );
445  continue;
446  }
447 
448  /* Otherwise, attempt to transmit the first pending packet */
449  netdev_tx ( netdev, iobuf );
450  break;
451  }
452 }
453 
454 /**
455  * Complete network transmission
456  *
457  * @v netdev Network device
458  * @v rc Packet status code
459  *
460  * Completes the oldest outstanding packet in the TX queue.
461  */
463  struct io_buffer *iobuf;
464 
465  if ( ( iobuf = list_first_entry ( &netdev->tx_queue, struct io_buffer,
466  list ) ) != NULL ) {
467  netdev_tx_complete_err ( netdev, iobuf, rc );
468  }
469 }
470 
471 /**
472  * Flush device's transmit queue
473  *
474  * @v netdev Network device
475  */
476 static void netdev_tx_flush ( struct net_device *netdev ) {
477 
478  /* Discard any packets in the TX queue. This will also cause
479  * any packets in the deferred TX queue to be discarded
480  * automatically.
481  */
482  while ( ! list_empty ( &netdev->tx_queue ) ) {
484  }
485  assert ( list_empty ( &netdev->tx_queue ) );
487 }
488 
489 /**
490  * Add packet to receive queue
491  *
492  * @v netdev Network device
493  * @v iobuf I/O buffer
494  *
495  * The packet is added to the network device's RX queue. This
496  * function takes ownership of the I/O buffer.
497  *
498  * The I/O buffer will be automatically unmapped for DMA, if
499  * applicable.
500  */
501 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
502  int rc;
503 
504  DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
505  netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
506 
507  /* Discard packet (for test purposes) if applicable */
508  if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 ) {
509  netdev_rx_err ( netdev, iobuf, rc );
510  return;
511  }
512 
513  /* Unmap I/O buffer, if required */
514  if ( dma_mapped ( &iobuf->map ) )
515  iob_unmap ( iobuf );
516 
517  /* Enqueue packet */
518  list_add_tail ( &iobuf->list, &netdev->rx_queue );
519 
520  /* Update statistics counter */
522 }
523 
524 /**
525  * Discard received packet
526  *
527  * @v netdev Network device
528  * @v iobuf I/O buffer, or NULL
529  * @v rc Packet status code
530  *
531  * The packet is discarded and an RX error is recorded. This function
532  * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
533  * example, the net device wishes to report an error due to being
534  * unable to allocate an I/O buffer.
535  *
536  * The I/O buffer will be automatically unmapped for DMA, if
537  * applicable.
538  */
540  struct io_buffer *iobuf, int rc ) {
541 
542  DBGC ( netdev, "NETDEV %s failed to receive %p: %s\n",
543  netdev->name, iobuf, strerror ( rc ) );
544 
545  /* Unmap I/O buffer, if required */
546  if ( iobuf && dma_mapped ( &iobuf->map ) )
547  iob_unmap ( iobuf );
548 
549  /* Discard packet */
550  free_iob ( iobuf );
551 
552  /* Update statistics counter */
554 }
555 
556 /**
557  * Poll for completed and received packets on network device
558  *
559  * @v netdev Network device
560  *
561  * Polls the network device for completed transmissions and received
562  * packets. Any received packets will be added to the RX packet queue
563  * via netdev_rx().
564  */
565 void netdev_poll ( struct net_device *netdev ) {
566 
567  /* Avoid calling poll() on unopened network devices */
568  if ( ! netdev_is_open ( netdev ) )
569  return;
570 
571  /* Guard against re-entry */
573  return;
574 
575  /* Poll device */
577  netdev->op->poll ( netdev );
579 }
580 
581 /**
582  * Remove packet from device's receive queue
583  *
584  * @v netdev Network device
585  * @ret iobuf I/O buffer, or NULL
586  *
587  * Removes the first packet from the device's RX queue and returns it.
588  * Ownership of the packet is transferred to the caller.
589  */
591  struct io_buffer *iobuf;
592 
593  iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
594  if ( ! iobuf )
595  return NULL;
596 
597  list_del ( &iobuf->list );
598  return iobuf;
599 }
600 
601 /**
602  * Flush device's receive queue
603  *
604  * @v netdev Network device
605  */
606 static void netdev_rx_flush ( struct net_device *netdev ) {
607  struct io_buffer *iobuf;
608 
609  /* Discard any packets in the RX queue */
610  while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
611  netdev_rx_err ( netdev, iobuf, -ECANCELED );
612  }
613 }
614 
615 /**
616  * Finish network device configuration
617  *
618  * @v config Network device configuration
619  * @v rc Reason for completion
620  */
621 static void netdev_config_close ( struct net_device_configuration *config,
622  int rc ) {
623  struct net_device_configurator *configurator = config->configurator;
624  struct net_device *netdev = config->netdev;
625 
626  /* Restart interface */
627  intf_restart ( &config->job, rc );
628 
629  /* Record configuration result */
630  config->rc = rc;
631  if ( rc == 0 ) {
632  DBGC ( netdev, "NETDEV %s configured via %s\n",
633  netdev->name, configurator->name );
634  } else {
635  DBGC ( netdev, "NETDEV %s configuration via %s failed: %s\n",
636  netdev->name, configurator->name, strerror ( rc ) );
637  }
638 }
639 
640 /** Network device configuration interface operations */
644 };
645 
646 /** Network device configuration interface descriptor */
649 
650 /**
651  * Free network device
652  *
653  * @v refcnt Network device reference counter
654  */
655 static void free_netdev ( struct refcnt *refcnt ) {
656  struct net_device *netdev =
657  container_of ( refcnt, struct net_device, refcnt );
658 
663  free ( netdev );
664 }
665 
666 /**
667  * Allocate network device
668  *
669  * @v priv_len Length of private data area (net_device::priv)
670  * @ret netdev Network device, or NULL
671  *
672  * Allocates space for a network device and its private data area.
673  */
674 struct net_device * alloc_netdev ( size_t priv_len ) {
675  struct net_device *netdev;
676  struct net_device_configurator *configurator;
677  struct net_device_configuration *config;
678  unsigned int num_configs;
679  size_t confs_len;
680  size_t total_len;
681 
683  confs_len = ( num_configs * sizeof ( netdev->configs[0] ) );
684  total_len = ( sizeof ( *netdev ) + confs_len + priv_len );
685  netdev = zalloc ( total_len );
686  if ( netdev ) {
690  &netdev->refcnt );
695  config = netdev->configs;
697  config->netdev = netdev;
698  config->configurator = configurator;
699  config->rc = -EUNUSED_CONFIG;
700  intf_init ( &config->job, &netdev_config_desc,
701  &netdev->refcnt );
702  config++;
703  }
704  netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) +
705  confs_len );
706  }
707  return netdev;
708 }
709 
710 /**
711  * Register network device
712  *
713  * @v netdev Network device
714  * @ret rc Return status code
715  *
716  * Gives the network device a name and adds it to the list of network
717  * devices.
718  */
721  struct net_driver *driver;
722  struct net_device *duplicate;
723  unsigned int i;
724  uint32_t seed;
725  int rc;
726 
727  /* Set initial link-layer address, if not already set */
728  if ( ! netdev_has_ll_addr ( netdev ) ) {
730  }
731 
732  /* Set MTU, if not already set */
733  if ( ! netdev->mtu ) {
734  netdev->mtu = ( netdev->max_pkt_len -
736  }
737 
738  /* Reject named network devices that already exist */
739  if ( netdev->name[0] && ( duplicate = find_netdev ( netdev->name ) ) ) {
740  DBGC ( netdev, "NETDEV rejecting duplicate name %s\n",
741  duplicate->name );
742  rc = -EEXIST;
743  goto err_duplicate;
744  }
745 
746  /* Assign a unique device name, if not already set */
747  if ( netdev->name[0] == '\0' ) {
748  for ( i = 0 ; ; i++ ) {
749  snprintf ( netdev->name, sizeof ( netdev->name ),
750  "net%d", i );
751  if ( find_netdev ( netdev->name ) == NULL )
752  break;
753  }
754  }
755 
756  /* Assign a unique non-zero scope ID */
757  for ( netdev->scope_id = 1 ; ; netdev->scope_id++ ) {
759  break;
760  }
761 
762  /* Use least significant bits of the link-layer address to
763  * improve the randomness of the (non-cryptographic) random
764  * number generator.
765  */
767  - sizeof ( seed ) ), sizeof ( seed ) );
768  srand ( rand() ^ seed );
769 
770  /* Add to device list */
771  netdev_get ( netdev );
773  DBGC ( netdev, "NETDEV %s registered (phys %s hwaddr %s)\n",
774  netdev->name, netdev->dev->name,
775  netdev_addr ( netdev ) );
776 
777  /* Register per-netdev configuration settings */
779  NULL, netdev->name ) ) != 0 ) {
780  DBGC ( netdev, "NETDEV %s could not register settings: %s\n",
781  netdev->name, strerror ( rc ) );
782  goto err_register_settings;
783  }
784 
785  /* Probe device */
786  for_each_table_entry ( driver, NET_DRIVERS ) {
787  if ( driver->probe && ( rc = driver->probe ( netdev ) ) != 0 ) {
788  DBGC ( netdev, "NETDEV %s could not add %s device: "
789  "%s\n", netdev->name, driver->name,
790  strerror ( rc ) );
791  goto err_probe;
792  }
793  }
794 
795  return 0;
796 
797  err_probe:
799  if ( driver->remove )
800  driver->remove ( netdev );
801  }
804  err_register_settings:
805  list_del ( &netdev->list );
806  netdev_put ( netdev );
807  err_duplicate:
808  return rc;
809 }
810 
811 /**
812  * Open network device
813  *
814  * @v netdev Network device
815  * @ret rc Return status code
816  */
817 int netdev_open ( struct net_device *netdev ) {
818  int rc;
819 
820  /* Do nothing if device is already open */
821  if ( netdev->state & NETDEV_OPEN )
822  return 0;
823 
824  DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
825 
826  /* Mark as opened */
828 
829  /* Open the device */
830  if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
831  goto err;
832 
833  /* Add to head of open devices list */
835 
836  /* Notify drivers of device state change */
837  netdev_notify ( netdev );
838 
839  return 0;
840 
841  err:
842  netdev->state &= ~NETDEV_OPEN;
843  return rc;
844 }
845 
846 /**
847  * Close network device
848  *
849  * @v netdev Network device
850  */
851 void netdev_close ( struct net_device *netdev ) {
852  unsigned int num_configs;
853  unsigned int i;
854 
855  /* Do nothing if device is already closed */
856  if ( ! ( netdev->state & NETDEV_OPEN ) )
857  return;
858 
859  DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
860 
861  /* Terminate any ongoing configurations. Use intf_close()
862  * rather than intf_restart() to allow the cancellation to be
863  * reported back to us if a configuration is actually in
864  * progress.
865  */
867  for ( i = 0 ; i < num_configs ; i++ )
869 
870  /* Remove from open devices list */
871  list_del ( &netdev->open_list );
872 
873  /* Mark as closed */
874  netdev->state &= ~NETDEV_OPEN;
875 
876  /* Notify drivers of device state change */
877  netdev_notify ( netdev );
878 
879  /* Close the device */
880  netdev->op->close ( netdev );
881 
882  /* Flush TX and RX queues */
885 }
886 
887 /**
888  * Unregister network device
889  *
890  * @v netdev Network device
891  *
892  * Removes the network device from the list of network devices.
893  */
895  struct net_driver *driver;
896 
897  /* Ensure device is closed */
898  netdev_close ( netdev );
899 
900  /* Remove device */
902  if ( driver->remove )
903  driver->remove ( netdev );
904  }
905 
906  /* Unregister per-netdev configuration settings */
909 
910  /* Remove from device list */
911  DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
912  list_del ( &netdev->list );
913  netdev_put ( netdev );
914 }
915 
916 /** Enable or disable interrupts
917  *
918  * @v netdev Network device
919  * @v enable Interrupts should be enabled
920  */
921 void netdev_irq ( struct net_device *netdev, int enable ) {
922 
923  /* Enable or disable device interrupts, if applicable */
924  if ( netdev_irq_supported ( netdev ) )
925  netdev->op->irq ( netdev, enable );
926 
927  /* Record interrupt enabled state */
929  if ( enable )
931 }
932 
933 /**
934  * Get network device by name
935  *
936  * @v name Network device name
937  * @ret netdev Network device, or NULL
938  */
939 struct net_device * find_netdev ( const char *name ) {
940  struct net_device *netdev;
941 
942  /* Allow "netX" shortcut */
943  if ( strcmp ( name, "netX" ) == 0 )
944  return last_opened_netdev();
945 
946  /* Identify network device by name */
948  if ( strcmp ( netdev->name, name ) == 0 )
949  return netdev;
950  }
951 
952  return NULL;
953 }
954 
955 /**
956  * Get network device by scope ID
957  *
958  * @v index Network device index
959  * @ret netdev Network device, or NULL
960  */
961 struct net_device * find_netdev_by_scope_id ( unsigned int scope_id ) {
962  struct net_device *netdev;
963 
964  /* Identify network device by index */
966  if ( netdev->scope_id == scope_id )
967  return netdev;
968  }
969 
970  return NULL;
971 }
972 
973 /**
974  * Get network device by PCI bus:dev.fn address
975  *
976  * @v bus_type Bus type
977  * @v location Bus location
978  * @ret netdev Network device, or NULL
979  */
980 struct net_device * find_netdev_by_location ( unsigned int bus_type,
981  unsigned int location ) {
982  struct net_device *netdev;
983 
985  if ( ( netdev->dev->desc.bus_type == bus_type ) &&
986  ( netdev->dev->desc.location == location ) )
987  return netdev;
988  }
989 
990  return NULL;
991 }
992 
993 /**
994  * Get most recently opened network device
995  *
996  * @ret netdev Most recently opened network device, or NULL
997  */
998 struct net_device * last_opened_netdev ( void ) {
999  struct net_device *netdev;
1000 
1002  open_list );
1003  if ( ! netdev )
1004  return NULL;
1005 
1006  assert ( netdev_is_open ( netdev ) );
1007  return netdev;
1008 }
1009 
1010 /**
1011  * Transmit network-layer packet
1012  *
1013  * @v iobuf I/O buffer
1014  * @v netdev Network device
1015  * @v net_protocol Network-layer protocol
1016  * @v ll_dest Destination link-layer address
1017  * @v ll_source Source link-layer address
1018  * @ret rc Return status code
1019  *
1020  * Prepends link-layer headers to the I/O buffer and transmits the
1021  * packet via the specified network device. This function takes
1022  * ownership of the I/O buffer.
1023  */
1024 int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
1025  struct net_protocol *net_protocol, const void *ll_dest,
1026  const void *ll_source ) {
1028  int rc;
1029 
1030  /* Add link-layer header */
1031  if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, ll_source,
1032  net_protocol->net_proto ) ) != 0 ) {
1033  /* Record error for diagnosis */
1034  netdev_tx_err ( netdev, iobuf, rc );
1035  return rc;
1036  }
1037 
1038  /* Transmit packet */
1039  return netdev_tx ( netdev, iobuf );
1040 }
1041 
1042 /**
1043  * Process received network-layer packet
1044  *
1045  * @v iobuf I/O buffer
1046  * @v netdev Network device
1047  * @v net_proto Network-layer protocol, in network-byte order
1048  * @v ll_dest Destination link-layer address
1049  * @v ll_source Source link-layer address
1050  * @v flags Packet flags
1051  * @ret rc Return status code
1052  */
1053 int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
1054  uint16_t net_proto, const void *ll_dest, const void *ll_source,
1055  unsigned int flags ) {
1056  struct net_protocol *net_protocol;
1057 
1058  /* Hand off to network-layer protocol, if any */
1060  if ( net_protocol->net_proto == net_proto )
1061  return net_protocol->rx ( iobuf, netdev, ll_dest,
1062  ll_source, flags );
1063  }
1064 
1065  DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
1066  netdev->name, ntohs ( net_proto ) );
1067  free_iob ( iobuf );
1068  return -ENOTSUP;
1069 }
1070 
1071 /**
1072  * Poll the network stack
1073  *
1074  * This polls all interfaces for received packets, and processes
1075  * packets from the RX queue.
1076  */
1077 void net_poll ( void ) {
1078  struct net_device *netdev;
1079  struct io_buffer *iobuf;
1080  struct ll_protocol *ll_protocol;
1081  const void *ll_dest;
1082  const void *ll_source;
1083  uint16_t net_proto;
1084  unsigned int flags;
1085  int rc;
1086 
1087  /* Poll and process each network device */
1088  list_for_each_entry ( netdev, &net_devices, list ) {
1089 
1090  /* Poll for new packets */
1091  profile_start ( &net_poll_profiler );
1092  netdev_poll ( netdev );
1093  profile_stop ( &net_poll_profiler );
1094 
1095  /* Leave received packets on the queue if receive
1096  * queue processing is currently frozen. This will
1097  * happen when the raw packets are to be manually
1098  * dequeued using netdev_rx_dequeue(), rather than
1099  * processed via the usual networking stack.
1100  */
1101  if ( netdev_rx_frozen ( netdev ) )
1102  continue;
1103 
1104  /* Process all received packets */
1105  while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
1106 
1107  DBGC2 ( netdev, "NETDEV %s processing %p (%p+%zx)\n",
1108  netdev->name, iobuf, iobuf->data,
1109  iob_len ( iobuf ) );
1110  profile_start ( &net_rx_profiler );
1111 
1112  /* Remove link-layer header */
1114  if ( ( rc = ll_protocol->pull ( netdev, iobuf,
1115  &ll_dest, &ll_source,
1116  &net_proto,
1117  &flags ) ) != 0 ) {
1118  free_iob ( iobuf );
1119  continue;
1120  }
1121 
1122  /* Hand packet to network layer */
1123  if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
1124  net_proto, ll_dest,
1125  ll_source, flags ) ) != 0 ) {
1126  /* Record error for diagnosis */
1127  netdev_rx_err ( netdev, NULL, rc );
1128  }
1129  profile_stop ( &net_rx_profiler );
1130  }
1131  }
1132 }
1133 
1134 /**
1135  * Single-step the network stack
1136  *
1137  * @v process Network stack process
1138  */
1139 static void net_step ( struct process *process __unused ) {
1140  net_poll();
1141 }
1142 
1143 /**
1144  * Get the VLAN tag control information (when VLAN support is not present)
1145  *
1146  * @v netdev Network device
1147  * @ret tag 0, indicating that device is not a VLAN device
1148  */
1149 __weak unsigned int vlan_tci ( struct net_device *netdev __unused ) {
1150  return 0;
1151 }
1152 
1153 /**
1154  * Add VLAN tag-stripped packet to queue (when VLAN support is not present)
1155  *
1156  * @v netdev Network device
1157  * @v tag VLAN tag, or zero
1158  * @v iobuf I/O buffer
1159  */
1160 __weak void vlan_netdev_rx ( struct net_device *netdev, unsigned int tag,
1161  struct io_buffer *iobuf ) {
1162 
1163  if ( tag == 0 ) {
1164  netdev_rx ( netdev, iobuf );
1165  } else {
1166  netdev_rx_err ( netdev, iobuf, -ENODEV );
1167  }
1168 }
1169 
1170 /**
1171  * Discard received VLAN tag-stripped packet (when VLAN support is not present)
1172  *
1173  * @v netdev Network device
1174  * @v tag VLAN tag, or zero
1175  * @v iobuf I/O buffer, or NULL
1176  * @v rc Packet status code
1177  */
1179  unsigned int tag __unused,
1180  struct io_buffer *iobuf, int rc ) {
1181 
1182  netdev_rx_err ( netdev, iobuf, rc );
1183 }
1184 
1185 /** Networking stack process */
1186 PERMANENT_PROCESS ( net_process, net_step );
1187 
1188 /**
1189  * Discard some cached network device data
1190  *
1191  * @ret discarded Number of cached items discarded
1192  */
1193 static unsigned int net_discard ( void ) {
1194  struct net_device *netdev;
1195  struct io_buffer *iobuf;
1196  unsigned int discarded = 0;
1197 
1198  /* Try to drop one deferred TX packet from each network device */
1199  for_each_netdev ( netdev ) {
1200  if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
1201  struct io_buffer,
1202  list ) ) != NULL ) {
1203 
1204  /* Discard first deferred packet */
1205  list_del ( &iobuf->list );
1206  if ( dma_mapped ( &iobuf->map ) )
1207  iob_unmap ( iobuf );
1208  free_iob ( iobuf );
1209 
1210  /* Report discard */
1211  discarded++;
1212  }
1213  }
1214 
1215  return discarded;
1216 }
1217 
1218 /** Network device cache discarder */
1219 struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
1220  .discard = net_discard,
1221 };
1222 
1223 /**
1224  * Find network device configurator
1225  *
1226  * @v name Name
1227  * @ret configurator Network device configurator, or NULL
1228  */
1230  struct net_device_configurator *configurator;
1231 
1233  if ( strcmp ( configurator->name, name ) == 0 )
1234  return configurator;
1235  }
1236  return NULL;
1237 }
1238 
1239 /**
1240  * Start network device configuration
1241  *
1242  * @v netdev Network device
1243  * @v configurator Network device configurator
1244  * @ret rc Return status code
1245  */
1247  struct net_device_configurator *configurator ) {
1248  struct net_device_configuration *config =
1250  int rc;
1251 
1252  /* Check applicability of configurator */
1254  DBGC ( netdev, "NETDEV %s does not support configuration via "
1255  "%s\n", netdev->name, configurator->name );
1256  return -ENOTSUP;
1257  }
1258 
1259  /* Terminate any ongoing configuration */
1260  intf_restart ( &config->job, -ECANCELED );
1261 
1262  /* Mark configuration as being in progress */
1263  config->rc = -EINPROGRESS_CONFIG;
1264 
1265  DBGC ( netdev, "NETDEV %s starting configuration via %s\n",
1267 
1268  /* Start configuration */
1269  if ( ( rc = configurator->start ( &config->job, netdev ) ) != 0 ) {
1270  DBGC ( netdev, "NETDEV %s could not start configuration via "
1271  "%s: %s\n", netdev->name, configurator->name,
1272  strerror ( rc ) );
1273  config->rc = rc;
1274  return rc;
1275  }
1276 
1277  return 0;
1278 }
1279 
1280 /**
1281  * Start network device configuration via all supported configurators
1282  *
1283  * @v netdev Network device
1284  * @ret rc Return status code
1285  */
1287  struct net_device_configurator *configurator;
1288  int rc;
1289 
1290  /* Start configuration for each configurator */
1292 
1293  /* Skip any inapplicable configurators */
1294  if ( ! netdev_configurator_applies ( netdev, configurator ) )
1295  continue;
1296 
1297  /* Start configuration */
1298  if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 )
1299  return rc;
1300  }
1301 
1302  return 0;
1303 }
1304 
1305 /**
1306  * Check if network device has a configuration with a specified status code
1307  *
1308  * @v netdev Network device
1309  * @v rc Status code
1310  * @ret has_rc Network device has a configuration with this status code
1311  */
1312 static int netdev_has_configuration_rc ( struct net_device *netdev, int rc ) {
1313  unsigned int num_configs;
1314  unsigned int i;
1315 
1316  num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
1317  for ( i = 0 ; i < num_configs ; i++ ) {
1318  if ( netdev->configs[i].rc == rc )
1319  return 1;
1320  }
1321  return 0;
1322 }
1323 
1324 /**
1325  * Check if network device configuration is in progress
1326  *
1327  * @v netdev Network device
1328  * @ret is_in_progress Network device configuration is in progress
1329  */
1331 
1333 }
1334 
1335 /**
1336  * Check if network device has at least one successful configuration
1337  *
1338  * @v netdev Network device
1339  * @v configurator Configurator
1340  * @ret rc Return status code
1341  */
1343 
1344  return netdev_has_configuration_rc ( netdev, 0 );
1345 }
A process.
Definition: process.h:17
#define EINPROGRESS_CONFIG
Default configuration-in-progress status code.
Definition: netdevice.c:78
An object interface operation.
Definition: interface.h:17
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
unsigned short uint16_t
Definition: stdint.h:11
const char * name
Name.
Definition: netdevice.h:315
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:249
static void netdev_settings_init(struct net_device *netdev)
Initialise a per-netdevice configuration settings block.
Definition: netdevice.h:601
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:342
int netdev_configuration_in_progress(struct net_device *netdev)
Check if network device configuration is in progress.
Definition: netdevice.c:1330
Error message tables.
void netdev_rx_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Discard received packet.
Definition: netdevice.c:539
void netdev_tx_defer(struct net_device *netdev, struct io_buffer *iobuf)
Defer transmitted packet.
Definition: netdevice.c:365
#define EUNKNOWN_LINK_STATUS
Default unknown link status code.
Definition: netdevice.c:68
#define EBUSY
Device or resource busy.
Definition: errno.h:338
uint8_t ll_header_len
Link-layer header length.
Definition: netdevice.h:200
#define EINFO_EINPROGRESS_CONFIG
Definition: netdevice.c:79
uint8_t ll_addr_len
Link-layer address length.
Definition: netdevice.h:198
int(* start)(struct interface *job, struct net_device *netdev)
Start configuring network device.
Definition: netdevice.h:328
#define NETDEV_RX_FROZEN
Network device receive queue processing is frozen.
Definition: netdevice.h:444
struct dma_mapping map
DMA mapping.
Definition: iobuf.h:43
void unregister_settings(struct settings *settings)
Unregister settings block.
Definition: settings.c:514
int(* open)(struct net_device *netdev)
Open network device.
Definition: netdevice.h:222
void net_poll(void)
Poll the network stack.
Definition: netdevice.c:1077
#define list_add(new, head)
Add a new entry to the head of a list.
Definition: list.h:69
void netdev_tx_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Discard transmitted packet.
Definition: netdevice.c:393
#define NETDEV_OPEN
Network device is open.
Definition: netdevice.h:438
struct interface job
Job control interface.
Definition: netdevice.h:309
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
Error codes.
#define EEXIST
File exists.
Definition: errno.h:388
struct retry_timer link_block
Link block timer.
Definition: netdevice.h:403
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
#define __einfo_errortab(einfo)
Definition: errortab.h:23
size_t mtu
Maximum transmission unit length.
Definition: netdevice.h:415
struct arbelprm_completion_with_error error
Definition: arbel.h:12
struct list_head list
List of network devices.
Definition: netdevice.h:356
#define DBGC(...)
Definition: compiler.h:505
char name[40]
Name.
Definition: device.h:75
#define NETDEV_DISCARD_RATE
Definition: fault.h:15
A retry timer.
Definition: retry.h:21
int netdev_configure(struct net_device *netdev, struct net_device_configurator *configurator)
Start network device configuration.
Definition: netdevice.c:1246
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned int state
Current device state.
Definition: netdevice.h:395
PERMANENT_PROCESS(net_process, net_step)
Networking stack process.
struct dma_device * dma
DMA device.
Definition: netdevice.h:366
void netdev_tx_complete_next_err(struct net_device *netdev, int rc)
Complete network transmission.
Definition: netdevice.c:462
void netdev_link_down(struct net_device *netdev)
Mark network device as having link down.
Definition: netdevice.c:183
#define ntohs(value)
Definition: byteswap.h:136
static void netdev_config_close(struct net_device_configuration *config, int rc)
Finish network device configuration.
Definition: netdevice.c:621
Network device error.
Definition: netdevice.h:280
static void netdev_record_stat(struct net_device_stats *stats, int rc)
Record network device statistic.
Definition: netdevice.c:244
#define for_each_table_entry_reverse(pointer, table)
Iterate through all entries within a linker table in reverse order.
Definition: tables.h:440
A data structure for storing profiling information.
Definition: profile.h:26
static int netdev_link_blocked(struct net_device *netdev)
Check link block state of network device.
Definition: netdevice.h:653
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:171
struct net_device_operations * op
Network device operations.
Definition: netdevice.h:369
static const char * netdev_addr(struct net_device *netdev)
Get printable network device link-layer address.
Definition: netdevice.h:533
struct net_device_stats tx_stats
TX statistics.
Definition: netdevice.h:423
unsigned int scope_id
Scope ID.
Definition: netdevice.h:360
void(* init_addr)(const void *hw_addr, void *ll_addr)
Initialise link-layer address.
Definition: netdevice.h:150
A network upper-layer driver.
Definition: netdevice.h:473
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:589
static struct interface_operation netdev_config_ops[]
Network device configuration interface operations.
Definition: netdevice.c:641
A link-layer protocol.
Definition: netdevice.h:114
struct list_head rx_queue
RX packet queue.
Definition: netdevice.h:421
unsigned int count
Error count.
Definition: netdevice.h:284
#define ECANCELED
Operation canceled.
Definition: errno.h:343
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
A doubly-linked list entry (or list head)
Definition: list.h:18
void(* poll)(struct net_device *netdev)
Poll for completed and received packets.
Definition: netdevice.h:267
int(* push)(struct net_device *netdev, struct io_buffer *iobuf, const void *ll_dest, const void *ll_source, uint16_t net_proto)
Add link-layer header.
Definition: netdevice.h:127
Dynamic memory allocation.
A reference counter.
Definition: refcnt.h:26
A timer.
Definition: timer.h:28
static __always_inline int iob_map_tx(struct io_buffer *iobuf, struct dma_device *dma)
Map I/O buffer for transmit DMA.
Definition: iobuf.h:240
int netdev_configure_all(struct net_device *netdev)
Start network device configuration via all supported configurators.
Definition: netdevice.c:1286
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
#define EINFO_ENOTCONN_LINK_DOWN
Definition: netdevice.c:84
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition: list.h:333
struct net_device * alloc_netdev(size_t priv_len)
Allocate network device.
Definition: netdevice.c:674
#define EUNUSED_CONFIG
Default not-yet-attempted-configuration status code.
Definition: netdevice.c:73
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
void(* irq)(struct net_device *netdev, int enable)
Enable or disable interrupts.
Definition: netdevice.h:276
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:212
void * memcpy(void *dest, const void *src, size_t len) __nonnull
const char * name
Name.
Definition: netdevice.h:475
const char * name
Name.
Definition: profile.h:28
static int netdev_is_open(struct net_device *netdev)
Check whether or not network device is open.
Definition: netdevice.h:664
static __always_inline int dma_mapped(struct dma_mapping *map)
Check if DMA unmapping is required.
Definition: dma.h:449
struct net_device_configurator * find_netdev_configurator(const char *name)
Find network device configurator.
Definition: netdevice.c:1229
int link_rc
Link status code.
Definition: netdevice.h:401
void netdev_link_block(struct net_device *netdev, unsigned long timeout)
Mark network device link as being blocked.
Definition: netdevice.c:200
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition: netdevice.h:567
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static int rand(void)
Definition: stdlib.h:59
unsigned int(* discard)(void)
Discard some cached data.
Definition: malloc.h:89
static void srand(unsigned int seed)
Definition: stdlib.h:63
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
void * priv
Driver private data.
Definition: netdevice.h:431
void(* remove)(struct net_device *netdev)
Remove device.
Definition: netdevice.h:491
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
static __always_inline void iob_unmap(struct io_buffer *iobuf)
Unmap I/O buffer for DMA.
Definition: iobuf.h:275
void netdev_link_unblock(struct net_device *netdev)
Mark network device link as being unblocked.
Definition: netdevice.c:215
struct list_head tx_queue
TX packet queue.
Definition: netdevice.h:417
struct net_device_configuration configs[0]
Network device configurations (variable length)
Definition: netdevice.h:434
static struct net_device * netdev
Definition: gdbudp.c:52
struct net_device * last_opened_netdev(void)
Get most recently opened network device.
Definition: netdevice.c:998
struct list_head tx_deferred
Deferred TX packet queue.
Definition: netdevice.h:419
static void netdev_rx_flush(struct net_device *netdev)
Flush device's receive queue.
Definition: netdevice.c:606
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:158
Profiling.
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition: netdevice.c:894
#define NET_DRIVERS
Network driver table.
Definition: netdevice.h:495
unsigned int location
Location.
Definition: device.h:29
static struct interface_descriptor netdev_config_desc
Network device configuration interface descriptor.
Definition: netdevice.c:647
#define NET_PROTOCOLS
Network-layer protocol table.
Definition: netdevice.h:467
An object interface descriptor.
Definition: interface.h:55
void netdev_poll(struct net_device *netdev)
Poll for completed and received packets on network device.
Definition: netdevice.c:565
static struct list_head open_net_devices
List of open network devices, in reverse order of opening.
Definition: netdevice.c:56
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct refcnt refcnt
Reference counter.
Definition: netdevice.h:354
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
static struct profiler net_poll_profiler __profiler
Network polling profiler.
Definition: netdevice.c:59
#define for_each_netdev(netdev)
Iterate over all network devices.
Definition: netdevice.h:538
int register_netdev(struct net_device *netdev)
Register network device.
Definition: netdevice.c:719
struct cache_discarder net_discarder __cache_discarder(CACHE_NORMAL)
Network device cache discarder.
struct net_device_stats rx_stats
RX statistics.
Definition: netdevice.h:425
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:155
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
unsigned int bad
Count of error completions.
Definition: netdevice.h:295
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
void netdev_irq(struct net_device *netdev, int enable)
Enable or disable interrupts.
Definition: netdevice.c:921
int net_rx(struct io_buffer *iobuf, struct net_device *netdev, uint16_t net_proto, const void *ll_dest, const void *ll_source, unsigned int flags)
Process received network-layer packet.
Definition: netdevice.c:1053
A network device.
Definition: netdevice.h:352
void netdev_link_err(struct net_device *netdev, int rc)
Mark network device as having a specific link state.
Definition: netdevice.c:160
#define ENODEV
No such device.
Definition: errno.h:509
int netdev_tx(struct net_device *netdev, struct io_buffer *iobuf)
Transmit raw packet via network device.
Definition: netdevice.c:287
Processes.
A cache discarder.
Definition: malloc.h:83
struct net_device_error errors[NETDEV_MAX_UNIQUE_ERRORS]
Error breakdowns.
Definition: netdevice.h:297
unsigned char uint8_t
Definition: stdint.h:10
Network device statistics.
Definition: netdevice.h:291
static struct net_device * netdev_get(struct net_device *netdev)
Get reference to network device.
Definition: netdevice.h:556
static void netdev_link_block_expired(struct retry_timer *timer, int fail __unused)
Handle network device link block timer expiry.
Definition: netdevice.c:229
void(* close)(struct net_device *netdev)
Close network device.
Definition: netdevice.h:230
uint16_t net_proto
Network-layer protocol.
Definition: netdevice.h:99
struct errortab netdev_errors [] __errortab
Human-readable message for the default link statuses.
Definition: netdevice.c:88
unsigned int uint32_t
Definition: stdint.h:12
struct net_device * find_netdev_by_scope_id(unsigned int scope_id)
Get network device by scope ID.
Definition: netdevice.c:961
#define CACHE_NORMAL
Items with a normal replacement cost.
Definition: malloc.h:104
int(* transmit)(struct net_device *netdev, struct io_buffer *iobuf)
Transmit packet.
Definition: netdevice.h:254
static unsigned int net_discard(void)
Discard some cached network device data.
Definition: netdevice.c:1193
static int netdev_has_ll_addr(struct net_device *netdev)
Check whether or not network device has a link-layer address.
Definition: netdevice.c:101
void netdev_rx(struct net_device *netdev, struct io_buffer *iobuf)
Add packet to receive queue.
Definition: netdevice.c:501
struct device * dev
Underlying hardware device.
Definition: netdevice.h:364
void netdev_close(struct net_device *netdev)
Close network device.
Definition: netdevice.c:851
void netdev_rx_freeze(struct net_device *netdev)
Freeze network device receive queue processing.
Definition: netdevice.c:131
A network-layer protocol.
Definition: netdevice.h:64
void netdev_rx_unfreeze(struct net_device *netdev)
Unfreeze network device receive queue processing.
Definition: netdevice.c:145
Network device management.
void start_timer_fixed(struct retry_timer *timer, unsigned long timeout)
Start timer with a specified timeout.
Definition: retry.c:64
#define NETDEV_TX_IN_PROGRESS
Network device transmission is in progress.
Definition: netdevice.h:455
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static void net_step(struct process *process __unused)
Single-step the network stack.
Definition: netdevice.c:1139
static int netdev_configurator_applies(struct net_device *netdev, struct net_device_configurator *configurator)
Check if configurator applies to network device.
Definition: netdevice.h:629
#define EINFO_EUNKNOWN_LINK_STATUS
Definition: netdevice.c:69
#define NETDEV_POLL_IN_PROGRESS
Network device poll is in progress.
Definition: netdevice.h:458
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:80
void stop_timer(struct retry_timer *timer)
Stop timer.
Definition: retry.c:117
void netdev_tx_complete_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Complete network transmission.
Definition: netdevice.c:423
unsigned int good
Count of successful completions.
Definition: netdevice.h:293
int(* pull)(struct net_device *netdev, struct io_buffer *iobuf, const void **ll_dest, const void **ll_source, uint16_t *net_proto, unsigned int *flags)
Remove link-layer header.
Definition: netdevice.h:141
unsigned int bus_type
Bus type.
Definition: device.h:24
char name[NETDEV_NAME_LEN]
Name of this network device.
Definition: netdevice.h:362
General configuration.
struct list_head list
List of which this buffer is a member.
Definition: iobuf.h:40
__weak void vlan_netdev_rx(struct net_device *netdev, unsigned int tag, struct io_buffer *iobuf)
Add VLAN tag-stripped packet to queue (when VLAN support is not present)
Definition: netdevice.c:1160
int net_tx(struct io_buffer *iobuf, struct net_device *netdev, struct net_protocol *net_protocol, const void *ll_dest, const void *ll_source)
Transmit network-layer packet.
Definition: netdevice.c:1024
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
#define EINFO_EUNUSED_CONFIG
Definition: netdevice.c:74
#define DBGC2(...)
Definition: compiler.h:522
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
struct net_device * netdev
Network device.
Definition: netdevice.h:303
struct net_device * find_netdev(const char *name)
Get network device by name.
Definition: netdevice.c:939
void * data
Start of data.
Definition: iobuf.h:48
int rc
Configuration status.
Definition: netdevice.h:307
struct list_head open_list
List of open network devices.
Definition: netdevice.h:358
A network device configurator.
Definition: netdevice.h:313
__weak void vlan_netdev_rx_err(struct net_device *netdev, unsigned int tag __unused, struct io_buffer *iobuf, int rc)
Discard received VLAN tag-stripped packet (when VLAN support is not present)
Definition: netdevice.c:1178
Virtual LANs.
struct device_description desc
Device description.
Definition: device.h:79
Linker tables.
#define __weak
Declare a function as weak (use before the definition)
Definition: compiler.h:219
__weak unsigned int vlan_tci(struct net_device *netdev __unused)
Get the VLAN tag control information (when VLAN support is not present)
Definition: netdevice.c:1149
struct net_device_configurator * configurator
Network device configurator.
Definition: netdevice.h:305
void(* notify)(struct net_device *netdev)
Notify of device or link state change.
Definition: netdevice.h:486
Device model.
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
static void free_netdev(struct refcnt *refcnt)
Free network device.
Definition: netdevice.c:655
#define NET_DEVICE_CONFIGURATORS
Network device configurator table.
Definition: netdevice.h:332
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition: settings.c:475
void timeout(int)
void clear_settings(struct settings *settings)
Clear settings block.
Definition: settings.c:1102
struct list_head net_devices
List of network devices.
Definition: netdevice.c:53
Fault injection.
int(* probe)(struct net_device *netdev)
Probe device.
Definition: netdevice.h:481
uint8_t ll_addr[MAX_LL_ADDR_LEN]
Link-layer address.
Definition: netdevice.h:387
size_t max_pkt_len
Maximum packet length.
Definition: netdevice.h:409
int rc
Error status code.
Definition: netdevice.h:282
A network device configuration.
Definition: netdevice.h:301
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:190
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:30
static int netdev_rx_frozen(struct net_device *netdev)
Check whether or not network device receive queue processing is frozen.
Definition: netdevice.h:698
static void netdev_tx_flush(struct net_device *netdev)
Flush device's transmit queue.
Definition: netdevice.c:476
uint64_t tag
Identity tag.
Definition: edd.h:30
#define list_check_contains_entry(entry, head, member)
Check list contains a specified entry.
Definition: list.h:522
struct net_device * find_netdev_by_location(unsigned int bus_type, unsigned int location)
Get network device by PCI bus:dev.fn address.
Definition: netdevice.c:980
int netdev_configuration_ok(struct net_device *netdev)
Check if network device has at least one successful configuration.
Definition: netdevice.c:1342
#define ENETUNREACH
Network unreachable.
Definition: errno.h:488
#define for_each_table_entry_continue_reverse(pointer, table)
Iterate through all remaining entries within a linker table in reverse order.
Definition: tables.h:469
static int netdev_has_configuration_rc(struct net_device *netdev, int rc)
Check if network device has a configuration with a specified status code.
Definition: netdevice.c:1312
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:381
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
String functions.
static int netdev_irq_supported(struct net_device *netdev)
Check whether or not network device supports interrupts.
Definition: netdevice.h:675
int netdev_open(struct net_device *netdev)
Open network device.
Definition: netdevice.c:817
#define ENOTCONN_LINK_DOWN
Default link-down status code.
Definition: netdevice.c:83
#define NETDEV_IRQ_ENABLED
Network device interrupts are enabled.
Definition: netdevice.h:441
static struct net_device_configuration * netdev_configuration(struct net_device *netdev, struct net_device_configurator *configurator)
Get network device configuration.
Definition: netdevice.h:614
struct ll_protocol * ll_protocol
Link-layer protocol.
Definition: netdevice.h:372
static void netdev_notify(struct net_device *netdev)
Notify drivers of network device or link state change.
Definition: netdevice.c:117
int(* rx)(struct io_buffer *iobuf, struct net_device *netdev, const void *ll_dest, const void *ll_source, unsigned int flags)
Process received packet.
Definition: netdevice.h:79
struct io_buffer * netdev_rx_dequeue(struct net_device *netdev)
Remove packet from device's receive queue.
Definition: netdevice.c:590
A persistent I/O buffer.
Definition: iobuf.h:33
uint8_t flags
Flags.
Definition: ena.h:18
#define table_num_entries(table)
Get number of entries in linker table.
Definition: tables.h:335