summaryrefslogtreecommitdiff
path: root/src/lib/shm_flow_set.c
blob: c960bd25b36fd101134a114b4ae61355db5bb294 (plain)
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
/*
 * Ouroboros - Copyright (C) 2016
 *
 * Management of flow_sets for fqueue
 *
 *    Dimitri Staessens <dimitri.staessens@intec.ugent.be>
 *
 * 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.
 *
 * This program 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <ouroboros/config.h>
#include <ouroboros/lockfile.h>
#include <ouroboros/time_utils.h>
#include <ouroboros/shm_flow_set.h>
#include <ouroboros/fqueue.h>
#include <ouroboros/errno.h>

#define OUROBOROS_PREFIX "shm_flow_set"

#include <ouroboros/logs.h>

#include <pthread.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <string.h>
#include <assert.h>

#define FN_MAX_CHARS 255

#define FQUEUESIZE (SHM_BUFFER_SIZE * sizeof(int))

#define SHM_FLOW_SET_FILE_SIZE (IRMD_MAX_FLOWS * sizeof(ssize_t)          \
                                + AP_MAX_FQUEUES * sizeof(size_t)         \
                                + AP_MAX_FQUEUES * sizeof(pthread_cond_t) \
                                + AP_MAX_FQUEUES * FQUEUESIZE             \
                                + sizeof(pthread_mutex_t))

#define fqueue_ptr(fs, idx) (fs->fqueues + SHM_BUFFER_SIZE * idx)

struct shm_flow_set {
        ssize_t *         mtable;
        size_t *          heads;
        pthread_cond_t *  conds;
        int *             fqueues;
        pthread_mutex_t * lock;

        pid_t             api;
};

struct shm_flow_set * shm_flow_set_create()
{
        struct shm_flow_set * set;
        ssize_t *             shm_base;
        pthread_mutexattr_t   mattr;
        pthread_condattr_t    cattr;
        char                  fn[FN_MAX_CHARS];
        mode_t                mask;
        int                   shm_fd;
        int                   i;

        sprintf(fn, SHM_FLOW_SET_PREFIX "%d", getpid());

        set = malloc(sizeof(*set));
        if (set == NULL) {
                LOG_DBG("Could not allocate struct.");
                return NULL;
        }

        mask = umask(0);

        shm_fd = shm_open(fn, O_CREAT | O_EXCL | O_RDWR, 0666);
        if (shm_fd == -1) {
                LOG_DBG("Failed creating flag file.");
                free(set);
                return NULL;
        }

        umask(mask);

        if (ftruncate(shm_fd, SHM_FLOW_SET_FILE_SIZE - 1) < 0) {
                LOG_DBG("Failed to extend flag file.");
                free(set);
                close(shm_fd);
                return NULL;
        }

        shm_base = mmap(NULL,
                        SHM_FLOW_SET_FILE_SIZE,
                        PROT_READ | PROT_WRITE,
                        MAP_SHARED,
                        shm_fd,
                        0);

        close(shm_fd);

        if (shm_base == MAP_FAILED) {
                LOG_DBG("Failed to map shared memory.");
                if (shm_unlink(fn) == -1)
                        LOG_DBG("Failed to remove invalid shm.");

                free(set);
                return NULL;
        }

        set->mtable  = shm_base;
        set->heads   = (size_t *) (set->mtable + IRMD_MAX_FLOWS);
        set->conds   = (pthread_cond_t *)(set->heads + AP_MAX_FQUEUES);
        set->fqueues = (int *) (set->conds + AP_MAX_FQUEUES);
        set->lock    = (pthread_mutex_t *)
                (set->fqueues + AP_MAX_FQUEUES * SHM_BUFFER_SIZE);

        pthread_mutexattr_init(&mattr);
#ifndef __APPLE__
        pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
#endif
        pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
        pthread_mutex_init(set->lock, &mattr);

        pthread_condattr_init(&cattr);
        pthread_condattr_setpshared(&cattr, PTHREAD_PROCESS_SHARED);
#ifndef __APPLE__
        pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK);
#endif
        for (i = 0; i < AP_MAX_FQUEUES; ++i) {
                set->heads[i] = 0;
                pthread_cond_init(&set->conds[i], &cattr);
        }

        for (i = 0; i < IRMD_MAX_FLOWS; ++i)
                set->mtable[i] = -1;

        set->api = getpid();

        return set;
}

struct shm_flow_set * shm_flow_set_open(pid_t api)
{
        struct shm_flow_set * set;
        ssize_t *             shm_base;
        char                  fn[FN_MAX_CHARS];
        int                   shm_fd;

        sprintf(fn, SHM_FLOW_SET_PREFIX "%d", api);

        set = malloc(sizeof(*set));
        if (set == NULL) {
                LOG_DBG("Could not allocate struct.");
                return NULL;
        }

        shm_fd = shm_open(fn, O_RDWR, 0666);
        if (shm_fd == -1) {
                LOG_DBG("%d failed opening shared memory %s.", getpid(), fn);
                free(set);
                return NULL;
        }

        shm_base = mmap(NULL,
                        SHM_FLOW_SET_FILE_SIZE,
                        PROT_READ | PROT_WRITE,
                        MAP_SHARED,
                        shm_fd,
                        0);

        close(shm_fd);

        if (shm_base == MAP_FAILED) {
                LOG_DBG("Failed to map shared memory.");
                if (shm_unlink(fn) == -1)
                        LOG_DBG("Failed to remove invalid shm.");
                free(set);
                return NULL;
        }

        set->mtable  = shm_base;
        set->heads   = (size_t *) (set->mtable + IRMD_MAX_FLOWS);
        set->conds   = (pthread_cond_t *)(set->heads + AP_MAX_FQUEUES);
        set->fqueues = (int *) (set->conds + AP_MAX_FQUEUES);
        set->lock    = (pthread_mutex_t *)
                (set->fqueues + AP_MAX_FQUEUES * SHM_BUFFER_SIZE);

        set->api = api;

        return set;
}

void shm_flow_set_destroy(struct shm_flow_set * set)
{
        char fn[25];
        struct lockfile * lf = NULL;

        assert(set);

        if (set->api != getpid()) {
                lf = lockfile_open();
                if (lf == NULL) {
                        LOG_ERR("Failed to open lockfile.");
                        return;
                }

                if (lockfile_owner(lf) == getpid()) {
                        LOG_DBG("Flow set %d destroyed by IRMd %d.",
                                set->api, getpid());
                        lockfile_close(lf);
                } else {
                        LOG_ERR("AP-I %d tried to destroy flowset owned by %d.",
                                getpid(), set->api);
                        lockfile_close(lf);
                        return;
                }
        }

        sprintf(fn, SHM_FLOW_SET_PREFIX "%d", set->api);

        if (munmap(set->mtable, SHM_FLOW_SET_FILE_SIZE) == -1)
                LOG_DBG("Couldn't unmap shared memory.");

        if (shm_unlink(fn) == -1)
                LOG_DBG("Failed to unlink shm.");

        free(set);
}

void shm_flow_set_close(struct shm_flow_set * set)
{
        assert(set);

        if (munmap(set->mtable, SHM_FLOW_SET_FILE_SIZE) == -1)
                LOG_DBG("Couldn't unmap shared memory.");

        free(set);
}

void shm_flow_set_zero(struct shm_flow_set * shm_set,
                       ssize_t               idx)
{
        ssize_t i = 0;

        assert(!(idx < 0) && idx < AP_MAX_FQUEUES);

        pthread_mutex_lock(shm_set->lock);

        for (i = 0; i < IRMD_MAX_FLOWS; ++i)
                if (shm_set->mtable[i] == idx)
                        shm_set->mtable[i] = -1;

        shm_set->heads[idx] = 0;

        pthread_mutex_unlock(shm_set->lock);
}


int shm_flow_set_add(struct shm_flow_set * shm_set,
                     ssize_t               idx,
                     int                   port_id)
{
        assert(shm_set);
        assert(!(port_id < 0) && port_id < IRMD_MAX_FLOWS);
        assert(!(idx < 0) && idx < AP_MAX_FQUEUES);

        pthread_mutex_lock(shm_set->lock);

        if (shm_set->mtable[port_id] != -1) {
                pthread_mutex_unlock(shm_set->lock);
                return -EPERM;
        }

        shm_set->mtable[port_id] = idx;

        pthread_mutex_unlock(shm_set->lock);

        return 0;
}

void shm_flow_set_del(struct shm_flow_set * shm_set,
                      ssize_t               idx,
                      int                   port_id)
{
        assert(shm_set);
        assert(!(port_id < 0) && port_id < IRMD_MAX_FLOWS);
        assert(!(idx < 0) && idx < AP_MAX_FQUEUES);

        pthread_mutex_lock(shm_set->lock);

        if (shm_set->mtable[port_id] == idx)
                shm_set->mtable[port_id] = -1;

        pthread_mutex_unlock(shm_set->lock);
}

int shm_flow_set_has(struct shm_flow_set * shm_set,
                     ssize_t               idx,
                     int                   port_id)
{
        int ret = 0;

        assert(shm_set);
        assert(!(port_id < 0) && port_id < IRMD_MAX_FLOWS);
        assert(!(idx < 0) && idx < AP_MAX_FQUEUES);


        pthread_mutex_lock(shm_set->lock);

        if (shm_set->mtable[port_id] == idx)
                ret = 1;

        pthread_mutex_unlock(shm_set->lock);

        return ret;
}

void shm_flow_set_notify(struct shm_flow_set * shm_set, int port_id)
{
        assert(shm_set);
        assert(!(port_id < 0) && port_id < IRMD_MAX_FLOWS);

        pthread_mutex_lock(shm_set->lock);

        if (shm_set->mtable[port_id] == -1) {
                pthread_mutex_unlock(shm_set->lock);
                return;
        }

        *(fqueue_ptr(shm_set, shm_set->mtable[port_id]) +
                     (shm_set->heads[shm_set->mtable[port_id]])++) = port_id;

        pthread_cond_signal(&shm_set->conds[shm_set->mtable[port_id]]);

        pthread_mutex_unlock(shm_set->lock);
}


int shm_flow_set_wait(const struct shm_flow_set * shm_set,
                      ssize_t                     idx,
                      int *                       fqueue,
                      const struct timespec *     timeout)
{
        int ret = 0;
        struct timespec abstime;

        assert(shm_set);
        assert(!(idx < 0) && idx < AP_MAX_FQUEUES);

#ifdef __APPLE__
        pthread_mutex_lock(shm_set->lock);
#else
        if (pthread_mutex_lock(shm_set->lock) == EOWNERDEAD) {
                LOG_DBG("Recovering dead mutex.");
                pthread_mutex_consistent(shm_set->lock);
        }
#endif
        if (timeout != NULL) {
                clock_gettime(PTHREAD_COND_CLOCK, &abstime);
                ts_add(&abstime, timeout, &abstime);
        }

        pthread_cleanup_push((void(*)(void *))pthread_mutex_unlock,
                             (void *) shm_set->lock);

        while (shm_set->heads[idx] == 0 && ret != -ETIMEDOUT) {
                if (timeout != NULL)
                        ret = pthread_cond_timedwait(shm_set->conds + idx,
                                                     shm_set->lock,
                                                     &abstime);
                else
                        ret = pthread_cond_wait(shm_set->conds + idx,
                                                shm_set->lock);
#ifndef __APPLE__
                if (ret == EOWNERDEAD) {
                        LOG_DBG("Recovering dead mutex.");
                        pthread_mutex_consistent(shm_set->lock);
                }
#endif
                if (ret == ETIMEDOUT) {
                        ret = -ETIMEDOUT;
                        break;
                }
        }

        if (ret != -ETIMEDOUT) {
                memcpy(fqueue,
                       fqueue_ptr(shm_set, idx),
                       shm_set->heads[idx] * sizeof(int));
                ret = shm_set->heads[idx];
                shm_set->heads[idx] = 0;
        }

        pthread_cleanup_pop(true);

        return ret;
}