6 * Network device and network interface
11 #include <gpxe/list.h>
17 /** Maximum length of a link-layer address */
18 #define MAX_LLH_ADDR_LEN 6
20 /** Maximum length of a network-layer address */
21 #define MAX_NET_ADDR_LEN 4
26 * This structure represents a piece of networking hardware. It has
27 * properties such as a link-layer address and methods for
28 * transmitting and receiving raw packets. It does not know anything
29 * about network-layer protocols (e.g. IP) or their addresses; these
30 * are handled by struct @c net_interface instead.
32 * Note that this structure must represent a generic network device,
33 * not just an Ethernet device.
38 * @v netdev Network device
39 * @v pkb Packet buffer
40 * @ret rc Return status code
42 * This method should cause the hardware to initiate
43 * transmission of the packet buffer. The buffer may be
44 * reused immediately after the method returns, and so the
45 * method should either wait for packet transmission to
46 * complete, or take a copy of the buffer contents.
48 int ( * transmit ) ( struct net_device *netdev,
49 struct pk_buff *pkb );
50 /** Poll for received packet
52 * @v netdev Network device
53 * @v pkb Packet buffer to contain received packet
54 * @ret rc Return status code
56 * This method should cause the hardware to check for a
57 * received packet. If no packet is available, the method
58 * should return -EAGAIN (i.e. this method is *always*
59 * considered to be a non-blocking read). If a packet is
60 * available, the method should fill the packet buffer and
61 * return zero for success.
63 int ( * poll ) ( struct net_device *netdev, struct pk_buff *pkb );
64 /** Build link-layer header
66 * @v netdev Network device
67 * @v pkb Packet buffer
68 * @ret rc Return status code
70 * This method should fill in the link-layer header based on
71 * the metadata contained in @c pkb.
73 * If a link-layer header cannot be constructed (e.g. because
74 * of a missing ARP cache entry), then this method should
75 * return an error (after transmitting an ARP request, if
78 int ( * build_llh ) ( struct net_device *netdev, struct pk_buff *pkb );
79 /** Parse link-layer header
81 * @v netdev Network device
82 * @v pkb Packet buffer
83 * @ret rc Return status code
85 * This method should parse the link-layer header and fill in
86 * the metadata in @c pkb.
88 int ( * parse_llh ) ( struct net_device *netdev, struct pk_buff *pkb );
89 /** Link-layer protocol
91 * This is an ARPHRD_XXX constant, in network byte order.
94 /** Link-layer header length */
96 /** Link-layer address length */
98 /** Link-layer address
100 * For Ethernet, this is the MAC address.
102 uint8_t ll_addr[MAX_LLH_ADDR_LEN];
103 /** Linked list of network devices */
104 struct list_head devices;
105 /** List of network interfaces */
106 struct list_head interfaces;
107 /** Driver private data */
112 * A network interface
114 * This structure represents a particular network layer protocol's
115 * interface to a piece of network hardware (a struct @c net_device).
118 struct net_interface {
119 /** Underlying net device */
120 struct net_device *netdev;
121 /** Linked list of interfaces for this device */
122 struct list_head interfaces;
123 /** Network-layer protocol
125 * This is an ETH_P_XXX constant, in network byte order.
128 /** Network-layer address length */
129 uint8_t net_addr_len;
130 /** Network-layer address */
131 uint8_t net_addr[MAX_NET_ADDR_LEN];
132 /** Fill in packet metadata
134 * @v netif Network interface
135 * @v pkb Packet buffer
136 * @ret rc Return status code
138 * This method should fill in the @c pkb metadata with enough
139 * information to enable net_device::build_llh to construct
140 * the link-layer header.
142 int ( * add_llh_metadata ) ( struct net_interface *netif,
143 struct pk_buff *pkb );
144 /** Received packet processor
146 * @v netif Network interface
147 * @v pkb Packet buffer
148 * @ret rc Return status code
150 * This method is called for packets arriving on the
151 * associated network device that match this interface's
152 * network-layer protocol.
154 * When this method is called, the link-layer header will
155 * already have been stripped from the packet.
157 int ( * rx_packet ) ( struct net_interface *netif,
158 struct pk_buff *pkb );
162 * Find interface for a specific protocol
164 * @v netdev Network device
165 * @v net_proto Network-layer protocol, in network byte order
166 * @ret netif Network interface, or NULL if none found
169 static inline struct net_interface *
170 netdev_find_netif ( const struct net_device *netdev, uint16_t net_proto ) {
171 struct net_interface *netif;
173 list_for_each_entry ( netif, &netdev->interfaces, interfaces ) {
174 if ( netif->net_proto == net_proto )
180 extern int register_netdevice ( struct net_device *netdev );
181 extern void unregister_netdevice ( struct net_device *netdev );
182 extern int netdev_send ( struct net_device *netdev, struct pk_buff *pkb );
183 extern int netdev_poll ( struct net_device *netdev, struct pk_buff *pkb );
184 extern int netif_send ( struct net_interface *netif, struct pk_buff *pkb );
185 extern int netdev_rx_packet ( struct net_device *netdev, struct pk_buff *pkb );
186 extern int net_poll ( struct pk_buff *pkb, struct net_device **netdev );
190 extern struct net_device static_single_netdev;
192 /* Must be a macro because priv_data[] is of variable size */
193 #define alloc_netdevice( priv_size ) ( { \
194 static char priv_data[priv_size]; \
195 static_single_netdev.priv = priv_data; \
196 &static_single_netdev; } )
199 static inline void free_netdevice ( struct net_device *netdev __unused ) {
203 #endif /* _NETDEVICE_H */