-
Notifications
You must be signed in to change notification settings - Fork 171
/
vr_fragment_assembler.c
258 lines (208 loc) · 6.36 KB
/
vr_fragment_assembler.c
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
/*
* vr_fragment_assembler.c -- the OS specific parts of the assembler
*
* Copyright (c) 2015, Juniper Networks, Inc.
* All rights reserved
*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/workqueue.h>
#include <vr_os.h>
#include <vr_packet.h>
#include <vr_fragment.h>
struct workqueue_struct *vr_linux_assembler_wq;
static int vr_linux_assembler_scan_index;
static int vr_linux_assembler_scan_thresh = 1024;
struct vr_linux_fragment_bucket {
spinlock_t vfb_lock;
struct vr_fragment *vfb_frag_list;
};
struct vr_linux_fragment_bucket *vr_linux_assembler_table;
struct vr_linux_fragment_queue {
struct work_struct vrlfq_work;
struct vr_fragment_queue vrlfq_queue;
};
struct vr_linux_fragment_queue *vr_lfq_pcpu_queues;
static void
vr_linux_fragment_queue_free(struct vr_linux_fragment_queue *vlfq)
{
struct vr_fragment_queue_element *vfqe, *next;
vfqe = vlfq->vrlfq_queue.vfq_tail;
vlfq->vrlfq_queue.vfq_tail = NULL;
while (vfqe) {
next = vfqe->fqe_next;
if (vfqe->fqe_pnode.pl_packet)
vr_pfree(vfqe->fqe_pnode.pl_packet, VP_DROP_MISC);
vfqe->fqe_pnode.pl_packet = NULL;
vr_free(vfqe, VR_FRAGMENT_QUEUE_ELEMENT_OBJECT);
vfqe = next;
}
return;
}
static void
vr_linux_fragment_assembler(struct work_struct *work)
{
uint32_t hash, index;
struct vr_packet *pkt;
struct vr_linux_fragment_bucket *vfb;
struct vr_fragment_queue_element *tail, *tail_n, *tail_p, *tail_pn;
struct vr_linux_fragment_queue *lfq = CONTAINER_OF(vrlfq_work,
struct vr_linux_fragment_queue, work);
tail = __sync_lock_test_and_set(&lfq->vrlfq_queue.vfq_tail, NULL);
if (!tail)
return;
/*
* first, reverse the list, since packets that came later are at the
* head of the list
*/
tail_p = tail->fqe_next;
tail->fqe_next = NULL;
while (tail_p) {
tail_pn = tail_p->fqe_next;
tail_p->fqe_next = tail;
tail = tail_p;
tail_p = tail_pn;
}
/* go through the list and insert it in the assembler work area */
while (tail) {
tail_n = tail->fqe_next;
tail->fqe_next = NULL;
pkt = tail->fqe_pnode.pl_packet;
if (pkt) {
hash = vr_fragment_get_hash(tail->fqe_pnode.pl_vrf, pkt);
index = (hash % VR_LINUX_ASSEMBLER_BUCKETS);
vfb = &vr_linux_assembler_table[index];
spin_lock_bh(&vfb->vfb_lock);
vr_fragment_assembler(&vfb->vfb_frag_list, tail);
spin_unlock_bh(&vfb->vfb_lock);
}
tail = tail_n;
}
return;
}
int
lh_enqueue_to_assembler(struct vrouter *router, struct vr_packet *pkt,
struct vr_forwarding_md *fmd)
{
int ret;
unsigned int cpu;
cpu = vr_get_cpu();
if (cpu >= vr_num_cpus) {
printk("cpu is %u, but max cpu is only %u\n", cpu, vr_num_cpus);
vr_pfree(pkt, VP_DROP_FRAGMENTS);
return -EINVAL;
}
ret = vr_fragment_enqueue(router, &vr_lfq_pcpu_queues[cpu].vrlfq_queue,
pkt, fmd);
if (!ret)
queue_work(vr_linux_assembler_wq, &vr_lfq_pcpu_queues[cpu].vrlfq_work);
return 0;
}
static void
vr_linux_assembler_table_scan(void *arg)
{
unsigned int i, j, scanned = 0;
struct vr_linux_fragment_bucket *vfb;
i = vr_linux_assembler_scan_index;
for (j = 0; j < VR_LINUX_ASSEMBLER_BUCKETS; j++) {
vfb = &vr_linux_assembler_table[(i + j) % VR_LINUX_ASSEMBLER_BUCKETS];
spin_lock_bh(&vfb->vfb_lock);
if (vfb->vfb_frag_list)
scanned += vr_assembler_table_scan(&vfb->vfb_frag_list);
spin_unlock_bh(&vfb->vfb_lock);
if (scanned > vr_linux_assembler_scan_thresh) {
j++;
break;
}
}
vr_linux_assembler_scan_index = (i + j) % VR_LINUX_ASSEMBLER_BUCKETS;
return;
}
static void
vr_linux_assembler_table_exit(void)
{
vr_assembler_table_scan_exit();
if (vr_linux_assembler_table) {
vr_free(vr_linux_assembler_table, VR_ASSEMBLER_TABLE_OBJECT);
vr_linux_assembler_table = NULL;
}
return;
}
static int
vr_linux_assembler_table_init(void)
{
unsigned int i, size;
size = sizeof(struct vr_linux_fragment_bucket) * VR_LINUX_ASSEMBLER_BUCKETS;
vr_linux_assembler_table = vr_zalloc(size, VR_ASSEMBLER_TABLE_OBJECT);
if (!vr_linux_assembler_table) {
printk("%s:%d Allocation for %u failed\n",
__FUNCTION__, __LINE__, size);
return -ENOMEM;
}
for (i = 0; i < VR_LINUX_ASSEMBLER_BUCKETS; i++) {
spin_lock_init(&vr_linux_assembler_table[i].vfb_lock);
}
vr_assembler_table_scan_init(vr_linux_assembler_table_scan);
return 0;
}
static void
vr_linux_fragment_queue_exit(void)
{
int i;
if (vr_lfq_pcpu_queues) {
if (vr_linux_assembler_wq) {
for (i = 0; i < vr_num_cpus; i++) {
cancel_work_sync(&vr_lfq_pcpu_queues[i].vrlfq_work);
}
flush_workqueue(vr_linux_assembler_wq);
destroy_workqueue(vr_linux_assembler_wq);
vr_linux_assembler_wq = NULL;
}
for (i = 0; i < vr_num_cpus; i++)
vr_linux_fragment_queue_free(&vr_lfq_pcpu_queues[i]);
vr_free(vr_lfq_pcpu_queues, VR_FRAGMENT_QUEUE_OBJECT);
vr_lfq_pcpu_queues = NULL;
}
return;
}
static int
vr_linux_fragment_queue_init(void)
{
unsigned int i, size;
size = sizeof(struct vr_linux_fragment_queue) * vr_num_cpus;
vr_lfq_pcpu_queues = vr_zalloc(size, VR_FRAGMENT_QUEUE_OBJECT);
if (!vr_lfq_pcpu_queues) {
printk("%s:%d Allocation for %u failed\n",
__FUNCTION__, __LINE__, size);
return -ENOMEM;
}
for (i = 0; i < vr_num_cpus; i++) {
INIT_WORK(&vr_lfq_pcpu_queues[i].vrlfq_work,
vr_linux_fragment_assembler);
}
vr_linux_assembler_wq = create_workqueue("vr_linux_assembler");
if (!vr_linux_assembler_wq) {
printk("%s:%d Failed to create assembler work queue\n",
__FUNCTION__, __LINE__);
return -ENOMEM;
}
return 0;
}
void
vr_assembler_exit(void)
{
vr_linux_fragment_queue_exit();
vr_linux_assembler_table_exit();
return;
}
int
vr_assembler_init(void)
{
int ret;
if ((ret = vr_linux_fragment_queue_init()))
return ret;
if ((ret = vr_linux_assembler_table_init()))
return ret;
return 0;
}