* For Ethernet, this is the MAC address.
*/
uint8_t ll_addr[MAX_LLH_ADDR_LEN];
+ /** Linked list of network devices */
+ struct list_head devices;
/** List of network interfaces */
struct list_head interfaces;
/** Driver private data */
/** Underlying net device */
struct net_device *netdev;
/** Linked list of interfaces for this device */
- struct list_head list;
+ struct list_head interfaces;
/** Network-layer protocol
*
* This is an ETH_P_XXX constant, in network byte order.
uint8_t net_addr_len;
/** Network-layer address */
uint8_t net_addr[MAX_NET_ADDR_LEN];
- /** Packet processor
- *
- * @v netif Network interface
- * @v pkb Packet buffer
- * @ret rc Return status code
- *
- * This method is called for packets arriving on the
- * associated network device that match this interface's
- * network-layer protocol.
- *
- * When this method is called, the link-layer header will
- * already have been stripped from the packet.
- */
- int ( * process ) ( struct net_interface *netif,
- struct pk_buff *pkb );
/** Fill in packet metadata
*
* @v netif Network interface
*/
int ( * add_llh_metadata ) ( struct net_interface *netif,
struct pk_buff *pkb );
+ /** Received packet processor
+ *
+ * @v netif Network interface
+ * @v pkb Packet buffer
+ * @ret rc Return status code
+ *
+ * This method is called for packets arriving on the
+ * associated network device that match this interface's
+ * network-layer protocol.
+ *
+ * When this method is called, the link-layer header will
+ * already have been stripped from the packet.
+ */
+ int ( * rx_packet ) ( struct net_interface *netif,
+ struct pk_buff *pkb );
};
/**
netdev_find_netif ( const struct net_device *netdev, uint16_t net_proto ) {
struct net_interface *netif;
- list_for_each_entry ( netif, &netdev->interfaces, list ) {
+ list_for_each_entry ( netif, &netdev->interfaces, interfaces ) {
if ( netif->net_proto == net_proto )
return netif;
}
return NULL;
}
+extern int register_netdevice ( struct net_device *netdev );
+extern void unregister_netdevice ( struct net_device *netdev );
extern int netdev_send ( struct net_device *netdev, struct pk_buff *pkb );
+extern int netdev_poll ( struct net_device *netdev, struct pk_buff *pkb );
extern int netif_send ( struct net_interface *netif, struct pk_buff *pkb );
+extern int netdev_rx_packet ( struct net_device *netdev, struct pk_buff *pkb );
+extern int net_poll ( struct pk_buff *pkb, struct net_device **netdev );
+
+
extern struct net_device static_single_netdev;
static_single_netdev.priv = priv_data; \
&static_single_netdev; } )
-extern int register_netdevice ( struct net_device *netdev );
-
-extern void unregister_netdevice ( struct net_device *netdev );
static inline void free_netdevice ( struct net_device *netdev __unused ) {
/* Do nothing */
--- /dev/null
+/*
+ * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <stdint.h>
+#include <byteswap.h>
+#include <errno.h>
+#include <gpxe/if_ether.h>
+#include <gpxe/pkbuff.h>
+#include <gpxe/netdevice.h>
+
+/** @file
+ *
+ * Network devices and network interfaces
+ *
+ */
+
+/** List of all registered network devices */
+static LIST_HEAD ( net_devices );
+
+/**
+ * Register network device
+ *
+ * @v netdev Network device
+ * @ret rc Return status code
+ *
+ * Adds the network device to the list of network devices.
+ */
+int register_netdevice ( struct net_device *netdev ) {
+ list_add ( &netdev->devices, &net_devices );
+ return 0;
+}
+
+/**
+ * Unregister network device
+ *
+ * @v netdev Network device
+ *
+ * Removes the network device from the list of network devices.
+ */
+void unregister_netdevice ( struct net_device *netdev ) {
+ list_del ( &netdev->devices );
+}
+
+/**
+ * Transmit packet via network device
+ *
+ * @v netdev Network device
+ * @v pkb Packet buffer
+ * @ret rc Return status code
+ *
+ * Transmits the packet via the network device. The @c pkb link-layer
+ * metadata must already have been filled in, and space for the
+ * link-layer header must already be present in the packet buffer.
+ */
+int netdev_send ( struct net_device *netdev, struct pk_buff *pkb ) {
+ int rc;
+
+ if ( pkb->net_proto != ETH_P_RAW ) {
+ if ( ( rc = netdev->build_llh ( netdev, pkb ) ) != 0 )
+ return rc;
+ }
+ return netdev->transmit ( netdev, pkb );
+}
+
+/**
+ * Poll for packet on network device
+ *
+ * @v netdev Network device
+ * @v pkb Packet buffer
+ * @ret rc Return status code
+ *
+ * Polls the network device for a packet. If a packet is available,
+ * it will be added to the packet buffer, and the link-layer metadata
+ * fields in @c pkb will be filled in.
+ */
+int netdev_poll ( struct net_device *netdev, struct pk_buff *pkb ) {
+ int rc;
+
+ if ( ( rc = netdev->poll ( netdev, pkb ) ) != 0 )
+ return rc;
+ return netdev->parse_llh ( netdev, pkb );
+}
+
+/**
+ * Transmit packet via network interface
+ *
+ * @v netif Network interface
+ * @v pkb Packet buffer
+ * @ret rc Return status code
+ *
+ * Transmits the packet via the network interface. The packet must
+ * start with a network-layer header (e.g. an IP header, for an IP
+ * interface). The packet contents are undefined on return.
+ */
+int netif_send ( struct net_interface *netif, struct pk_buff *pkb ) {
+ struct net_device *netdev = netif->netdev;
+ int rc;
+
+ if ( ( rc = netif->add_llh_metadata ( netif, pkb ) ) != 0 )
+ return rc;
+ pkb_push ( pkb, netdev->ll_hlen );
+ return netdev_send ( netdev, pkb );
+}
+
+/**
+ * Process received packet
+ *
+ * @v netif Network interface
+ * @v pkb Packet buffer
+ * @ret rc Return status code
+ *
+ * Processes a packet received via netdev_poll(). The interface
+ * corresponding to the network-layer protocol is identified, the
+ * link-layer header is stripped from the packet and the packet is
+ * passed to the net_interface::rx_packet() method.
+ */
+int netdev_rx_packet ( struct net_device *netdev, struct pk_buff *pkb ) {
+ struct net_interface *netif;
+
+ netif = netdev_find_netif ( netdev, pkb->net_proto );
+ if ( ! netif )
+ return -EAFNOSUPPORT;
+
+ pkb_pull ( pkb, netdev->ll_hlen );
+ return netif->rx_packet ( netif, pkb );
+}
+
+/**
+ * Poll for packet on all network devices
+ *
+ * @v pkb Packet buffer
+ * @ret netdev Network device
+ * @ret rc Return status code
+ *
+ * Polls all network devices for a packet. If a packet is available
+ * on any interface, @c netdev will be filled in and the packet will
+ * be received as per netdev_poll().
+ */
+int net_poll ( struct pk_buff *pkb, struct net_device **netdev ) {
+ int rc;
+
+ list_for_each_entry ( (*netdev), &net_devices, devices ) {
+ if ( ( rc = netdev_poll ( *netdev, pkb ) ) == 0 )
+ return rc;
+ }
+
+ return -EAGAIN;
+}