File: net_glue.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (757 lines) | stat: -rw-r--r-- 16,813 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/*
 * Copyright (c) 1996-1999 The University of Utah and the Flux Group.
 * 
 * This file is part of the OSKit Linux Glue Libraries, which are free
 * software, also known as "open source;" you can redistribute them and/or
 * modify them under the terms of the GNU General Public License (GPL),
 * version 2, as published by the Free Software Foundation (FSF).
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 *		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 (at your option) any later version.
 */
/*
 * Definitions of things normally provided by Linux.
 */

#include <oskit/dev/net.h>
#include <oskit/io/bufio.h>
#include <oskit/io/netio.h>
#ifdef HPFQ
#include <oskit/hpfq.h>
#endif

#include <oskit/c/assert.h>

#include "net.h"
#include "net_debug.h"
#include "osenv.h"

#include <linux/skbuff.h>		/* struct sk_buff */
#include <linux/malloc.h>		/* kmalloc/free */
#include <linux/netdevice.h>		/* struct device */
#include <linux/interrupt.h>		/* NET_BH */
#include <linux/autoconf.h>		/* CONFIG_foo */
#include <linux/string.h>		/* memcmp */
#include <linux/rtnetlink.h>		/* shlock goo */

#include "irq.h"

static struct sk_buff_head backlog;	/* the receive queue */
static int backlog_size = 0;
const int backlog_max = 300;		/* wow, but that is what Linux has */

/* This no longer exists? */
#define IS_SKB(skb)

/*
 * Forward declarations.
 */
static OSKIT_COMDECL skbuff_io_query(oskit_bufio_t *io, const oskit_iid_t *iid,
                               void **out_ihandle);
static OSKIT_COMDECL_U skbuff_io_addref(oskit_bufio_t *io);
static OSKIT_COMDECL_U skbuff_io_release(oskit_bufio_t *io);

static OSKIT_COMDECL skbuff_io_getsize(oskit_bufio_t *io, oskit_off_t *out_size);
static OSKIT_COMDECL skbuff_io_setsize(oskit_bufio_t *io, oskit_off_t new_size);

static OSKIT_COMDECL skbuff_io_read(oskit_bufio_t *io, void *dest, 
				   oskit_off_t offset, oskit_size_t count,
				   oskit_size_t *out_actual);
static OSKIT_COMDECL skbuff_io_write(oskit_bufio_t *io, const void *dest, 
				    oskit_off_t offset, oskit_size_t count,
				    oskit_size_t *out_actual);

static OSKIT_COMDECL skbuff_io_map(oskit_bufio_t *io, void **out_addr,
				  oskit_off_t offset, oskit_size_t count);
static OSKIT_COMDECL skbuff_io_unmap(oskit_bufio_t *io, void *addr,
				    oskit_off_t offset, oskit_size_t count);
static OSKIT_COMDECL skbuff_io_wire(oskit_bufio_t *io, oskit_addr_t *out_phys_addr,
				   oskit_off_t offset, oskit_size_t count);
static OSKIT_COMDECL skbuff_io_unwire(oskit_bufio_t *io, oskit_addr_t phys_addr,
				     oskit_off_t offset, oskit_size_t count);

static OSKIT_COMDECL skbuff_io_copy(oskit_bufio_t *io, 
				   oskit_off_t offset,
				   oskit_size_t count,
				   oskit_bufio_t **out_io);


static struct oskit_bufio_ops linux_skbuff_io_ops = {
        skbuff_io_query, 
	skbuff_io_addref, 
	skbuff_io_release,
        (void *) 0,
	skbuff_io_read, 
	skbuff_io_write, 
	skbuff_io_getsize,
	skbuff_io_setsize,
        skbuff_io_map, 
	skbuff_io_unmap, 
	skbuff_io_wire, 
	skbuff_io_unwire,
	skbuff_io_copy
};




/*
 *      Add data to an sk_buff
 */

/*
 * Add data to an sk_buff.
 *
 * Linux	net/core/skbuff.c
 */
unsigned char *
skb_put(struct sk_buff *skb, unsigned int len)
{
        unsigned char *tmp = skb->tail;

        IS_SKB(skb);
        skb->tail += len;
        skb->len += len;
        IS_SKB(skb);

        if (skb->tail > skb->end)
                panic("skput:over: %p:%d", __builtin_return_address(0),len);

        return tmp;
}



/*
 * preallocate some space at the beginning of the sk_buff buffer. 
 *
 * Linux	net/core/skbuff.c
 */
void 
skb_reserve(struct sk_buff *skb, unsigned int len)
{
        IS_SKB(skb);
        skb->data += len;
        skb->tail += len;

        if (skb->tail > skb->end)
                panic("sk_res: over");
        if (skb->data < skb->head)
                panic("sk_res: under");
        IS_SKB(skb);
}


/*
 * Initialize the Linux network glue module.
 * This roughly corresponds to Linux's net_dev_init(),
 * except it doesn't walk the static device list
 * because we don't have one!
 */
oskit_error_t
oskit_linux_net_init(void)
{
	static int initialized;

	if (initialized)
		return 0;
	initialized = 1;

	skb_queue_head_init(&backlog);
	init_bh(NET_BH, net_bh);

	return 0;
}


/*
 * Close the device DEV.
 *
 * Compare:
 *	Linux	net/core/dev.c
 *	Mach4	linux_net.c
 *
 * The Linux one calls the dev->stop routine and purges any queued packets.
 * That is what we do.
 * The Mach one does NOTHING!  We used to do that but it was a bad idea.
 */
int
dev_close(struct device *dev)
{
	int ct = 0;

	/*
	 *	Call the device specific close. This cannot fail.
	 *	Only if device is UP
	 */
	 
	if ((dev->flags & IFF_UP) && dev->stop)
		dev->stop(dev);

	/*
	 *	Device is now down.
	 */
	 
	dev->flags&=~(IFF_UP|IFF_RUNNING);

	/*
	 *	Purge any queued packets when we down the link 
	 */
	while(ct < DEV_NUMBUFFS)
	{
		struct sk_buff *skb;
		while((skb = skb_dequeue(&dev->buffs[ct])) != NULL)
			kfree_skb(skb);
		ct++;
	}
	return 0;
}


/* Memory allocation. */


#define ALIGNBYTES              7
#define ALIGN(p)                (((u_long)(p) + ALIGNBYTES) & ~ALIGNBYTES)

/*
 * Allocate an sk_buff with SIZE bytes of data space and fill in a few
 * fields.
 *
 * This is adapted from the Mach one so we set skb->len to size
 * instead of zero like the Linux version does.
 * It is not clear to me why this is but I think it has to do with Linux
 * using the skb_put, etc macros to set len.
 * Also, the Mach one called kmalloc with GFP_KERNEL instead of ATOMIC for
 * some unknown reason.
 *
 * Compare
 *	Linux	net/core/skbuff.c
 *	Mach4	linux_net.c
 */
struct sk_buff *
alloc_skb(unsigned int size, int priority)
{
	struct sk_buff *skb;
	int len = size;

	assert(priority == GFP_ATOMIC);
	
	size += ALIGN(sizeof(struct sk_buff));

	skb = kmalloc(size, priority); 

	if (skb) {
		skb->dev = NULL;
		skb->len = 0;
		skb->truesize = size;
	
		skb->prev = skb->next = NULL;
		skb->list = NULL;

		skb->data = (len ? 
			     (char *)(skb) + ALIGN(sizeof(struct sk_buff)) : 
			     NULL);

		skb->head = skb->tail = skb->data;
		skb->end = skb->data + len;

		skb->fdev_count = 1;
		skb->data_io = NULL;

		skb->ioi.ops = &linux_skbuff_io_ops;

		skb->cloned = 0;
		atomic_set(&skb->users, 1); 
	}
	return skb;
}

/*
 * Wrapper for alloc_skb.
 *
 * The Linux version allocates 16 bytes too much for some unknown
 * reason.  The Mach version doesn't but reverses the sense of
 * alloc_skb and dev_alloc_skb.
 *
 * Compare
 *	Linux	net/core/skbuff.c
 *	Mach4	linux_net.c
 */
struct sk_buff *
dev_alloc_skb(unsigned int size)
{
        struct sk_buff *skb;

	/* Allocate and extra 16 bytes at the beginning for an
	 * ethernet header.
	 */
        skb = alloc_skb(size + 16, GFP_ATOMIC);
        if (skb)
                skb_reserve(skb, 16);

        return skb;
}

/*
 * Free the sk_buff SKB.
 *
 * The Linux version does some lock and refcount checking but,
 * like the Mach one, we don't.  And like before, I don't know why.
 * 
 * Compare
 *	Linux	net/core/skbuff.c
 *	Mach4	linux_net.c
 */
void
__kfree_skb(struct sk_buff *skb)
{

	if (skb == NULL) {
                printk(KERN_CRIT "kfree_skb: skb = NULL (from %p)\n",
                        __builtin_return_address(0));
                return;
	}

	/*
	 * If we've got a data_io pointer, then we need to unmap the 
	 * buffer and release our handler to the bufio object.
	 */
	if (skb->data_io) {
		oskit_bufio_unmap(skb->data_io, skb->data, 0, skb->len);
		oskit_bufio_release(skb->data_io);
	}

	/* 
	 * We don't have to deallocate our buffer explicitly, since
	 * the sk_buff and data get allocated as one huge contiguous
	 * buffer, our implementation of linux_kfree will deallocate
	 * the whole thing.
	 */
	kfree(skb);
}


/* Software interrupts. */

/*
 * This is similar but not equivalent to Linux's dev_transmit,
 * therefore it is not named dev_transmit.
 */
static void
my_dev_transmit(void)
{
	int len;
	struct sk_buff *skb;
	struct device *dev;

	/* Start transmission on interfaces.  */
	for (dev = dev_base; dev != NULL; dev = dev->next) {
		/* Linux just checks for non-zero dev->flags. */
		if (!(dev->flags & IFF_UP) || dev->tbusy)
			continue;

#ifdef HPFQ
		if (oskit_pfq_root) {
			oskit_pfq_reset_path(oskit_pfq_root);
			continue;
		}
#endif

		DPRINTF(D_VSEND, "%s: trying to send\n", dev->name);
		for (;;) {
			skb = skb_dequeue(&dev->buffs[0]);
			if (skb == NULL) {
				DPRINTF(D_VSEND, "%s: nothing to send\n",
					dev->name);
				break;
			}
			len = skb->len;

			if ((*dev->hard_start_xmit)(skb, dev) != 0) {
				DPRINTF(D_VSEND, "%s: send failed, requeue\n",
					dev->name);
				skb_queue_head(&dev->buffs[0], skb);
				mark_bh(NET_BH);
				break;
			}
			else
				DPRINTF(D_SEND, "%s: sent pkt w/len %d\n",
					dev->name, len);
		}
	}
}

/*
 * The software interrupt handler, or bottom-half handler.
 * Its job is to start transmission on all interfaces and to
 * try to empty the receive queue.
 *
 * We are called with interrupts on (XXX make an assert()ion.)
 *
 * The Linux one makes sure it is called atomically but that is only
 * needed if this is NOT called thru the bottom-half mechanism.
 *
 *	Linux	net/core/dev.c
 *	Mach4	linux_net.c
 */
void
net_bh(void)
{
	struct sk_buff *skb;
	
	my_dev_transmit();

	/*
	 * Be careful not to break out of this loop strangely
	 * since we need the cli before dequeueing.
	 * No, we can't just use the non-underscore version of skb_dequeue
	 * since we need to serialize access to backlog_size too.
	 */
	linux_cli();
	while ((skb = __skb_dequeue(&backlog)) != NULL) {
		struct device *dev = skb->dev;
		oskit_error_t rc;

		backlog_size--;
		linux_sti();
		DPRINTF(D_RECV, "passing packet of len %ld to OS\n", skb->len);

		rc = oskit_netio_push(dev->my_alias->recv_ioi, &skb->ioi, skb->len);
		if (rc != 0) {
			DPRINTF(D_VRECV, "oskit_netio_push failed\n");
		}			
		oskit_bufio_release(&skb->ioi);
		linux_cli();
	}
	linux_sti();

	my_dev_transmit();		/* supposedly a ood idea */
}


/* Hardware interrupts. */

/*
 * Accept packet SKB received on an interface and queue it.
 *
 * Compare
 *	Linux	net/core/dev.c
 *	Mach4	linux_net.c
 *
 * The Mach one copies it into a Mach ipc_kmsg_t, frees it, and calls
 * Mach's net_packet (which does queuing.)
 * The Linux one adds it to a backlog queue (which is emptied in the
 * bottom half handler), and then mark_bh(NET_BH)
 */
void
netif_rx(struct sk_buff *skb)
{
	static int dropping = 0;

	/*
	 *	Check that we aren't overdoing things.
	 */
	DPRINTF(D_RECV, "%s gave me pkt of len %ld, thanks! can I keep it?\n",
		skb->dev->name, skb->len);
	DPRINTF(D_VRECV, "backlog size %d\n", backlog_size);
	if (!backlog_size)
  		dropping = 0;
	else if (backlog_size > backlog_max)
		dropping = 1;

	if (dropping) 
	{
		kfree_skb(skb);
		return;
	}

	/*
	 *	Add it to the "backlog" queue. 
	 */
#if CONFIG_SKB_CHECK
	IS_SKB(skb);
#endif	

	skb_queue_tail(&backlog, skb);
	backlog_size++;
  
	/*
	 *	If any packet arrived, mark it for processing after the
	 *	hardware interrupt returns.
	 */

#ifdef CONFIG_NET_RUNONIRQ	/* Dont enable yet, needs some driver mods */
	net_bh();
#else
	mark_bh(NET_BH);
#endif
	return;
}



/*
 * skbuff COM interface definitions.
 */

#define GET_SKB_FROM_BUF_IO(io, skb) { \
	/* our bufio ops struct is at the end of the skbuff. */ \
	(skb) = (struct sk_buff *)(((char *)(io)) - \
		(sizeof(struct sk_buff) - sizeof(struct oskit_bufio))); \
	if ((skb) == NULL) \
                panic("%s:%d: null bufio_t", __FILE__, __LINE__); \
        if ((skb)->fdev_count == 0) \
                panic("%s:%d: bad count", __FILE__, __LINE__); \
} 


static OSKIT_COMDECL 
skbuff_io_query(oskit_bufio_t *io, const oskit_iid_t *iid,
			    void **out_ihandle) 
{
	struct sk_buff *skb;

	
	GET_SKB_FROM_BUF_IO(io, skb);

        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_bufio_iid, sizeof(*iid)) == 0) {
                *out_ihandle = &skb->ioi;
                ++skb->fdev_count;

                return 0;
        }

        *out_ihandle = NULL;
        return OSKIT_E_NOINTERFACE;
}


static OSKIT_COMDECL_U 
skbuff_io_addref(oskit_bufio_t *io)
{
	struct sk_buff *skb;

	GET_SKB_FROM_BUF_IO(io, skb);

        return ++skb->fdev_count;
}

static OSKIT_COMDECL_U 
skbuff_io_release(oskit_bufio_t *io) 
{
	struct sk_buff *skb;
	unsigned newcount;

	GET_SKB_FROM_BUF_IO(io, skb);

	if ((newcount = --skb->fdev_count) == 0) {
                /* ref count = 0, need to free! */

		/* 
		 * We ignore the parameter to kfree_skb, since it only
		 * has to do with freeing the struct sock in an sk_buff,
		 * which we don't support anyway. =)
		 */
		kfree_skb(skb);
        }

        return newcount;
}


static OSKIT_COMDECL skbuff_io_getsize(oskit_bufio_t *io, oskit_off_t *out_size)
{
	struct sk_buff *skb;

	GET_SKB_FROM_BUF_IO(io, skb);

	*out_size = skb->len;
	return 0;
}

static OSKIT_COMDECL skbuff_io_setsize(oskit_bufio_t *io, oskit_off_t new_size)
{
	return OSKIT_E_NOTIMPL;
}


static OSKIT_COMDECL 
skbuff_io_read(oskit_bufio_t *io, void *dest,
	       oskit_off_t offset, oskit_size_t count, oskit_size_t *out_actual) 
{
	struct sk_buff *skb;

	GET_SKB_FROM_BUF_IO(io, skb);

	if (offset + count > skb->len)
		count = skb->len - offset;

        memcpy(dest, skb->data + offset, count);
	
	*out_actual = count;

        return (0);
}


static OSKIT_COMDECL 
skbuff_io_write(oskit_bufio_t *io, const void *src,
		oskit_off_t offset, oskit_size_t count,
		oskit_size_t *out_actual)
{
	struct sk_buff *skb;

	GET_SKB_FROM_BUF_IO(io, skb);

	if (offset + count > skb->len)
		count = skb->len - offset;

        memcpy(skb->data + offset, src, count);

	*out_actual = count;

        return 0;
}


static OSKIT_COMDECL 
skbuff_io_map(oskit_bufio_t *io, void **out_addr,
	      oskit_off_t offset, oskit_size_t count)
{
	struct sk_buff *skb;

	GET_SKB_FROM_BUF_IO(io, skb);

	if (offset + count > skb->len)
		return OSKIT_EINVAL;

 	*out_addr = skb->data + offset;

        return 0;
}

/* 
 * XXX These should probably do checking on their inputs so we could 
 * catch bugs in the code that calls them.
 */

static OSKIT_COMDECL 
skbuff_io_unmap(oskit_bufio_t *io, void *addr,
		oskit_off_t offset, oskit_size_t count) 
{
	return 0;
}

static OSKIT_COMDECL 
skbuff_io_wire(oskit_bufio_t *io, oskit_addr_t *out_phys_addr,
	       oskit_off_t offset, oskit_size_t count)
{
	return OSKIT_E_NOTIMPL; /*XXX*/
}

static OSKIT_COMDECL 
skbuff_io_unwire(oskit_bufio_t *io, oskit_addr_t phys_addr,
	         oskit_off_t offset, oskit_size_t count)
{
	return OSKIT_E_NOTIMPL; /*XXX*/
}

static OSKIT_COMDECL skbuff_io_copy(oskit_bufio_t *io, 
				   oskit_off_t offset,
				   oskit_size_t count,
				   oskit_bufio_t **out_io)
{
    return OSKIT_E_NOTIMPL;
}

atomic_t rtnl_rlockct;
struct wait_queue *rtnl_wait;

void rtnl_lock()
{
	rtnl_shlock();
	rtnl_exlock();
}

void rtnl_unlock()
{
	rtnl_exunlock();
	rtnl_shunlock();
}

void skb_over_panic(struct sk_buff *skb, int sz, void *here)
{
	panic("skput:over: %p:%ld put:%d dev:%s", 
		here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>");
}

int register_netdevice(struct device *dev)
{
	struct device *d, **dp;
	static int devindex;

	dev->iflink = -1;

	/* Init, if this function is available */
	if (dev->init && dev->init(dev) != 0)
		return -EIO;

	/* Check for existence, and append to tail of chain */
	for (dp=&dev_base; (d=*dp) != NULL; dp=&d->next) {
		if (d == dev || strcmp(d->name, dev->name) == 0)
			return -EEXIST;
	}
	dev->next = NULL;

	dev->ifindex = devindex++;
	if (dev->iflink == -1)
		dev->iflink = dev->ifindex;
	*dp = dev;

	return 0;
}

int unregister_netdevice(struct device *dev)
{
	struct device *d, **dp;

	if (1) {
		/* If device is running, close it.
		   It is very bad idea, really we should
		   complain loudly here, but random hackery
		   in linux/drivers/net likes it.
		 */
		if (dev->flags & IFF_UP)
			dev_close(dev);

#ifdef CONFIG_NET_FASTROUTE
		dev_clear_fastroute(dev);
#endif
	}

	/* And unlink it from device chain. */
	for (dp = &dev_base; (d=*dp) != NULL; dp=&d->next) {
		if (d == dev) {
			*dp = d->next;
			synchronize_bh();
			d->next = NULL;

			if (dev->destructor)
				dev->destructor(dev);
			return 0;
		}
	}
	return -ENODEV;
}