athena.c 18.6 KB
Newer Older
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
/*
 * Copyright (c) 2015, Xerox Corporation (Xerox)and Palo Alto Research Center (PARC)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Patent rights are not granted under this agreement. Patent rights are
 *       available under FRAND terms.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL XEROX or PARC BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * @author Kevin Fox, Palo Alto Research Center (Xerox PARC)
 * @copyright 2015, Xerox Corporation (Xerox)and Palo Alto Research Center (PARC).  All rights reserved.
 */
/*
 * Athena Example Runtime implementation
 */

#include <config.h>
#include <pthread.h>
#include <unistd.h>

#include <ccnx/forwarder/athena/athena.h>
#include <ccnx/forwarder/athena/athena_Control.h>
#include <ccnx/forwarder/athena/athena_InterestControl.h>
#include <ccnx/forwarder/athena/athena_LRUContentStore.h>

#include <ccnx/common/ccnx_Interest.h>
#include <ccnx/common/ccnx_InterestReturn.h>
#include <ccnx/common/ccnx_ContentObject.h>
47
#include <ccnx/common/ccnx_Manifest.h>
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

#include <ccnx/common/validation/ccnxValidation_CRC32C.h>
#include <ccnx/common/codec/ccnxCodec_TlvPacket.h>

static PARCLog *
_athena_logger_create(void)
{
    PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO));
    PARCOutputStream *output = parcFileOutputStream_AsOutputStream(fileOutput);
    parcFileOutputStream_Release(&fileOutput);

    PARCLogReporter *reporter = parcLogReporterFile_Create(output);
    parcOutputStream_Release(&output);

    PARCLog *log = parcLog_Create("localhost", "athena", NULL, reporter);
    parcLogReporter_Release(&reporter);

    parcLog_SetLevel(log, PARCLogLevel_Info);
    return log;
}

static void
_removeLink(void *context, PARCBitVector *linkVector)
{
    Athena *athena = (Athena *) context;

    const char *linkVectorString = parcBitVector_ToString(linkVector);

    // cleanup specified links from the FIB and PIT, these calls are currently presumed synchronous
    bool result = athenaFIB_RemoveLink(athena->athenaFIB, linkVector);
    assertTrue(result, "Failed to remove link from FIB %s", linkVectorString);

    result = athenaPIT_RemoveLink(athena->athenaPIT, linkVector);
    assertTrue(result, "Failed to remove link from PIT %s", linkVectorString);

    parcMemory_Deallocate(&linkVectorString);
}

static void
_athenaDestroy(Athena **athena)
{
    ccnxName_Release(&((*athena)->athenaName));
    athenaTransportLinkAdapter_Destroy(&((*athena)->athenaTransportLinkAdapter));
    athenaContentStore_Release(&((*athena)->athenaContentStore));
    athenaPIT_Release(&((*athena)->athenaPIT));
    athenaFIB_Release(&((*athena)->athenaFIB));
    parcLog_Release(&((*athena)->log));
}

parcObject_ExtendPARCObject(Athena, _athenaDestroy, NULL, NULL, NULL, NULL, NULL, NULL);

Athena *
athena_Create(size_t contentStoreSizeInMB)
{
    Athena *athena = parcObject_CreateAndClearInstance(Athena);

104
    athena->athenaName = ccnxName_CreateFromCString(CCNxNameAthena_Forwarder);
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
    assertNotNull(athena->athenaName, "Failed to create forwarder name (%s)", CCNxNameAthena_Forwarder);

    athena->athenaFIB = athenaFIB_Create();
    assertNotNull(athena->athenaFIB, "Failed to create FIB");

    athena->athenaPIT = athenaPIT_Create();
    assertNotNull(athena->athenaPIT, "Failed to create PIT");

    AthenaLRUContentStoreConfig storeConfig;
    storeConfig.capacityInMB = contentStoreSizeInMB;

    athena->athenaContentStore = athenaContentStore_Create(&AthenaContentStore_LRUImplementation, &storeConfig);
    assertNotNull(athena->athenaContentStore, "Failed to create Content Store");

    athena->athenaTransportLinkAdapter = athenaTransportLinkAdapter_Create(_removeLink, athena);
    assertNotNull(athena->athenaTransportLinkAdapter, "Failed to create Transport Link Adapter");

    athena->log = _athena_logger_create();
    athena->athenaState = Athena_Running;

    return athena;
}

parcObject_ImplementAcquire(athena, Athena);

parcObject_ImplementRelease(athena, Athena);

static void
_processInterestControl(Athena *athena, CCNxInterest *interest, PARCBitVector *ingressVector)
{
    //
    // Management messages
    //
    athenaInterestControl(athena, interest, ingressVector);
}

static void
_processControl(Athena *athena, CCNxControl *control, PARCBitVector *ingressVector)
{
    //
    // Management messages
    //
    athenaControl(athena, control, ingressVector);
}

static void
_processInterest(Athena *athena, CCNxInterest *interest, PARCBitVector *ingressVector)
{
    uint8_t hoplimit;

    //
    // *   (0) Hoplimit check, exclusively on interest messages
    //
    int linkId = parcBitVector_NextBitSet(ingressVector, 0);
    if (athenaTransportLinkAdapter_IsNotLocal(athena->athenaTransportLinkAdapter, linkId)) {
        hoplimit = ccnxInterest_GetHopLimit(interest);
        if (hoplimit == 0) {
            // We should never receive a message with a hoplimit of 0 from a non-local source.
            parcLog_Error(athena->log,
                          "Received a message with a hoplimit of zero from a non-local source (%s).",
                          athenaTransportLinkAdapter_LinkIdToName(athena->athenaTransportLinkAdapter, linkId));
            return;
        }
        ccnxInterest_SetHopLimit(interest, hoplimit - 1);
    }

    //
    // *   (1) if the interest is in the ContentStore, reply and return,
    //     assuming that other PIT entries were satisified when the content arrived.
    //
    CCNxMetaMessage *content = athenaContentStore_GetMatch(athena->athenaContentStore, interest);
    if (content) {
        const char *ingressVectorString = parcBitVector_ToString(ingressVector);
        parcLog_Debug(athena->log, "Forwarding content from store to %s", ingressVectorString);
        parcMemory_Deallocate(&ingressVectorString);
        PARCBitVector *result = athenaTransportLinkAdapter_Send(athena->athenaTransportLinkAdapter, content, ingressVector);
        if (result) { // failed channels - client will resend interest unless we wish to optimize things here
            parcBitVector_Release(&result);
        }
        return;
    }

    //
    // *   (2) add it to the PIT, if it was aggregated or there was an error we're done, otherwise we
    //         forward the interest.  The expectedReturnVector is populated with information we get from
    //         the FIB and used to verify content objects ingress ports when they arrive.
    //
    PARCBitVector *expectedReturnVector;
    AthenaPITResolution result;
    if ((result = athenaPIT_AddInterest(athena->athenaPIT, interest, ingressVector, &expectedReturnVector)) != AthenaPITResolution_Forward) {
        if (result == AthenaPITResolution_Error) {
            parcLog_Error(athena->log, "PIT resolution error");
        }
        return;
    }

    // Divert interests destined to the forwarder, we assume these are control messages
    CCNxName *ccnxName = ccnxInterest_GetName(interest);
    if (ccnxName_StartsWith(ccnxName, athena->athenaName) == true) {
        _processInterestControl(athena, interest, ingressVector);
        return;
    }

    //
    // *   (3) if it's in the FIB, forward, then update the PIT expectedReturnVector so we can verify
    //         when the returned object arrives that it came from an interface it was expected from.
    //         Interest messages with a hoplimit of 0 will never be sent out by the link adapter to a
    //         non-local interface so we need not check that here.
    //
    ccnxName = ccnxInterest_GetName(interest);
Kev's avatar
Kev committed
215
    PARCBitVector *egressVector = athenaFIB_Lookup(athena->athenaFIB, ccnxName, ingressVector);
216 217

    if (egressVector != NULL) {
Kev's avatar
Kev committed
218
        // If no links are in the egress vector the FIB returned, return a no route interest message
219
        if (parcBitVector_NumberOfBitsSet(egressVector) == 0) {
220 221 222 223 224 225 226 227 228 229 230 231 232
            if (ccnxWireFormatMessage_ConvertInterestToInterestReturn(interest,
                                                                      CCNxInterestReturn_ReturnCode_NoRoute)) {

                // NOTE: The Interest has been modified in-place. It is now an InterestReturn.
                parcLog_Debug(athena->log, "Returning Interest as InterestReturn (code: NoRoute)");
                PARCBitVector *result = athenaTransportLinkAdapter_Send(athena->athenaTransportLinkAdapter, interest,
                                                                        ingressVector);
                parcBitVector_Release(&result);
            } else {
                const char *name = ccnxName_ToString(ccnxName);
                parcLog_Error(athena->log, "Unable to return Interest (%s) as InterestReturn (code: NoRoute).", name);
                parcMemory_Deallocate(&name);
            }
233 234 235 236 237 238 239 240
        } else {
            parcBitVector_SetVector(expectedReturnVector, egressVector);
            PARCBitVector *result = athenaTransportLinkAdapter_Send(athena->athenaTransportLinkAdapter, interest, egressVector);
            if (result) { // remove failed channels - client will resend interest unless we wish to optimize here
                parcBitVector_ClearVector(expectedReturnVector, result);
                parcBitVector_Release(&result);
            }
        }
Kev's avatar
Kev committed
241
        parcBitVector_Release(&egressVector);
242 243
    } else {
        // No FIB entry found, return a NoRoute interest return and remove the entry from the PIT.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

        if (ccnxWireFormatMessage_ConvertInterestToInterestReturn(interest,
                                                                  CCNxInterestReturn_ReturnCode_NoRoute)) {

            // NOTE: The Interest has been modified in-place. It is now an InterestReturn.
            parcLog_Debug(athena->log, "Returning Interest as InterestReturn (code: NoRoute)");
            PARCBitVector *result = athenaTransportLinkAdapter_Send(athena->athenaTransportLinkAdapter, interest,
                                                                    ingressVector);
            parcBitVector_Release(&result);
        } else {
            const char *name = ccnxName_ToString(ccnxName);
            parcLog_Error(athena->log, "Unable to return Interest (%s) as InterestReturn (code: NoRoute).", name);
            parcMemory_Deallocate(&name);
        }

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        const char *name = ccnxName_ToString(ccnxName);
        if (athenaPIT_RemoveInterest(athena->athenaPIT, interest, ingressVector) != true) {
            parcLog_Error(athena->log, "Unable to remove interest (%s) from the PIT.", name);
        }
        parcLog_Debug(athena->log, "Name (%s) not found in FIB and no default route. Message dropped.", name);
        parcMemory_Deallocate(&name);
    }
}

static void
_processInterestReturn(Athena *athena, CCNxInterestReturn *interestReturn, PARCBitVector *ingressVector)
{
    // We can ignore interest return messages and allow the PIT entry to timeout, or
    //
    // Verify the return came from the next-hop where the interest was originally sent to
    // if not, ignore
    // otherwise, may try another forwarding path or clear the PIT state and forward the interest return on the reverse path
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static PARCBuffer *
_createMessageHash(const CCNxMetaMessage *metaMessage)
{
    // We need to interact with the content message as a WireFormatMessage to get to
    // the content hash API.
    CCNxWireFormatMessage *wireFormatMessage = (CCNxWireFormatMessage *) metaMessage;

    PARCCryptoHash *hash = ccnxWireFormatMessage_CreateContentObjectHash(wireFormatMessage);
    if (hash != NULL) {
        PARCBuffer *buffer = parcBuffer_Acquire(parcCryptoHash_GetDigest(hash));
        parcCryptoHash_Release(&hash);
        return buffer;
    } else {
        return NULL;
    }
}

295 296 297 298 299 300
static void
_processContentObject(Athena *athena, CCNxContentObject *contentObject, PARCBitVector *ingressVector)
{
    //
    // *   (1) If it does not match anything in the PIT, drop it
    //
301
    const CCNxName *name = ccnxContentObject_GetName(contentObject);
302 303 304 305
    PARCBuffer *keyId = ccnxContentObject_GetKeyId(contentObject);
    PARCBuffer *digest = _createMessageHash(contentObject);

    PARCBitVector *egressVector = athenaPIT_Match(athena->athenaPIT, name, keyId, digest, ingressVector);
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    if (egressVector) {
        if (parcBitVector_NumberOfBitsSet(egressVector) > 0) {
            //
            // *   (2) Add to the Content Store
            //
            athenaContentStore_PutContentObject(athena->athenaContentStore, contentObject);

            //
            // *   (3) Reverse path forward it via PIT entries
            //
            const char *egressVectorString = parcBitVector_ToString(egressVector);
            parcLog_Debug(athena->log, "Content Object forwarded to %s.", egressVectorString);
            parcMemory_Deallocate(&egressVectorString);
            PARCBitVector *result = athenaTransportLinkAdapter_Send(athena->athenaTransportLinkAdapter, contentObject, egressVector);
            if (result) {
                // if there are failed channels, client will resend interest unless we wish to retry here
                parcBitVector_Release(&result);
            }
        }
        parcBitVector_Release(&egressVector);
    }
}

329 330 331 332 333 334
static void
_processManifest(Athena *athena, CCNxManifest *manifest, PARCBitVector *ingressVector)
{
    //
    // *   (1) If it does not match anything in the PIT, drop it
    //
335
    const CCNxName *name = ccnxManifest_GetName(manifest);
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
    PARCBuffer *digest = _createMessageHash(manifest);

    PARCBitVector *egressVector = athenaPIT_Match(athena->athenaPIT, name, NULL, digest, ingressVector);
    if (egressVector) {
        if (parcBitVector_NumberOfBitsSet(egressVector) > 0) {
            //
            // *   (2) Add to the Content Store
            //
            athenaContentStore_PutContentObject(athena->athenaContentStore, manifest);
            // _athenaPIT_RemoveInterestFromMap

            //
            // *   (3) Reverse path forward it via PIT entries
            //
            const char *egressVectorString = parcBitVector_ToString(egressVector);
            parcLog_Debug(athena->log, "Manifest forwarded to %s.", egressVectorString);
            parcMemory_Deallocate(&egressVectorString);
            PARCBitVector *result = athenaTransportLinkAdapter_Send(athena->athenaTransportLinkAdapter, manifest, egressVector);
            if (result) {
                // if there are failed channels, client will resend interest unless we wish to retry here
                parcBitVector_Release(&result);
            }
        }
        parcBitVector_Release(&egressVector);
    }
}

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
void
athena_ProcessMessage(Athena *athena, CCNxMetaMessage *ccnxMessage, PARCBitVector *ingressVector)
{
    if (ccnxMetaMessage_IsInterest(ccnxMessage)) {
        const char *name = ccnxName_ToString(ccnxInterest_GetName(ccnxMessage));
        parcLog_Debug(athena->log, "Processing Interest Message: %s", name);
        parcMemory_Deallocate(&name);

        CCNxInterest *interest = ccnxMetaMessage_GetInterest(ccnxMessage);
        _processInterest(athena, interest, ingressVector);
        athena->stats.numProcessedInterests++;
    } else if (ccnxMetaMessage_IsContentObject(ccnxMessage)) {
        const char *name = ccnxName_ToString(ccnxContentObject_GetName(ccnxMessage));
        parcLog_Debug(athena->log, "Processing Content Object Message: %s", name);
        parcMemory_Deallocate(&name);

        CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(ccnxMessage);
        _processContentObject(athena, contentObject, ingressVector);
        athena->stats.numProcessedContentObjects++;
    } else if (ccnxMetaMessage_IsControl(ccnxMessage)) {
        parcLog_Debug(athena->log, "Processing Control Message");

        CCNxControl *control = ccnxMetaMessage_GetControl(ccnxMessage);
        _processControl(athena, control, ingressVector);
        athena->stats.numProcessedControlMessages++;
    } else if (ccnxMetaMessage_IsInterestReturn(ccnxMessage)) {
        parcLog_Debug(athena->log, "Processing Interest Return Message");

        CCNxInterestReturn *interestReturn = ccnxMetaMessage_GetInterestReturn(ccnxMessage);
        _processInterestReturn(athena, interestReturn, ingressVector);
        athena->stats.numProcessedInterestReturns++;
394 395 396 397 398 399
    } else if (ccnxMetaMessage_IsManifest(ccnxMessage)) {
        parcLog_Debug(athena->log, "Processing Interest Return Message");

        CCNxManifest *manifest = ccnxMetaMessage_GetManifest(ccnxMessage);
        _processManifest(athena, manifest, ingressVector);
        athena->stats.numProcessedManifests++;
400 401 402 403 404 405 406 407 408 409
    } else {
        trapUnexpectedState("Invalid CCNxMetaMessage type");
    }
}

void
athena_EncodeMessage(CCNxMetaMessage *message)
{
    PARCSigner *signer = ccnxValidationCRC32C_CreateSigner();
    CCNxCodecNetworkBufferIoVec *iovec = ccnxCodecTlvPacket_DictionaryEncode(message, signer);
kfox's avatar
kfox committed
410 411
    bool result = ccnxWireFormatMessage_PutIoVec(message, iovec);
    assertTrue(result, "ccnxWireFormatMessage_PutIoVec failed");
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
    ccnxCodecNetworkBufferIoVec_Release(&iovec);
    parcSigner_Release(&signer);
}

void *
athena_ForwarderEngine(void *arg)
{
    Athena *athena = (Athena *) arg;

    if (athena) {
        while (athena->athenaState == Athena_Running) {
            CCNxMetaMessage *ccnxMessage;
            PARCBitVector *ingressVector;
            int receiveTimeout = -1; // block until message received
            ccnxMessage = athenaTransportLinkAdapter_Receive(athena->athenaTransportLinkAdapter,
                                                             &ingressVector, receiveTimeout);
            if (ccnxMessage) {
                athena_ProcessMessage(athena, ccnxMessage, ingressVector);

                parcBitVector_Release(&ingressVector);
                ccnxMetaMessage_Release(&ccnxMessage);
            }
        }
        usleep(1000); // workaround for coordinating with test infrastructure
        athena_Release(&athena);
    }
    return NULL;
}