summaryrefslogtreecommitdiff
path: root/src/ipcpd/normal/shm_pci.c
blob: 735bdee154fcb01c74e15658699363f221b138db (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
/*
 * Ouroboros - Copyright (C) 2016 - 2017
 *
 * Protocol Control Information in Shared Memory Map
 *
 *    Dimitri Staessens <dimitri.staessens@intec.ugent.be>
 *    Sander Vrijders   <sander.vrijders@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 version 2 as
 * published by the Free Software Foundation.
 *
 * 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.
 */

#define OUROBOROS_PREFIX "shm-pci"

#include <ouroboros/config.h>
#include <ouroboros/logs.h>
#include <ouroboros/errno.h>
#include <ouroboros/crc32.h>
#include <ouroboros/rib.h>

#include "shm_pci.h"
#include "dt_const.h"

#include <stdlib.h>
#include <string.h>

#define PDU_TYPE_SIZE 1
#define QOS_ID_SIZE 1
#define DEFAULT_TTL 60
#define TTL_SIZE 1
#define CHK_SIZE 4

#define BOOT_PATH "/" BOOT_NAME

struct {
        struct dt_const dtc;
        size_t head_size;
        size_t tail_size;
} pci_info;


static void ser_pci_head(uint8_t *    head,
                         struct pci * pci)
{
        int offset = 0;
        uint8_t ttl = DEFAULT_TTL;

        memcpy(head, &pci->pdu_type, PDU_TYPE_SIZE);
        offset += PDU_TYPE_SIZE;
        memcpy(head + offset, &pci->dst_addr, pci_info.dtc.addr_size);
        offset += pci_info.dtc.addr_size;
        memcpy(head + offset, &pci->src_addr, pci_info.dtc.addr_size);
        offset += pci_info.dtc.addr_size;
        memcpy(head + offset, &pci->dst_cep_id, pci_info.dtc.cep_id_size);
        offset += pci_info.dtc.cep_id_size;
        memcpy(head + offset, &pci->src_cep_id, pci_info.dtc.cep_id_size);
        offset += pci_info.dtc.cep_id_size;
        memcpy(head + offset, &pci->pdu_length, pci_info.dtc.pdu_length_size);
        offset += pci_info.dtc.pdu_length_size;
        memcpy(head + offset, &pci->seqno, pci_info.dtc.seqno_size);
        offset += pci_info.dtc.seqno_size;
        memcpy(head + offset, &pci->qos_id, QOS_ID_SIZE);
        offset += QOS_ID_SIZE;
        if (pci_info.dtc.has_ttl)
                memcpy(head + offset, &ttl, TTL_SIZE);
}

int shm_pci_init(void)
{
        /* read dt constants from the RIB */
        if (rib_read(BOOT_PATH "/dt/const/addr_size",
                     &pci_info.dtc.addr_size,
                     sizeof(pci_info.dtc.addr_size)) ||
            rib_read(BOOT_PATH "/dt/const/cep_id_size",
                      &pci_info.dtc.cep_id_size,
                      sizeof(pci_info.dtc.cep_id_size)) ||
            rib_read(BOOT_PATH "/dt/const/seqno_size",
                      &pci_info.dtc.seqno_size,
                      sizeof(pci_info.dtc.seqno_size)) ||
            rib_read(BOOT_PATH "/dt/const/has_ttl",
                      &pci_info.dtc.has_ttl,
                      sizeof(pci_info.dtc.has_ttl)) ||
            rib_read(BOOT_PATH "/dt/const/has_chk",
                      &pci_info.dtc.has_chk,
                      sizeof(pci_info.dtc.has_chk)) ||
            rib_read(BOOT_PATH "/dt/const/min_pdu_size",
                      &pci_info.dtc.min_pdu_size,
                      sizeof(pci_info.dtc.min_pdu_size)) ||
            rib_read(BOOT_PATH "/dt/const/max_pdu_size",
                      &pci_info.dtc.max_pdu_size,
                      sizeof(pci_info.dtc.max_pdu_size)))
                return -1;

        pci_info.head_size = PDU_TYPE_SIZE + pci_info.dtc.addr_size * 2 +
                pci_info.dtc.cep_id_size * 2 + pci_info.dtc.pdu_length_size +
                pci_info.dtc.seqno_size + QOS_ID_SIZE;

        if (pci_info.dtc.has_ttl)
                pci_info.head_size += TTL_SIZE;

        pci_info.tail_size = pci_info.dtc.has_chk ? CHK_SIZE : 0;

        return 0;
}

void shm_pci_fini(void) {
        return ;
}

int shm_pci_ser(struct shm_du_buff * sdb,
                struct pci * pci)
{
        uint8_t * head;
        uint8_t * tail;

        head = shm_du_buff_head_alloc(sdb, pci_info.head_size);
        if (head == NULL)
                return -1;

        ser_pci_head(head, pci);

        if (pci_info.dtc.has_chk) {
                tail = shm_du_buff_tail_alloc(sdb, pci_info.tail_size);
                if (tail == NULL) {
                        shm_du_buff_head_release(sdb, pci_info.head_size);
                        return -1;
                }

                crc32((uint32_t *) tail, head, tail - head);
        }

        return 0;
}

buffer_t * shm_pci_ser_buf(buffer_t *   buf,
                           struct pci * pci)
{
        buffer_t * buffer;

        if (buf == NULL || pci == NULL)
                return NULL;

        buffer = malloc(sizeof(*buffer));
        if (buffer == NULL)
                return NULL;

        buffer->len = buf->len + pci_info.head_size +
                pci_info.tail_size;

        buffer->data = malloc(buffer->len);
        if (buffer->data == NULL) {
                free(buffer);
                return NULL;
        }

        ser_pci_head(buffer->data, pci);
        memcpy(buffer->data + pci_info.head_size,
               buf->data, buf->len);

        free(buf->data);

        if (pci_info.dtc.has_chk)
                crc32((uint32_t *) (buffer->data +
                                    pci_info.head_size + buf->len),
                      buffer->data,
                      pci_info.head_size + buf->len);

        return buffer;
}

struct pci * shm_pci_des(struct shm_du_buff * sdb)
{
        uint8_t * head;
        struct pci * pci;
        int offset = 0;

        if (sdb == NULL)
                return NULL;

        head = shm_du_buff_head(sdb);

        pci = malloc(sizeof(*pci));
        if (pci == NULL)
                return NULL;

        memcpy(&pci->pdu_type, head, PDU_TYPE_SIZE);
        offset += PDU_TYPE_SIZE;
        memcpy(&pci->dst_addr, head + offset, pci_info.dtc.addr_size);
        offset += pci_info.dtc.addr_size;
        memcpy(&pci->src_addr, head + offset, pci_info.dtc.addr_size);
        offset += pci_info.dtc.addr_size;
        memcpy(&pci->dst_cep_id, head + offset, pci_info.dtc.cep_id_size);
        offset += pci_info.dtc.cep_id_size;
        memcpy(&pci->src_cep_id, head + offset, pci_info.dtc.cep_id_size);
        offset += pci_info.dtc.cep_id_size;
        memcpy(&pci->pdu_length, head + offset, pci_info.dtc.pdu_length_size);
        offset += pci_info.dtc.pdu_length_size;
        memcpy(&pci->seqno, head + offset, pci_info.dtc.seqno_size);
        offset += pci_info.dtc.seqno_size;
        memcpy(&pci->qos_id, head + offset, QOS_ID_SIZE);
        offset += QOS_ID_SIZE;
        pci->has_ttl = pci_info.dtc.has_ttl;
        if (pci_info.dtc.has_ttl)
                memcpy(&pci->ttl, head + offset, TTL_SIZE);

        return pci;
}

int shm_pci_shrink(struct shm_du_buff * sdb)
{
        if (sdb == NULL)
                return -1;

        if (shm_du_buff_head_release(sdb, pci_info.head_size)) {
                LOG_ERR("Failed to shrink head.");
                return -1;
        }

        if (shm_du_buff_tail_release(sdb, pci_info.tail_size)) {
                LOG_ERR("Failed to shrink tail.");
                return -1;
        }

        return 0;
}

int shm_pci_dec_ttl(struct shm_du_buff * sdb)
{
        uint8_t * head;
        uint8_t * tail;

        if (pci_info.dtc.has_ttl == false)
                return 0;

        head = shm_du_buff_head(sdb);
        if (head == NULL)
                return -1;

        head[pci_info.head_size - TTL_SIZE]--;

        if (pci_info.dtc.has_chk) {
                tail = shm_du_buff_tail(sdb);
                if (tail == NULL)
                        return -1;

                tail -= CHK_SIZE;

                crc32((uint32_t *) tail, head, tail - head);
        }

        return 0;
}