summaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/strategies/rnd.c
blob: 637fd90f9062e3efcd79ae78a6c9d79ca08a91da (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
/*
 * Copyright (c) 2017-2019 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 <hicn/hicn-light/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <parc/assert/parc_Assert.h>

#include <parc/algol/parc_HashMap.h>
#include <parc/algol/parc_Memory.h>

#include <hicn/strategies/rnd.h>

static void _strategyRnd_ReceiveObject(StrategyImpl *strategy,
                                       const NumberSet *egressId,
                                       const Message *objectMessage, Ticks rtt);
static void _strategyRnd_OnTimeout(StrategyImpl *strategy,
                                   const NumberSet *egressId);
static NumberSet *_strategyRnd_LookupNexthop(StrategyImpl *strategy,
#ifdef WITH_POLICY
    NumberSet * nexthops,
#endif /* WITH_POLICY */
    const Message *interestMessage);
#ifndef WITH_POLICY
static NumberSet *_strategyRnd_ReturnNexthops(StrategyImpl *strategy);
static unsigned _strategyRnd_CountNexthops(StrategyImpl *strategy);
#endif /* ! WITH_POLICY */
static void _strategyRnd_AddNexthop(StrategyImpl *strategy,
                                    unsigned connectionId);
static void _strategyRnd_RemoveNexthop(StrategyImpl *strategy,
                                       unsigned connectionId);
static void _strategyRnd_ImplDestroy(StrategyImpl **strategyPtr);
static strategy_type _strategyRnd_GetStrategy(StrategyImpl *strategy);

static StrategyImpl _template = {
    .context = NULL,
    .receiveObject = &_strategyRnd_ReceiveObject,
    .onTimeout = &_strategyRnd_OnTimeout,
    .lookupNexthop = &_strategyRnd_LookupNexthop,
#ifndef WITH_POLICY
    .returnNexthops = &_strategyRnd_ReturnNexthops,
    .countNexthops = &_strategyRnd_CountNexthops,
#endif /* ! WITH_POLICY */
    .addNexthop = &_strategyRnd_AddNexthop,
    .removeNexthop = &_strategyRnd_RemoveNexthop,
    .destroy = &_strategyRnd_ImplDestroy,
    .getStrategy = &_strategyRnd_GetStrategy,
};

struct strategy_rnd;
typedef struct strategy_rnd StrategyRnd;

struct strategy_rnd {
#ifndef WITH_POLICY
  NumberSet *nexthops;
#endif /* ! WITH_POLICY */
};

StrategyImpl *strategyRnd_Create() {
  StrategyRnd *strategy = parcMemory_AllocateAndClear(sizeof(StrategyRnd));
  parcAssertNotNull(strategy, "parcMemory_AllocateAndClear(%zu) returned NULL",
                    sizeof(StrategyRnd));

#ifndef WITH_POLICY
  strategy->nexthops = numberSet_Create();
#endif /* ! WITH_POLICY */
  srand((unsigned int)time(NULL));

  StrategyImpl *impl = parcMemory_AllocateAndClear(sizeof(StrategyImpl));
  parcAssertNotNull(impl, "parcMemory_AllocateAndClear(%zu) returned NULL",
                    sizeof(StrategyImpl));
  memcpy(impl, &_template, sizeof(StrategyImpl));
  impl->context = strategy;
  return impl;
}

// =======================================================
// Dispatch API

strategy_type _strategyRnd_GetStrategy(StrategyImpl *strategy) {
  return SET_STRATEGY_RANDOM;
}

#ifndef WITH_POLICY
static int _select_Nexthop(StrategyRnd *strategy) {
  unsigned len = (unsigned)numberSet_Length(strategy->nexthops);
  if (len == 0) {
    return -1;
  }

  int rnd = (rand() % len);
  return numberSet_GetItem(strategy->nexthops, rnd);
}
#endif /* ! WITH_POLICY */

static void _strategyRnd_ReceiveObject(StrategyImpl *strategy,
                                       const NumberSet *egressId,
                                       const Message *objectMessage,
                                       Ticks rtt) {}

static void _strategyRnd_OnTimeout(StrategyImpl *strategy,
                                   const NumberSet *egressId) {}

static NumberSet *_strategyRnd_LookupNexthop(StrategyImpl *strategy,
#ifdef WITH_POLICY
        NumberSet * nexthops,
#endif /* WITH_POLICY */
        const Message *interestMessage) {
  unsigned out_connection;
  NumberSet *out = numberSet_Create();

#ifdef WITH_POLICY
  // We return one next hop at random
  out_connection = numberSet_GetItem(nexthops, rand() % numberSet_Length(nexthops));

#else
  StrategyRnd *srnd = (StrategyRnd *)strategy->context;
  unsigned in_connection = message_GetIngressConnectionId(interestMessage);
  unsigned nexthopSize = (unsigned)numberSet_Length(srnd->nexthops);

  if ((nexthopSize == 0) ||
      ((nexthopSize == 1) &&
       numberSet_Contains(srnd->nexthops, in_connection))) {
    // there are no output faces or the input face is also the only output face.
    // return null to avoid loops
    return out;
  }

  do {
    out_connection = _select_Nexthop(srnd);
  } while (out_connection == in_connection);

  if (out_connection == -1) {
    return out;
  }
#endif /* WITH_POLICY */

  numberSet_Add(out, out_connection);
  return out;
}

#ifndef WITH_POLICY
static NumberSet *_strategyRnd_ReturnNexthops(StrategyImpl *strategy) {
  StrategyRnd *srnd = (StrategyRnd *)strategy->context;
  return srnd->nexthops;
}

unsigned _strategyRnd_CountNexthops(StrategyImpl *strategy) {
  StrategyRnd *srnd = (StrategyRnd *)strategy->context;
  return (unsigned)numberSet_Length(srnd->nexthops);
}
#endif /* ! WITH_POLICY */

static void _strategyRnd_AddNexthop(StrategyImpl *strategy,
                                    unsigned connectionId) {
#ifndef WITH_POLICY
  StrategyRnd *srnd = (StrategyRnd *)strategy->context;
  if (!numberSet_Contains(srnd->nexthops, connectionId)) {
    numberSet_Add(srnd->nexthops, connectionId);
  }
#endif /* ! WITH_POLICY */
}

static void _strategyRnd_RemoveNexthop(StrategyImpl *strategy,
                                       unsigned connectionId) {
#ifndef WITH_POLICY
  StrategyRnd *srnd = (StrategyRnd *)strategy->context;

  if (numberSet_Contains(srnd->nexthops, connectionId)) {
    numberSet_Remove(srnd->nexthops, connectionId);
  }
#endif /* ! WITH_POLICY */
}

static void _strategyRnd_ImplDestroy(StrategyImpl **strategyPtr) {
  parcAssertNotNull(strategyPtr, "Parameter must be non-null double pointer");
  parcAssertNotNull(*strategyPtr,
                    "Parameter must dereference to non-null pointer");

  StrategyImpl *impl = *strategyPtr;
  StrategyRnd *strategy = (StrategyRnd *)impl->context;

#ifndef WITH_POLICY
  numberSet_Release(&(strategy->nexthops));
#endif /* ! WITH_POLICY */

  parcMemory_Deallocate((void **)&strategy);
  parcMemory_Deallocate((void **)&impl);
  *strategyPtr = NULL;
}