-
Notifications
You must be signed in to change notification settings - Fork 5
/
pkqueue.c
333 lines (304 loc) · 8.23 KB
/
pkqueue.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
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
/* Copyright © 2020 Björn Victor ([email protected]) */
/* Bridge program for various Chaosnet implementations. */
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
// ncp.c
int pktnum_less(u_short a, u_short b);
int pktnum_equal(u_short a, u_short b);
#include "cbridge.h"
#include "pkqueue.h"
// cf https://gist.github.com/diabloneo/9619917?permalink_comment_id=3364033#gistcomment-3364033
static inline void timespec_diff(struct timespec *a, struct timespec *b,
struct timespec *result) {
result->tv_sec = a->tv_sec - b->tv_sec;
result->tv_nsec = a->tv_nsec - b->tv_nsec;
if (result->tv_nsec < 0) {
--result->tv_sec;
result->tv_nsec += 1000000000L;
}
}
static void
print_pkqueue_holding_lock(struct pkqueue *q)
{
struct pkt_elem *e;
int nelem = 0;
struct timespec now, diff;
timespec_get(&now, TIME_UTC);
if (q == NULL) { printf("#<pkq NULL>\n"); return; }
printf("#<pkq %p len %d first %p last %p", q, q->pkq_len, q->first, q->last);
for (e = q->first; e != NULL; e = e->next) {
nelem++;
printf("\n elem %p ", e);
if (e->pkt != NULL) {
timespec_diff(&now,&e->transmitted, &diff);
printf("%s pkt %#x nbytes %d trans %f ago", ch_opcode_name(ch_opcode(e->pkt)), ch_packetno(e->pkt),
ch_nbytes(e->pkt), diff.tv_sec + (float)(diff.tv_nsec)/(float)1000000000);
}
else
printf("NULL");
}
printf(">\n");
if (nelem != q->pkq_len) {
printf("PKQ: number of elements printed: %d differs from pkq_len %d\n",
nelem, q->pkq_len);
}
}
void
print_pkqueue(struct pkqueue *q)
{
PTLOCKN(q->pkq_mutex,"pkq_mutex");
print_pkqueue_holding_lock(q);
PTUNLOCKN(q->pkq_mutex,"pkq_mutex");
}
struct pkqueue *
make_pkqueue(void)
{
struct pkqueue *q = (struct pkqueue *)malloc(sizeof(struct pkqueue));
if (pthread_mutex_init(&q->pkq_mutex, NULL) != 0)
perror("pthread_mutex_init(pkq_mutex)");
q->pkq_len = 0;
q->first = q->last = NULL;
return q;
}
void
free_pkqueue(struct pkqueue *q)
{
struct pkt_elem *p = NULL, *e;
PTLOCKN(q->pkq_mutex,"pkq_mutex");
for (e = q->first; e != NULL; e = e->next) {
free(e->pkt);
if (p != NULL)
free(p);
p = e;
}
if (p != NULL)
free(p);
PTUNLOCKN(q->pkq_mutex,"pkq_mutex");
pthread_mutex_destroy(&q->pkq_mutex);
free(q);
}
struct pkt_elem * // returns new elem
pkqueue_add(struct chaos_header *pkt, struct pkqueue *q)
{
struct pkt_elem *ql = NULL, *nl;
PTLOCKN(q->pkq_mutex,"pkq_mutex");
ql = q->last; // last in queue
// consistency checks
if (ql != NULL) { // non-empty queue
assert(ql->next == NULL); // last elem has no cdr
} else {
assert(q->pkq_len == 0); // empty: zero length
assert(q->first == NULL); // last NULL => first NULL
}
// make new element
nl = (struct pkt_elem *)malloc(sizeof(struct pkt_elem)); // new last
if (nl == NULL) {
perror("malloc(pkt_elem)"); exit(1);
}
nl->pkt = pkt;
nl->transmitted.tv_sec = 0;
nl->transmitted.tv_nsec = 0;
nl->next = NULL;
if (ql != NULL)
// append new element
ql->next = nl;
else {
// queue was empty
// assert((q->first == NULL) && (q->pkq_len == 0));
// length 1: first and last the same
q->first = nl;
}
// now put the new element last
q->last = nl;
// we added one
q->pkq_len++;
PTUNLOCKN(q->pkq_mutex,"pkq_mutex");
return nl;
}
int // returns some interesting number
pkqueue_insert_by_packetno(struct chaos_header *pkt, struct pkqueue *q)
{
struct pkt_elem *l, *ql = NULL, *nl, *pl;
PTLOCKN(q->pkq_mutex,"pkq_mutex");
nl = (struct pkt_elem *)malloc(sizeof(struct pkt_elem)); // new last
if (nl == NULL) {
perror("malloc(pkt_elem)"); exit(1);
}
nl->pkt = pkt;
nl->transmitted.tv_sec = 0;
nl->transmitted.tv_nsec = 0;
nl->next = NULL;
if (q->first == NULL) {
// optimization: see if q was empty
assert(q->last == NULL);
assert(q->pkq_len == 0);
q->first = nl;
q->last = nl;
q->pkq_len = 1;
} else if ((q->last != NULL) && (q->last->pkt != NULL) && (pktnum_less(ch_packetno(q->last->pkt), ch_packetno(pkt)))) {
// optimization: see if it goes last
// @@@@ add pkt last
nl->next = NULL;
ql = q->last;
ql->next = nl;
q->last = nl;
q->pkq_len++;
} else {
// scan for the right place
for (pl = NULL, l = q->first; (l != NULL) && (l->pkt != NULL) && (pktnum_less(ch_packetno(l->pkt), ch_packetno(pkt))); l = l->next)
// previous
pl = l;
// avoid re-inserting an old pkt
if (pl == NULL && !pktnum_equal(ch_packetno(q->first->pkt), ch_packetno(pkt))) {
// insert first
nl->next = q->first;
q->first = nl;
q->pkq_len++;
} else if (pl != NULL && (l == NULL || !pktnum_equal(ch_packetno(l->pkt), ch_packetno(pkt)))) {
// insert after previous
// @@@@ assert(pl->next == l)
nl->next = l;
pl->next = nl;
q->pkq_len++;
}
}
int len = q->pkq_len;
PTUNLOCKN(q->pkq_mutex,"pkq_mutex");
return len;
}
// get and remove first
struct chaos_header *
pkqueue_get_first(struct pkqueue *q)
{
if ((q == NULL) || (q->first == NULL))
return NULL;
PTLOCKN(q->pkq_mutex,"pkq_mutex");
struct pkt_elem *f = q->first;
struct chaos_header *p = f->pkt;
q->first = f->next;
q->pkq_len--;
if (q->first == NULL) {
q->last = NULL;
// assert(q->pkq_len == 0);
if (q->pkq_len != 0) {
printf("PKQ: unexpected length %d\n", q->pkq_len);
print_pkqueue_holding_lock(q);
abort();
}
}
free(f);
PTUNLOCKN(q->pkq_mutex,"pkq_mutex");
return p;
}
struct chaos_header *
pkqueue_peek_first(struct pkqueue *q)
{
if ((q == NULL) || (q->first == NULL))
return NULL;
else
return q->first->pkt;
}
struct pkt_elem *
pkqueue_peek_first_elem(struct pkqueue *q)
{
if ((q == NULL) || (q->first == NULL))
return NULL;
else
return q->first;
}
struct chaos_header *
pkqueue_peek_last(struct pkqueue *q)
{
if ((q == NULL) || (q->last == NULL))
return NULL;
else
return q->last->pkt;
}
struct chaos_header *
pkqueue_peek_next(struct pkqueue *q)
{
if ((q == NULL) || (q->first == NULL))
return NULL;
else
return q->first->next->pkt;
}
struct pkt_elem *
pkqueue_first_elem(struct pkqueue *q)
{
if (q != NULL)
return q->first;
else
return NULL;
}
struct pkt_elem *
pkqueue_next_elem(struct pkt_elem *e)
{
if (e != NULL)
return e->next;
else
return NULL;
}
struct chaos_header *
pkqueue_elem_pkt(struct pkt_elem *e)
{
if (e != NULL)
return e->pkt;
else
return NULL;
}
struct timespec *
pkqueue_elem_transmitted(struct pkt_elem *e)
{
if (e != NULL)
return &e->transmitted;
else
return NULL;
}
int
pkqueue_length(struct pkqueue *q)
{
return q->pkq_len;
}
void
set_pkqueue_elem_transmitted(struct pkt_elem *e, struct timespec *ts)
{
e->transmitted.tv_sec = ts->tv_sec;
e->transmitted.tv_nsec = ts->tv_nsec;
}
// Unlink a pkt_elem, given the previous elem on the queue (which may be NULL).
// Return the previous (with updated next link), or the first (if prev was NULL).
struct pkt_elem *
pkqueue_unlink_pkt_elem(struct pkt_elem *elem, struct pkt_elem *prev, struct pkqueue *queue)
{
struct pkt_elem *next = elem->next;
struct chaos_header *pkt = elem->pkt;
if (prev != NULL) {
prev->next = next; // skip over this
elem = prev; // back up so the for loop finds the next one
} else {
// This was the first pkt on the queue, update it
queue->first = next;
if (queue->last == elem) // maybe there was only this one pkt in the queue
queue->last = next;
// back up - this might result in one pkt being skipped, but it will be handled next time...
elem = pkqueue_first_elem(queue);
}
queue->pkq_len--;
if (pkt != NULL)
free(pkt);
return elem;
}