aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/io_modules/memif/hicn_vapi.c
blob: b83a36b474f4563d18f19ecadcf9a275857ee95f (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
/*
 * Copyright (c) 2017-2020 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/transport/config.h>
#include <hicn/transport/utils/log.h>
#include <io_modules/memif/hicn_vapi.h>

#define HICN_VPP_PLUGIN
#include <hicn/name.h>
#undef HICN_VPP_PLUGIN

#include <vapi/hicn.api.vapi.h>
#include <vapi/ip.api.vapi.h>
#include <vapi/vapi_safe.h>
#include <vlib/vlib.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vnet/ip/format.h>
#include <vnet/ip/ip4_packet.h>
#include <vnet/ip/ip6_packet.h>
#include <vpp_plugins/hicn/error.h>
#include <vppinfra/error.h>

/////////////////////////////////////////////////////
const char *HICN_ERROR_STRING[] = {
#define _(a, b, c) c,
    foreach_hicn_error
#undef _
};
/////////////////////////////////////////////////////

/*********************** Missing Symbol in vpp libraries
 * *************************/
u8 *format_vl_api_address_union(u8 *s, va_list *args) { return NULL; }

/*********************************************************************************/

DEFINE_VAPI_MSG_IDS_HICN_API_JSON
DEFINE_VAPI_MSG_IDS_IP_API_JSON

static vapi_error_e register_prod_app_cb(
    vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv, bool is_last,
    vapi_payload_hicn_api_register_prod_app_reply *reply) {
  hicn_producer_output_params *output_params =
      (hicn_producer_output_params *)callback_ctx;

  if (reply == NULL) return rv;

  output_params->cs_reserved = reply->cs_reserved;
  output_params->prod_addr = (ip_address_t *)malloc(sizeof(ip_address_t));
  memset(output_params->prod_addr, 0, sizeof(ip_address_t));
  if (reply->prod_addr.af == ADDRESS_IP6)
    memcpy(&output_params->prod_addr->v6, reply->prod_addr.un.ip6,
           sizeof(ip6_address_t));
  else
    memcpy(&output_params->prod_addr->v4, reply->prod_addr.un.ip4,
           sizeof(ip4_address_t));
  output_params->face_id = reply->faceid;

  return reply->retval;
}

int hicn_vapi_register_prod_app(vapi_ctx_t ctx,
                                hicn_producer_input_params *input_params,
                                hicn_producer_output_params *output_params) {
  vapi_lock();
  vapi_msg_hicn_api_register_prod_app *msg =
      vapi_alloc_hicn_api_register_prod_app(ctx);

  if (ip46_address_is_ip4((ip46_address_t *)&input_params->prefix->address)) {
    memcpy(&msg->payload.prefix.address.un.ip4, &input_params->prefix->address,
           sizeof(ip4_address_t));
    msg->payload.prefix.address.af = ADDRESS_IP4;
  } else {
    memcpy(&msg->payload.prefix.address.un.ip6, &input_params->prefix->address,
           sizeof(ip6_address_t));
    msg->payload.prefix.address.af = ADDRESS_IP6;
  }
  msg->payload.prefix.len = input_params->prefix->len;

  msg->payload.swif = input_params->swif;
  msg->payload.cs_reserved = input_params->cs_reserved;

  int ret = vapi_hicn_api_register_prod_app(ctx, msg, register_prod_app_cb,
                                            output_params);
  vapi_unlock();
  return ret;
}

static vapi_error_e face_prod_del_cb(
    vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv, bool is_last,
    vapi_payload_hicn_api_face_prod_del_reply *reply) {
  if (reply == NULL) return rv;

  return reply->retval;
}

int hicn_vapi_face_prod_del(vapi_ctx_t ctx,
                            hicn_del_face_app_input_params *input_params) {
  vapi_lock();
  vapi_msg_hicn_api_face_prod_del *msg = vapi_alloc_hicn_api_face_prod_del(ctx);

  msg->payload.faceid = input_params->face_id;

  int ret = vapi_hicn_api_face_prod_del(ctx, msg, face_prod_del_cb, NULL);
  vapi_unlock();
  return ret;
}

static vapi_error_e register_cons_app_cb(
    vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv, bool is_last,
    vapi_payload_hicn_api_register_cons_app_reply *reply) {
  hicn_consumer_output_params *output_params =
      (hicn_consumer_output_params *)callback_ctx;

  if (reply == NULL) return rv;

  output_params->src6 = (ip_address_t *)malloc(sizeof(ip_address_t));
  output_params->src4 = (ip_address_t *)malloc(sizeof(ip_address_t));
  memset(output_params->src6, 0, sizeof(ip_address_t));
  memset(output_params->src4, 0, sizeof(ip_address_t));
  memcpy(&output_params->src6->v6, &reply->src_addr6.un.ip6,
         sizeof(ip6_address_t));
  memcpy(&output_params->src4->v4, &reply->src_addr4.un.ip4,
         sizeof(ip4_address_t));

  output_params->face_id1 = reply->faceid1;
  output_params->face_id2 = reply->faceid2;

  return reply->retval;
}

int hicn_vapi_register_cons_app(vapi_ctx_t ctx,
                                hicn_consumer_input_params *input_params,
                                hicn_consumer_output_params *output_params) {
  vapi_lock();
  vapi_msg_hicn_api_register_cons_app *msg =
      vapi_alloc_hicn_api_register_cons_app(ctx);

  msg->payload.swif = input_params->swif;

  int ret = vapi_hicn_api_register_cons_app(ctx, msg, register_cons_app_cb,
                                            output_params);
  vapi_unlock();
  return ret;
}

static vapi_error_e face_cons_del_cb(
    vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv, bool is_last,
    vapi_payload_hicn_api_face_cons_del_reply *reply) {
  if (reply == NULL) return rv;

  return reply->retval;
}

int hicn_vapi_face_cons_del(vapi_ctx_t ctx,
                            hicn_del_face_app_input_params *input_params) {
  vapi_lock();
  vapi_msg_hicn_api_face_cons_del *msg = vapi_alloc_hicn_api_face_cons_del(ctx);

  msg->payload.faceid = input_params->face_id;

  int ret = vapi_hicn_api_face_cons_del(ctx, msg, face_cons_del_cb, NULL);
  vapi_unlock();
  return ret;
}

static vapi_error_e reigster_route_cb(
    vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv, bool is_last,
    vapi_payload_ip_route_add_del_reply *reply) {
  if (reply == NULL) return rv;

  return reply->retval;
}

int hicn_vapi_register_route(vapi_ctx_t ctx,
                             hicn_producer_set_route_params *input_params) {
  vapi_lock();
  vapi_msg_ip_route_add_del *msg = vapi_alloc_ip_route_add_del(ctx, 1);

  msg->payload.is_add = 1;
  if (ip46_address_is_ip4((ip46_address_t *)(input_params->prod_addr))) {
    memcpy(&msg->payload.route.prefix.address.un.ip4,
           &input_params->prefix->address.v4, sizeof(ip4_address_t));
    msg->payload.route.prefix.address.af = ADDRESS_IP4;
    msg->payload.route.prefix.len = input_params->prefix->len;
  } else {
    memcpy(&msg->payload.route.prefix.address.un.ip6,
           &input_params->prefix->address.v6, sizeof(ip6_address_t));
    msg->payload.route.prefix.address.af = ADDRESS_IP6;
    msg->payload.route.prefix.len = input_params->prefix->len;
  }

  msg->payload.route.paths[0].sw_if_index = ~0;
  msg->payload.route.paths[0].table_id = 0;
  if (ip46_address_is_ip4((ip46_address_t *)(input_params->prod_addr))) {
    memcpy(&(msg->payload.route.paths[0].nh.address.ip4),
           input_params->prod_addr->v4.as_u8, sizeof(ip4_address_t));
    msg->payload.route.paths[0].proto = FIB_API_PATH_NH_PROTO_IP4;
  } else {
    memcpy(&(msg->payload.route.paths[0].nh.address.ip6),
           input_params->prod_addr->v6.as_u8, sizeof(ip6_address_t));
    msg->payload.route.paths[0].proto = FIB_API_PATH_NH_PROTO_IP6;
  }

  msg->payload.route.paths[0].type = FIB_API_PATH_FLAG_NONE;
  msg->payload.route.paths[0].flags = FIB_API_PATH_FLAG_NONE;

  int ret = vapi_ip_route_add_del(ctx, msg, reigster_route_cb, NULL);

  vapi_unlock();
  return ret;
}

char *hicn_vapi_get_error_string(int ret_val) {
  return get_error_string(ret_val);
}