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