File: klink.c

package info (click to toggle)
tcng 10b-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,644 kB
  • sloc: ansic: 19,040; pascal: 4,640; yacc: 2,619; sh: 1,914; perl: 1,546; lex: 772; makefile: 751
file content (320 lines) | stat: -rw-r--r-- 7,487 bytes parent folder | download | duplicates (5)
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
/*
 * klink.c - Communication with the kernel
 *
 * Written 2001,2002 by Werner Almesberger
 * Copyright 2001 EPFL-ICA
 * Copyright 2002 Bivio Networks, Werner Almesberger
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "tckernel.h"

#include <linux/socket.h>
#include <asm/system.h> /* grr, pkt_sched needs it */
#include <net/pkt_sched.h>

#include <memutil.h>

#include "tcsim.h"
#include "host.h"
#include "timer.h"
#include "attr.h"


extern int rtnetlink_rcv_skb(struct sk_buff *skb);


static struct sk_buff busy_hack;


const char *print_skb_id(unsigned long skb_id)
{
    static char buf[20];

    if (use_generation) sprintf(buf,"0x%08lx",skb_id);
    else sprintf(buf,"%p",(void *) skb_id);
    return buf;
}


unsigned long get_skb_id(struct sk_buff *skb)
{
    return use_generation ? (unsigned long) SKB_GEN(skb) : (unsigned long) skb;
}


const char *print_skb(struct sk_buff *skb)
{
    return print_skb_id(get_skb_id(skb));
}


struct net_device *lookup_net_device(const char *name)
{
    struct net_device *dev;

    for (dev = dev_base; dev; dev = dev->next)
	if (!strcmp(dev->name,name)) break;
    return dev;
}


void create_net_device(struct host *host,const char *name,int kbps)
{
    struct net_device **next,*dev;
    int index = 1;

    for (next = &dev_base; *next; next = &(*next)->next) {
	if (!strcmp((*next)->name,name))
	    errorf("duplicate interface name \"%s\"",name);
	index++;
    }
    dev = *next = alloc_t(struct net_device);
    memset(dev,0,sizeof(*dev));
    dev->name = stralloc(name);
    dev->mtu = 1500;
    dev->ifindex = index;
    dev->flags = IFF_UP;
    dev->tx_queue_len = 100; /* kernel default */
    dev->kbps = kbps;
    dev->txing = kbps <= 0 ? &busy_hack : NULL;
    dev->host = host;
    dev->peer = NULL;
    dev_activate(dev);
    next = &dev->next;
}


void find_stalled_devices(void)
{
    struct net_device *dev;

    for (dev = dev_base; dev; dev = dev->next)
	if (dev->kbps <= 0 && dev->qdisc->q.qlen)
	    errorf("device \"%s\" is stopped but has unsent packets",dev->name);
}


/*
 * Adapted from net/core/rtnetlink.c
 */


int pseudo_netlink_to_kernel(const void *buf,int size)
{
    struct sk_buff *skb;

    skb = alloc_skb(size,GFP_KERNEL);
    skb->tail += size;
    skb->len += size;
    memcpy(skb->head,buf,size);
    return rtnetlink_rcv_skb(skb);
}


struct rtnetlink_link /* @@@ */
{
    int (*doit)(struct sk_buff *, struct nlmsghdr*, void *attr);
    int (*dumpit)(struct sk_buff *, void *cb);
};


static struct rtnetlink_link link_rtnetlink_table[RTM_MAX-RTM_BASE+1];
struct rtnetlink_link *rtnetlink_links[1] = { /* PF_UNSPEC */
  link_rtnetlink_table };


static void dump_packet(void *buf,int size)
{
    int len = size < snap_len ? size : snap_len;
    int i;

    for (i = 0; i < len; i++)
	printf("%s%02x",i & 3 ? "" : " ",((unsigned char *) buf)[i]);
    printf("%s\n",size == len ? "" : " ...");
}


static int __kernel_enqueue(struct net_device *dev,struct sk_buff *skb)
{
    unsigned long skb_id;
    int res,skb_len;

    netif_schedule(skb->dev);
    if (verbose >= 0) {
	print_time(now);
	printf(" E : %s %d : %s:",print_skb(skb),skb->len,dev->name);
	dump_packet(skb->head,skb->len);
    }
/* @@@ should compute checksum on first enqueuing */
/* @@@ should decrement TTL when forwarding */
    skb_id = get_skb_id(skb);
    skb_len = skb->len;
    res = dev->qdisc->enqueue(skb,dev->qdisc);
    if (res) {
	print_time(now);
	printf(" * : %s %d : %s: enqueue returns %s\n",print_skb_id(skb_id),
	  skb_len,dev->name,enqueue_res(res));
    }
    return res;
}


int kernel_enqueue(struct net_device *dev,void *buf,int size,
  struct attributes attr)
{
    struct sk_buff *skb;

    if (!dev) {
	if (!dev_base) errorf("no device configured");
	if (dev_base->next)
	    errorf("must specify device when using multiple devices");
	dev = dev_base;
    }
    if (!dev->qdisc || !dev->qdisc->enqueue)
	errorf("device %s is not configured\n",dev->name);
    skb = alloc_skb(size,0);
    memcpy(skb->data,buf,size); /* skb_put */
    skb->tail += size;
    skb->len += size;
    skb->dev = dev;
    skb->nh.raw = skb->head;
    skb->nh.iph = (void *) skb->head;
    if (skb->nh.iph->version == 4 && skb->nh.iph->ihl >= 5 && size >= 20)
	skb->nh.iph->tot_len = htons(size);
    skb->protocol = htons(attr.protocol);
    skb->nfmark = attr.nfmark;
    skb->priority = attr.priority;
    skb->tc_index = attr.tc_index;
    return __kernel_enqueue(dev,skb);
}


static void incoming(struct net_device *dev,struct sk_buff *skb)
{
    struct route *rt;
    __u32 addr;

    if (!preserve) {
	skb->nfmark = default_attributes.nfmark;
	skb->priority = default_attributes.priority;
	skb->tc_index = default_attributes.tc_index;
    }
    if (dev->qdisc_ingress) {
	int res;

	if (verbose >= 0) {
	    print_time(now);
	    printf(" I : %s %d : %s:",print_skb(skb),skb->len,dev->name);
	    dump_packet(skb->head,skb->len);
	}
	res = dev->qdisc_ingress->enqueue(skb,dev->qdisc_ingress);
	if (res != NF_ACCEPT) {
	    print_time(now);
	    printf(" * : %s %d : %s: enqueue returns %s\n",print_skb(skb),
	      skb->len,dev->name,ingress_res(res));
	    goto drop;
	}
    }
    if (skb->len < 20)  {
	print_time(now);
	printf(" * : %s %d : %s: too short for IPv4\n",print_skb(skb),skb->len,
	  dev->name);
	goto drop;
    }
    addr = ((__u32 *) skb->nh.iph)[4];
    /* @@@ ridiculously inefficient, I know */
    for (rt = ((struct host *) dev->host)->routes; rt; rt = rt->next)
	if (!((htonl(rt->addr)^addr) & htonl(rt->mask))) break;
    if (!rt) {
	print_time(now);
	printf(" * : %s %d : %s: no route to %u.%u.%u.%u\n",print_skb(skb),
	  skb->len,dev->name,IPQ(ntohl(addr)));
	goto drop;
    }
    skb->dev = rt->dev;
    (void) __kernel_enqueue(rt->dev,skb);
    return;

drop:
    __kfree_skb(skb);
}


static void deliver(struct net_device *dev)
{
    if (dev->txing != &busy_hack) {
	if (!dev->peer) __kfree_skb(dev->txing);
	else incoming(dev->peer,dev->txing);
    }
    dev->txing = NULL;
}


struct dev_poll {
    struct timer_list timer;
    struct net_device *dev;
};


static void dev_do_poll(unsigned long data)
{
    struct dev_poll *dsc = (struct dev_poll *) data;
    struct net_device *dev = dsc->dev;

    free(dsc);
    deliver(dev);
    kernel_poll(dev,0);
}


static void kernel_poll_device(struct net_device *dev,int unbusy)
{
    struct sk_buff *skb;

    if (unbusy && dev->txing) deliver(dev);
    if (dev->txing || !dev->scheduled) return;
    if (!dev->qdisc || !dev->qdisc->dequeue) return;
    skb = dev->qdisc->dequeue(dev->qdisc);
    if (!skb) return;
    if (verbose >= 0) {
	print_time(now);
	printf(" D : %s %d : %s:",print_skb(skb),(int) skb->len,dev->name);
	dump_packet(skb->head,skb->len);
    }
    dev->txing = skb;
    if (dev->kbps > 0) {
	struct dev_poll *dsc;
	struct jiffval next;

	dsc = alloc_t(struct dev_poll);
	next = dtojiffval(jiffvaltod(now)+skb->len*8.0/1000.0/dev->kbps*HZ);
	dsc->timer.expires = next.jiffies;
	dsc->timer.expires_ujiffies = next.ujiffies;
	dsc->timer.data = (unsigned long) dsc;
	dsc->timer.function = dev_do_poll;
	dsc->dev = dev;
	add_hires_timer(&dsc->timer);
    }
}


void kernel_poll(struct net_device *dev,int unbusy)
{
    if (dev) kernel_poll_device(dev,unbusy);
    else for (dev = dev_base; dev; dev = dev->next)
	    kernel_poll_device(dev,unbusy);
}


void set_timer_resolution(void)
{
    extern double tick_in_usec;

    tick_in_usec = (HZ << PSCHED_JSCALE)/1000000.0;
}