OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [core/] [dst.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1278 phoenix
/*
2
 * net/dst.c    Protocol independent destination cache.
3
 *
4
 * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
5
 *
6
 */
7
 
8
#include <linux/bitops.h>
9
#include <linux/types.h>
10
#include <linux/kernel.h>
11
#include <linux/sched.h>
12
#include <linux/mm.h>
13
#include <linux/string.h>
14
#include <linux/errno.h>
15
#include <linux/netdevice.h>
16
#include <linux/skbuff.h>
17
#include <linux/init.h>
18
 
19
#include <net/dst.h>
20
 
21
/* Locking strategy:
22
 * 1) Garbage collection state of dead destination cache
23
 *    entries is protected by dst_lock.
24
 * 2) GC is run only from BH context, and is the only remover
25
 *    of entries.
26
 * 3) Entries are added to the garbage list from both BH
27
 *    and non-BH context, so local BH disabling is needed.
28
 * 4) All operations modify state, so a spinlock is used.
29
 */
30
static struct dst_entry         *dst_garbage_list;
31
#if RT_CACHE_DEBUG >= 2 
32
static atomic_t                  dst_total = ATOMIC_INIT(0);
33
#endif
34
static spinlock_t                dst_lock = SPIN_LOCK_UNLOCKED;
35
 
36
static unsigned long dst_gc_timer_expires;
37
static unsigned long dst_gc_timer_inc = DST_GC_MAX;
38
static void dst_run_gc(unsigned long);
39
 
40
static struct timer_list dst_gc_timer =
41
        { data: DST_GC_MIN, function: dst_run_gc };
42
 
43
 
44
static void dst_run_gc(unsigned long dummy)
45
{
46
        int    delayed = 0;
47
        struct dst_entry * dst, **dstp;
48
 
49
        if (!spin_trylock(&dst_lock)) {
50
                mod_timer(&dst_gc_timer, jiffies + HZ/10);
51
                return;
52
        }
53
 
54
 
55
        del_timer(&dst_gc_timer);
56
        dstp = &dst_garbage_list;
57
        while ((dst = *dstp) != NULL) {
58
                if (atomic_read(&dst->__refcnt)) {
59
                        dstp = &dst->next;
60
                        delayed++;
61
                        continue;
62
                }
63
                *dstp = dst->next;
64
                dst_destroy(dst);
65
        }
66
        if (!dst_garbage_list) {
67
                dst_gc_timer_inc = DST_GC_MAX;
68
                goto out;
69
        }
70
        if ((dst_gc_timer_expires += dst_gc_timer_inc) > DST_GC_MAX)
71
                dst_gc_timer_expires = DST_GC_MAX;
72
        dst_gc_timer_inc += DST_GC_INC;
73
        dst_gc_timer.expires = jiffies + dst_gc_timer_expires;
74
#if RT_CACHE_DEBUG >= 2
75
        printk("dst_total: %d/%d %ld\n",
76
               atomic_read(&dst_total), delayed,  dst_gc_timer_expires);
77
#endif
78
        add_timer(&dst_gc_timer);
79
 
80
out:
81
        spin_unlock(&dst_lock);
82
}
83
 
84
static int dst_discard(struct sk_buff *skb)
85
{
86
        kfree_skb(skb);
87
        return 0;
88
}
89
 
90
static int dst_blackhole(struct sk_buff *skb)
91
{
92
        kfree_skb(skb);
93
        return 0;
94
}
95
 
96
void * dst_alloc(struct dst_ops * ops)
97
{
98
        struct dst_entry * dst;
99
 
100
        if (ops->gc && atomic_read(&ops->entries) > ops->gc_thresh) {
101
                if (ops->gc())
102
                        return NULL;
103
        }
104
        dst = kmem_cache_alloc(ops->kmem_cachep, SLAB_ATOMIC);
105
        if (!dst)
106
                return NULL;
107
        memset(dst, 0, ops->entry_size);
108
        atomic_set(&dst->__refcnt, 0);
109
        dst->ops = ops;
110
        dst->lastuse = jiffies;
111
        dst->input = dst_discard;
112
        dst->output = dst_blackhole;
113
#if RT_CACHE_DEBUG >= 2 
114
        atomic_inc(&dst_total);
115
#endif
116
        atomic_inc(&ops->entries);
117
        return dst;
118
}
119
 
120
void __dst_free(struct dst_entry * dst)
121
{
122
        spin_lock_bh(&dst_lock);
123
 
124
        /* The first case (dev==NULL) is required, when
125
           protocol module is unloaded.
126
         */
127
        if (dst->dev == NULL || !(dst->dev->flags&IFF_UP)) {
128
                dst->input = dst_discard;
129
                dst->output = dst_blackhole;
130
        }
131
        dst->obsolete = 2;
132
        dst->next = dst_garbage_list;
133
        dst_garbage_list = dst;
134
        if (dst_gc_timer_inc > DST_GC_INC) {
135
                dst_gc_timer_inc = DST_GC_INC;
136
                dst_gc_timer_expires = DST_GC_MIN;
137
                mod_timer(&dst_gc_timer, jiffies + dst_gc_timer_expires);
138
        }
139
 
140
        spin_unlock_bh(&dst_lock);
141
}
142
 
143
void dst_destroy(struct dst_entry * dst)
144
{
145
        struct neighbour *neigh = dst->neighbour;
146
        struct hh_cache *hh = dst->hh;
147
 
148
        dst->hh = NULL;
149
        if (hh && atomic_dec_and_test(&hh->hh_refcnt))
150
                kfree(hh);
151
 
152
        if (neigh) {
153
                dst->neighbour = NULL;
154
                neigh_release(neigh);
155
        }
156
 
157
        atomic_dec(&dst->ops->entries);
158
 
159
        if (dst->ops->destroy)
160
                dst->ops->destroy(dst);
161
        if (dst->dev)
162
                dev_put(dst->dev);
163
#if RT_CACHE_DEBUG >= 2 
164
        atomic_dec(&dst_total);
165
#endif
166
        kmem_cache_free(dst->ops->kmem_cachep, dst);
167
}
168
 
169
static int dst_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
170
{
171
        struct net_device *dev = ptr;
172
        struct dst_entry *dst;
173
 
174
        switch (event) {
175
        case NETDEV_UNREGISTER:
176
        case NETDEV_DOWN:
177
                spin_lock_bh(&dst_lock);
178
                for (dst = dst_garbage_list; dst; dst = dst->next) {
179
                        if (dst->dev == dev) {
180
                                /* Dirty hack. We did it in 2.2 (in __dst_free),
181
                                   we have _very_ good reasons not to repeat
182
                                   this mistake in 2.3, but we have no choice
183
                                   now. _It_ _is_ _explicit_ _deliberate_
184
                                   _race_ _condition_.
185
                                 */
186
                                if (event!=NETDEV_DOWN &&
187
                                    !(dev->features & NETIF_F_DYNALLOC) &&
188
                                    dst->output == dst_blackhole) {
189
                                        dst->dev = &loopback_dev;
190
                                        dev_put(dev);
191
                                        dev_hold(&loopback_dev);
192
                                        dst->output = dst_discard;
193
                                        if (dst->neighbour && dst->neighbour->dev == dev) {
194
                                                dst->neighbour->dev = &loopback_dev;
195
                                                dev_put(dev);
196
                                                dev_hold(&loopback_dev);
197
                                        }
198
                                } else {
199
                                        dst->input = dst_discard;
200
                                        dst->output = dst_blackhole;
201
                                }
202
                        }
203
                }
204
                spin_unlock_bh(&dst_lock);
205
                break;
206
        }
207
        return NOTIFY_DONE;
208
}
209
 
210
struct notifier_block dst_dev_notifier = {
211
        dst_dev_event,
212
        NULL,
213
 
214
};
215
 
216
void __init dst_init(void)
217
{
218
        register_netdevice_notifier(&dst_dev_notifier);
219
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.