Fix vpp crash when adding macip acl
[vpp.git] / src / plugins / acl / acl_test.c
index 39d7e1c..2b51758 100644 (file)
@@ -144,9 +144,9 @@ static void vl_api_acl_interface_list_details_t_handler
        out = format(out, "sw_if_index: %d, count: %d, n_input: %d\n", mp->sw_if_index, mp->count, mp->n_input);
         out = format(out, "   input ");
        for(i=0; i<mp->count; i++) {
-          out = format(out, "%d ", mp->acls[i]);
-          if (i == mp->n_input-1)
+          if (i == mp->n_input)
             out = format(out, "\n  output ");
+         out = format(out, "%d ", ntohl (mp->acls[i]));
        }
         out = format(out, "\n");
         clib_warning("%s", out);
@@ -164,14 +164,14 @@ vl_api_acl_rule_t_pretty_format (u8 *out, vl_api_acl_rule_t * a)
   inet_ntop(af, a->src_ip_addr, (void *)src, sizeof(src));
   inet_ntop(af, a->dst_ip_addr, (void *)dst, sizeof(dst));
 
-  out = format(out, "%s action %d src %s/%d dst %s/%d proto %d sport %d-%d dport %d-%d tcpflags %d %d",
+  out = format(out, "%s action %d src %s/%d dst %s/%d proto %d sport %d-%d dport %d-%d tcpflags %d mask %d",
                      a->is_ipv6 ? "ipv6" : "ipv4", a->is_permit,
                      src, a->src_ip_prefix_len,
                      dst, a->dst_ip_prefix_len,
                      a->proto,
                      a->srcport_or_icmptype_first, a->srcport_or_icmptype_last,
                     a->dstport_or_icmpcode_first, a->dstport_or_icmpcode_last,
-                     a->tcp_flags_mask, a->tcp_flags_value);
+                     a->tcp_flags_value, a->tcp_flags_mask);
   return(out);
 }
 
@@ -243,6 +243,22 @@ static void vl_api_macip_acl_interface_get_reply_t_handler
         vam->result_ready = 1;
     }
 
+static void vl_api_acl_plugin_control_ping_reply_t_handler
+  (vl_api_acl_plugin_control_ping_reply_t * mp)
+{
+  vat_main_t *vam = &vat_main;
+  i32 retval = ntohl (mp->retval);
+  if (vam->async_mode)
+    {
+      vam->async_errors += (retval < 0);
+    }
+  else
+    {
+      vam->retval = retval;
+      vam->result_ready = 1;
+    }
+}
+
 
 /*
  * Table of message reply handlers, must include boilerplate handlers
@@ -260,6 +276,7 @@ _(MACIP_ACL_DEL_REPLY, macip_acl_del_reply) \
 _(MACIP_ACL_DETAILS, macip_acl_details)  \
 _(MACIP_ACL_INTERFACE_ADD_DEL_REPLY, macip_acl_interface_add_del_reply)  \
 _(MACIP_ACL_INTERFACE_GET_REPLY, macip_acl_interface_get_reply)  \
+_(ACL_PLUGIN_CONTROL_PING_REPLY, acl_plugin_control_ping_reply) \
 _(ACL_PLUGIN_GET_VERSION_REPLY, acl_plugin_get_version_reply)
 
 static int api_acl_plugin_get_version (vat_main_t * vam)
@@ -267,6 +284,7 @@ static int api_acl_plugin_get_version (vat_main_t * vam)
     acl_test_main_t * sm = &acl_test_main;
     vl_api_acl_plugin_get_version_t * mp;
     u32 msg_size = sizeof(*mp);
+    int ret;
 
     vam->result_ready = 0;
     mp = vl_msg_api_alloc_as_if_client(msg_size);
@@ -278,9 +296,8 @@ static int api_acl_plugin_get_version (vat_main_t * vam)
     S(mp);
 
     /* Wait for a reply... */
-    W;
-
-    return 0;
+    W (ret);
+    return ret;
 }
 
 static int api_macip_acl_interface_get (vat_main_t * vam)
@@ -288,6 +305,7 @@ static int api_macip_acl_interface_get (vat_main_t * vam)
     acl_test_main_t * sm = &acl_test_main;
     vl_api_acl_plugin_get_version_t * mp;
     u32 msg_size = sizeof(*mp);
+    int ret;
 
     vam->result_ready = 0;
     mp = vl_msg_api_alloc_as_if_client(msg_size);
@@ -299,9 +317,8 @@ static int api_macip_acl_interface_get (vat_main_t * vam)
     S(mp);
 
     /* Wait for a reply... */
-    W;
-
-    return 0;
+    W (ret);
+    return ret;
 }
 
 #define vec_validate_acl_rules(v, idx) \
@@ -326,6 +343,7 @@ static int api_acl_add_replace (vat_main_t * vam)
     vl_api_acl_rule_t *rules = 0;
     int rule_idx = 0;
     int n_rules = 0;
+    int n_rules_override = -1;
     u32 proto = 0;
     u32 port1 = 0;
     u32 port2 = 0;
@@ -335,6 +353,7 @@ static int api_acl_add_replace (vat_main_t * vam)
     ip4_address_t src_v4address, dst_v4address;
     ip6_address_t src_v6address, dst_v6address;
     u8 *tag = 0;
+    int ret;
 
     if (!unformat (i, "%d", &acl_index)) {
        /* Just assume -1 */
@@ -362,6 +381,15 @@ static int api_acl_add_replace (vat_main_t * vam)
             vec_validate_acl_rules(rules, rule_idx);
             rules[rule_idx].is_permit = 1;
           }
+        else if (unformat (i, "deny"))
+          {
+            vec_validate_acl_rules(rules, rule_idx);
+            rules[rule_idx].is_permit = 0;
+          }
+        else if (unformat (i, "count %d", &n_rules_override))
+          {
+            /* we will use this later */
+          }
         else if (unformat (i, "action %d", &action))
           {
             vec_validate_acl_rules(rules, rule_idx);
@@ -429,6 +457,12 @@ static int api_acl_add_replace (vat_main_t * vam)
             rules[rule_idx].tcp_flags_value = tcpflags;
             rules[rule_idx].tcp_flags_mask = tcpmask;
           }
+        else if (unformat (i, "tcpflags %d mask %d", &tcpflags, &tcpmask))
+          {
+            vec_validate_acl_rules(rules, rule_idx);
+            rules[rule_idx].tcp_flags_value = tcpflags;
+            rules[rule_idx].tcp_flags_mask = tcpmask;
+          }
         else if (unformat (i, "proto %d", &proto))
           {
             vec_validate_acl_rules(rules, rule_idx);
@@ -454,13 +488,16 @@ static int api_acl_add_replace (vat_main_t * vam)
     else
       n_rules = 0;
 
+    if (n_rules_override >= 0)
+      n_rules = n_rules_override;
+
     msg_size += n_rules*sizeof(rules[0]);
 
     mp = vl_msg_api_alloc_as_if_client(msg_size);
     memset (mp, 0, msg_size);
     mp->_vl_msg_id = ntohs (VL_API_ACL_ADD_REPLACE + sm->msg_id_base);
     mp->client_index = vam->my_client_index;
-    if (n_rules > 0)
+    if ((n_rules > 0) && rules)
       clib_memcpy(mp->r, rules, n_rules*sizeof (vl_api_acl_rule_t));
     if (tag)
       {
@@ -479,7 +516,8 @@ static int api_acl_add_replace (vat_main_t * vam)
     S(mp);
 
     /* Wait for a reply... */
-    W;
+    W (ret);
+    return ret;
 }
 
 static int api_acl_del (vat_main_t * vam)
@@ -487,6 +525,7 @@ static int api_acl_del (vat_main_t * vam)
     unformat_input_t * i = vam->input;
     vl_api_acl_del_t * mp;
     u32 acl_index = ~0;
+    int ret;
 
     if (!unformat (i, "%d", &acl_index)) {
       errmsg ("missing acl index\n");
@@ -501,7 +540,8 @@ static int api_acl_del (vat_main_t * vam)
     S(mp);
 
     /* Wait for a reply... */
-    W;
+    W (ret);
+    return ret;
 }
 
 static int api_macip_acl_del (vat_main_t * vam)
@@ -509,6 +549,7 @@ static int api_macip_acl_del (vat_main_t * vam)
     unformat_input_t * i = vam->input;
     vl_api_acl_del_t * mp;
     u32 acl_index = ~0;
+    int ret;
 
     if (!unformat (i, "%d", &acl_index)) {
       errmsg ("missing acl index\n");
@@ -523,7 +564,8 @@ static int api_macip_acl_del (vat_main_t * vam)
     S(mp);
 
     /* Wait for a reply... */
-    W;
+    W (ret);
+    return ret;
 }
 
 static int api_acl_interface_add_del (vat_main_t * vam)
@@ -534,6 +576,7 @@ static int api_acl_interface_add_del (vat_main_t * vam)
     u32 acl_index = ~0;
     u8 is_input = 0;
     u8 is_add = 0;
+    int ret;
 
 //    acl_interface_add_del <intfc> | sw_if_index <if-idx> acl_index <acl-idx> [out] [del]
 
@@ -589,7 +632,8 @@ static int api_acl_interface_add_del (vat_main_t * vam)
     S(mp);
 
     /* Wait for a reply... */
-    W;
+    W (ret);
+    return ret;
 }
 
 static int api_macip_acl_interface_add_del (vat_main_t * vam)
@@ -599,6 +643,7 @@ static int api_macip_acl_interface_add_del (vat_main_t * vam)
     u32 sw_if_index = ~0;
     u32 acl_index = ~0;
     u8 is_add = 0;
+    int ret;
 
     /* Parse args required to build the message */
     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
@@ -638,7 +683,8 @@ static int api_macip_acl_interface_add_del (vat_main_t * vam)
     S(mp);
 
     /* Wait for a reply... */
-    W;
+    W (ret);
+    return ret;
 }
 
 static int api_acl_interface_set_acl_list (vat_main_t * vam)
@@ -650,6 +696,7 @@ static int api_acl_interface_set_acl_list (vat_main_t * vam)
     u32 *inacls = 0;
     u32 *outacls = 0;
     u8 is_input = 0;
+    int ret;
 
 //  acl_interface_set_acl_list <intfc> | sw_if_index <if-idx> input [acl-idx list] output [acl-idx list]
 
@@ -694,7 +741,17 @@ static int api_acl_interface_set_acl_list (vat_main_t * vam)
     S(mp);
 
     /* Wait for a reply... */
-    W;
+    W (ret);
+    return ret;
+}
+
+static void
+api_acl_send_control_ping(vat_main_t *vam)
+{
+  vl_api_acl_plugin_control_ping_t *mp_ping;
+
+  M(ACL_PLUGIN_CONTROL_PING, mp_ping);
+  S(mp_ping);
 }
 
 
@@ -703,6 +760,7 @@ static int api_acl_interface_list_dump (vat_main_t * vam)
     unformat_input_t * i = vam->input;
     u32 sw_if_index = ~0;
     vl_api_acl_interface_list_dump_t * mp;
+    int ret;
 
     /* Parse args required to build the message */
     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
@@ -721,8 +779,12 @@ static int api_acl_interface_list_dump (vat_main_t * vam)
     /* send it... */
     S(mp);
 
+    /* Use control ping for synchronization */
+    api_acl_send_control_ping(vam);
+
     /* Wait for a reply... */
-    W;
+    W (ret);
+    return ret;
 }
 
 static int api_acl_dump (vat_main_t * vam)
@@ -730,6 +792,7 @@ static int api_acl_dump (vat_main_t * vam)
     unformat_input_t * i = vam->input;
     u32 acl_index = ~0;
     vl_api_acl_dump_t * mp;
+    int ret;
 
     /* Parse args required to build the message */
     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
@@ -746,8 +809,12 @@ static int api_acl_dump (vat_main_t * vam)
     /* send it... */
     S(mp);
 
+    /* Use control ping for synchronization */
+    api_acl_send_control_ping(vam);
+
     /* Wait for a reply... */
-    W;
+    W (ret);
+    return ret;
 }
 
 static int api_macip_acl_dump (vat_main_t * vam)
@@ -755,6 +822,7 @@ static int api_macip_acl_dump (vat_main_t * vam)
     unformat_input_t * i = vam->input;
     u32 acl_index = ~0;
     vl_api_acl_dump_t * mp;
+    int ret;
 
     /* Parse args required to build the message */
     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
@@ -771,8 +839,12 @@ static int api_macip_acl_dump (vat_main_t * vam)
     /* send it... */
     S(mp);
 
+    /* Use control ping for synchronization */
+    api_acl_send_control_ping(vam);
+
     /* Wait for a reply... */
-    W;
+    W (ret);
+    return ret;
 }
 
 #define vec_validate_macip_acl_rules(v, idx) \
@@ -794,6 +866,7 @@ static int api_macip_acl_add (vat_main_t * vam)
     vl_api_macip_acl_rule_t *rules = 0;
     int rule_idx = 0;
     int n_rules = 0;
+    int n_rules_override = -1;
     u32 src_prefix_length = 0;
     u32 action = 0;
     ip4_address_t src_v4address;
@@ -801,6 +874,7 @@ static int api_macip_acl_add (vat_main_t * vam)
     u8 src_mac[6];
     u8 *tag = 0;
     u8 mac_mask_all_1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
+    int ret;
 
     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
     {
@@ -812,7 +886,7 @@ static int api_macip_acl_add (vat_main_t * vam)
         else if (unformat (i, "ipv4"))
           {
             vec_validate_macip_acl_rules(rules, rule_idx);
-            rules[rule_idx].is_ipv6 = 1;
+            rules[rule_idx].is_ipv6 = 0;
           }
         else if (unformat (i, "permit"))
           {
@@ -824,22 +898,38 @@ static int api_macip_acl_add (vat_main_t * vam)
             vec_validate_macip_acl_rules(rules, rule_idx);
             rules[rule_idx].is_permit = 0;
           }
+        else if (unformat (i, "count %d", &n_rules_override))
+          {
+            /* we will use this later */
+          }
         else if (unformat (i, "action %d", &action))
           {
             vec_validate_macip_acl_rules(rules, rule_idx);
             rules[rule_idx].is_permit = action;
           }
         else if (unformat (i, "ip %U/%d",
-         unformat_ip4_address, &src_v4address, &src_prefix_length))
+         unformat_ip4_address, &src_v4address, &src_prefix_length) ||
+                 unformat (i, "ip %U",
+         unformat_ip4_address, &src_v4address))
           {
+            if (src_prefix_length == 0)
+              src_prefix_length = 32;
             vec_validate_macip_acl_rules(rules, rule_idx);
             memcpy (rules[rule_idx].src_ip_addr, &src_v4address, 4);
             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
             rules[rule_idx].is_ipv6 = 0;
           }
+        else if (unformat (i, "src"))
+          {
+            /* Everything in MACIP is "source" but allow this verbosity */
+          }
         else if (unformat (i, "ip %U/%d",
-         unformat_ip6_address, &src_v6address, &src_prefix_length))
+         unformat_ip6_address, &src_v6address, &src_prefix_length) ||
+                 unformat (i, "ip %U",
+         unformat_ip6_address, &src_v6address))
           {
+            if (src_prefix_length == 0)
+              src_prefix_length = 128;
             vec_validate_macip_acl_rules(rules, rule_idx);
             memcpy (rules[rule_idx].src_ip_addr, &src_v6address, 16);
             rules[rule_idx].src_ip_prefix_len = src_prefix_length;
@@ -878,13 +968,16 @@ static int api_macip_acl_add (vat_main_t * vam)
     else
       n_rules = 0;
 
+    if (n_rules_override >= 0)
+      n_rules = n_rules_override;
+
     msg_size += n_rules*sizeof(rules[0]);
 
     mp = vl_msg_api_alloc_as_if_client(msg_size);
     memset (mp, 0, msg_size);
     mp->_vl_msg_id = ntohs (VL_API_MACIP_ACL_ADD + sm->msg_id_base);
     mp->client_index = vam->my_client_index;
-    if (n_rules > 0)
+    if ((n_rules > 0) && rules)
       clib_memcpy(mp->r, rules, n_rules*sizeof (mp->r[0]));
     if (tag)
       {
@@ -903,7 +996,8 @@ static int api_macip_acl_add (vat_main_t * vam)
     S(mp);
 
     /* Wait for a reply... */
-    W;
+    W (ret);
+    return ret;
 }
 
 /*