aboutsummaryrefslogtreecommitdiffstats
path: root/apps/iping/iPing_Client.c
blob: 85d92eb043690dc547634efe69900f7065522f53 (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
/*
 * 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 <stdio.h>
#include <inttypes.h>
#include <getopt.h>
#include <LongBow/runtime.h>
#include <ccnx/api/ccnx_Portal/ccnx_Portal.h>
#include <ccnx/api/ccnx_Portal/ccnx_PortalRTA.h>
#include <parc/algol/parc_Clock.h>
#include <parc/security/parc_Security.h>
#include <parc/algol/parc_DisplayIndented.h>
#include "iPing_Stats.h"
#include "iPing_Common.h"

typedef enum {
  CCNxPingClientMode_None = 0, CCNxPingClientMode_Flood, CCNxPingClientMode_PingPong, CCNxPingClientMode_All
} CCNxPingClientMode;

typedef struct ccnx_Ping_client {
  CCNxPortal *portal;
  CCNxPortalFactory *factory;
  CCNxPingStats *stats;
  CCNxPingClientMode mode;

  CCNxName *prefix;

  size_t numberOfOutstanding;
  uint64_t receiveTimeoutInMs;
  uint32_t interestLifetime;
  int interestCounter;
  size_t count;
  uint64_t intervalInMs;
  size_t payloadSize;
  int nonce;
} CCNxPingClient;

/**
 * Create a new CCNxPortalFactory instance using a randomly generated identity saved to
 * the specified keystore.
 *
 * @return A new CCNxPortalFactory instance which must eventually be released by calling ccnxPortalFactory_Release().
 */
static CCNxPortalFactory *_setupClientPortalFactory(void) {
  const char *keystoreName = "client.keystore";
  const char *keystorePassword = "keystore_password";
  const char *subjectName = "client";

  return ccnxPingCommon_SetupPortalFactory(keystoreName, keystorePassword, subjectName);
}

/**
 * Release the references held by the `CCNxPingClient`.
 */
static bool _ccnxPingClient_Destructor(CCNxPingClient **clientPtr) {
  CCNxPingClient *client = *clientPtr;

  if (client->factory != NULL) {
    ccnxPortalFactory_Release(&(client->factory));
  }
  if (client->portal != NULL) {
    ccnxPortal_Release(&(client->portal));
  }
  if (client->prefix != NULL) {
    ccnxName_Release(&(client->prefix));
  }
  if (client->stats != NULL) {
    ccnxPingStats_Release(&client->stats);
  }
  return true;
}

parcObject_Override(CCNxPingClient, PARCObject, .destructor = (PARCObjectDestructor *) _ccnxPingClient_Destructor);

parcObject_ImplementAcquire(ccnxPingClient, CCNxPingClient);
parcObject_ImplementRelease(ccnxPingClient, CCNxPingClient);

/**
 * Create a new empty `CCNxPingClient` instance.
 */
static CCNxPingClient *ccnxPingClient_Create(void) {
  CCNxPingClient *client = parcObject_CreateInstance(CCNxPingClient);

  client->stats = ccnxPingStats_Create();
  client->interestCounter = 100;
  client->prefix = ccnxName_CreateFromCString(ccnxPing_DefaultPrefix);
  client->receiveTimeoutInMs = ccnxPing_DefaultReceiveTimeoutInUs / 1000;
  client->count = 10;
  client->intervalInMs = 1000;
  client->nonce = rand();
  client->numberOfOutstanding = 0;
  client->interestLifetime = 4 * 1000; //4s

  return client;
}

/**
 * Get the next `CCNxName` to issue. Increment the interest counter
 * for the client.
 */
static CCNxName *_ccnxPingClient_CreateNextName(CCNxPingClient *client) {
  client->interestCounter++;
  char *suffixBuffer = NULL;

  asprintf(&suffixBuffer, "%x", client->nonce);
  CCNxName *name1 = ccnxName_ComposeNAME(client->prefix, suffixBuffer);
  free(suffixBuffer);

  suffixBuffer = NULL;
  asprintf(&suffixBuffer, "%u", (unsigned int) client->payloadSize);
  CCNxName *name2 = ccnxName_ComposeNAME(name1, suffixBuffer);
  free(suffixBuffer);
  ccnxName_Release(&name1);

  suffixBuffer = NULL;
  asprintf(&suffixBuffer, "%06lu", (long) client->interestCounter);
  CCNxName *name3 = ccnxName_ComposeNAME(name2, suffixBuffer);
  free(suffixBuffer);
  ccnxName_Release(&name2);

  return name3;
}

/**
 * Convert a timeval struct to a single microsecond count.
 */
static uint64_t _ccnxPingClient_CurrentTimeInUs(PARCClock *clock) {
  struct timeval currentTimeVal;
  parcClock_GetTimeval(clock, &currentTimeVal);
  uint64_t microseconds = ((uint64_t) currentTimeVal.tv_sec) * 1000000 + currentTimeVal.tv_usec;
  return microseconds;
}

/**
 * Run a single ping test.
 */
static void _ccnxPingClient_RunPing(CCNxPingClient *client, size_t totalPings) {
  uint64_t delayInUs = client->intervalInMs * 1000;
  uint64_t timeoutInUs = client->receiveTimeoutInMs * 1000;
  PARCClock *clock = parcClock_Wallclock();

  client->factory = _setupClientPortalFactory();
  client->portal = ccnxPortalFactory_CreatePortal(client->factory, ccnxPortalRTA_Message);

  size_t outstanding = 0;

  uint64_t nextPacketSendTime = 0;
  uint64_t currentTimeInUs = 0;

  for (int pings = 0; pings < totalPings; pings++) {

    CCNxName *name;
    CCNxInterest *interest;
    CCNxMetaMessage *message;

    name = _ccnxPingClient_CreateNextName(client);
    interest = ccnxInterest_CreateSimple(name);
    message = ccnxMetaMessage_CreateFromInterest(interest);
    ccnxInterest_SetLifetime(interest, client->interestLifetime);

    if (ccnxPortal_Send(client->portal, message, CCNxStackTimeout_Never)) {
      currentTimeInUs = _ccnxPingClient_CurrentTimeInUs(clock);
      nextPacketSendTime = currentTimeInUs + delayInUs;

      ccnxPingStats_RecordRequest(client->stats, name, currentTimeInUs);
    } else {
      printf("Error in sending\n");
      nextPacketSendTime = 0;
    }

    ccnxMetaMessage_Release(&message);
    ccnxInterest_Release(&interest);
    outstanding++;

    /* Now wait for the responses and record their times
     * First packet wait until its arrives or timeout
     */
    currentTimeInUs = _ccnxPingClient_CurrentTimeInUs(clock);
    uint64_t receiveDelay = timeoutInUs;
    CCNxMetaMessage *response = ccnxPortal_Receive(client->portal, &receiveDelay);

    //Remove the timeout ping only if we are in the ping mode
    if (response == NULL && (client->mode != CCNxPingClientMode_Flood)) {
      ccnxPingStats_RecordLost(client->stats, name);
      outstanding--;
    }
    ccnxName_Release(&name);

    /* Loop to handle content that arrives too late, that is after receiveDelay
     * They will be just evicted from the hashtable and not considered in the
     */
    while (response != NULL) {
      printf("Received content\n");
      currentTimeInUs = _ccnxPingClient_CurrentTimeInUs(clock);
      if (ccnxMetaMessage_IsContentObject(response)) {
        CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(response);
        CCNxName *responseName = ccnxContentObject_GetName(contentObject);
        bool existing;
        uint64_t
            delta = ccnxPingStats_RecordResponse(client->stats, responseName, currentTimeInUs, response, &existing);
        // Only display output if we're in ping mode
        if (client->mode == CCNxPingClientMode_PingPong && existing) {
          size_t contentSize = parcBuffer_Remaining(ccnxContentObject_GetPayload(contentObject));
          char *nameString = ccnxName_ToString(responseName);
          printf("%zu bytes from %s: time=%"PRIu64" us\n", contentSize, nameString, delta);
          parcMemory_Deallocate(&nameString);
        }
      }
      ccnxMetaMessage_Release(&response);

      outstanding--;

      currentTimeInUs = _ccnxPingClient_CurrentTimeInUs(clock);

      /* The following covers the case in which a content arrived after
       * receiveDelay. If that happens, it will be read from the portal
       * after the next interest sending, therefore we might have two
       * contents to read rather than one. This code is fine as long as we
       * do not consider in statistics content arriving after receiveDelay
       */
      receiveDelay = nextPacketSendTime - currentTimeInUs;
      response = ccnxPortal_Receive(client->portal, &receiveDelay);
    }
  }

  /* We're done with pings, so let's wait to see if we have any
   * stragglers. We wait for 30s more
   */
  currentTimeInUs = _ccnxPingClient_CurrentTimeInUs(clock);
  nextPacketSendTime = currentTimeInUs + 10000000; //10s

  uint64_t receiveDelay = nextPacketSendTime - currentTimeInUs;
  CCNxMetaMessage *response = ccnxPortal_Receive(client->portal, &receiveDelay);

  /* Loop to handle content that arrives too late, that is after receiveDelay
   * They will be just evicted from the hashtable and not considered in the
   */
  while (response != NULL) {
    printf("Received content\n");
    currentTimeInUs = _ccnxPingClient_CurrentTimeInUs(clock);
    if (ccnxMetaMessage_IsContentObject(response)) {
      CCNxContentObject *contentObject = ccnxMetaMessage_GetContentObject(response);
      CCNxName *responseName = ccnxContentObject_GetName(contentObject);
      bool existing;
      uint64_t delta = ccnxPingStats_RecordResponse(client->stats, responseName, currentTimeInUs, response, &existing);
      // Only display output if we're in ping mode
      if (client->mode == CCNxPingClientMode_PingPong && existing) {
        size_t contentSize = parcBuffer_Remaining(ccnxContentObject_GetPayload(contentObject));
        char *nameString = ccnxName_ToString(responseName);
        printf("%zu bytes from %s: time=%"PRIu64" us\n", contentSize, nameString, delta);
        parcMemory_Deallocate(&nameString);
      }
    }
    ccnxMetaMessage_Release(&response);
    outstanding--;
    currentTimeInUs = _ccnxPingClient_CurrentTimeInUs(clock);
    /* The following covers the case in which a content arrived after
     * receiveDelay. If that happens, it will be read from the portal
     * after the next interest sending, therefore we might have two
     * contents to read rather than one. This code is fine as long as we
     * do not consider in statistics content arriving after receiveDelay
     */
    receiveDelay = nextPacketSendTime - currentTimeInUs;
    response = ccnxPortal_Receive(client->portal, &receiveDelay);
  }
  parcClock_Release(&clock);
}

/**
 * Display the usage message.
 */
static void _displayUsage(char *progName) {
  printf("CCNx Simple Ping Performance Test\n");
  printf("   (you must have ccnxPing_Server running)\n");
  printf("\n");
  printf("Usage: %s -p [ -c count ] [ -s size ] [ -i interval ]\n", progName);
  printf("       %s -f [ -c count ] [ -s size ]\n", progName);
  printf("       %s -h\n", progName);
  printf("\n");
  printf("Example:\n");
  printf("    ccnxPing_Client -l ccnx:/some/prefix -c 100 -f\n");
  printf("\n");
  printf("Options:\n");
  printf("     -h (--help) Show this help message\n");
  printf("     -p (--ping) ping mode - \n");
  printf(
      "     -f (--flood) flood mode - send as fast as possible. ATTENTION, NO MIGHT USE A LOT OF MEMORY IF THE NUMBER OF SENDING INTEREST IS HUGE\n");
  printf("     -c (--count) Number of count to run\n");
  printf(
      "     -i (--interval) Interval in milliseconds between interests in ping mode. Such interval cannot always be satisfied because in ping mode the application waits to receive a content before issuing the next interest. If any content is received interval==timeout\n");
  printf("     -s (--size) Size of the interests\n");
  printf("     -l (--locator) Set the locator for this server. The default is 'ccnx:/locator'. \n");
  printf(
      "     -t (--timeout) Time that the application waits for a content. When elapsed the content will be dropped and RTT not considered. Default timeout==1s\n");
  printf(
      "     -e (--lifetime) Set interest lifetime in milliseconds. When elapsed the interest is evicted only from the PIT. Eviction from the application internal state (used for recording interest sending time adn calculate RTT). Default lifetime==4s.\n");
}

/**
 * Parse the command lines to initialize the state of the
 */
static bool _ccnxPingClient_ParseCommandline(CCNxPingClient *client, int argc, char *argv[argc]) {
  static struct option longopts[] =
      {{"ping", no_argument, NULL, 'p'}, {"flood", no_argument, NULL, 'f'}, {"count", required_argument, NULL, 'c'}, {
          "size", required_argument, NULL, 's'
      }, {"interval", required_argument, NULL, 'i'}, {"locator", required_argument, NULL, 'l'}, {"outstanding"
                                                                                                 , required_argument
                                                                                                 , NULL, 'o'
      }, {"help", no_argument, NULL, 'h'}, {"timeout", required_argument, NULL, 't'}, {"lifetime", required_argument
                                                                                       , NULL, 'e'
      }, {NULL, 0, NULL, 0}};

  client->payloadSize = ccnxPing_DefaultPayloadSize;

  int c;
  while ((c = getopt_long(argc, argv, "phfc:s:i:l:o:t:e:", longopts, NULL)) != -1) {
    switch (c) {
      case 'p':
        if (client->mode != CCNxPingClientMode_None) {
          return false;
        }
        client->mode = CCNxPingClientMode_PingPong;
        break;
      case 'f':
        if (client->mode != CCNxPingClientMode_None) {
          return false;
        }
        client->mode = CCNxPingClientMode_Flood;
        client->intervalInMs = 0;
        client->receiveTimeoutInMs = 0;
        break;
      case 'c':
        sscanf(optarg, "%zu", &(client->count));
        break;
      case 'i':
        if (client->mode != CCNxPingClientMode_Flood) {
          sscanf(optarg, "%"PRIu64"", &(client->intervalInMs));
          printf("Timer %"PRIu64"\n", (client->intervalInMs));
        }
        break;
      case 't':
        if (client->mode != CCNxPingClientMode_Flood) {
          sscanf(optarg, "%"PRIu64"", &(client->receiveTimeoutInMs));
        }
        break;
      case 's':
        sscanf(optarg, "%zu", &(client->payloadSize));
        break;
      case 'o':
        sscanf(optarg, "%zu", &(client->numberOfOutstanding));
        break;
      case 'l':
        ccnxName_Release(&(client->prefix));
        client->prefix = ccnxName_CreateFromCString(optarg);
        break;
      case 'h':
        _displayUsage(argv[0]);
        return false;
      case 'e':
        sscanf(optarg, "%"PRIu32"", &(client->interestLifetime));
        break;
      default:
        break;
    }
  }
  if (client->mode == CCNxPingClientMode_None) {
    _displayUsage(argv[0]);
    return false;
  }
  return true;
};

static void _ccnxPingClient_DisplayStatistics(CCNxPingClient *client) {
  bool ableToCompute = ccnxPingStats_Display(client->stats);
  if (!ableToCompute) {
    parcDisplayIndented_PrintLine(0,
                                  "No packets were received. Check to make sure the client and server are configured correctly and that the forwarder is running.\n");
  }
}

static void _ccnxPingClient_RunPingormanceTest(CCNxPingClient *client) {
  switch (client->mode) {
    case CCNxPingClientMode_All:
      _ccnxPingClient_RunPing(client, mediumNumberOfPings);
      _ccnxPingClient_DisplayStatistics(client);

      ccnxPingStats_Release(&client->stats);
      client->stats = ccnxPingStats_Create();

      _ccnxPingClient_RunPing(client, smallNumberOfPings);
      _ccnxPingClient_DisplayStatistics(client);
      break;
    case CCNxPingClientMode_Flood:
      _ccnxPingClient_RunPing(client, client->count);
      _ccnxPingClient_DisplayStatistics(client);
      break;
    case CCNxPingClientMode_PingPong:
      _ccnxPingClient_RunPing(client, client->count);
      _ccnxPingClient_DisplayStatistics(client);
      break;
    case CCNxPingClientMode_None:
    default:
      fprintf(stderr, "Error, unknown mode");
      break;
  }
}

int main(int argc, char *argv[argc]) {
  parcSecurity_Init();
  CCNxPingClient *client = ccnxPingClient_Create();
  bool runPing = _ccnxPingClient_ParseCommandline(client, argc, argv);
  if (runPing) {
    _ccnxPingClient_RunPingormanceTest(client);
  }
  ccnxPingClient_Release(&client);
  parcSecurity_Fini();
  return EXIT_SUCCESS;
}