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  * Get offset of network device driver private data
114  *
115  * @v driver Upper-layer driver, or NULL for device driver
116  * @ret offset Offset of driver private data
117  */
118 static size_t netdev_priv_offset ( struct net_driver *driver ) {
119  struct net_device *netdev;
120  unsigned int num_configs;
121  size_t offset;
122 
123  /* Allow space for network device */
124  offset = sizeof ( *netdev );
125 
126  /* Allow space for configurations */
128  offset += ( num_configs * sizeof ( netdev->configs[0] ) );
129 
130  /* Place variable-length device driver private data at end */
131  if ( ! driver )
132  driver = table_end ( NET_DRIVERS );
133 
134  /* Allow space for preceding upper-layer drivers' private data */
136  offset += driver->priv_len;
137  }
138 
139  /* Sanity check */
140  assert ( ( offset & ( sizeof ( void * ) - 1 ) ) == 0 );
141 
142  return offset;
143 }
144 
145 /**
146  * Get network device driver private data
147  *
148  * @v netdev Network device
149  * @v driver Upper-layer driver, or NULL for device driver
150  * @ret priv Driver private data
151  */
152 void * netdev_priv ( struct net_device *netdev, struct net_driver *driver ) {
153 
154  return ( ( ( void * ) netdev ) + netdev_priv_offset ( driver ) );
155 }
156 
157 /**
158  * Notify drivers of network device or link state change
159  *
160  * @v netdev Network device
161  */
162 static void netdev_notify ( struct net_device *netdev ) {
163  struct net_driver *driver;
164  void *priv;
165 
166  for_each_table_entry ( driver, NET_DRIVERS ) {
167  priv = netdev_priv ( netdev, driver );
168  if ( driver->notify )
169  driver->notify ( netdev, priv );
170  }
171 }
172 
173 /**
174  * Freeze network device receive queue processing
175  *
176  * @v netdev Network device
177  */
179 
180  /* Mark receive queue processing as frozen */
182 
183  /* Notify drivers of change */
184  netdev_notify ( netdev );
185 }
186 
187 /**
188  * Unfreeze network device receive queue processing
189  *
190  * @v netdev Network device
191  */
193 
194  /* Mark receive queue processing as not frozen */
196 
197  /* Notify drivers of change */
198  netdev_notify ( netdev );
199 }
200 
201 /**
202  * Mark network device as having a specific link state
203  *
204  * @v netdev Network device
205  * @v rc Link status code
206  */
207 void netdev_link_err ( struct net_device *netdev, int rc ) {
208 
209  /* Stop link block timer */
211 
212  /* Record link state */
213  netdev->link_rc = rc;
214  if ( netdev->link_rc == 0 ) {
215  DBGC ( netdev, "NETDEV %s link is up\n", netdev->name );
216  } else {
217  DBGC ( netdev, "NETDEV %s link is down: %s\n",
218  netdev->name, strerror ( netdev->link_rc ) );
219  }
220 
221  /* Notify drivers of link state change */
222  netdev_notify ( netdev );
223 }
224 
225 /**
226  * Mark network device as having link down
227  *
228  * @v netdev Network device
229  */
231 
232  /* Avoid clobbering a more detailed link status code, if one
233  * is already set.
234  */
235  if ( ( netdev->link_rc == 0 ) ||
236  ( netdev->link_rc == -EUNKNOWN_LINK_STATUS ) ) {
238  }
239 }
240 
241 /**
242  * Mark network device link as being blocked
243  *
244  * @v netdev Network device
245  * @v timeout Timeout (in ticks)
246  */
247 void netdev_link_block ( struct net_device *netdev, unsigned long timeout ) {
248 
249  /* Start link block timer */
250  if ( ! netdev_link_blocked ( netdev ) ) {
251  DBGC ( netdev, "NETDEV %s link blocked for %ld ticks\n",
252  netdev->name, timeout );
253  }
255 }
256 
257 /**
258  * Mark network device link as being unblocked
259  *
260  * @v netdev Network device
261  */
263 
264  /* Stop link block timer */
265  if ( netdev_link_blocked ( netdev ) )
266  DBGC ( netdev, "NETDEV %s link unblocked\n", netdev->name );
268 }
269 
270 /**
271  * Handle network device link block timer expiry
272  *
273  * @v timer Link block timer
274  * @v fail Failure indicator
275  */
277  int fail __unused ) {
278  struct net_device *netdev =
280 
281  /* Assume link is no longer blocked */
282  DBGC ( netdev, "NETDEV %s link block expired\n", netdev->name );
283 }
284 
285 /**
286  * Record network device statistic
287  *
288  * @v stats Network device statistics
289  * @v rc Status code
290  */
291 static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
292  struct net_device_error *error;
293  struct net_device_error *least_common_error;
294  unsigned int i;
295 
296  /* If this is not an error, just update the good counter */
297  if ( rc == 0 ) {
298  stats->good++;
299  return;
300  }
301 
302  /* Update the bad counter */
303  stats->bad++;
304 
305  /* Locate the appropriate error record */
306  least_common_error = &stats->errors[0];
307  for ( i = 0 ; i < ( sizeof ( stats->errors ) /
308  sizeof ( stats->errors[0] ) ) ; i++ ) {
309  error = &stats->errors[i];
310  /* Update matching record, if found */
311  if ( error->rc == rc ) {
312  error->count++;
313  return;
314  }
315  if ( error->count < least_common_error->count )
316  least_common_error = error;
317  }
318 
319  /* Overwrite the least common error record */
320  least_common_error->rc = rc;
321  least_common_error->count = 1;
322 }
323 
324 /**
325  * Transmit raw packet via network device
326  *
327  * @v netdev Network device
328  * @v iobuf I/O buffer
329  * @ret rc Return status code
330  *
331  * Transmits the packet via the specified network device. This
332  * function takes ownership of the I/O buffer.
333  */
334 int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
335  int rc;
336 
337  DBGC2 ( netdev, "NETDEV %s transmitting %p (%p+%zx)\n",
338  netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
339  profile_start ( &net_tx_profiler );
340 
341  /* Enqueue packet */
342  list_add_tail ( &iobuf->list, &netdev->tx_queue );
343 
344  /* Guard against re-entry */
346  rc = -EBUSY;
347  goto err_busy;
348  }
350 
351  /* Avoid calling transmit() on unopened network devices */
352  if ( ! netdev_is_open ( netdev ) ) {
353  rc = -ENETUNREACH;
354  goto err_closed;
355  }
356 
357  /* Discard packet (for test purposes) if applicable */
358  if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 )
359  goto err_fault;
360 
361  /* Map for DMA, if required */
362  if ( netdev->dma && ( ! dma_mapped ( &iobuf->map ) ) ) {
363  if ( ( rc = iob_map_tx ( iobuf, netdev->dma ) ) != 0 )
364  goto err_map;
365  }
366 
367  /* Transmit packet */
368  if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
369  goto err_transmit;
370 
371  /* Clear in-progress flag */
373 
374  profile_stop ( &net_tx_profiler );
375  return 0;
376 
377  err_transmit:
378  err_map:
379  err_fault:
380  err_closed:
382  err_busy:
383  netdev_tx_complete_err ( netdev, iobuf, rc );
384  return rc;
385 }
386 
387 /**
388  * Defer transmitted packet
389  *
390  * @v netdev Network device
391  * @v iobuf I/O buffer
392  *
393  * Drivers may call netdev_tx_defer() if there is insufficient space
394  * in the transmit descriptor ring. Any packets deferred in this way
395  * will be automatically retransmitted as soon as space becomes
396  * available (i.e. as soon as the driver calls netdev_tx_complete()).
397  *
398  * The packet must currently be in the network device's TX queue.
399  *
400  * Drivers utilising netdev_tx_defer() must ensure that space in the
401  * transmit descriptor ring is freed up @b before calling
402  * netdev_tx_complete(). For example, if the ring is modelled using a
403  * producer counter and a consumer counter, then the consumer counter
404  * must be incremented before the call to netdev_tx_complete().
405  * Failure to do this will cause the retransmitted packet to be
406  * immediately redeferred (which will result in out-of-order
407  * transmissions and other nastiness).
408  *
409  * I/O buffers that have been mapped for DMA will remain mapped while
410  * present in the deferred transmit queue.
411  */
412 void netdev_tx_defer ( struct net_device *netdev, struct io_buffer *iobuf ) {
413 
414  /* Catch data corruption as early as possible */
415  list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
416 
417  /* Remove from transmit queue */
418  list_del ( &iobuf->list );
419 
420  /* Add to deferred transmit queue */
421  list_add_tail ( &iobuf->list, &netdev->tx_deferred );
422 
423  /* Record "out of space" statistic */
425 }
426 
427 /**
428  * Discard transmitted packet
429  *
430  * @v netdev Network device
431  * @v iobuf I/O buffer, or NULL
432  * @v rc Packet status code
433  *
434  * The packet is discarded and a TX error is recorded. This function
435  * takes ownership of the I/O buffer.
436  *
437  * The I/O buffer will be automatically unmapped for DMA, if
438  * applicable.
439  */
441  struct io_buffer *iobuf, int rc ) {
442 
443  /* Update statistics counter */
445  if ( rc == 0 ) {
446  DBGC2 ( netdev, "NETDEV %s transmission %p complete\n",
447  netdev->name, iobuf );
448  } else {
449  DBGC ( netdev, "NETDEV %s transmission %p failed: %s\n",
450  netdev->name, iobuf, strerror ( rc ) );
451  }
452 
453  /* Unmap I/O buffer, if required */
454  if ( iobuf && dma_mapped ( &iobuf->map ) )
455  iob_unmap ( iobuf );
456 
457  /* Discard packet */
458  free_iob ( iobuf );
459 }
460 
461 /**
462  * Complete network transmission
463  *
464  * @v netdev Network device
465  * @v iobuf I/O buffer
466  * @v rc Packet status code
467  *
468  * The packet must currently be in the network device's TX queue.
469  */
471  struct io_buffer *iobuf, int rc ) {
472 
473  /* Catch data corruption as early as possible */
474  list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
475 
476  /* Dequeue and free I/O buffer */
477  list_del ( &iobuf->list );
478  netdev_tx_err ( netdev, iobuf, rc );
479 
480  /* Handle pending transmit queue */
481  while ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
482  struct io_buffer, list ) ) ) {
483 
484  /* Remove from pending transmit queue */
485  list_del ( &iobuf->list );
486 
487  /* When any transmit completion fails, cancel all
488  * pending transmissions.
489  */
490  if ( rc != 0 ) {
491  netdev_tx_err ( netdev, iobuf, -ECANCELED );
492  continue;
493  }
494 
495  /* Otherwise, attempt to transmit the first pending packet */
496  netdev_tx ( netdev, iobuf );
497  break;
498  }
499 }
500 
501 /**
502  * Complete network transmission
503  *
504  * @v netdev Network device
505  * @v rc Packet status code
506  *
507  * Completes the oldest outstanding packet in the TX queue.
508  */
510  struct io_buffer *iobuf;
511 
512  if ( ( iobuf = list_first_entry ( &netdev->tx_queue, struct io_buffer,
513  list ) ) != NULL ) {
514  netdev_tx_complete_err ( netdev, iobuf, rc );
515  }
516 }
517 
518 /**
519  * Flush device's transmit queue
520  *
521  * @v netdev Network device
522  */
523 static void netdev_tx_flush ( struct net_device *netdev ) {
524 
525  /* Discard any packets in the TX queue. This will also cause
526  * any packets in the deferred TX queue to be discarded
527  * automatically.
528  */
529  while ( ! list_empty ( &netdev->tx_queue ) ) {
531  }
532  assert ( list_empty ( &netdev->tx_queue ) );
534 }
535 
536 /**
537  * Add packet to receive queue
538  *
539  * @v netdev Network device
540  * @v iobuf I/O buffer
541  *
542  * The packet is added to the network device's RX queue. This
543  * function takes ownership of the I/O buffer.
544  *
545  * The I/O buffer will be automatically unmapped for DMA, if
546  * applicable.
547  */
548 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
549  int rc;
550 
551  DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
552  netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
553 
554  /* Discard packet (for test purposes) if applicable */
555  if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 ) {
556  netdev_rx_err ( netdev, iobuf, rc );
557  return;
558  }
559 
560  /* Unmap I/O buffer, if required */
561  if ( dma_mapped ( &iobuf->map ) )
562  iob_unmap ( iobuf );
563 
564  /* Enqueue packet */
565  list_add_tail ( &iobuf->list, &netdev->rx_queue );
566 
567  /* Update statistics counter */
569 }
570 
571 /**
572  * Discard received packet
573  *
574  * @v netdev Network device
575  * @v iobuf I/O buffer, or NULL
576  * @v rc Packet status code
577  *
578  * The packet is discarded and an RX error is recorded. This function
579  * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
580  * example, the net device wishes to report an error due to being
581  * unable to allocate an I/O buffer.
582  *
583  * The I/O buffer will be automatically unmapped for DMA, if
584  * applicable.
585  */
587  struct io_buffer *iobuf, int rc ) {
588 
589  DBGC ( netdev, "NETDEV %s failed to receive %p: %s\n",
590  netdev->name, iobuf, strerror ( rc ) );
591 
592  /* Unmap I/O buffer, if required */
593  if ( iobuf && dma_mapped ( &iobuf->map ) )
594  iob_unmap ( iobuf );
595 
596  /* Discard packet */
597  free_iob ( iobuf );
598 
599  /* Update statistics counter */
601 }
602 
603 /**
604  * Poll for completed and received packets on network device
605  *
606  * @v netdev Network device
607  *
608  * Polls the network device for completed transmissions and received
609  * packets. Any received packets will be added to the RX packet queue
610  * via netdev_rx().
611  */
612 void netdev_poll ( struct net_device *netdev ) {
613 
614  /* Avoid calling poll() on unopened network devices */
615  if ( ! netdev_is_open ( netdev ) )
616  return;
617 
618  /* Guard against re-entry */
620  return;
621 
622  /* Poll device */
624  netdev->op->poll ( netdev );
626 }
627 
628 /**
629  * Remove packet from device's receive queue
630  *
631  * @v netdev Network device
632  * @ret iobuf I/O buffer, or NULL
633  *
634  * Removes the first packet from the device's RX queue and returns it.
635  * Ownership of the packet is transferred to the caller.
636  */
638  struct io_buffer *iobuf;
639 
640  iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
641  if ( ! iobuf )
642  return NULL;
643 
644  list_del ( &iobuf->list );
645  return iobuf;
646 }
647 
648 /**
649  * Flush device's receive queue
650  *
651  * @v netdev Network device
652  */
653 static void netdev_rx_flush ( struct net_device *netdev ) {
654  struct io_buffer *iobuf;
655 
656  /* Discard any packets in the RX queue */
657  while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
658  netdev_rx_err ( netdev, iobuf, -ECANCELED );
659  }
660 }
661 
662 /**
663  * Finish network device configuration
664  *
665  * @v config Network device configuration
666  * @v rc Reason for completion
667  */
668 static void netdev_config_close ( struct net_device_configuration *config,
669  int rc ) {
670  struct net_device_configurator *configurator = config->configurator;
671  struct net_device *netdev = config->netdev;
672 
673  /* Restart interface */
674  intf_restart ( &config->job, rc );
675 
676  /* Record configuration result */
677  config->rc = rc;
678  if ( rc == 0 ) {
679  DBGC ( netdev, "NETDEV %s configured via %s\n",
680  netdev->name, configurator->name );
681  } else {
682  DBGC ( netdev, "NETDEV %s configuration via %s failed: %s\n",
683  netdev->name, configurator->name, strerror ( rc ) );
684  }
685 }
686 
687 /** Network device configuration interface operations */
691 };
692 
693 /** Network device configuration interface descriptor */
696 
697 /**
698  * Free network device
699  *
700  * @v refcnt Network device reference counter
701  */
702 static void free_netdev ( struct refcnt *refcnt ) {
703  struct net_device *netdev =
704  container_of ( refcnt, struct net_device, refcnt );
705 
706  assert ( ! timer_running ( &netdev->link_block ) );
710  free ( netdev );
711 }
712 
713 /**
714  * Allocate network device
715  *
716  * @v priv_len Length of private data area (net_device::priv)
717  * @ret netdev Network device, or NULL
718  *
719  * Allocates space for a network device and its private data area.
720  */
721 struct net_device * alloc_netdev ( size_t priv_len ) {
722  struct net_device *netdev;
723  struct net_device_configurator *configurator;
724  struct net_device_configuration *config;
725 
726  netdev = zalloc ( netdev_priv_offset ( NULL ) + priv_len );
727  if ( netdev ) {
731  &netdev->refcnt );
736  config = netdev->configs;
738  config->netdev = netdev;
739  config->configurator = configurator;
740  config->rc = -EUNUSED_CONFIG;
741  intf_init ( &config->job, &netdev_config_desc,
742  &netdev->refcnt );
743  config++;
744  }
746  }
747  return netdev;
748 }
749 
750 /**
751  * Register network device
752  *
753  * @v netdev Network device
754  * @ret rc Return status code
755  *
756  * Gives the network device a name and adds it to the list of network
757  * devices.
758  */
761  struct net_driver *driver;
762  struct net_device *duplicate;
763  unsigned int i;
764  uint32_t seed;
765  void *priv;
766  int rc;
767 
768  /* Set initial link-layer address, if not already set */
769  if ( ! netdev_has_ll_addr ( netdev ) ) {
771  }
772 
773  /* Set MTU, if not already set */
774  if ( ! netdev->mtu ) {
775  netdev->mtu = ( netdev->max_pkt_len -
777  }
778 
779  /* Reject named network devices that already exist */
780  if ( netdev->name[0] && ( duplicate = find_netdev ( netdev->name ) ) ) {
781  DBGC ( netdev, "NETDEV rejecting duplicate name %s\n",
782  duplicate->name );
783  rc = -EEXIST;
784  goto err_duplicate;
785  }
786 
787  /* Assign a unique device name, if not already set */
788  if ( netdev->name[0] == '\0' ) {
789  for ( i = 0 ; ; i++ ) {
790  snprintf ( netdev->name, sizeof ( netdev->name ),
791  "net%d", i );
792  if ( find_netdev ( netdev->name ) == NULL )
793  break;
794  }
795  }
796 
797  /* Assign a unique non-zero scope ID */
798  for ( netdev->scope_id = 1 ; ; netdev->scope_id++ ) {
800  break;
801  }
802 
803  /* Use least significant bits of the link-layer address to
804  * improve the randomness of the (non-cryptographic) random
805  * number generator.
806  */
808  - sizeof ( seed ) ), sizeof ( seed ) );
809  srand ( rand() ^ seed );
810 
811  /* Add to device list */
812  netdev_get ( netdev );
814  DBGC ( netdev, "NETDEV %s registered (phys %s hwaddr %s)\n",
815  netdev->name, netdev->dev->name,
816  netdev_addr ( netdev ) );
817 
818  /* Register per-netdev configuration settings */
820  NULL, netdev->name ) ) != 0 ) {
821  DBGC ( netdev, "NETDEV %s could not register settings: %s\n",
822  netdev->name, strerror ( rc ) );
823  goto err_register_settings;
824  }
825 
826  /* Probe device */
827  for_each_table_entry ( driver, NET_DRIVERS ) {
828  priv = netdev_priv ( netdev, driver );
829  if ( driver->probe &&
830  ( rc = driver->probe ( netdev, priv ) ) != 0 ) {
831  DBGC ( netdev, "NETDEV %s could not add %s device: "
832  "%s\n", netdev->name, driver->name,
833  strerror ( rc ) );
834  goto err_probe;
835  }
836  }
837 
838  return 0;
839 
840  err_probe:
842  priv = netdev_priv ( netdev, driver );
843  if ( driver->remove )
844  driver->remove ( netdev, priv );
845  }
848  err_register_settings:
849  list_del ( &netdev->list );
850  netdev_put ( netdev );
851  err_duplicate:
852  return rc;
853 }
854 
855 /**
856  * Open network device
857  *
858  * @v netdev Network device
859  * @ret rc Return status code
860  */
861 int netdev_open ( struct net_device *netdev ) {
862  int rc;
863 
864  /* Do nothing if device is already open */
865  if ( netdev->state & NETDEV_OPEN )
866  return 0;
867 
868  DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
869 
870  /* Mark as opened */
872 
873  /* Open the device */
874  if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
875  goto err;
876 
877  /* Add to head of open devices list */
879 
880  /* Notify drivers of device state change */
881  netdev_notify ( netdev );
882 
883  return 0;
884 
885  err:
886  netdev->state &= ~NETDEV_OPEN;
887  return rc;
888 }
889 
890 /**
891  * Close network device
892  *
893  * @v netdev Network device
894  */
895 void netdev_close ( struct net_device *netdev ) {
896  unsigned int num_configs;
897  unsigned int i;
898 
899  /* Do nothing if device is already closed */
900  if ( ! ( netdev->state & NETDEV_OPEN ) )
901  return;
902 
903  DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
904 
905  /* Terminate any ongoing configurations. Use intf_close()
906  * rather than intf_restart() to allow the cancellation to be
907  * reported back to us if a configuration is actually in
908  * progress.
909  */
911  for ( i = 0 ; i < num_configs ; i++ )
913 
914  /* Remove from open devices list */
915  list_del ( &netdev->open_list );
916 
917  /* Mark as closed */
918  netdev->state &= ~NETDEV_OPEN;
919 
920  /* Notify drivers of device state change */
921  netdev_notify ( netdev );
922 
923  /* Close the device */
924  netdev->op->close ( netdev );
925 
926  /* Stop link block timer */
928 
929  /* Flush TX and RX queues */
932 }
933 
934 /**
935  * Unregister network device
936  *
937  * @v netdev Network device
938  *
939  * Removes the network device from the list of network devices.
940  */
942  struct net_driver *driver;
943  void *priv;
944 
945  /* Ensure device is closed */
946  netdev_close ( netdev );
947 
948  /* Remove device */
950  priv = netdev_priv ( netdev, driver );
951  if ( driver->remove )
952  driver->remove ( netdev, priv );
953  }
954 
955  /* Unregister per-netdev configuration settings */
958 
959  /* Remove from device list */
960  DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
961  list_del ( &netdev->list );
962  netdev_put ( netdev );
963 }
964 
965 /** Enable or disable interrupts
966  *
967  * @v netdev Network device
968  * @v enable Interrupts should be enabled
969  */
970 void netdev_irq ( struct net_device *netdev, int enable ) {
971 
972  /* Enable or disable device interrupts, if applicable */
973  if ( netdev_irq_supported ( netdev ) )
974  netdev->op->irq ( netdev, enable );
975 
976  /* Record interrupt enabled state */
978  if ( enable )
980 }
981 
982 /**
983  * Get network device by name
984  *
985  * @v name Network device name
986  * @ret netdev Network device, or NULL
987  */
988 struct net_device * find_netdev ( const char *name ) {
989  struct net_device *netdev;
990 
991  /* Allow "netX" shortcut */
992  if ( strcmp ( name, "netX" ) == 0 )
993  return last_opened_netdev();
994 
995  /* Identify network device by name */
997  if ( strcmp ( netdev->name, name ) == 0 )
998  return netdev;
999  }
1000 
1001  return NULL;
1002 }
1003 
1004 /**
1005  * Get network device by scope ID
1006  *
1007  * @v index Network device index
1008  * @ret netdev Network device, or NULL
1009  */
1010 struct net_device * find_netdev_by_scope_id ( unsigned int scope_id ) {
1011  struct net_device *netdev;
1012 
1013  /* Identify network device by index */
1015  if ( netdev->scope_id == scope_id )
1016  return netdev;
1017  }
1018 
1019  return NULL;
1020 }
1021 
1022 /**
1023  * Get network device by PCI bus:dev.fn address
1024  *
1025  * @v bus_type Bus type
1026  * @v location Bus location
1027  * @ret netdev Network device, or NULL
1028  */
1029 struct net_device * find_netdev_by_location ( unsigned int bus_type,
1030  unsigned int location ) {
1031  struct net_device *netdev;
1032 
1034  if ( ( netdev->dev->desc.bus_type == bus_type ) &&
1035  ( netdev->dev->desc.location == location ) )
1036  return netdev;
1037  }
1038 
1039  return NULL;
1040 }
1041 
1042 /**
1043  * Get most recently opened network device
1044  *
1045  * @ret netdev Most recently opened network device, or NULL
1046  */
1047 struct net_device * last_opened_netdev ( void ) {
1048  struct net_device *netdev;
1049 
1051  open_list );
1052  if ( ! netdev )
1053  return NULL;
1054 
1055  assert ( netdev_is_open ( netdev ) );
1056  return netdev;
1057 }
1058 
1059 /**
1060  * Transmit network-layer packet
1061  *
1062  * @v iobuf I/O buffer
1063  * @v netdev Network device
1064  * @v net_protocol Network-layer protocol
1065  * @v ll_dest Destination link-layer address
1066  * @v ll_source Source link-layer address
1067  * @ret rc Return status code
1068  *
1069  * Prepends link-layer headers to the I/O buffer and transmits the
1070  * packet via the specified network device. This function takes
1071  * ownership of the I/O buffer.
1072  */
1073 int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
1074  struct net_protocol *net_protocol, const void *ll_dest,
1075  const void *ll_source ) {
1077  int rc;
1078 
1079  /* Add link-layer header */
1080  if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, ll_source,
1081  net_protocol->net_proto ) ) != 0 ) {
1082  /* Record error for diagnosis */
1083  netdev_tx_err ( netdev, iobuf, rc );
1084  return rc;
1085  }
1086 
1087  /* Transmit packet */
1088  return netdev_tx ( netdev, iobuf );
1089 }
1090 
1091 /**
1092  * Process received network-layer packet
1093  *
1094  * @v iobuf I/O buffer
1095  * @v netdev Network device
1096  * @v net_proto Network-layer protocol, in network-byte order
1097  * @v ll_dest Destination link-layer address
1098  * @v ll_source Source link-layer address
1099  * @v flags Packet flags
1100  * @ret rc Return status code
1101  */
1102 int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
1103  uint16_t net_proto, const void *ll_dest, const void *ll_source,
1104  unsigned int flags ) {
1105  struct net_protocol *net_protocol;
1106 
1107  /* Hand off to network-layer protocol, if any */
1109  if ( net_protocol->net_proto == net_proto )
1110  return net_protocol->rx ( iobuf, netdev, ll_dest,
1111  ll_source, flags );
1112  }
1113 
1114  DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
1115  netdev->name, ntohs ( net_proto ) );
1116  free_iob ( iobuf );
1117  return -ENOTSUP;
1118 }
1119 
1120 /**
1121  * Poll the network stack
1122  *
1123  * This polls all interfaces for received packets, and processes
1124  * packets from the RX queue.
1125  */
1126 void net_poll ( void ) {
1127  struct net_device *netdev;
1128  struct io_buffer *iobuf;
1129  struct ll_protocol *ll_protocol;
1130  const void *ll_dest;
1131  const void *ll_source;
1132  uint16_t net_proto;
1133  unsigned int flags;
1134  int rc;
1135 
1136  /* Poll and process each network device */
1137  list_for_each_entry ( netdev, &net_devices, list ) {
1138 
1139  /* Poll for new packets */
1140  profile_start ( &net_poll_profiler );
1141  netdev_poll ( netdev );
1142  profile_stop ( &net_poll_profiler );
1143 
1144  /* Leave received packets on the queue if receive
1145  * queue processing is currently frozen. This will
1146  * happen when the raw packets are to be manually
1147  * dequeued using netdev_rx_dequeue(), rather than
1148  * processed via the usual networking stack.
1149  */
1150  if ( netdev_rx_frozen ( netdev ) )
1151  continue;
1152 
1153  /* Process all received packets */
1154  while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
1155 
1156  DBGC2 ( netdev, "NETDEV %s processing %p (%p+%zx)\n",
1157  netdev->name, iobuf, iobuf->data,
1158  iob_len ( iobuf ) );
1159  profile_start ( &net_rx_profiler );
1160 
1161  /* Remove link-layer header */
1163  if ( ( rc = ll_protocol->pull ( netdev, iobuf,
1164  &ll_dest, &ll_source,
1165  &net_proto,
1166  &flags ) ) != 0 ) {
1167  free_iob ( iobuf );
1168  continue;
1169  }
1170 
1171  /* Hand packet to network layer */
1172  if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
1173  net_proto, ll_dest,
1174  ll_source, flags ) ) != 0 ) {
1175  /* Record error for diagnosis */
1176  netdev_rx_err ( netdev, NULL, rc );
1177  }
1178  profile_stop ( &net_rx_profiler );
1179  }
1180  }
1181 }
1182 
1183 /**
1184  * Single-step the network stack
1185  *
1186  * @v process Network stack process
1187  */
1188 static void net_step ( struct process *process __unused ) {
1189  net_poll();
1190 }
1191 
1192 /**
1193  * Get the VLAN tag control information (when VLAN support is not present)
1194  *
1195  * @v netdev Network device
1196  * @ret tag 0, indicating that device is not a VLAN device
1197  */
1198 __weak unsigned int vlan_tci ( struct net_device *netdev __unused ) {
1199  return 0;
1200 }
1201 
1202 /**
1203  * Add VLAN tag-stripped packet to queue (when VLAN support is not present)
1204  *
1205  * @v netdev Network device
1206  * @v tag VLAN tag, or zero
1207  * @v iobuf I/O buffer
1208  */
1209 __weak void vlan_netdev_rx ( struct net_device *netdev, unsigned int tag,
1210  struct io_buffer *iobuf ) {
1211 
1212  if ( tag == 0 ) {
1213  netdev_rx ( netdev, iobuf );
1214  } else {
1215  netdev_rx_err ( netdev, iobuf, -ENODEV );
1216  }
1217 }
1218 
1219 /**
1220  * Discard received VLAN tag-stripped packet (when VLAN support is not present)
1221  *
1222  * @v netdev Network device
1223  * @v tag VLAN tag, or zero
1224  * @v iobuf I/O buffer, or NULL
1225  * @v rc Packet status code
1226  */
1228  unsigned int tag __unused,
1229  struct io_buffer *iobuf, int rc ) {
1230 
1231  netdev_rx_err ( netdev, iobuf, rc );
1232 }
1233 
1234 /** Networking stack process */
1235 PERMANENT_PROCESS ( net_process, net_step );
1236 
1237 /**
1238  * Discard some cached network device data
1239  *
1240  * @ret discarded Number of cached items discarded
1241  */
1242 static unsigned int net_discard ( void ) {
1243  struct net_device *netdev;
1244  struct io_buffer *iobuf;
1245  unsigned int discarded = 0;
1246 
1247  /* Try to drop one deferred TX packet from each network device */
1248  for_each_netdev ( netdev ) {
1249  if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
1250  struct io_buffer,
1251  list ) ) != NULL ) {
1252 
1253  /* Discard first deferred packet */
1254  list_del ( &iobuf->list );
1255  if ( dma_mapped ( &iobuf->map ) )
1256  iob_unmap ( iobuf );
1257  free_iob ( iobuf );
1258 
1259  /* Report discard */
1260  discarded++;
1261  }
1262  }
1263 
1264  return discarded;
1265 }
1266 
1267 /** Network device cache discarder */
1268 struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
1269  .discard = net_discard,
1270 };
1271 
1272 /**
1273  * Find network device configurator
1274  *
1275  * @v name Name
1276  * @ret configurator Network device configurator, or NULL
1277  */
1279  struct net_device_configurator *configurator;
1280 
1282  if ( strcmp ( configurator->name, name ) == 0 )
1283  return configurator;
1284  }
1285  return NULL;
1286 }
1287 
1288 /**
1289  * Start network device configuration
1290  *
1291  * @v netdev Network device
1292  * @v configurator Network device configurator
1293  * @ret rc Return status code
1294  */
1296  struct net_device_configurator *configurator ) {
1297  struct net_device_configuration *config =
1299  int rc;
1300 
1301  /* Check applicability of configurator */
1303  DBGC ( netdev, "NETDEV %s does not support configuration via "
1304  "%s\n", netdev->name, configurator->name );
1305  return -ENOTSUP;
1306  }
1307 
1308  /* Terminate any ongoing configuration */
1309  intf_restart ( &config->job, -ECANCELED );
1310 
1311  /* Mark configuration as being in progress */
1312  config->rc = -EINPROGRESS_CONFIG;
1313 
1314  DBGC ( netdev, "NETDEV %s starting configuration via %s\n",
1316 
1317  /* Start configuration */
1318  if ( ( rc = configurator->start ( &config->job, netdev ) ) != 0 ) {
1319  DBGC ( netdev, "NETDEV %s could not start configuration via "
1320  "%s: %s\n", netdev->name, configurator->name,
1321  strerror ( rc ) );
1322  config->rc = rc;
1323  return rc;
1324  }
1325 
1326  return 0;
1327 }
1328 
1329 /**
1330  * Start network device configuration via all supported configurators
1331  *
1332  * @v netdev Network device
1333  * @ret rc Return status code
1334  */
1336  struct net_device_configurator *configurator;
1337  int rc;
1338 
1339  /* Start configuration for each configurator */
1341 
1342  /* Skip any inapplicable configurators */
1343  if ( ! netdev_configurator_applies ( netdev, configurator ) )
1344  continue;
1345 
1346  /* Start configuration */
1347  if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 )
1348  return rc;
1349  }
1350 
1351  return 0;
1352 }
1353 
1354 /**
1355  * Check if network device has a configuration with a specified status code
1356  *
1357  * @v netdev Network device
1358  * @v rc Status code
1359  * @ret has_rc Network device has a configuration with this status code
1360  */
1361 static int netdev_has_configuration_rc ( struct net_device *netdev, int rc ) {
1362  unsigned int num_configs;
1363  unsigned int i;
1364 
1365  num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
1366  for ( i = 0 ; i < num_configs ; i++ ) {
1367  if ( netdev->configs[i].rc == rc )
1368  return 1;
1369  }
1370  return 0;
1371 }
1372 
1373 /**
1374  * Check if network device configuration is in progress
1375  *
1376  * @v netdev Network device
1377  * @ret is_in_progress Network device configuration is in progress
1378  */
1380 
1382 }
1383 
1384 /**
1385  * Check if network device has at least one successful configuration
1386  *
1387  * @v netdev Network device
1388  * @v configurator Configurator
1389  * @ret rc Return status code
1390  */
1392 
1393  return netdev_has_configuration_rc ( netdev, 0 );
1394 }
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:595
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:343
int netdev_configuration_in_progress(struct net_device *netdev)
Check if network device configuration is in progress.
Definition: netdevice.c:1379
Error message tables.
void netdev_rx_err(struct net_device *netdev, struct io_buffer *iobuf, int rc)
Discard received packet.
Definition: netdevice.c:586
void netdev_tx_defer(struct net_device *netdev, struct io_buffer *iobuf)
Defer transmitted packet.
Definition: netdevice.c:412
#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:1126
#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:440
#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:1295
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned int state
Current device state.
Definition: netdevice.h:395
static size_t netdev_priv_offset(struct net_driver *driver)
Get offset of network device driver private data.
Definition: netdevice.c:118
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:509
void netdev_link_down(struct net_device *netdev)
Mark network device as having link down.
Definition: netdevice.c:230
#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:668
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:291
#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:647
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:538
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
int(* probe)(struct net_device *netdev, void *priv)
Probe device.
Definition: netdevice.h:484
static struct settings * netdev_settings(struct net_device *netdev)
Get per-netdevice configuration settings block.
Definition: netdevice.h:583
size_t priv_len
Size of private data.
Definition: netdevice.h:477
static struct interface_operation netdev_config_ops[]
Network device configuration interface operations.
Definition: netdevice.c:688
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:1335
#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:721
#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:658
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:1278
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:247
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:572
#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:431
void * priv
Driver private data.
Definition: netdevice.h:431
void * netdev_priv(struct net_device *netdev, struct net_driver *driver)
Get network device driver private data.
Definition: netdevice.c:152
#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:262
struct list_head tx_queue
TX packet queue.
Definition: netdevice.h:417
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
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:1047
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:653
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:941
#define NET_DRIVERS
Network driver table.
Definition: netdevice.h:500
unsigned int location
Location.
Definition: device.h:29
static struct interface_descriptor netdev_config_desc
Network device configuration interface descriptor.
Definition: netdevice.c:694
#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:612
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:543
int register_netdev(struct net_device *netdev)
Register network device.
Definition: netdevice.c:759
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:970
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:1102
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:207
#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:334
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:561
void(* notify)(struct net_device *netdev, void *priv)
Notify of device or link state change.
Definition: netdevice.h:490
static void netdev_link_block_expired(struct retry_timer *timer, int fail __unused)
Handle network device link block timer expiry.
Definition: netdevice.c:276
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:1010
#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:1242
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:548
struct device * dev
Underlying hardware device.
Definition: netdevice.h:364
void netdev_close(struct net_device *netdev)
Close network device.
Definition: netdevice.c:895
void netdev_rx_freeze(struct net_device *netdev)
Freeze network device receive queue processing.
Definition: netdevice.c:178
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:192
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:1188
static int netdev_configurator_applies(struct net_device *netdev, struct net_device_configurator *configurator)
Check if configurator applies to network device.
Definition: netdevice.h:623
#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:470
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:1209
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:1073
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
#define EINFO_EUNUSED_CONFIG
Definition: netdevice.c:74
#define DBGC2(...)
Definition: compiler.h:522
static struct tlan_private * priv
Definition: tlan.c:224
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
struct net_device * netdev
Network device.
Definition: netdevice.h:303
void(* remove)(struct net_device *netdev, void *priv)
Remove device.
Definition: netdevice.h:496
struct net_device * find_netdev(const char *name)
Get network device by name.
Definition: netdevice.c:988
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:1227
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:1198
struct net_device_configurator * configurator
Network device configurator.
Definition: netdevice.h:305
#define table_end(table)
Get end of linker table.
Definition: tables.h:308
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:702
#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.
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:203
#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:692
static void netdev_tx_flush(struct net_device *netdev)
Flush device's transmit queue.
Definition: netdevice.c:523
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:549
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:1029
int netdev_configuration_ok(struct net_device *netdev)
Check if network device has at least one successful configuration.
Definition: netdevice.c:1391
#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:1361
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:381
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
static int netdev_irq_supported(struct net_device *netdev)
Check whether or not network device supports interrupts.
Definition: netdevice.h:669
int netdev_open(struct net_device *netdev)
Open network device.
Definition: netdevice.c:861
#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:608
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:162
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:637
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