summaryrefslogtreecommitdiffstats
path: root/hicn-plugin/src/strategy_dpo_manager.c
blob: 470d8d1851d2b91532694c5549a245f88ff0c5d6 (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
/*
 * 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 <vnet/dpo/dpo.h>

#include "strategy_dpo_manager.h"
#include "strategies/dpo_mw.h"
#include "strategies/dpo_rr.h"
#include "strategy.h"
#include "faces/face.h"

static dpo_type_t *strategies_id;
static const hicn_dpo_vft_t **hicn_dpo_vfts;

static const hicn_strategy_vft_t **hicn_strategy_vfts;

int hicn_strategies = 0;

hicn_dpo_vft_t default_dpo;

dpo_type_t
hicn_dpo_register_new_type (const char *const *const *hicn_nodes,
			    const hicn_dpo_vft_t * hicn_dpo_vft,
			    const hicn_strategy_vft_t * hicn_strategy_vft,
			    const dpo_vft_t * dpo_ctx_vft)
{
  dpo_type_t dpo_type = dpo_register_new_type (dpo_ctx_vft, hicn_nodes);
  vec_validate (hicn_dpo_vfts, dpo_type);
  hicn_dpo_vfts[dpo_type] = hicn_dpo_vft;

  vec_validate (hicn_strategy_vfts, dpo_type);
  hicn_strategy_vfts[dpo_type] = hicn_strategy_vft;

  vec_validate (strategies_id, hicn_strategies);
  strategies_id[hicn_strategies] = dpo_type;
  hicn_strategies++;

  return dpo_type;
}

u32
dpo_is_hicn (const dpo_id_t * dpo)
{
  for (int i = 0; i < hicn_strategies; i++)
    {
      if (hicn_dpo_vfts[strategies_id[i]]->hicn_dpo_is_type (dpo))
	return 1;
    }
  return 0;
}

dpo_type_t
hicn_dpo_get_vft_id (const dpo_id_t * dpo)
{
  return dpo->dpoi_type;
}

const hicn_dpo_vft_t *
hicn_dpo_get_vft (dpo_type_t vfts_id)
{
  return hicn_dpo_vfts[vfts_id];
}

const hicn_dpo_vft_t *
hicn_dpo_get_vft_from_id (u8 strategy_id)
{
  return hicn_dpo_vfts[strategies_id[strategy_id]];
}

const hicn_strategy_vft_t *
hicn_dpo_get_strategy_vft (dpo_type_t vfts_id)
{
  return hicn_strategy_vfts[vfts_id];
}

const hicn_strategy_vft_t *
hicn_dpo_get_strategy_vft_from_id (u8 vfts_id)
{
  return hicn_strategy_vfts[strategies_id[vfts_id]];
}

void
hicn_dpos_init (void)
{
  hicn_dpo_strategy_mw_module_init ();
  hicn_dpo_strategy_rr_module_init ();

  default_dpo.hicn_dpo_get_ctx = &hicn_strategy_mw_ctx_get;
  default_dpo.hicn_dpo_is_type = &hicn_dpo_is_type_strategy_mw;
  default_dpo.hicn_dpo_get_type = &hicn_dpo_strategy_mw_get_type;
  default_dpo.hicn_dpo_module_init = &hicn_dpo_strategy_mw_module_init;
  default_dpo.hicn_dpo_create = &hicn_strategy_mw_ctx_create;
  default_dpo.hicn_dpo_add_update_nh = &hicn_strategy_mw_ctx_add_nh;
  default_dpo.hicn_dpo_del_nh = &hicn_strategy_mw_ctx_del_nh;
  default_dpo.hicn_dpo_lock_dpo_ctx = &hicn_strategy_mw_ctx_lock;
  default_dpo.hicn_dpo_unlock_dpo_ctx = hicn_strategy_mw_ctx_unlock;
  default_dpo.format_hicn_dpo = &format_hicn_strategy_mw_ctx;
}

u8 *
format_hicn_strategy_list (u8 * s, int n, ...)
{
  va_list ap;
  va_start (ap, n);
  u32 indent = va_arg (ap, u32);
  va_end (ap);

  s = format (s, "Strategies:\n", indent);
  indent += 4;
  int i;
  vec_foreach_index (i, strategies_id)
  {
    s = format (s, "(%d) ", i, indent);
    s = hicn_dpo_vfts[strategies_id[i]]->format_hicn_dpo (s, &ap);
  }

  return (s);
}

u8
hicn_dpo_strategy_id_is_valid (int strategy_id)
{
  return vec_len (strategies_id) > strategy_id ?
    HICN_ERROR_NONE : HICN_ERROR_DPO_MGR_ID_NOT_VALID;
}

int
hicn_strategy_get_all_available (void)
{
  return hicn_strategies;
}

/**
 * @brief Registers a dpo by calling its module init function.
 *
 * This is typically called from the ctor for dpo's registered at compilation
 * time.
 */
void
hicn_dpo_register (const hicn_dpo_vft_t * hicn_dpo)
{
  hicn_dpo->hicn_dpo_module_init ();
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables: eval: (c-set-style "gnu") End:
 */