aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-plugin/src/faces/ip/dpo_ip.c
blob: 1b2dbcff9f0e4f92c2a372d947e60a3c8f8a5993 (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
/*
 * 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 "dpo_ip.h"

mhash_t hicn_face_ip_local_hashtb;
mhash_t hicn_face_ip_remote_hashtb;
dpo_type_t hicn_face_ip_type;

const static char *const hicn_face_ip4dpoi_nodes[] = {
  "hicn-face-ip4-input",
  "hicn-face-ip4-output",
  "hicn-iface-ip4-input",
  "hicn-iface-ip4-output",
  NULL,
};

const static char *const hicn_face_ip6dpoi_nodes[] = {
  "hicn-face-ip6-input",
  "hicn-face-ip6-output",
  "hicn-iface-ip6-input",
  "hicn-iface-ip6-output",
  NULL,
};

const static char *const *const hicn_ip_nodes[DPO_PROTO_NUM] = {
  [DPO_PROTO_IP4] = hicn_face_ip4dpoi_nodes,
  [DPO_PROTO_IP6] = hicn_face_ip6dpoi_nodes
};

const static dpo_vft_t hicn_face_ip_vft = {
  .dv_lock = hicn_face_lock,
  .dv_unlock = hicn_face_unlock,
  .dv_format = format_hicn_face_ip,
};

/* Must be executed after all the strategy nodes are created */
void
hicn_dpo_ip_module_init (void)
{
  mhash_init (&hicn_face_ip_local_hashtb,
	      sizeof (hicn_face_id_t) /* value */ ,
	      sizeof (hicn_face_ip_key_t) /* key */ );
  mhash_init (&hicn_face_ip_remote_hashtb,
	      sizeof (hicn_face_id_t) /* value */ ,
	      sizeof (hicn_face_ip_key_t) /* key */ );

  /*
   * How much useful is the following registration?
   * So far it seems that we need it only for setting the dpo_type.
   */
  hicn_face_ip_type =
    dpo_register_new_type (&hicn_face_ip_vft, hicn_ip_nodes);
}


int
hicn_dpo_ip4_create (dpo_id_t * dpo,
		     const ip4_address_t * local_addr,
		     const ip4_address_t * remote_addr,
		     u32 sw_if,
		     adj_index_t adj,
		     u32 node_index,
		     hicn_face_flags_t flags, hicn_face_id_t * face_id)
{
  /* If local matches the dpoi is a face */
  hicn_face_t *face =
    hicn_face_ip4_get (local_addr, sw_if, &hicn_face_ip_local_hashtb);
  u8 is_appface;

  if (face != NULL)
    return HICN_ERROR_FACE_ALREADY_CREATED;

  face = hicn_face_ip4_get (remote_addr, sw_if, &hicn_face_ip_remote_hashtb);

  if (face == NULL)
    {
      hicn_dpo_ip4_add_and_lock_from_remote (dpo, &is_appface, local_addr,
					     remote_addr, sw_if, node_index);
      *face_id = (hicn_face_id_t) dpo->dpoi_index;
      face = hicn_dpoi_get_from_idx (*face_id);
    }
  else
    {
      *face_id = hicn_dpoi_get_index (face);
      dpo_set (dpo, hicn_face_ip_type, DPO_PROTO_IP4, *face_id);
      dpo->dpoi_next_node = node_index;
    }


  hicn_face_ip_key_t key;
  hicn_face_ip4_get_key (local_addr, sw_if, &key);

  mhash_set_mem (&hicn_face_ip_local_hashtb, &key, (uword *) face_id, 0);

  hicn_face_ip_t *ip_face = (hicn_face_ip_t *) face->data;
  ip46_address_set_ip4 (&ip_face->local_addr, local_addr);
  ip46_address_set_ip4 (&ip_face->remote_addr, remote_addr);
  face->shared.flags = flags;
  face->shared.adj = adj;

  return HICN_ERROR_NONE;
}

int
hicn_dpo_ip6_create (dpo_id_t * dpo,
		     const ip6_address_t * local_addr,
		     const ip6_address_t * remote_addr,
		     u32 sw_if,
		     adj_index_t adj,
		     u32 node_index,
		     hicn_face_flags_t flags, hicn_face_id_t * face_id)
{
  /* If local matches the dpoi is a face */
  hicn_face_t *face =
    hicn_face_ip6_get (local_addr, sw_if, &hicn_face_ip_local_hashtb);

  u8 is_appface;

  if (face != NULL)
    return HICN_ERROR_FACE_ALREADY_CREATED;

  face = hicn_face_ip6_get (remote_addr, sw_if, &hicn_face_ip_remote_hashtb);

  /* If remote matches the dpoi is a iface */
  if (face == NULL)
    {
      hicn_dpo_ip6_add_and_lock_from_remote (dpo, &is_appface, local_addr,
					     remote_addr, sw_if, node_index);
      *face_id = (hicn_face_id_t) dpo->dpoi_index;
      face = hicn_dpoi_get_from_idx (*face_id);
    }
  else
    {
      *face_id = hicn_dpoi_get_index (face);
      dpo_set (dpo, hicn_face_ip_type, DPO_PROTO_IP6, *face_id);
      dpo->dpoi_next_node = node_index;
    }

  hicn_face_ip_key_t key;
  hicn_face_ip6_get_key (local_addr, sw_if, &key);

  mhash_set_mem (&hicn_face_ip_local_hashtb, &key, (uword *) face_id, 0);

  hicn_face_ip_t *ip_face = (hicn_face_ip_t *) face->data;
  clib_memcpy (&ip_face->local_addr, local_addr, sizeof (ip6_address_t));
  clib_memcpy (&ip_face->remote_addr, remote_addr, sizeof (ip6_address_t));
  face->shared.sw_if = sw_if;
  face->shared.flags = flags;
  face->shared.adj = adj;


  return HICN_ERROR_NONE;
}

void
hicn_dpo_ip_create_from_face (hicn_face_t * face, dpo_id_t * dpo,
			      u16 dpoi_next_node)
{
  hicn_face_id_t face_dpoi_id = hicn_dpoi_get_index (face);
  hicn_face_ip_t *ip_face = (hicn_face_ip_t *) face->data;
  dpo_set (dpo, face->shared.face_type,
	   ip46_address_is_ip4 (&ip_face->
				local_addr) ? DPO_PROTO_IP4 : DPO_PROTO_IP6,
	   face_dpoi_id);
  dpo->dpoi_next_node = dpoi_next_node;
}

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