Add buffer pointer-to-index and index-to-pointer array functions
[vpp.git] / src / plugins / kubeproxy / kp.c
1 /*
2  * Copyright (c) 2017 Intel and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or anated to in writing, software
10  * distributed under the License is distributed on an "POD IS" BPODIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <kubeproxy/kp.h>
17 #include <vnet/plugin/plugin.h>
18 #include <vpp/app/version.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/udp/udp.h>
21
22 //GC runs at most once every so many seconds
23 #define KP_GARBAGE_RUN 60
24
25 //After so many seconds. It is assumed that inter-core race condition will not occur.
26 #define KP_CONCURRENCY_TIMEOUT 10
27
28 kp_main_t kp_main;
29
30 #define kp_get_writer_lock() do {} while(__sync_lock_test_and_set (kp_main.writer_lock, 1))
31 #define kp_put_writer_lock() kp_main.writer_lock[0] = 0
32
33 static void kp_pod_stack (kp_pod_t *pod);
34
35 void ip46_prefix_normalize(ip46_address_t *prefix, u8 plen)
36 {
37   if (plen == 0) {
38     prefix->as_u64[0] = 0;
39     prefix->as_u64[1] = 0;
40   } else if (plen <= 64) {
41     prefix->as_u64[0] &= clib_host_to_net_u64(0xffffffffffffffffL << (64 - plen));
42     prefix->as_u64[1] = 0;
43   } else {
44     prefix->as_u64[1] &= clib_host_to_net_u64(0xffffffffffffffffL << (128 - plen));
45   }
46
47 }
48
49 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
50 {
51   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
52   u8 *len = va_arg (*args, u8 *);
53   ip46_type_t type = va_arg (*args, ip46_type_t);
54
55   u32 l;
56   if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
57     if (l > 32)
58       return 0;
59     *len = l + 96;
60     ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
61   } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
62     if (l > 128)
63       return 0;
64     *len = l;
65   } else {
66     return 0;
67   }
68   return 1;
69 }
70
71 u8 *format_ip46_prefix (u8 * s, va_list * args)
72 {
73   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
74   u32 len = va_arg (*args, u32); //va_arg cannot use u8 or u16
75   ip46_type_t type = va_arg (*args, ip46_type_t);
76
77   int is_ip4 = 0;
78   if (type == IP46_TYPE_IP4)
79     is_ip4 = 1;
80   else if (type == IP46_TYPE_IP6)
81     is_ip4 = 0;
82   else
83     is_ip4 = (len >= 96) && ip46_address_is_ip4(ip46);
84
85   return is_ip4 ?
86       format(s, "%U/%d", format_ip4_address, &ip46->ip4, len - 96):
87       format(s, "%U/%d", format_ip6_address, &ip46->ip6, len);
88 }
89
90 const static char * const kp_dpo_nat4_ip4[] = { "kp4-nat4" , NULL };
91 const static char * const kp_dpo_nat4_ip6[] = { "kp6-nat4" , NULL };
92 const static char* const * const kp_dpo_nat4_nodes[DPO_PROTO_NUM] =
93     {
94         [DPO_PROTO_IP4]  = kp_dpo_nat4_ip4,
95         [DPO_PROTO_IP6]  = kp_dpo_nat4_ip6,
96     };
97
98 const static char * const kp_dpo_nat6_ip4[] = { "kp4-nat6" , NULL };
99 const static char * const kp_dpo_nat6_ip6[] = { "kp6-nat6" , NULL };
100 const static char* const * const kp_dpo_nat6_nodes[DPO_PROTO_NUM] =
101     {
102         [DPO_PROTO_IP4]  = kp_dpo_nat6_ip4,
103         [DPO_PROTO_IP6]  = kp_dpo_nat6_ip6,
104     };
105
106 u32 kp_hash_time_now(vlib_main_t * vm)
107 {
108   return (u32) (vlib_time_now(vm) + 10000);
109 }
110
111 u8 *format_kp_main (u8 * s, va_list * args)
112 {
113   vlib_thread_main_t *tm = vlib_get_thread_main();
114   kp_main_t *kpm = &kp_main;
115   s = format(s, "kp_main");
116   s = format(s, " #vips: %u\n", pool_elts(kpm->vips));
117   s = format(s, " #pods: %u\n", pool_elts(kpm->pods) - 1);
118
119   u32 thread_index;
120   for(thread_index = 0; thread_index < tm->n_vlib_mains; thread_index++ ) {
121     kp_hash_t *h = kpm->per_cpu[thread_index].sticky_ht;
122     if (h) {
123       s = format(s, "core %d\n", thread_index);
124       s = format(s, "  timeout: %ds\n", h->timeout);
125       s = format(s, "  usage: %d / %d\n", kp_hash_elts(h, kp_hash_time_now(vlib_get_main())),  kp_hash_size(h));
126     }
127   }
128
129   return s;
130 }
131
132 static char *kp_vip_type_strings[] = {
133     [KP_VIP_TYPE_IP4_NAT44] = "ip4-nat44",
134     [KP_VIP_TYPE_IP4_NAT46] = "ip4-nat46",
135     [KP_VIP_TYPE_IP6_NAT64] = "ip6-nat64",
136     [KP_VIP_TYPE_IP6_NAT66] = "ip6-nat66",
137 };
138
139 u8 *format_kp_vip_type (u8 * s, va_list * args)
140 {
141   kp_vip_type_t vipt = va_arg (*args, kp_vip_type_t);
142   u32 i;
143   for (i=0; i<KP_VIP_N_TYPES; i++)
144     if (vipt == i)
145       return format(s, kp_vip_type_strings[i]);
146   return format(s, "_WRONG_TYPE_");
147 }
148
149 uword unformat_kp_vip_type (unformat_input_t * input, va_list * args)
150 {
151   kp_vip_type_t *vipt = va_arg (*args, kp_vip_type_t *);
152   u32 i;
153   for (i=0; i<KP_VIP_N_TYPES; i++)
154     if (unformat(input, kp_vip_type_strings[i])) {
155       *vipt = i;
156       return 1;
157     }
158   return 0;
159 }
160
161 u8 *format_kp_vip (u8 * s, va_list * args)
162 {
163   kp_vip_t *vip = va_arg (*args, kp_vip_t *);
164   return format(s, "%U %U port:%u target_port:%u node_port:%u "
165                    "new_size:%u #pod:%u%s",
166              format_kp_vip_type, vip->type,
167              format_ip46_prefix, &vip->prefix, vip->plen, IP46_TYPE_ANY,
168              ntohs(vip->port), ntohs(vip->target_port),
169              ntohs(vip->node_port),
170              vip->new_flow_table_mask + 1,
171              pool_elts(vip->pod_indexes),
172              (vip->flags & KP_VIP_FLAGS_USED)?"":" removed");
173 }
174
175 u8 *format_kp_pod (u8 * s, va_list * args)
176 {
177   kp_pod_t *pod = va_arg (*args, kp_pod_t *);
178   return format(s, "%U %s", format_ip46_address,
179                 &pod->address, IP46_TYPE_ANY,
180                 (pod->flags & KP_POD_FLAGS_USED)?"used":"removed");
181 }
182
183 u8 *format_kp_vip_detailed (u8 * s, va_list * args)
184 {
185   kp_main_t *kpm = &kp_main;
186   kp_vip_t *vip = va_arg (*args, kp_vip_t *);
187   uword indent = format_get_indent (s);
188
189   s = format(s, "%U %U [%u] %U port:%u target_port:%u node_port:%u%s\n"
190                    "%U  new_size:%u\n",
191                   format_white_space, indent,
192                   format_kp_vip_type, vip->type,
193                   vip - kpm->vips, format_ip46_prefix, &vip->prefix, vip->plen, IP46_TYPE_ANY,
194                   ntohs(vip->port), ntohs(vip->target_port),
195                   ntohs(vip->node_port),
196                   (vip->flags & KP_VIP_FLAGS_USED)?"":" removed",
197                   format_white_space, indent,
198                   vip->new_flow_table_mask + 1);
199
200   //Print counters
201   s = format(s, "%U  counters:\n",
202              format_white_space, indent);
203   u32 i;
204   for (i=0; i<KP_N_VIP_COUNTERS; i++)
205     s = format(s, "%U    %s: %d\n",
206                format_white_space, indent,
207                kpm->vip_counters[i].name,
208                vlib_get_simple_counter(&kpm->vip_counters[i], vip - kpm->vips));
209
210
211   s = format(s, "%U  #pod:%u\n",
212              format_white_space, indent,
213              pool_elts(vip->pod_indexes));
214
215   //Let's count the buckets for each POD
216   u32 *count = 0;
217   vec_validate(count, pool_len(kpm->pods)); //Possibly big alloc for not much...
218   kp_new_flow_entry_t *nfe;
219   vec_foreach(nfe, vip->new_flow_table)
220     count[nfe->pod_index]++;
221
222   kp_pod_t *pod;
223   u32 *pod_index;
224   pool_foreach(pod_index, vip->pod_indexes, {
225       pod = &kpm->pods[*pod_index];
226       s = format(s, "%U    %U %d buckets   %d flows  dpo:%u %s\n",
227                    format_white_space, indent,
228                    format_ip46_address, &pod->address, IP46_TYPE_ANY,
229                    count[pod - kpm->pods],
230                    vlib_refcount_get(&kpm->pod_refcount, pod - kpm->pods),
231                    pod->dpo.dpoi_index,
232                    (pod->flags & KP_POD_FLAGS_USED)?"used":" removed");
233   });
234
235   vec_free(count);
236
237   /*
238   s = format(s, "%U  new flows table:\n", format_white_space, indent);
239   kp_new_flow_entry_t *nfe;
240   vec_foreach(nfe, vip->new_flow_table) {
241     s = format(s, "%U    %d: %d\n", format_white_space, indent, nfe - vip->new_flow_table, nfe->pod_index);
242   }
243   */
244   return s;
245 }
246
247 typedef struct {
248   u32 pod_index;
249   u32 last;
250   u32 skip;
251 } kp_pseudorand_t;
252
253 static int kp_pseudorand_compare(void *a, void *b)
254 {
255   kp_pod_t *poda, *podb;
256   kp_main_t *kpm = &kp_main;
257   poda = &kpm->pods[((kp_pseudorand_t *)a)->pod_index];
258   podb = &kpm->pods[((kp_pseudorand_t *)b)->pod_index];
259   return memcmp(&poda->address, &podb->address, sizeof(podb->address));
260 }
261
262 static void kp_vip_garbage_collection(kp_vip_t *vip)
263 {
264   kp_main_t *kpm = &kp_main;
265   ASSERT (kpm->writer_lock[0]);
266
267   u32 now = (u32) vlib_time_now(vlib_get_main());
268   if (!clib_u32_loop_gt(now, vip->last_garbage_collection + KP_GARBAGE_RUN))
269     return;
270
271   vip->last_garbage_collection = now;
272   kp_pod_t *pod;
273   u32 *pod_index;
274   pool_foreach(pod_index, vip->pod_indexes, {
275       pod = &kpm->pods[*pod_index];
276       if (!(pod->flags & KP_POD_FLAGS_USED) && //Not used
277           clib_u32_loop_gt(now, pod->last_used + KP_CONCURRENCY_TIMEOUT) && //Not recently used
278           (vlib_refcount_get(&kpm->pod_refcount, pod - kpm->pods) == 0))
279         { //Not referenced
280           fib_entry_child_remove(pod->next_hop_fib_entry_index,
281                                  pod->next_hop_child_index);
282           fib_table_entry_delete_index(pod->next_hop_fib_entry_index,
283                                        FIB_SOURCE_RR);
284           pod->next_hop_fib_entry_index = FIB_NODE_INDEX_INVALID;
285
286           pool_put(vip->pod_indexes, pod_index);
287           pool_put(kpm->pods, pod);
288         }
289   });
290 }
291
292 void kp_garbage_collection()
293 {
294   kp_main_t *kpm = &kp_main;
295   kp_get_writer_lock();
296   kp_vip_t *vip;
297   u32 *to_be_removed_vips = 0, *i;
298   pool_foreach(vip, kpm->vips, {
299       kp_vip_garbage_collection(vip);
300
301       if (!(vip->flags & KP_VIP_FLAGS_USED) &&
302           (pool_elts(vip->pod_indexes) == 0)) {
303         vec_add1(to_be_removed_vips, vip - kpm->vips);
304       }
305   });
306
307   vec_foreach(i, to_be_removed_vips) {
308     vip = &kpm->vips[*i];
309     pool_put(kpm->vips, vip);
310     pool_free(vip->pod_indexes);
311   }
312
313   vec_free(to_be_removed_vips);
314   kp_put_writer_lock();
315 }
316
317 static void kp_vip_update_new_flow_table(kp_vip_t *vip)
318 {
319   kp_main_t *kpm = &kp_main;
320   kp_new_flow_entry_t *old_table;
321   u32 i, *pod_index;
322   kp_new_flow_entry_t *new_flow_table = 0;
323   kp_pod_t *pod;
324   kp_pseudorand_t *pr, *sort_arr = 0;
325   u32 count;
326
327   ASSERT (kpm->writer_lock[0]); //We must have the lock
328
329   //Check if some POD is configured or not
330   i = 0;
331   pool_foreach(pod_index, vip->pod_indexes, {
332       pod = &kpm->pods[*pod_index];
333       if (pod->flags & KP_POD_FLAGS_USED) { //Not used anymore
334         i = 1;
335         goto out; //Not sure 'break' works in this macro-loop
336       }
337   });
338
339 out:
340   if (i == 0) {
341     //Only the default. i.e. no POD
342     vec_validate(new_flow_table, vip->new_flow_table_mask);
343     for (i=0; i<vec_len(new_flow_table); i++)
344       new_flow_table[i].pod_index = 0;
345
346     goto finished;
347   }
348
349   //First, let's sort the PODs
350   sort_arr = 0;
351   vec_alloc(sort_arr, pool_elts(vip->pod_indexes));
352
353   i = 0;
354   pool_foreach(pod_index, vip->pod_indexes, {
355       pod = &kpm->pods[*pod_index];
356       if (!(pod->flags & KP_POD_FLAGS_USED)) //Not used anymore
357         continue;
358
359       sort_arr[i].pod_index = pod - kpm->pods;
360       i++;
361   });
362   _vec_len(sort_arr) = i;
363
364   vec_sort_with_function(sort_arr, kp_pseudorand_compare);
365
366   //Now let's pseudo-randomly generate permutations
367   vec_foreach(pr, sort_arr) {
368     kp_pod_t *pod = &kpm->pods[pr->pod_index];
369
370     u64 seed = clib_xxhash(pod->address.as_u64[0] ^
371                            pod->address.as_u64[1]);
372     /* We have 2^n buckets.
373      * skip must be prime with 2^n.
374      * So skip must be odd.
375      * MagLev actually state that M should be prime,
376      * but this has a big computation cost (% operation).
377      * Using 2^n is more better (& operation).
378      */
379     pr->skip = ((seed & 0xffffffff) | 1) & vip->new_flow_table_mask;
380     pr->last = (seed >> 32) & vip->new_flow_table_mask;
381   }
382
383   //Let's create a new flow table
384   vec_validate(new_flow_table, vip->new_flow_table_mask);
385   for (i=0; i<vec_len(new_flow_table); i++)
386     new_flow_table[i].pod_index = ~0;
387
388   u32 done = 0;
389   while (1) {
390     vec_foreach(pr, sort_arr) {
391       while (1) {
392         u32 last = pr->last;
393         pr->last = (pr->last + pr->skip) & vip->new_flow_table_mask;
394         if (new_flow_table[last].pod_index == ~0) {
395           new_flow_table[last].pod_index = pr->pod_index;
396           break;
397         }
398       }
399       done++;
400       if (done == vec_len(new_flow_table))
401         goto finished;
402     }
403   }
404
405   vec_free(sort_arr);
406
407 finished:
408
409 //Count number of changed entries
410   count = 0;
411   for (i=0; i<vec_len(new_flow_table); i++)
412     if (vip->new_flow_table == 0 ||
413         new_flow_table[i].pod_index != vip->new_flow_table[i].pod_index)
414       count++;
415
416   old_table = vip->new_flow_table;
417   vip->new_flow_table = new_flow_table;
418   vec_free(old_table);
419 }
420
421 int kp_conf(u32 per_cpu_sticky_buckets, u32 flow_timeout)
422 {
423   kp_main_t *kpm = &kp_main;
424
425   if (!is_pow2(per_cpu_sticky_buckets))
426     return VNET_API_ERROR_INVALID_MEMORY_SIZE;
427
428   kp_get_writer_lock(); //Not exactly necessary but just a reminder that it exists for my future self
429   kpm->per_cpu_sticky_buckets = per_cpu_sticky_buckets;
430   kpm->flow_timeout = flow_timeout;
431   kp_put_writer_lock();
432   return 0;
433 }
434
435 static
436 int kp_vip_find_index_with_lock(ip46_address_t *prefix, u8 plen, u32 *vip_index)
437 {
438   kp_main_t *kpm = &kp_main;
439   kp_vip_t *vip;
440   ASSERT (kpm->writer_lock[0]); //This must be called with the lock owned
441   ip46_prefix_normalize(prefix, plen);
442   pool_foreach(vip, kpm->vips, {
443       if ((vip->flags & KP_POD_FLAGS_USED) &&
444           vip->plen == plen &&
445           vip->prefix.as_u64[0] == prefix->as_u64[0] &&
446           vip->prefix.as_u64[1] == prefix->as_u64[1]) {
447         *vip_index = vip - kpm->vips;
448         return 0;
449       }
450   });
451   return VNET_API_ERROR_NO_SUCH_ENTRY;
452 }
453
454 int kp_vip_find_index(ip46_address_t *prefix, u8 plen, u32 *vip_index)
455 {
456   int ret;
457   kp_get_writer_lock();
458   ret = kp_vip_find_index_with_lock(prefix, plen, vip_index);
459   kp_put_writer_lock();
460   return ret;
461 }
462
463 static int kp_pod_find_index_vip(kp_vip_t *vip, ip46_address_t *address, u32 *pod_index)
464 {
465   kp_main_t *kpm = &kp_main;
466   ASSERT (kpm->writer_lock[0]); //This must be called with the lock owned
467   kp_pod_t *pod;
468   u32 *podi;
469   pool_foreach(podi, vip->pod_indexes, {
470       pod = &kpm->pods[*podi];
471       if (pod->vip_index == (vip - kpm->vips) &&
472           pod->address.as_u64[0] == address->as_u64[0] &&
473           pod->address.as_u64[1] == address->as_u64[1]) {
474         *pod_index = pod - kpm->pods;
475         return 0;
476       }
477   });
478   return -1;
479 }
480
481 int kp_vip_add_pods(u32 vip_index, ip46_address_t *addresses, u32 n)
482 {
483   kp_main_t *kpm = &kp_main;
484   kp_get_writer_lock();
485   kp_vip_t *vip;
486   if (!(vip = kp_vip_get_by_index(vip_index))) {
487     kp_put_writer_lock();
488     return VNET_API_ERROR_NO_SUCH_ENTRY;
489   }
490
491   ip46_type_t type = kp_vip_is_nat4(vip)?IP46_TYPE_IP4:IP46_TYPE_IP6;
492   u32 *to_be_added = 0;
493   u32 *to_be_updated = 0;
494   u32 i;
495   u32 *ip;
496   kp_snat_mapping_t *m;
497   kp_snat4_key_t m_key4;
498   clib_bihash_kv_8_8_t kv;
499
500   //Sanity check
501   while (n--) {
502
503     if (!kp_pod_find_index_vip(vip, &addresses[n], &i)) {
504       if (kpm->pods[i].flags & KP_POD_FLAGS_USED) {
505         vec_free(to_be_added);
506         vec_free(to_be_updated);
507         kp_put_writer_lock();
508         return VNET_API_ERROR_VALUE_EXIST;
509       }
510       vec_add1(to_be_updated, i);
511       goto next;
512     }
513
514     if (ip46_address_type(&addresses[n]) != type) {
515       vec_free(to_be_added);
516       vec_free(to_be_updated);
517       kp_put_writer_lock();
518       return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
519     }
520
521     if (n) {
522       u32 n2 = n;
523       while(n2--) //Check for duplicates
524         if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
525             addresses[n2].as_u64[1] == addresses[n].as_u64[1])
526           goto next;
527     }
528
529     vec_add1(to_be_added, n);
530
531 next:
532     continue;
533   }
534
535   //Update reused PODs
536   vec_foreach(ip, to_be_updated) {
537     kpm->pods[*ip].flags = KP_POD_FLAGS_USED;
538   }
539   vec_free(to_be_updated);
540
541   //Create those who have to be created
542   vec_foreach(ip, to_be_added) {
543     kp_pod_t *pod;
544     u32 *pod_index;
545     pool_get(kpm->pods, pod);
546     pod->address = addresses[*ip];
547     pod->flags = KP_POD_FLAGS_USED;
548     pod->vip_index = vip_index;
549     pool_get(vip->pod_indexes, pod_index);
550     *pod_index = pod - kpm->pods;
551
552     /*
553      * become a child of the FIB entry
554      * so we are informed when its forwarding changes
555      */
556     fib_prefix_t nh = {};
557     if (kp_vip_is_nat4(vip)) {
558         nh.fp_addr.ip4 = pod->address.ip4;
559         nh.fp_len = 32;
560         nh.fp_proto = FIB_PROTOCOL_IP4;
561     } else {
562         nh.fp_addr.ip6 = pod->address.ip6;
563         nh.fp_len = 128;
564         nh.fp_proto = FIB_PROTOCOL_IP6;
565     }
566
567     pod->next_hop_fib_entry_index =
568         fib_table_entry_special_add(0,
569                                     &nh,
570                                     FIB_SOURCE_RR,
571                                     FIB_ENTRY_FLAG_NONE);
572     pod->next_hop_child_index =
573         fib_entry_child_add(pod->next_hop_fib_entry_index,
574                             kpm->fib_node_type,
575                             pod - kpm->pods);
576
577     kp_pod_stack(pod);
578
579     /* Add SNAT static mapping */
580     pool_get (kpm->snat_mappings, m);
581     memset (m, 0, sizeof (*m));
582     if (kp_vip_is_nat4(vip)) {
583         m_key4.addr = pod->address.ip4;
584         m_key4.port = vip->target_port;
585         m_key4.protocol = 0;
586         m_key4.fib_index = 0;
587
588         m->vip.ip4 = vip->prefix.ip4;;
589         m->node_ip.ip4.as_u32 = 0;
590         m->pod_ip.ip4 = pod->address.ip4;
591         m->vip_is_ipv6 = 0;
592         m->node_ip_is_ipv6 = 0;
593         m->pod_ip_is_ipv6 = 0;
594         m->port = vip->port;
595         m->node_port = vip->node_port;
596         m->target_port = vip->target_port;
597         m->vrf_id = 0;
598         m->fib_index = 0;
599
600         kv.key = m_key4.as_u64;
601         kv.value = m - kpm->snat_mappings;
602         clib_bihash_add_del_8_8(&kpm->mapping_by_pod, &kv, 1);
603     } else {
604         /* TBD */
605     }
606
607   }
608   vec_free(to_be_added);
609
610   //Recompute flows
611   kp_vip_update_new_flow_table(vip);
612
613   //Garbage collection maybe
614   kp_vip_garbage_collection(vip);
615
616   kp_put_writer_lock();
617   return 0;
618 }
619
620 int kp_vip_del_pods_withlock(u32 vip_index, ip46_address_t *addresses, u32 n)
621 {
622   kp_main_t *kpm = &kp_main;
623   u32 now = (u32) vlib_time_now(vlib_get_main());
624   u32 *ip = 0;
625
626   kp_vip_t *vip;
627   if (!(vip = kp_vip_get_by_index(vip_index))) {
628     return VNET_API_ERROR_NO_SUCH_ENTRY;
629   }
630
631   u32 *indexes = NULL;
632   while (n--) {
633     u32 i;
634     if (kp_pod_find_index_vip(vip, &addresses[n], &i)) {
635       vec_free(indexes);
636       return VNET_API_ERROR_NO_SUCH_ENTRY;
637     }
638
639     if (n) { //Check for duplicates
640       u32 n2 = n - 1;
641       while(n2--) {
642         if (addresses[n2].as_u64[0] == addresses[n].as_u64[0] &&
643             addresses[n2].as_u64[1] == addresses[n].as_u64[1])
644           goto next;
645       }
646     }
647
648     vec_add1(indexes, i);
649 next:
650   continue;
651   }
652
653   //Garbage collection maybe
654   kp_vip_garbage_collection(vip);
655
656   if (indexes != NULL) {
657     vec_foreach(ip, indexes) {
658       kpm->pods[*ip].flags &= ~KP_POD_FLAGS_USED;
659       kpm->pods[*ip].last_used = now;
660     }
661
662     //Recompute flows
663     kp_vip_update_new_flow_table(vip);
664   }
665
666   vec_free(indexes);
667   return 0;
668 }
669
670 int kp_vip_del_pods(u32 vip_index, ip46_address_t *addresses, u32 n)
671 {
672   kp_get_writer_lock();
673   int ret = kp_vip_del_pods_withlock(vip_index, addresses, n);
674   kp_put_writer_lock();
675   return ret;
676 }
677
678 /**
679  * Add the VIP adjacency to the ip4 or ip6 fib
680  */
681 static void kp_vip_add_adjacency(kp_main_t *kpm, kp_vip_t *vip)
682 {
683   dpo_proto_t proto = 0;
684   dpo_id_t dpo = DPO_INVALID;
685   fib_prefix_t pfx = {};
686   if (kp_vip_is_ip4(vip)) {
687       pfx.fp_addr.ip4 = vip->prefix.ip4;
688       pfx.fp_len = vip->plen - 96;
689       pfx.fp_proto = FIB_PROTOCOL_IP4;
690       proto = DPO_PROTO_IP4;
691   } else {
692       pfx.fp_addr.ip6 = vip->prefix.ip6;
693       pfx.fp_len = vip->plen;
694       pfx.fp_proto = FIB_PROTOCOL_IP6;
695       proto = DPO_PROTO_IP6;
696   }
697   dpo_set(&dpo, kp_vip_is_nat4(vip)?kpm->dpo_nat4_type:kpm->dpo_nat6_type,
698       proto, vip - kpm->vips);
699   fib_table_entry_special_dpo_add(0,
700                                   &pfx,
701                                   FIB_SOURCE_PLUGIN_HI,
702                                   FIB_ENTRY_FLAG_EXCLUSIVE,
703                                   &dpo);
704   dpo_reset(&dpo);
705 }
706
707 /**
708  * Deletes the adjacency podsociated with the VIP
709  */
710 static void kp_vip_del_adjacency(kp_main_t *kpm, kp_vip_t *vip)
711 {
712   fib_prefix_t pfx = {};
713   if (kp_vip_is_ip4(vip)) {
714       pfx.fp_addr.ip4 = vip->prefix.ip4;
715       pfx.fp_len = vip->plen - 96;
716       pfx.fp_proto = FIB_PROTOCOL_IP4;
717   } else {
718       pfx.fp_addr.ip6 = vip->prefix.ip6;
719       pfx.fp_len = vip->plen;
720       pfx.fp_proto = FIB_PROTOCOL_IP6;
721   }
722   fib_table_entry_special_remove(0, &pfx, FIB_SOURCE_PLUGIN_HI);
723 }
724
725 int kp_vip_add(ip46_address_t *prefix, u8 plen, kp_vip_type_t type,
726                u32 new_length, u32 *vip_index,
727                u16 port, u16 target_port, u16 node_port)
728 {
729   kp_main_t *kpm = &kp_main;
730   vlib_main_t *vm = kpm->vlib_main;
731   kp_vip_t *vip;
732   u32 key, *key_copy;
733   uword * entry;
734
735   kp_get_writer_lock();
736   ip46_prefix_normalize(prefix, plen);
737
738   if (!kp_vip_find_index_with_lock(prefix, plen, vip_index)) {
739     kp_put_writer_lock();
740     return VNET_API_ERROR_VALUE_EXIST;
741   }
742
743   if (!is_pow2(new_length)) {
744     kp_put_writer_lock();
745     return VNET_API_ERROR_INVALID_MEMORY_SIZE;
746   }
747
748   if (ip46_prefix_is_ip4(prefix, plen) &&
749       (type != KP_VIP_TYPE_IP4_NAT44) &&
750       (type != KP_VIP_TYPE_IP4_NAT46)) {
751     kp_put_writer_lock();
752     return VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
753   }
754
755
756   //Allocate
757   pool_get(kpm->vips, vip);
758
759   //Init
760   vip->prefix = *prefix;
761   vip->plen = plen;
762   vip->port = clib_host_to_net_u16(port);
763   vip->target_port = clib_host_to_net_u16(target_port);
764   vip->node_port = clib_host_to_net_u16(node_port);
765   vip->last_garbage_collection = (u32) vlib_time_now(vlib_get_main());
766   vip->type = type;
767   vip->flags = KP_VIP_FLAGS_USED;
768   vip->pod_indexes = 0;
769
770   //Validate counters
771   u32 i;
772   for (i = 0; i < KP_N_VIP_COUNTERS; i++) {
773     vlib_validate_simple_counter(&kpm->vip_counters[i], vip - kpm->vips);
774     vlib_zero_simple_counter(&kpm->vip_counters[i], vip - kpm->vips);
775   }
776
777   //Configure new flow table
778   vip->new_flow_table_mask = new_length - 1;
779   vip->new_flow_table = 0;
780
781   //Create a new flow hash table full of the default entry
782   kp_vip_update_new_flow_table(vip);
783
784   //Create adjacency to direct traffic
785   kp_vip_add_adjacency(kpm, vip);
786
787   //Create maping from nodeport to vip_index
788   key = clib_host_to_net_u16(node_port);
789   entry = hash_get_mem (kpm->nodeport_by_key, &key);
790   if (entry) {
791     kp_put_writer_lock();
792     return VNET_API_ERROR_VALUE_EXIST;
793   }
794
795   key_copy = clib_mem_alloc (sizeof (*key_copy));
796   clib_memcpy (key_copy, &key, sizeof (*key_copy));
797   hash_set_mem (kpm->nodeport_by_key, key_copy, vip - kpm->vips);
798
799   /* receive packets destined to NodeIP:NodePort */
800   udp_register_dst_port (vm, node_port, kp4_nodeport_node.index, 1);
801   udp_register_dst_port (vm, node_port, kp6_nodeport_node.index, 0);
802
803   //Return result
804   *vip_index = vip - kpm->vips;
805
806   kp_put_writer_lock();
807   return 0;
808 }
809
810 int kp_vip_del(u32 vip_index)
811 {
812   kp_main_t *kpm = &kp_main;
813   kp_vip_t *vip;
814   kp_get_writer_lock();
815   if (!(vip = kp_vip_get_by_index(vip_index))) {
816     kp_put_writer_lock();
817     return VNET_API_ERROR_NO_SUCH_ENTRY;
818   }
819
820   //FIXME: This operation is actually not working
821   //We will need to remove state before performing this.
822
823   {
824     //Remove all PODs
825     ip46_address_t *pods = 0;
826     kp_pod_t *pod;
827     u32 *pod_index;
828     pool_foreach(pod_index, vip->pod_indexes, {
829         pod = &kpm->pods[*pod_index];
830         vec_add1(pods, pod->address);
831     });
832     if (vec_len(pods))
833       kp_vip_del_pods_withlock(vip_index, pods, vec_len(pods));
834     vec_free(pods);
835   }
836
837   //Delete adjacency
838   kp_vip_del_adjacency(kpm, vip);
839
840   //Set the VIP pod unused
841   vip->flags &= ~KP_VIP_FLAGS_USED;
842
843   kp_put_writer_lock();
844   return 0;
845 }
846
847 /* *INDENT-OFF* */
848 VLIB_PLUGIN_REGISTER () = {
849     .version = VPP_BUILD_VER,
850     .description = "kube-proxy data plane",
851 };
852 /* *INDENT-ON* */
853
854 u8 *format_kp_dpo (u8 * s, va_list * va)
855 {
856   index_t index = va_arg (*va, index_t);
857   CLIB_UNUSED(u32 indent) = va_arg (*va, u32);
858   kp_main_t *kpm = &kp_main;
859   kp_vip_t *vip = pool_elt_at_index (kpm->vips, index);
860   return format (s, "%U", format_kp_vip, vip);
861 }
862
863 static void kp_dpo_lock (dpo_id_t *dpo) {}
864 static void kp_dpo_unlock (dpo_id_t *dpo) {}
865
866 static fib_node_t *
867 kp_fib_node_get_node (fib_node_index_t index)
868 {
869   kp_main_t *kpm = &kp_main;
870   kp_pod_t *pod = pool_elt_at_index (kpm->pods, index);
871   return (&pod->fib_node);
872 }
873
874 static void
875 kp_fib_node_last_lock_gone (fib_node_t *node)
876 {
877 }
878
879 static kp_pod_t *
880 kp_pod_from_fib_node (fib_node_t *node)
881 {
882   return ((kp_pod_t*)(((char*)node) -
883       STRUCT_OFFSET_OF(kp_pod_t, fib_node)));
884 }
885
886 static void
887 kp_pod_stack (kp_pod_t *pod)
888 {
889   kp_main_t *kpm = &kp_main;
890   kp_vip_t *vip = &kpm->vips[pod->vip_index];
891   dpo_stack(kp_vip_is_nat4(vip)?kpm->dpo_nat4_type:kpm->dpo_nat6_type,
892             kp_vip_is_ip4(vip)?DPO_PROTO_IP4:DPO_PROTO_IP6,
893             &pod->dpo,
894             fib_entry_contribute_ip_forwarding(
895                 pod->next_hop_fib_entry_index));
896 }
897
898 static fib_node_back_walk_rc_t
899 kp_fib_node_back_walk_notify (fib_node_t *node,
900                                fib_node_back_walk_ctx_t *ctx)
901 {
902     kp_pod_stack(kp_pod_from_fib_node(node));
903     return (FIB_NODE_BACK_WALK_CONTINUE);
904 }
905
906 int kp_nat4_interface_add_del (u32 sw_if_index, int is_del)
907 {
908   if (is_del)
909     {
910       vnet_feature_enable_disable ("ip4-unicast", "kp-nat4-in2out",
911                                    sw_if_index, 0, 0, 0);
912     }
913   else
914     {
915       vnet_feature_enable_disable ("ip4-unicast", "kp-nat4-in2out",
916                                    sw_if_index, 1, 0, 0);
917     }
918
919   return 0;
920 }
921
922 clib_error_t *
923 kp_init (vlib_main_t * vm)
924 {
925   vlib_thread_main_t *tm = vlib_get_thread_main ();
926   kp_main_t *kpm = &kp_main;
927   kpm->vnet_main = vnet_get_main ();
928   kpm->vlib_main = vm;
929
930   kp_pod_t *default_pod;
931   fib_node_vft_t kp_fib_node_vft = {
932       .fnv_get = kp_fib_node_get_node,
933       .fnv_last_lock = kp_fib_node_last_lock_gone,
934       .fnv_back_walk = kp_fib_node_back_walk_notify,
935   };
936   dpo_vft_t kp_vft = {
937       .dv_lock = kp_dpo_lock,
938       .dv_unlock = kp_dpo_unlock,
939       .dv_format = format_kp_dpo,
940   };
941
942   kpm->vips = 0;
943   kpm->per_cpu = 0;
944   vec_validate(kpm->per_cpu, tm->n_vlib_mains - 1);
945   kpm->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,  CLIB_CACHE_LINE_BYTES);
946   kpm->writer_lock[0] = 0;
947   kpm->per_cpu_sticky_buckets = KP_DEFAULT_PER_CPU_STICKY_BUCKETS;
948   kpm->flow_timeout = KP_DEFAULT_FLOW_TIMEOUT;
949   kpm->dpo_nat4_type = dpo_register_new_type(&kp_vft, kp_dpo_nat4_nodes);
950   kpm->dpo_nat6_type = dpo_register_new_type(&kp_vft, kp_dpo_nat6_nodes);
951   kpm->fib_node_type = fib_node_register_new_type(&kp_fib_node_vft);
952
953   //Init POD reference counters
954   vlib_refcount_init(&kpm->pod_refcount);
955
956   //Allocate and init default POD.
957   kpm->pods = 0;
958   pool_get(kpm->pods, default_pod);
959   default_pod->flags = 0;
960   default_pod->dpo.dpoi_next_node = KP_NEXT_DROP;
961   default_pod->vip_index = ~0;
962   default_pod->address.ip6.as_u64[0] = 0xffffffffffffffffL;
963   default_pod->address.ip6.as_u64[1] = 0xffffffffffffffffL;
964
965   kpm->nodeport_by_key
966     = hash_create_mem (0, sizeof(u16), sizeof (uword));
967
968   clib_bihash_init_8_8 (&kpm->mapping_by_pod,
969                         "mapping_by_pod", KP_MAPPING_BUCKETS,
970                         KP_MAPPING_MEMORY_SIZE);
971
972 #define _(a,b,c) kpm->vip_counters[c].name = b;
973   kp_foreach_vip_counter
974 #undef _
975   return NULL;
976 }
977
978 VLIB_INIT_FUNCTION (kp_init);