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