summaryrefslogtreecommitdiffstats
path: root/src/plugins/srv6-un/un.c
blob: 7fd02e2c93418dc5a8aa14eccc90ff161484e2ea (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
/*
 * Copyright (c) 2015 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.
 */
/*
 *------------------------------------------------------------------
 * un.c - SRv6 Masquerading Proxy (AM) function
 *------------------------------------------------------------------
 */

#include <vnet/vnet.h>
#include <vnet/adj/adj.h>
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>
#include <srv6-un/un.h>

unsigned char function_name[] = "SRv6-uN";
unsigned char keyword_str[] = "uN(32b+16b)";
unsigned char def_str[] = "SRv6 uSID uN";
unsigned char params_str[] = "";
u8 prefix_length = 48;

srv6_un_main_t srv6_un_main;

/*****************************************/
/* SRv6 LocalSID instantiation and removal functions */
static int
srv6_un_localsid_creation_fn (ip6_sr_localsid_t * localsid)
{
  srv6_un_localsid_t *ls_mem = localsid->plugin_mem;
  //Nothing to do here.
  ls_mem->shift = 16;
  return 0;
}

static int
srv6_un_localsid_removal_fn (ip6_sr_localsid_t * localsid)
{
  //Nothing to do here.

  /* Clean up local SID memory */
  clib_mem_free (localsid->plugin_mem);

  return 0;
}

/**********************************/
/* SRv6 LocalSID format functions */
/*
 * Prints nicely the parameters of a localsid
 * Example: print "Table 5"
 */
u8 *
format_srv6_un_localsid (u8 * s, va_list * args)
{
  srv6_un_localsid_t *ls_mem = va_arg (*args, void *);

  return (format (s, "Shift:\t\t%u", ls_mem->shift));
}

/*
 * Process the parameters of a localsid
 * Example: process from:
 * sr localsid address cafe::1 behavior new_srv6_localsid 5
 * everything from behavior on... so in this case 'new_srv6_localsid 5'
 * Notice that it MUST match the keyword_str and params_str defined above.
 */
uword
unformat_srv6_un_localsid (unformat_input_t * input, va_list * args)
{
  void **plugin_mem_p = va_arg (*args, void **);
  srv6_un_localsid_t *ls_mem;

  if (unformat (input, "uN(32b+16b)"))
    {
      /* Allocate a portion of memory */
      ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);

      /* Set to zero the memory */
      clib_memset (ls_mem, 0, sizeof *ls_mem);

      /* Dont forget to add it to the localsid */
      *plugin_mem_p = ls_mem;
      return 1;
    }
  return 0;
}

/*************************/
/* SRv6 LocalSID FIB DPO */
static u8 *
format_srv6_un_dpo (u8 * s, va_list * args)
{
  index_t index = va_arg (*args, index_t);
  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);

  return (format (s, "SR: shift-and-forward 16b:[%u]", index));
}

void
srv6_un_dpo_lock (dpo_id_t * dpo)
{
}

void
srv6_un_dpo_unlock (dpo_id_t * dpo)
{
}

const static dpo_vft_t srv6_un_vft = {
  .dv_lock = srv6_un_dpo_lock,
  .dv_unlock = srv6_un_dpo_unlock,
  .dv_format = format_srv6_un_dpo,
};

const static char *const srv6_un_ip6_nodes[] = {
  "srv6-un-localsid",
  NULL,
};

const static char *const *const srv6_un_nodes[DPO_PROTO_NUM] = {
  [DPO_PROTO_IP6] = srv6_un_ip6_nodes,
};

/**********************/
static clib_error_t *
srv6_un_init (vlib_main_t * vm)
{
  srv6_un_main_t *sm = &srv6_un_main;
  int rv = 0;

  sm->vlib_main = vm;
  sm->vnet_main = vnet_get_main ();

  /* Create DPO */
  sm->srv6_un16_dpo_type =
    dpo_register_new_type (&srv6_un_vft, srv6_un_nodes);

  /* Register SRv6 LocalSID */
  rv = sr_localsid_register_function (vm,
				      function_name,
				      keyword_str,
				      def_str,
				      params_str,
				      prefix_length,
				      &sm->srv6_un16_dpo_type,
				      format_srv6_un_localsid,
				      unformat_srv6_un_localsid,
				      srv6_un_localsid_creation_fn,
				      srv6_un_localsid_removal_fn);
  if (rv < 0)
    clib_error_return (0, "SRv6 LocalSID function could not be registered.");
  else
    sm->srv6_localsid_behavior_id = rv;

  return 0;
}


/* *INDENT-OFF* */
VLIB_INIT_FUNCTION (srv6_un_init);

VLIB_PLUGIN_REGISTER () = {
  .version = VPP_BUILD_VER,
  .description = "Segment Routing Shift And Forward uN 16b",
};
/* *INDENT-ON* */

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