lb: add source ip based sticky load balancing
[vpp.git] / src / plugins / lb / api.c
index bf4a50d..9ccd7b5 100644 (file)
@@ -127,6 +127,80 @@ vl_api_lb_add_del_vip_t_handler
  REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_REPLY);
 }
 
+static void
+vl_api_lb_add_del_vip_v2_t_handler (vl_api_lb_add_del_vip_v2_t *mp)
+{
+  lb_main_t *lbm = &lb_main;
+  vl_api_lb_conf_reply_t *rmp;
+  int rv = 0;
+  lb_vip_add_args_t args;
+
+  /* if port == 0, it means all-port VIP */
+  if (mp->port == 0)
+    {
+      mp->protocol = ~0;
+    }
+
+  ip_address_decode (&mp->pfx.address, &(args.prefix));
+
+  if (mp->is_del)
+    {
+      u32 vip_index;
+      if (!(rv = lb_vip_find_index (&(args.prefix), mp->pfx.len, mp->protocol,
+                                   ntohs (mp->port), &vip_index)))
+       rv = lb_vip_del (vip_index);
+    }
+  else
+    {
+      u32 vip_index;
+      lb_vip_type_t type = 0;
+
+      if (ip46_prefix_is_ip4 (&(args.prefix), mp->pfx.len))
+       {
+         if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
+           type = LB_VIP_TYPE_IP4_GRE4;
+         else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
+           type = LB_VIP_TYPE_IP4_GRE6;
+         else if (mp->encap == LB_API_ENCAP_TYPE_L3DSR)
+           type = LB_VIP_TYPE_IP4_L3DSR;
+         else if (mp->encap == LB_API_ENCAP_TYPE_NAT4)
+           type = LB_VIP_TYPE_IP4_NAT4;
+       }
+      else
+       {
+         if (mp->encap == LB_API_ENCAP_TYPE_GRE4)
+           type = LB_VIP_TYPE_IP6_GRE4;
+         else if (mp->encap == LB_API_ENCAP_TYPE_GRE6)
+           type = LB_VIP_TYPE_IP6_GRE6;
+         else if (mp->encap == LB_API_ENCAP_TYPE_NAT6)
+           type = LB_VIP_TYPE_IP6_NAT6;
+       }
+
+      args.plen = mp->pfx.len;
+      args.protocol = mp->protocol;
+      args.port = ntohs (mp->port);
+      args.type = type;
+      args.new_length = ntohl (mp->new_flows_table_length);
+
+      if (mp->src_ip_sticky)
+       args.src_ip_sticky = 1;
+
+      if (mp->encap == LB_API_ENCAP_TYPE_L3DSR)
+       {
+         args.encap_args.dscp = (u8) (mp->dscp & 0x3F);
+       }
+      else if ((mp->encap == LB_API_ENCAP_TYPE_NAT4) ||
+              (mp->encap == LB_API_ENCAP_TYPE_NAT6))
+       {
+         args.encap_args.srv_type = mp->type;
+         args.encap_args.target_port = ntohs (mp->target_port);
+       }
+
+      rv = lb_vip_add (args, &vip_index);
+    }
+  REPLY_MACRO (VL_API_LB_ADD_DEL_VIP_V2_REPLY);
+}
+
 static void
 vl_api_lb_add_del_as_t_handler
 (vl_api_lb_add_del_as_t * mp)