diff options
author | Rusty Russell <rusty@rustcorp.com.au> | 2007-07-26 13:41:03 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-07-26 14:35:17 -0400 |
commit | e2c9784325490c878b7f69aeec1bed98b288bd97 (patch) | |
tree | d474007607c713a30db818107ca0581269f059a2 /drivers/net/lguest_net.c | |
parent | b2b47c214f4e85ce3968120d42e8b18eccb4f4e3 (diff) |
lguest: documentation III: Drivers
Documentation: The Drivers
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/net/lguest_net.c')
-rw-r--r-- | drivers/net/lguest_net.c | 218 |
1 files changed, 201 insertions, 17 deletions
diff --git a/drivers/net/lguest_net.c b/drivers/net/lguest_net.c index 112778652f7d..20df6a848923 100644 --- a/drivers/net/lguest_net.c +++ b/drivers/net/lguest_net.c | |||
@@ -1,6 +1,13 @@ | |||
1 | /* A simple network driver for lguest. | 1 | /*D:500 |
2 | * The Guest network driver. | ||
2 | * | 3 | * |
3 | * Copyright 2006 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation | 4 | * This is very simple a virtual network driver, and our last Guest driver. |
5 | * The only trick is that it can talk directly to multiple other recipients | ||
6 | * (ie. other Guests on the same network). It can also be used with only the | ||
7 | * Host on the network. | ||
8 | :*/ | ||
9 | |||
10 | /* Copyright 2006 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation | ||
4 | * | 11 | * |
5 | * This program is free software; you can redistribute it and/or modify | 12 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by | 13 | * it under the terms of the GNU General Public License as published by |
@@ -28,23 +35,28 @@ | |||
28 | #define MAX_LANS 4 | 35 | #define MAX_LANS 4 |
29 | #define NUM_SKBS 8 | 36 | #define NUM_SKBS 8 |
30 | 37 | ||
38 | /*D:530 The "struct lguestnet_info" contains all the information we need to | ||
39 | * know about the network device. */ | ||
31 | struct lguestnet_info | 40 | struct lguestnet_info |
32 | { | 41 | { |
33 | /* The shared page(s). */ | 42 | /* The mapped device page(s) (an array of "struct lguest_net"). */ |
34 | struct lguest_net *peer; | 43 | struct lguest_net *peer; |
44 | /* The physical address of the device page(s) */ | ||
35 | unsigned long peer_phys; | 45 | unsigned long peer_phys; |
46 | /* The size of the device page(s). */ | ||
36 | unsigned long mapsize; | 47 | unsigned long mapsize; |
37 | 48 | ||
38 | /* The lguest_device I come from */ | 49 | /* The lguest_device I come from */ |
39 | struct lguest_device *lgdev; | 50 | struct lguest_device *lgdev; |
40 | 51 | ||
41 | /* My peerid. */ | 52 | /* My peerid (ie. my slot in the array). */ |
42 | unsigned int me; | 53 | unsigned int me; |
43 | 54 | ||
44 | /* Receive queue. */ | 55 | /* Receive queue: the network packets waiting to be filled. */ |
45 | struct sk_buff *skb[NUM_SKBS]; | 56 | struct sk_buff *skb[NUM_SKBS]; |
46 | struct lguest_dma dma[NUM_SKBS]; | 57 | struct lguest_dma dma[NUM_SKBS]; |
47 | }; | 58 | }; |
59 | /*:*/ | ||
48 | 60 | ||
49 | /* How many bytes left in this page. */ | 61 | /* How many bytes left in this page. */ |
50 | static unsigned int rest_of_page(void *data) | 62 | static unsigned int rest_of_page(void *data) |
@@ -52,39 +64,82 @@ static unsigned int rest_of_page(void *data) | |||
52 | return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE); | 64 | return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE); |
53 | } | 65 | } |
54 | 66 | ||
55 | /* Simple convention: offset 4 * peernum. */ | 67 | /*D:570 Each peer (ie. Guest or Host) on the network binds their receive |
68 | * buffers to a different key: we simply use the physical address of the | ||
69 | * device's memory page plus the peer number. The Host insists that all keys | ||
70 | * be a multiple of 4, so we multiply the peer number by 4. */ | ||
56 | static unsigned long peer_key(struct lguestnet_info *info, unsigned peernum) | 71 | static unsigned long peer_key(struct lguestnet_info *info, unsigned peernum) |
57 | { | 72 | { |
58 | return info->peer_phys + 4 * peernum; | 73 | return info->peer_phys + 4 * peernum; |
59 | } | 74 | } |
60 | 75 | ||
76 | /* This is the routine which sets up a "struct lguest_dma" to point to a | ||
77 | * network packet, similar to req_to_dma() in lguest_blk.c. The structure of a | ||
78 | * "struct sk_buff" has grown complex over the years: it consists of a "head" | ||
79 | * linear section pointed to by "skb->data", and possibly an array of | ||
80 | * "fragments" in the case of a non-linear packet. | ||
81 | * | ||
82 | * Our receive buffers don't use fragments at all but outgoing skbs might, so | ||
83 | * we handle it. */ | ||
61 | static void skb_to_dma(const struct sk_buff *skb, unsigned int headlen, | 84 | static void skb_to_dma(const struct sk_buff *skb, unsigned int headlen, |
62 | struct lguest_dma *dma) | 85 | struct lguest_dma *dma) |
63 | { | 86 | { |
64 | unsigned int i, seg; | 87 | unsigned int i, seg; |
65 | 88 | ||
89 | /* First, we put the linear region into the "struct lguest_dma". Each | ||
90 | * entry can't go over a page boundary, so even though all our packets | ||
91 | * are 1514 bytes or less, we might need to use two entries here: */ | ||
66 | for (i = seg = 0; i < headlen; seg++, i += rest_of_page(skb->data+i)) { | 92 | for (i = seg = 0; i < headlen; seg++, i += rest_of_page(skb->data+i)) { |
67 | dma->addr[seg] = virt_to_phys(skb->data + i); | 93 | dma->addr[seg] = virt_to_phys(skb->data + i); |
68 | dma->len[seg] = min((unsigned)(headlen - i), | 94 | dma->len[seg] = min((unsigned)(headlen - i), |
69 | rest_of_page(skb->data + i)); | 95 | rest_of_page(skb->data + i)); |
70 | } | 96 | } |
97 | |||
98 | /* Now we handle the fragments: at least they're guaranteed not to go | ||
99 | * over a page. skb_shinfo(skb) returns a pointer to the structure | ||
100 | * which tells us about the number of fragments and the fragment | ||
101 | * array. */ | ||
71 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, seg++) { | 102 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, seg++) { |
72 | const skb_frag_t *f = &skb_shinfo(skb)->frags[i]; | 103 | const skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
73 | /* Should not happen with MTU less than 64k - 2 * PAGE_SIZE. */ | 104 | /* Should not happen with MTU less than 64k - 2 * PAGE_SIZE. */ |
74 | if (seg == LGUEST_MAX_DMA_SECTIONS) { | 105 | if (seg == LGUEST_MAX_DMA_SECTIONS) { |
106 | /* We will end up sending a truncated packet should | ||
107 | * this ever happen. Plus, a cool log message! */ | ||
75 | printk("Woah dude! Megapacket!\n"); | 108 | printk("Woah dude! Megapacket!\n"); |
76 | break; | 109 | break; |
77 | } | 110 | } |
78 | dma->addr[seg] = page_to_phys(f->page) + f->page_offset; | 111 | dma->addr[seg] = page_to_phys(f->page) + f->page_offset; |
79 | dma->len[seg] = f->size; | 112 | dma->len[seg] = f->size; |
80 | } | 113 | } |
114 | |||
115 | /* If after all that we didn't use the entire "struct lguest_dma" | ||
116 | * array, we terminate it with a 0 length. */ | ||
81 | if (seg < LGUEST_MAX_DMA_SECTIONS) | 117 | if (seg < LGUEST_MAX_DMA_SECTIONS) |
82 | dma->len[seg] = 0; | 118 | dma->len[seg] = 0; |
83 | } | 119 | } |
84 | 120 | ||
85 | /* We overload multicast bit to show promiscuous mode. */ | 121 | /* |
122 | * Packet transmission. | ||
123 | * | ||
124 | * Our packet transmission is a little unusual. A real network card would just | ||
125 | * send out the packet and leave the receivers to decide if they're interested. | ||
126 | * Instead, we look through the network device memory page and see if any of | ||
127 | * the ethernet addresses match the packet destination, and if so we send it to | ||
128 | * that Guest. | ||
129 | * | ||
130 | * This is made a little more complicated in two cases. The first case is | ||
131 | * broadcast packets: for that we send the packet to all Guests on the network, | ||
132 | * one at a time. The second case is "promiscuous" mode, where a Guest wants | ||
133 | * to see all the packets on the network. We need a way for the Guest to tell | ||
134 | * us it wants to see all packets, so it sets the "multicast" bit on its | ||
135 | * published MAC address, which is never valid in a real ethernet address. | ||
136 | */ | ||
86 | #define PROMISC_BIT 0x01 | 137 | #define PROMISC_BIT 0x01 |
87 | 138 | ||
139 | /* This is the callback which is summoned whenever the network device's | ||
140 | * multicast or promiscuous state changes. If the card is in promiscuous mode, | ||
141 | * we advertise that in our ethernet address in the device's memory. We do the | ||
142 | * same if Linux wants any or all multicast traffic. */ | ||
88 | static void lguestnet_set_multicast(struct net_device *dev) | 143 | static void lguestnet_set_multicast(struct net_device *dev) |
89 | { | 144 | { |
90 | struct lguestnet_info *info = netdev_priv(dev); | 145 | struct lguestnet_info *info = netdev_priv(dev); |
@@ -95,11 +150,14 @@ static void lguestnet_set_multicast(struct net_device *dev) | |||
95 | info->peer[info->me].mac[0] &= ~PROMISC_BIT; | 150 | info->peer[info->me].mac[0] &= ~PROMISC_BIT; |
96 | } | 151 | } |
97 | 152 | ||
153 | /* A simple test function to see if a peer wants to see all packets.*/ | ||
98 | static int promisc(struct lguestnet_info *info, unsigned int peer) | 154 | static int promisc(struct lguestnet_info *info, unsigned int peer) |
99 | { | 155 | { |
100 | return info->peer[peer].mac[0] & PROMISC_BIT; | 156 | return info->peer[peer].mac[0] & PROMISC_BIT; |
101 | } | 157 | } |
102 | 158 | ||
159 | /* Another simple function to see if a peer's advertised ethernet address | ||
160 | * matches a packet's destination ethernet address. */ | ||
103 | static int mac_eq(const unsigned char mac[ETH_ALEN], | 161 | static int mac_eq(const unsigned char mac[ETH_ALEN], |
104 | struct lguestnet_info *info, unsigned int peer) | 162 | struct lguestnet_info *info, unsigned int peer) |
105 | { | 163 | { |
@@ -109,6 +167,8 @@ static int mac_eq(const unsigned char mac[ETH_ALEN], | |||
109 | return memcmp(mac+1, info->peer[peer].mac+1, ETH_ALEN-1) == 0; | 167 | return memcmp(mac+1, info->peer[peer].mac+1, ETH_ALEN-1) == 0; |
110 | } | 168 | } |
111 | 169 | ||
170 | /* This is the function which actually sends a packet once we've decided a | ||
171 | * peer wants it: */ | ||
112 | static void transfer_packet(struct net_device *dev, | 172 | static void transfer_packet(struct net_device *dev, |
113 | struct sk_buff *skb, | 173 | struct sk_buff *skb, |
114 | unsigned int peernum) | 174 | unsigned int peernum) |
@@ -116,76 +176,134 @@ static void transfer_packet(struct net_device *dev, | |||
116 | struct lguestnet_info *info = netdev_priv(dev); | 176 | struct lguestnet_info *info = netdev_priv(dev); |
117 | struct lguest_dma dma; | 177 | struct lguest_dma dma; |
118 | 178 | ||
179 | /* We use our handy "struct lguest_dma" packing function to prepare | ||
180 | * the skb for sending. */ | ||
119 | skb_to_dma(skb, skb_headlen(skb), &dma); | 181 | skb_to_dma(skb, skb_headlen(skb), &dma); |
120 | pr_debug("xfer length %04x (%u)\n", htons(skb->len), skb->len); | 182 | pr_debug("xfer length %04x (%u)\n", htons(skb->len), skb->len); |
121 | 183 | ||
184 | /* This is the actual send call which copies the packet. */ | ||
122 | lguest_send_dma(peer_key(info, peernum), &dma); | 185 | lguest_send_dma(peer_key(info, peernum), &dma); |
186 | |||
187 | /* Check that the entire packet was transmitted. If not, it could mean | ||
188 | * that the other Guest registered a short receive buffer, but this | ||
189 | * driver should never do that. More likely, the peer is dead. */ | ||
123 | if (dma.used_len != skb->len) { | 190 | if (dma.used_len != skb->len) { |
124 | dev->stats.tx_carrier_errors++; | 191 | dev->stats.tx_carrier_errors++; |
125 | pr_debug("Bad xfer to peer %i: %i of %i (dma %p/%i)\n", | 192 | pr_debug("Bad xfer to peer %i: %i of %i (dma %p/%i)\n", |
126 | peernum, dma.used_len, skb->len, | 193 | peernum, dma.used_len, skb->len, |
127 | (void *)dma.addr[0], dma.len[0]); | 194 | (void *)dma.addr[0], dma.len[0]); |
128 | } else { | 195 | } else { |
196 | /* On success we update the stats. */ | ||
129 | dev->stats.tx_bytes += skb->len; | 197 | dev->stats.tx_bytes += skb->len; |
130 | dev->stats.tx_packets++; | 198 | dev->stats.tx_packets++; |
131 | } | 199 | } |
132 | } | 200 | } |
133 | 201 | ||
202 | /* Another helper function to tell is if a slot in the device memory is unused. | ||
203 | * Since we always set the Local Assignment bit in the ethernet address, the | ||
204 | * first byte can never be 0. */ | ||
134 | static int unused_peer(const struct lguest_net peer[], unsigned int num) | 205 | static int unused_peer(const struct lguest_net peer[], unsigned int num) |
135 | { | 206 | { |
136 | return peer[num].mac[0] == 0; | 207 | return peer[num].mac[0] == 0; |
137 | } | 208 | } |
138 | 209 | ||
210 | /* Finally, here is the routine which handles an outgoing packet. It's called | ||
211 | * "start_xmit" for traditional reasons. */ | ||
139 | static int lguestnet_start_xmit(struct sk_buff *skb, struct net_device *dev) | 212 | static int lguestnet_start_xmit(struct sk_buff *skb, struct net_device *dev) |
140 | { | 213 | { |
141 | unsigned int i; | 214 | unsigned int i; |
142 | int broadcast; | 215 | int broadcast; |
143 | struct lguestnet_info *info = netdev_priv(dev); | 216 | struct lguestnet_info *info = netdev_priv(dev); |
217 | /* Extract the destination ethernet address from the packet. */ | ||
144 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; | 218 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
145 | 219 | ||
146 | pr_debug("%s: xmit %02x:%02x:%02x:%02x:%02x:%02x\n", | 220 | pr_debug("%s: xmit %02x:%02x:%02x:%02x:%02x:%02x\n", |
147 | dev->name, dest[0],dest[1],dest[2],dest[3],dest[4],dest[5]); | 221 | dev->name, dest[0],dest[1],dest[2],dest[3],dest[4],dest[5]); |
148 | 222 | ||
223 | /* If it's a multicast packet, we broadcast to everyone. That's not | ||
224 | * very efficient, but there are very few applications which actually | ||
225 | * use multicast, which is a shame really. | ||
226 | * | ||
227 | * As etherdevice.h points out: "By definition the broadcast address is | ||
228 | * also a multicast address." So we don't have to test for broadcast | ||
229 | * packets separately. */ | ||
149 | broadcast = is_multicast_ether_addr(dest); | 230 | broadcast = is_multicast_ether_addr(dest); |
231 | |||
232 | /* Look through all the published ethernet addresses to see if we | ||
233 | * should send this packet. */ | ||
150 | for (i = 0; i < info->mapsize/sizeof(struct lguest_net); i++) { | 234 | for (i = 0; i < info->mapsize/sizeof(struct lguest_net); i++) { |
235 | /* We don't send to ourselves (we actually can't SEND_DMA to | ||
236 | * ourselves anyway), and don't send to unused slots.*/ | ||
151 | if (i == info->me || unused_peer(info->peer, i)) | 237 | if (i == info->me || unused_peer(info->peer, i)) |
152 | continue; | 238 | continue; |
153 | 239 | ||
240 | /* If it's broadcast we send it. If they want every packet we | ||
241 | * send it. If the destination matches their address we send | ||
242 | * it. Otherwise we go to the next peer. */ | ||
154 | if (!broadcast && !promisc(info, i) && !mac_eq(dest, info, i)) | 243 | if (!broadcast && !promisc(info, i) && !mac_eq(dest, info, i)) |
155 | continue; | 244 | continue; |
156 | 245 | ||
157 | pr_debug("lguestnet %s: sending from %i to %i\n", | 246 | pr_debug("lguestnet %s: sending from %i to %i\n", |
158 | dev->name, info->me, i); | 247 | dev->name, info->me, i); |
248 | /* Our routine which actually does the transfer. */ | ||
159 | transfer_packet(dev, skb, i); | 249 | transfer_packet(dev, skb, i); |
160 | } | 250 | } |
251 | |||
252 | /* An xmit routine is expected to dispose of the packet, so we do. */ | ||
161 | dev_kfree_skb(skb); | 253 | dev_kfree_skb(skb); |
254 | |||
255 | /* As per kernel convention, 0 means success. This is why I love | ||
256 | * networking: even if we never sent to anyone, that's still | ||
257 | * success! */ | ||
162 | return 0; | 258 | return 0; |
163 | } | 259 | } |
164 | 260 | ||
165 | /* Find a new skb to put in this slot in shared mem. */ | 261 | /*D:560 |
262 | * Packet receiving. | ||
263 | * | ||
264 | * First, here's a helper routine which fills one of our array of receive | ||
265 | * buffers: */ | ||
166 | static int fill_slot(struct net_device *dev, unsigned int slot) | 266 | static int fill_slot(struct net_device *dev, unsigned int slot) |
167 | { | 267 | { |
168 | struct lguestnet_info *info = netdev_priv(dev); | 268 | struct lguestnet_info *info = netdev_priv(dev); |
169 | /* Try to create and register a new one. */ | 269 | |
270 | /* We can receive ETH_DATA_LEN (1500) byte packets, plus a standard | ||
271 | * ethernet header of ETH_HLEN (14) bytes. */ | ||
170 | info->skb[slot] = netdev_alloc_skb(dev, ETH_HLEN + ETH_DATA_LEN); | 272 | info->skb[slot] = netdev_alloc_skb(dev, ETH_HLEN + ETH_DATA_LEN); |
171 | if (!info->skb[slot]) { | 273 | if (!info->skb[slot]) { |
172 | printk("%s: could not fill slot %i\n", dev->name, slot); | 274 | printk("%s: could not fill slot %i\n", dev->name, slot); |
173 | return -ENOMEM; | 275 | return -ENOMEM; |
174 | } | 276 | } |
175 | 277 | ||
278 | /* skb_to_dma() is a helper which sets up the "struct lguest_dma" to | ||
279 | * point to the data in the skb: we also use it for sending out a | ||
280 | * packet. */ | ||
176 | skb_to_dma(info->skb[slot], ETH_HLEN + ETH_DATA_LEN, &info->dma[slot]); | 281 | skb_to_dma(info->skb[slot], ETH_HLEN + ETH_DATA_LEN, &info->dma[slot]); |
282 | |||
283 | /* This is a Write Memory Barrier: it ensures that the entry in the | ||
284 | * receive buffer array is written *before* we set the "used_len" entry | ||
285 | * to 0. If the Host were looking at the receive buffer array from a | ||
286 | * different CPU, it could potentially see "used_len = 0" and not see | ||
287 | * the updated receive buffer information. This would be a horribly | ||
288 | * nasty bug, so make sure the compiler and CPU know this has to happen | ||
289 | * first. */ | ||
177 | wmb(); | 290 | wmb(); |
178 | /* Now we tell hypervisor it can use the slot. */ | 291 | /* Writing 0 to "used_len" tells the Host it can use this receive |
292 | * buffer now. */ | ||
179 | info->dma[slot].used_len = 0; | 293 | info->dma[slot].used_len = 0; |
180 | return 0; | 294 | return 0; |
181 | } | 295 | } |
182 | 296 | ||
297 | /* This is the actual receive routine. When we receive an interrupt from the | ||
298 | * Host to tell us a packet has been delivered, we arrive here: */ | ||
183 | static irqreturn_t lguestnet_rcv(int irq, void *dev_id) | 299 | static irqreturn_t lguestnet_rcv(int irq, void *dev_id) |
184 | { | 300 | { |
185 | struct net_device *dev = dev_id; | 301 | struct net_device *dev = dev_id; |
186 | struct lguestnet_info *info = netdev_priv(dev); | 302 | struct lguestnet_info *info = netdev_priv(dev); |
187 | unsigned int i, done = 0; | 303 | unsigned int i, done = 0; |
188 | 304 | ||
305 | /* Look through our entire receive array for an entry which has data | ||
306 | * in it. */ | ||
189 | for (i = 0; i < ARRAY_SIZE(info->dma); i++) { | 307 | for (i = 0; i < ARRAY_SIZE(info->dma); i++) { |
190 | unsigned int length; | 308 | unsigned int length; |
191 | struct sk_buff *skb; | 309 | struct sk_buff *skb; |
@@ -194,10 +312,16 @@ static irqreturn_t lguestnet_rcv(int irq, void *dev_id) | |||
194 | if (length == 0) | 312 | if (length == 0) |
195 | continue; | 313 | continue; |
196 | 314 | ||
315 | /* We've found one! Remember the skb (we grabbed the length | ||
316 | * above), and immediately refill the slot we've taken it | ||
317 | * from. */ | ||
197 | done++; | 318 | done++; |
198 | skb = info->skb[i]; | 319 | skb = info->skb[i]; |
199 | fill_slot(dev, i); | 320 | fill_slot(dev, i); |
200 | 321 | ||
322 | /* This shouldn't happen: micropackets could be sent by a | ||
323 | * badly-behaved Guest on the network, but the Host will never | ||
324 | * stuff more data in the buffer than the buffer length. */ | ||
201 | if (length < ETH_HLEN || length > ETH_HLEN + ETH_DATA_LEN) { | 325 | if (length < ETH_HLEN || length > ETH_HLEN + ETH_DATA_LEN) { |
202 | pr_debug(KERN_WARNING "%s: unbelievable skb len: %i\n", | 326 | pr_debug(KERN_WARNING "%s: unbelievable skb len: %i\n", |
203 | dev->name, length); | 327 | dev->name, length); |
@@ -205,36 +329,72 @@ static irqreturn_t lguestnet_rcv(int irq, void *dev_id) | |||
205 | continue; | 329 | continue; |
206 | } | 330 | } |
207 | 331 | ||
332 | /* skb_put(), what a great function! I've ranted about this | ||
333 | * function before (http://lkml.org/lkml/1999/9/26/24). You | ||
334 | * call it after you've added data to the end of an skb (in | ||
335 | * this case, it was the Host which wrote the data). */ | ||
208 | skb_put(skb, length); | 336 | skb_put(skb, length); |
337 | |||
338 | /* The ethernet header contains a protocol field: we use the | ||
339 | * standard helper to extract it, and place the result in | ||
340 | * skb->protocol. The helper also sets up skb->pkt_type and | ||
341 | * eats up the ethernet header from the front of the packet. */ | ||
209 | skb->protocol = eth_type_trans(skb, dev); | 342 | skb->protocol = eth_type_trans(skb, dev); |
210 | /* This is a reliable transport. */ | 343 | |
344 | /* If this device doesn't need checksums for sending, we also | ||
345 | * don't need to check the packets when they come in. */ | ||
211 | if (dev->features & NETIF_F_NO_CSUM) | 346 | if (dev->features & NETIF_F_NO_CSUM) |
212 | skb->ip_summed = CHECKSUM_UNNECESSARY; | 347 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
348 | |||
349 | /* As a last resort for debugging the driver or the lguest I/O | ||
350 | * subsystem, you can uncomment the "#define DEBUG" at the top | ||
351 | * of this file, which turns all the pr_debug() into printk() | ||
352 | * and floods the logs. */ | ||
213 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", | 353 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", |
214 | ntohs(skb->protocol), skb->len, skb->pkt_type); | 354 | ntohs(skb->protocol), skb->len, skb->pkt_type); |
215 | 355 | ||
356 | /* Update the packet and byte counts (visible from ifconfig, | ||
357 | * and good for debugging). */ | ||
216 | dev->stats.rx_bytes += skb->len; | 358 | dev->stats.rx_bytes += skb->len; |
217 | dev->stats.rx_packets++; | 359 | dev->stats.rx_packets++; |
360 | |||
361 | /* Hand our fresh network packet into the stack's "network | ||
362 | * interface receive" routine. That will free the packet | ||
363 | * itself when it's finished. */ | ||
218 | netif_rx(skb); | 364 | netif_rx(skb); |
219 | } | 365 | } |
366 | |||
367 | /* If we found any packets, we assume the interrupt was for us. */ | ||
220 | return done ? IRQ_HANDLED : IRQ_NONE; | 368 | return done ? IRQ_HANDLED : IRQ_NONE; |
221 | } | 369 | } |
222 | 370 | ||
371 | /*D:550 This is where we start: when the device is brought up by dhcpd or | ||
372 | * ifconfig. At this point we advertise our MAC address to the rest of the | ||
373 | * network, and register receive buffers ready for incoming packets. */ | ||
223 | static int lguestnet_open(struct net_device *dev) | 374 | static int lguestnet_open(struct net_device *dev) |
224 | { | 375 | { |
225 | int i; | 376 | int i; |
226 | struct lguestnet_info *info = netdev_priv(dev); | 377 | struct lguestnet_info *info = netdev_priv(dev); |
227 | 378 | ||
228 | /* Set up our MAC address */ | 379 | /* Copy our MAC address into the device page, so others on the network |
380 | * can find us. */ | ||
229 | memcpy(info->peer[info->me].mac, dev->dev_addr, ETH_ALEN); | 381 | memcpy(info->peer[info->me].mac, dev->dev_addr, ETH_ALEN); |
230 | 382 | ||
231 | /* Turn on promisc mode if needed */ | 383 | /* We might already be in promisc mode (dev->flags & IFF_PROMISC). Our |
384 | * set_multicast callback handles this already, so we call it now. */ | ||
232 | lguestnet_set_multicast(dev); | 385 | lguestnet_set_multicast(dev); |
233 | 386 | ||
387 | /* Allocate packets and put them into our "struct lguest_dma" array. | ||
388 | * If we fail to allocate all the packets we could still limp along, | ||
389 | * but it's a sign of real stress so we should probably give up now. */ | ||
234 | for (i = 0; i < ARRAY_SIZE(info->dma); i++) { | 390 | for (i = 0; i < ARRAY_SIZE(info->dma); i++) { |
235 | if (fill_slot(dev, i) != 0) | 391 | if (fill_slot(dev, i) != 0) |
236 | goto cleanup; | 392 | goto cleanup; |
237 | } | 393 | } |
394 | |||
395 | /* Finally we tell the Host where our array of "struct lguest_dma" | ||
396 | * receive buffers is, binding it to the key corresponding to the | ||
397 | * device's physical memory plus our peerid. */ | ||
238 | if (lguest_bind_dma(peer_key(info,info->me), info->dma, | 398 | if (lguest_bind_dma(peer_key(info,info->me), info->dma, |
239 | NUM_SKBS, lgdev_irq(info->lgdev)) != 0) | 399 | NUM_SKBS, lgdev_irq(info->lgdev)) != 0) |
240 | goto cleanup; | 400 | goto cleanup; |
@@ -245,22 +405,29 @@ cleanup: | |||
245 | dev_kfree_skb(info->skb[i]); | 405 | dev_kfree_skb(info->skb[i]); |
246 | return -ENOMEM; | 406 | return -ENOMEM; |
247 | } | 407 | } |
408 | /*:*/ | ||
248 | 409 | ||
410 | /* The close routine is called when the device is no longer in use: we clean up | ||
411 | * elegantly. */ | ||
249 | static int lguestnet_close(struct net_device *dev) | 412 | static int lguestnet_close(struct net_device *dev) |
250 | { | 413 | { |
251 | unsigned int i; | 414 | unsigned int i; |
252 | struct lguestnet_info *info = netdev_priv(dev); | 415 | struct lguestnet_info *info = netdev_priv(dev); |
253 | 416 | ||
254 | /* Clear all trace: others might deliver packets, we'll ignore it. */ | 417 | /* Clear all trace of our existence out of the device memory by setting |
418 | * the slot which held our MAC address to 0 (unused). */ | ||
255 | memset(&info->peer[info->me], 0, sizeof(info->peer[info->me])); | 419 | memset(&info->peer[info->me], 0, sizeof(info->peer[info->me])); |
256 | 420 | ||
257 | /* Deregister sg lists. */ | 421 | /* Unregister our array of receive buffers */ |
258 | lguest_unbind_dma(peer_key(info, info->me), info->dma); | 422 | lguest_unbind_dma(peer_key(info, info->me), info->dma); |
259 | for (i = 0; i < ARRAY_SIZE(info->dma); i++) | 423 | for (i = 0; i < ARRAY_SIZE(info->dma); i++) |
260 | dev_kfree_skb(info->skb[i]); | 424 | dev_kfree_skb(info->skb[i]); |
261 | return 0; | 425 | return 0; |
262 | } | 426 | } |
263 | 427 | ||
428 | /*D:510 The network device probe function is basically a standard ethernet | ||
429 | * device setup. It reads the "struct lguest_device_desc" and sets the "struct | ||
430 | * net_device". Oh, the line-by-line excitement! Let's skip over it. :*/ | ||
264 | static int lguestnet_probe(struct lguest_device *lgdev) | 431 | static int lguestnet_probe(struct lguest_device *lgdev) |
265 | { | 432 | { |
266 | int err, irqf = IRQF_SHARED; | 433 | int err, irqf = IRQF_SHARED; |
@@ -290,10 +457,16 @@ static int lguestnet_probe(struct lguest_device *lgdev) | |||
290 | dev->stop = lguestnet_close; | 457 | dev->stop = lguestnet_close; |
291 | dev->hard_start_xmit = lguestnet_start_xmit; | 458 | dev->hard_start_xmit = lguestnet_start_xmit; |
292 | 459 | ||
293 | /* Turning on/off promisc will call dev->set_multicast_list. | 460 | /* We don't actually support multicast yet, but turning on/off |
294 | * We don't actually support multicast yet */ | 461 | * promisc also calls dev->set_multicast_list. */ |
295 | dev->set_multicast_list = lguestnet_set_multicast; | 462 | dev->set_multicast_list = lguestnet_set_multicast; |
296 | SET_NETDEV_DEV(dev, &lgdev->dev); | 463 | SET_NETDEV_DEV(dev, &lgdev->dev); |
464 | |||
465 | /* The network code complains if you have "scatter-gather" capability | ||
466 | * if you don't also handle checksums (it seem that would be | ||
467 | * "illogical"). So we use a lie of omission and don't tell it that we | ||
468 | * can handle scattered packets unless we also don't want checksums, | ||
469 | * even though to us they're completely independent. */ | ||
297 | if (desc->features & LGUEST_NET_F_NOCSUM) | 470 | if (desc->features & LGUEST_NET_F_NOCSUM) |
298 | dev->features = NETIF_F_SG|NETIF_F_NO_CSUM; | 471 | dev->features = NETIF_F_SG|NETIF_F_NO_CSUM; |
299 | 472 | ||
@@ -325,6 +498,9 @@ static int lguestnet_probe(struct lguest_device *lgdev) | |||
325 | } | 498 | } |
326 | 499 | ||
327 | pr_debug("lguestnet: registered device %s\n", dev->name); | 500 | pr_debug("lguestnet: registered device %s\n", dev->name); |
501 | /* Finally, we put the "struct net_device" in the generic "struct | ||
502 | * lguest_device"s private pointer. Again, it's not necessary, but | ||
503 | * makes sure the cool kernel kids don't tease us. */ | ||
328 | lgdev->private = dev; | 504 | lgdev->private = dev; |
329 | return 0; | 505 | return 0; |
330 | 506 | ||
@@ -352,3 +528,11 @@ module_init(lguestnet_init); | |||
352 | 528 | ||
353 | MODULE_DESCRIPTION("Lguest network driver"); | 529 | MODULE_DESCRIPTION("Lguest network driver"); |
354 | MODULE_LICENSE("GPL"); | 530 | MODULE_LICENSE("GPL"); |
531 | |||
532 | /*D:580 | ||
533 | * This is the last of the Drivers, and with this we have covered the many and | ||
534 | * wonderous and fine (and boring) details of the Guest. | ||
535 | * | ||
536 | * "make Launcher" beckons, where we answer questions like "Where do Guests | ||
537 | * come from?", and "What do you do when someone asks for optimization?" | ||
538 | */ | ||