/src/suricata7/src/tm-queues.c
Line | Count | Source |
1 | | /* Copyright (C) 2007-2019 Open Information Security Foundation |
2 | | * |
3 | | * You can copy, redistribute or modify this Program under the terms of |
4 | | * the GNU General Public License version 2 as published by the Free |
5 | | * Software Foundation. |
6 | | * |
7 | | * This program is distributed in the hope that it will be useful, |
8 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 | | * GNU General Public License for more details. |
11 | | * |
12 | | * You should have received a copy of the GNU General Public License |
13 | | * version 2 along with this program; if not, write to the Free Software |
14 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
15 | | * 02110-1301, USA. |
16 | | */ |
17 | | |
18 | | /** |
19 | | * \file |
20 | | * |
21 | | * \author Victor Julien <victor@inliniac.net> |
22 | | * |
23 | | * Thread module management functions |
24 | | */ |
25 | | |
26 | | #include "suricata.h" |
27 | | #include "threads.h" |
28 | | #include "tm-queues.h" |
29 | | #include "util-debug.h" |
30 | | |
31 | | static TAILQ_HEAD(TmqList_, Tmq_) tmq_list = TAILQ_HEAD_INITIALIZER(tmq_list); |
32 | | |
33 | | static uint16_t tmq_id = 0; |
34 | | |
35 | | Tmq *TmqCreateQueue(const char *name) |
36 | 0 | { |
37 | 0 | Tmq *q = SCCalloc(1, sizeof(*q)); |
38 | 0 | if (q == NULL) |
39 | 0 | FatalError("SCCalloc failed"); |
40 | | |
41 | 0 | q->name = SCStrdup(name); |
42 | 0 | if (q->name == NULL) |
43 | 0 | FatalError("SCStrdup failed"); |
44 | | |
45 | 0 | q->id = tmq_id++; |
46 | 0 | q->is_packet_pool = (strcmp(q->name, "packetpool") == 0); |
47 | 0 | if (!q->is_packet_pool) { |
48 | 0 | q->pq = PacketQueueAlloc(); |
49 | 0 | if (q->pq == NULL) |
50 | 0 | FatalError("PacketQueueAlloc failed"); |
51 | 0 | } |
52 | | |
53 | 0 | TAILQ_INSERT_HEAD(&tmq_list, q, next); |
54 | |
|
55 | 0 | SCLogDebug("created queue \'%s\', %p", name, q); |
56 | 0 | return q; |
57 | 0 | } |
58 | | |
59 | | Tmq *TmqGetQueueByName(const char *name) |
60 | 0 | { |
61 | 0 | Tmq *tmq = NULL; |
62 | 0 | TAILQ_FOREACH(tmq, &tmq_list, next) { |
63 | 0 | if (strcmp(tmq->name, name) == 0) |
64 | 0 | return tmq; |
65 | 0 | } |
66 | 0 | return NULL; |
67 | 0 | } |
68 | | |
69 | | void TmqDebugList(void) |
70 | 0 | { |
71 | 0 | Tmq *tmq = NULL; |
72 | 0 | TAILQ_FOREACH(tmq, &tmq_list, next) { |
73 | | /* get a lock accessing the len */ |
74 | 0 | SCMutexLock(&tmq->pq->mutex_q); |
75 | 0 | printf("TmqDebugList: id %" PRIu32 ", name \'%s\', len %" PRIu32 "\n", tmq->id, tmq->name, tmq->pq->len); |
76 | 0 | SCMutexUnlock(&tmq->pq->mutex_q); |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | void TmqResetQueues(void) |
81 | 0 | { |
82 | 0 | Tmq *tmq; |
83 | |
|
84 | 0 | while ((tmq = TAILQ_FIRST(&tmq_list))) { |
85 | 0 | TAILQ_REMOVE(&tmq_list, tmq, next); |
86 | 0 | if (tmq->name) { |
87 | 0 | SCFree(tmq->name); |
88 | 0 | } |
89 | 0 | if (tmq->pq) { |
90 | 0 | PacketQueueFree(tmq->pq); |
91 | 0 | } |
92 | 0 | SCFree(tmq); |
93 | 0 | } |
94 | 0 | tmq_id = 0; |
95 | 0 | } |
96 | | |
97 | | /** |
98 | | * \brief Checks if all the queues allocated so far have at least one reader |
99 | | * and writer. |
100 | | */ |
101 | | void TmValidateQueueState(void) |
102 | 0 | { |
103 | 0 | bool err = false; |
104 | |
|
105 | 0 | Tmq *tmq = NULL; |
106 | 0 | TAILQ_FOREACH(tmq, &tmq_list, next) { |
107 | 0 | SCMutexLock(&tmq->pq->mutex_q); |
108 | 0 | if (tmq->reader_cnt == 0) { |
109 | 0 | SCLogError("queue \"%s\" doesn't have a reader (id %d max %u)", tmq->name, tmq->id, |
110 | 0 | tmq_id); |
111 | 0 | err = true; |
112 | 0 | } else if (tmq->writer_cnt == 0) { |
113 | 0 | SCLogError("queue \"%s\" doesn't have a writer (id %d, max %u)", tmq->name, tmq->id, |
114 | 0 | tmq_id); |
115 | 0 | err = true; |
116 | 0 | } |
117 | 0 | SCMutexUnlock(&tmq->pq->mutex_q); |
118 | |
|
119 | 0 | if (err == true) |
120 | 0 | goto error; |
121 | 0 | } |
122 | | |
123 | 0 | return; |
124 | | |
125 | 0 | error: |
126 | | FatalError("fatal error during threading setup"); |
127 | 0 | } |