summaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/transport/transport_rta/core/rta_Framework_Commands.c
blob: a02efefada19627c09fd0a36bae2241a8bc02128 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * 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 <config.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#include <errno.h>
#include <LongBow/runtime.h>

#include <parc/algol/parc_Memory.h>

#include <ccnx/transport/transport_rta/core/rta_Framework_private.h>

#include <ccnx/transport/transport_rta/core/rta_Connection.h>
#include <ccnx/transport/transport_rta/commands/rta_Command.h>

#include <parc/algol/parc_Event.h>

#ifdef DEBUG_OUTPUT
#undef DEBUG_OUTPUT
#endif

#define DEBUG_OUTPUT 0

extern FILE *GlobalStatisticsFile;

static bool _rtaFramework_ExecuteCreateStack(RtaFramework *framework, const RtaCommandCreateProtocolStack *createStack);
static bool _rtaFramework_ExecuteDestroyStack(RtaFramework *framework, const RtaCommandDestroyProtocolStack *destroyStack);
static bool _rtaFramework_ExecuteOpenConnection(RtaFramework *framework, const RtaCommandOpenConnection *openConnection);
static bool _rtaFramework_ExecuteCloseConnection(RtaFramework *framework, const RtaCommandCloseConnection *closeConnection);
static bool _rtaFramework_ExecuteTransmitStatistics(RtaFramework *framework, const RtaCommandTransmitStatistics *transmitStats);
static bool _rtaFramework_ExecuteShutdownFramework(RtaFramework *framework);

static void rtaFramework_DrainApiDescriptor(int fd);

void
rtaFramework_CommandCallback(int fd, PARCEventType what, void *user_framework)
{
    RtaFramework *framework = (RtaFramework *) user_framework;

    // flag the notifier that we are starting a batch of reads
    parcNotifier_PauseEvents(framework->commandNotifier);

    RtaCommand *command = NULL;
    while ((command = rtaCommand_Read(framework->commandRingBuffer)) != NULL) {
        // The shutdown command can broadcast a change of state before the function
        // returns, so we need to free the RtaCommand before executing the shutdown.
        // Therefore, we include the rtaCommand_Destroy() as part of the switch.

        if (rtaCommand_IsOpenConnection(command)) {
            _rtaFramework_ExecuteOpenConnection(framework, rtaCommand_GetOpenConnection(command));
            rtaCommand_Release(&command);
        } else if (rtaCommand_IsCloseConnection(command)) {
            _rtaFramework_ExecuteCloseConnection(framework, rtaCommand_GetCloseConnection(command));
            rtaCommand_Release(&command);
        } else if (rtaCommand_IsCreateProtocolStack(command)) {
            _rtaFramework_ExecuteCreateStack(framework, rtaCommand_GetCreateProtocolStack(command));
            rtaCommand_Release(&command);
        } else if (rtaCommand_IsDestroyProtocolStack(command)) {
            _rtaFramework_ExecuteDestroyStack(framework, rtaCommand_GetDestroyProtocolStack(command));
            rtaCommand_Release(&command);
        } else if (rtaCommand_IsTransmitStatistics(command)) {
            _rtaFramework_ExecuteTransmitStatistics(framework, rtaCommand_GetTransmitStatistics(command));
            rtaCommand_Release(&command);
        } else if (rtaCommand_IsShutdownFramework(command)) {
            // release the command before executing shutdown
            rtaCommand_Release(&command);
            _rtaFramework_ExecuteShutdownFramework(framework);
        } else {
            rtaCommand_Display(command, 3);
            rtaCommand_Release(&command);
            trapUnexpectedState("Got unknown command type");
        }
    }

    // resume notifications
    parcNotifier_StartEvents(framework->commandNotifier);
}

// =========================================
// Internal command processing

/**
 * Create a protocol holder and insert it in the framework's
 * protocols_head list.
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static FrameworkProtocolHolder *
rtaFramework_CreateProtocolHolder(RtaFramework *framework, PARCJSON *params, uint64_t kv_hash, int stack_id)
{
    // request for a new protocol stack, create it
    FrameworkProtocolHolder *holder = parcMemory_AllocateAndClear(sizeof(FrameworkProtocolHolder));
    assertNotNull(holder, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(FrameworkProtocolHolder));

    TAILQ_INSERT_TAIL(&framework->protocols_head, holder, list);

    holder->kv_hash = kv_hash;
    holder->stack_id = stack_id;

    if (DEBUG_OUTPUT) {
        printf("%s created protocol holder %p hash %" PRIu64 "\n",
               __func__,
               (void *) holder,
               kv_hash);
    }

    return holder;
}

/**
 * Lookup the existing protocol holder for stackid.
 * Returns NULL if not found.
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static FrameworkProtocolHolder *
rtaFramework_GetProtocolStackByStackId(RtaFramework *framework, int stack_id)
{
    FrameworkProtocolHolder *holder;
    TAILQ_FOREACH(holder, &framework->protocols_head, list)
    {
        if (holder->stack_id == stack_id) {
            return holder;
        }
    }
    return NULL;
}

static bool
_rtaFramework_ExecuteCreateStack(RtaFramework *framework, const RtaCommandCreateProtocolStack *createStack)
{
    // if we're in INIT mode, we need to bump
    // wait for notificaiton from event thread
    if (framework->status == FRAMEWORK_INIT) {
        rta_Framework_LockStatus(framework);
        if (framework->status == FRAMEWORK_INIT) {
            framework->status = FRAMEWORK_SETUP;
        }
        rta_Framework_BroadcastStatus(framework);
        rta_Framework_UnlockStatus(framework);
    }

    FrameworkProtocolHolder *holder =
        rtaFramework_GetProtocolStackByStackId(framework, rtaCommandCreateProtocolStack_GetStackId(createStack));
    assertNull(holder, "Found a holder with stack_id %d, but we're asked to create it!",
               rtaCommandCreateProtocolStack_GetStackId(createStack));

    uint64_t kv_hash = ccnxStackConfig_HashCode(rtaCommandCreateProtocolStack_GetStackConfig(createStack));

    // this creates it and inserts in framework->protocols_head
    holder = rtaFramework_CreateProtocolHolder(framework, NULL, kv_hash, rtaCommandCreateProtocolStack_GetStackId(createStack));

    holder->stack =
        rtaProtocolStack_Create(framework, rtaCommandCreateProtocolStack_GetConfig(createStack), rtaCommandCreateProtocolStack_GetStackId(createStack));
    rtaProtocolStack_Configure(holder->stack);

    if (DEBUG_OUTPUT) {
        printf("%s created protocol %p kv_hash %016" PRIX64 " stack_id %d\n",
               __func__, (void *) holder->stack, kv_hash, rtaCommandCreateProtocolStack_GetStackId(createStack));
    }
    return 0;
}

static bool
_rtaFramework_ExecuteOpenConnection(RtaFramework *framework, const RtaCommandOpenConnection *openConnection)
{
    int res;
    FrameworkProtocolHolder *holder;
    RtaConnection *rtaConnection;

    if (DEBUG_OUTPUT) {
        printf("%9" PRIu64 " %s framework %p\n",
               rtaFramework_GetTicks(framework), __func__, (void *) framework);
    }

    holder = rtaFramework_GetProtocolStackByStackId(framework, rtaCommandOpenConnection_GetStackId(openConnection));
    assertNotNull(holder, "Could not find stack_id %d", rtaCommandOpenConnection_GetStackId(openConnection));

    rtaConnection = rtaConnectionTable_GetByApiFd(framework->connectionTable, rtaCommandOpenConnection_GetApiNotifierFd(openConnection));
    assertNull(rtaConnection, "Found api_fd %d, but it should not exist!", rtaCommandOpenConnection_GetApiNotifierFd(openConnection));

    rtaConnection = rtaConnection_Create(holder->stack, openConnection);
    res = rtaConnectionTable_AddConnection(framework->connectionTable, rtaConnection);
    assertTrue(res == 0, "Got error from rtaConnectionTable_AddConnection: %d", res);

    res = rtaProtocolStack_Open(holder->stack, rtaConnection);
    assertTrue(res == 0, "Got error from rtaProtocolStack_Open: %d", res);

    rtaConnection_SetState(rtaConnection, CONN_OPEN);

    if (DEBUG_OUTPUT) {
        printf("%9" PRIu64 " %s created connection %p stack_id %d api_fd %d transport_fd %d\n",
               rtaFramework_GetTicks(framework),
               __func__,
               (void *) rtaConnection,
               rtaCommandOpenConnection_GetStackId(openConnection),
               rtaCommandOpenConnection_GetApiNotifierFd(openConnection),
               rtaCommandOpenConnection_GetTransportNotifierFd(openConnection));
    }

    return true;
}


/**
 * Mark a connection as closed.  If there are no pending
 * packets in queues, destroy it too.
 * It's non-static because we call from rta_Framework.c
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
int
rtaFramework_CloseConnection(RtaFramework *framework, RtaConnection *connection)
{
    if (DEBUG_OUTPUT) {
        printf("%9" PRIu64 " %s connection %p api_fd %d\n",
               rtaFramework_GetTicks(framework),
               __func__, (void *) connection, rtaConnection_GetApiFd(connection));
    }

    assertFalse(rtaConnection_GetState(connection) == CONN_CLOSED,
                "connection api_fd %d is already closed", rtaConnection_GetApiFd(connection));

    rtaConnection_SetState(connection, CONN_CLOSED);
    rtaProtocolStack_Close(rtaConnection_GetStack(connection), connection);

    rtaFramework_DrainApiDescriptor(rtaConnection_GetApiFd(connection));


    // Remove it from the connection table, which will free our reference to it.

    rtaConnectionTable_Remove(framework->connectionTable, connection);

    // Done.  The rtaConnection will be removed when the last queued
    // messages for it are gone.  We keep the connection holder, so if we do
    // a Destroy we'll know about it.  RtaConnection will call
    // rtaFramework_RemoveConnection(...) when RtaConnection_Destroy() refcount
    // is zero and it's going to fully remove the connection.

    return 0;
}


static bool
_rtaFramework_ExecuteCloseConnection(RtaFramework *framework, const RtaCommandCloseConnection *closeConnection)
{
    RtaConnection *connection = rtaConnectionTable_GetByApiFd(framework->connectionTable, rtaCommandCloseConnection_GetApiNotifierFd(closeConnection));
    assertNotNull(connection, "Could not find api_fd %d", rtaCommandCloseConnection_GetApiNotifierFd(closeConnection));

    return (rtaFramework_CloseConnection(framework, connection) == 0);
}

/**
 * When the transport is closing the descriptor
 * to the API, it should call this to drain any pending but unretrieved
 * messages out of the API's side of the socket
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static void
rtaFramework_DrainApiDescriptor(int fd)
{
    unsigned count = 0;

    if (DEBUG_OUTPUT) {
        printf("%s fd %d\n", __func__, fd);
    }

    // Set non-blocking flag
    int flags = fcntl(fd, F_GETFL, NULL);
    assertTrue(flags != -1, "fcntl failed to obtain file descriptor flags (%d)\n", errno);
    int failure = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    assertFalse(failure, "fcntl failed to set file descriptor flags (%d)\n", errno);

    // Now drain the user side of stuff they have not read
    CCNxMetaMessage *msg;
    while (read(fd, &msg, sizeof(CCNxMetaMessage *)) == sizeof(CCNxMetaMessage *)) {
        count++;
        ccnxMetaMessage_Release(&msg);
    }

    if (DEBUG_OUTPUT) {
        printf("%s destroyed %u messages\n", __func__, count);
    }
}

/**
 * This is a deferred callback from the RtaConnection when its last TransportMessage
 * has been purged from the queues.
 *
 * Don't call anything inside here that ends up back in the RtaConnection.
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
void
rtaFramework_RemoveConnection(RtaFramework *framework, RtaConnection *rtaConnection)
{
    rtaFramework_DrainApiDescriptor(rtaConnection_GetApiFd(rtaConnection));

    if (DEBUG_OUTPUT) {
        printf("%9" PRIu64 " %s connection %p closing api_fd %d\n",
               rtaFramework_GetTicks(framework),
               __func__, (void *) rtaConnection, rtaConnection_GetApiFd(rtaConnection));
    }

    close(rtaConnection_GetApiFd(rtaConnection));
    close(rtaConnection_GetTransportFd(rtaConnection));
}

void
rtaFramework_DestroyProtocolHolder(RtaFramework *framework, FrameworkProtocolHolder *holder)
{
    if (DEBUG_OUTPUT) {
        printf("%9" PRIu64 " %s proto_holder %p\n",
               rtaFramework_GetTicks(framework),
               __func__, (void *) holder);
    }

    // remove any and all connections associated with this protocol stack.
    // If the connections still have packets floating around in queues, the connection
    // will stay around until they all get flushed then will destroy on
    // the last packet destruction
    rtaConnectionTable_RemoveByStack(framework->connectionTable, holder->stack_id);

    rtaProtocolStack_Destroy(&holder->stack);

    TAILQ_REMOVE(&framework->protocols_head, holder, list);

    parcMemory_Deallocate((void **) &holder);
}


static bool
_rtaFramework_ExecuteDestroyStack(RtaFramework *framework, const RtaCommandDestroyProtocolStack *destroyStack)
{
    FrameworkProtocolHolder *holder = rtaFramework_GetProtocolStackByStackId(framework, rtaCommandDestroyProtocolStack_GetStackId(destroyStack));
    assertNotNull(holder, "Could not find stack_id %d", rtaCommandDestroyProtocolStack_GetStackId(destroyStack));

    rtaConnectionTable_RemoveByStack(framework->connectionTable, rtaCommandDestroyProtocolStack_GetStackId(destroyStack));

    if (DEBUG_OUTPUT) {
        printf("%9" PRIu64 " %s proto_holder %p\n",
               rtaFramework_GetTicks(framework),
               __func__, (void *) holder);
    }

    rtaFramework_DestroyProtocolHolder(framework, holder);

    return true;
}

/**
 * This will update the shared framework->status, so needs a lock around
 * the work it does.
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static bool
_rtaFramework_ExecuteShutdownFramework(RtaFramework *framework)
{
    FrameworkProtocolHolder *holder;

    // %%% LOCK
    rta_Framework_LockStatus(framework);
    if (framework->status != FRAMEWORK_RUNNING) {
        RtaFrameworkStatus status = framework->status;
        rta_Framework_UnlockStatus(framework);
        // %%% UNLOCK
        assertTrue(0, "Invalid state, expected FRAMEWORK_RUNNING or later, got %d", status);
        return -1;
    }

    holder = TAILQ_FIRST(&framework->protocols_head);
    while (holder != NULL) {
        FrameworkProtocolHolder *temp = TAILQ_NEXT(holder, list);
        if (DEBUG_OUTPUT) {
            printf("%9" PRIu64 " %s stack_id %d\n",
                   framework->clock_ticks, __func__, holder->stack_id);
        }

        rtaFramework_DestroyProtocolHolder(framework, holder);
        holder = temp;
    }

    parcEventScheduler_Stop(framework->base, &(struct timeval) { .tv_sec = 0, .tv_usec = 1000 });
    framework->status = FRAMEWORK_STOPPING;
    rta_Framework_BroadcastStatus(framework);
    rta_Framework_UnlockStatus(framework);
    // %%% UNLOCK

    return 0;
}

// Goes into rta_Framework_Commands.c
static bool
_rtaFramework_ExecuteTransmitStatistics(RtaFramework *framework, const RtaCommandTransmitStatistics *transmitStats)
{
    if (GlobalStatisticsFile != NULL) {
        fclose(GlobalStatisticsFile);
    }

    GlobalStatisticsFile = fopen(rtaCommandTransmitStatistics_GetFilename(transmitStats), "a");
    assertNotNull(GlobalStatisticsFile, "Failed to open %s", rtaCommandTransmitStatistics_GetFilename(transmitStats));

    if (GlobalStatisticsFile != NULL) {
        struct timeval period = rtaCommandTransmitStatistics_GetPeriod(transmitStats);
        parcEventTimer_Start(framework->transmit_statistics_event, &period);
    } else {
        fprintf(stderr, "Will not report statistics: Failed to open %s for output.", rtaCommandTransmitStatistics_GetFilename(transmitStats));
    }

    return 0;
}