API:replaced all REPLY_MACRO's with api_helper_macros.h
[vpp.git] / src / plugins / acl / acl.c
index 3fe084b..0d06531 100644 (file)
 
 acl_main_t acl_main;
 
-/*
- * A handy macro to set up a message reply.
- * Assumes that the following variables are available:
- * mp - pointer to request message
- * rmp - pointer to reply message type
- * rv - return value
- */
-
-#define REPLY_MACRO(t)                                          \
-do {                                                            \
-    unix_shared_memory_queue_t * q =                            \
-    vl_api_client_index_to_input_queue (mp->client_index);      \
-    if (!q)                                                     \
-        return;                                                 \
-                                                                \
-    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
-    rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
-    rmp->context = mp->context;                                 \
-    rmp->retval = ntohl(rv);                                    \
-                                                                \
-    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
-} while(0);
-
-#define REPLY_MACRO2(t, body)                                   \
-do {                                                            \
-    unix_shared_memory_queue_t * q;                             \
-    rv = vl_msg_api_pd_handler (mp, rv);                        \
-    q = vl_api_client_index_to_input_queue (mp->client_index);  \
-    if (!q)                                                     \
-        return;                                                 \
-                                                                \
-    rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
-    rmp->_vl_msg_id = ntohs((t)+am->msg_id_base);                               \
-    rmp->context = mp->context;                                 \
-    rmp->retval = ntohl(rv);                                    \
-    do {body;} while (0);                                       \
-    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
-} while(0);
-
-#define REPLY_MACRO3(t, n, body)                                \
-do {                                                            \
-    unix_shared_memory_queue_t * q;                             \
-    rv = vl_msg_api_pd_handler (mp, rv);                        \
-    q = vl_api_client_index_to_input_queue (mp->client_index);  \
-    if (!q)                                                     \
-        return;                                                 \
-                                                                \
-    rmp = vl_msg_api_alloc (sizeof (*rmp) + n);                 \
-    rmp->_vl_msg_id = ntohs((t)+am->msg_id_base);                               \
-    rmp->context = mp->context;                                 \
-    rmp->retval = ntohl(rv);                                    \
-    do {body;} while (0);                                       \
-    vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
-} while(0);
-
+#define REPLY_MSG_ID_BASE am->msg_id_base
+#include <vlibapi/api_helper_macros.h>
 
 /* List of message types that this plugin understands */
 
@@ -208,10 +155,10 @@ acl_add_list (u32 count, vl_api_acl_rule_t rules[],
       r->src_prefixlen = rules[i].src_ip_prefix_len;
       r->dst_prefixlen = rules[i].dst_ip_prefix_len;
       r->proto = rules[i].proto;
-      r->src_port_or_type_first = rules[i].srcport_or_icmptype_first;
-      r->src_port_or_type_last = rules[i].srcport_or_icmptype_last;
-      r->dst_port_or_code_first = rules[i].dstport_or_icmpcode_first;
-      r->dst_port_or_code_last = rules[i].dstport_or_icmpcode_last;
+      r->src_port_or_type_first = ntohs ( rules[i].srcport_or_icmptype_first );
+      r->src_port_or_type_last = ntohs ( rules[i].srcport_or_icmptype_last );
+      r->dst_port_or_code_first = ntohs ( rules[i].dstport_or_icmpcode_first );
+      r->dst_port_or_code_last = ntohs ( rules[i].dstport_or_icmpcode_last );
       r->tcp_flags_value = rules[i].tcp_flags_value;
       r->tcp_flags_mask = rules[i].tcp_flags_mask;
     }
@@ -839,8 +786,8 @@ acl_packet_match (acl_main_t * am, u32 acl_index, vlib_buffer_t * b0,
   int is_ip6;
   int is_ip4;
   u8 proto;
-  u16 src_port;
-  u16 dst_port;
+  u16 src_port = 0;
+  u16 dst_port = 0;
   u8 tcp_flags = 0;
   int i;
   acl_list_t *a;
@@ -866,15 +813,13 @@ acl_packet_match (acl_main_t * am, u32 acl_index, vlib_buffer_t * b0,
        {
          *trace_bitmap |= 0x00000001;
          /* type */
-         src_port = *(u8 *) get_ptr_to_offset (b0, 34);
+         src_port = ((u16) (*(u8 *) get_ptr_to_offset (b0, 34)));
          /* code */
-         dst_port = *(u8 *) get_ptr_to_offset (b0, 35);
-       }
-      else
-       {
+         dst_port = ((u16) (*(u8 *) get_ptr_to_offset (b0, 35)));
+       } else {
          /* assume TCP/UDP */
-         src_port = (*(u16 *) get_ptr_to_offset (b0, 34));
-         dst_port = (*(u16 *) get_ptr_to_offset (b0, 36));
+         src_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 34)));
+         dst_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 36)));
          /* UDP gets ability to check on an oddball data byte as a bonus */
          tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 20 + 13);
        }
@@ -888,15 +833,15 @@ acl_packet_match (acl_main_t * am, u32 acl_index, vlib_buffer_t * b0,
        {
          *trace_bitmap |= 0x00000002;
          /* type */
-         src_port = *(u8 *) get_ptr_to_offset (b0, 54);
+         src_port = (u16) (*(u8 *) get_ptr_to_offset (b0, 54));
          /* code */
-         dst_port = *(u8 *) get_ptr_to_offset (b0, 55);
+         dst_port = (u16) (*(u8 *) get_ptr_to_offset (b0, 55));
        }
       else
        {
          /* assume TCP/UDP */
-         src_port = (*(u16 *) get_ptr_to_offset (b0, 54));
-         dst_port = (*(u16 *) get_ptr_to_offset (b0, 56));
+         src_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 54)));
+         dst_port = ntohs ((u16) (*(u16 *) get_ptr_to_offset (b0, 56)));
          tcp_flags = *(u8 *) get_ptr_to_offset (b0, 14 + 40 + 13);
        }
     }
@@ -1411,7 +1356,7 @@ vl_api_acl_add_replace_t_handler (vl_api_acl_add_replace_t * mp)
 static void
 vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
 {
-  acl_main_t *sm = &acl_main;
+  acl_main_t *am = &acl_main;
   vl_api_acl_del_reply_t *rmp;
   int rv;
 
@@ -1423,8 +1368,8 @@ vl_api_acl_del_t_handler (vl_api_acl_del_t * mp)
 static void
 vl_api_acl_interface_add_del_t_handler (vl_api_acl_interface_add_del_t * mp)
 {
-  acl_main_t *sm = &acl_main;
-  vnet_interface_main_t *im = &sm->vnet_main->interface_main;
+  acl_main_t *am = &acl_main;
+  vnet_interface_main_t *im = &am->vnet_main->interface_main;
   u32 sw_if_index = ntohl (mp->sw_if_index);
   vl_api_acl_interface_add_del_reply_t *rmp;
   int rv = -1;
@@ -1443,11 +1388,11 @@ static void
 vl_api_acl_interface_set_acl_list_t_handler
   (vl_api_acl_interface_set_acl_list_t * mp)
 {
-  acl_main_t *sm = &acl_main;
+  acl_main_t *am = &acl_main;
   vl_api_acl_interface_set_acl_list_reply_t *rmp;
   int rv = 0;
   int i;
-  vnet_interface_main_t *im = &sm->vnet_main->interface_main;
+  vnet_interface_main_t *im = &am->vnet_main->interface_main;
   u32 sw_if_index = ntohl (mp->sw_if_index);
 
   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
@@ -1485,10 +1430,10 @@ copy_acl_rule_to_api_rule (vl_api_acl_rule_t * api_rule, acl_rule_t * r)
   api_rule->src_ip_prefix_len = r->src_prefixlen;
   api_rule->dst_ip_prefix_len = r->dst_prefixlen;
   api_rule->proto = r->proto;
-  api_rule->srcport_or_icmptype_first = r->src_port_or_type_first;
-  api_rule->srcport_or_icmptype_last = r->src_port_or_type_last;
-  api_rule->dstport_or_icmpcode_first = r->dst_port_or_code_first;
-  api_rule->dstport_or_icmpcode_last = r->dst_port_or_code_last;
+  api_rule->srcport_or_icmptype_first = htons (r->src_port_or_type_first);
+  api_rule->srcport_or_icmptype_last = htons (r->src_port_or_type_last);
+  api_rule->dstport_or_icmpcode_first = htons (r->dst_port_or_code_first);
+  api_rule->dstport_or_icmpcode_last = htons (r->dst_port_or_code_last);
   api_rule->tcp_flags_mask = r->tcp_flags_mask;
   api_rule->tcp_flags_value = r->tcp_flags_value;
 }
@@ -1669,7 +1614,7 @@ vl_api_macip_acl_add_t_handler (vl_api_macip_acl_add_t * mp)
 static void
 vl_api_macip_acl_del_t_handler (vl_api_macip_acl_del_t * mp)
 {
-  acl_main_t *sm = &acl_main;
+  acl_main_t *am = &acl_main;
   vl_api_macip_acl_del_reply_t *rmp;
   int rv;
 
@@ -1682,10 +1627,10 @@ static void
 vl_api_macip_acl_interface_add_del_t_handler
   (vl_api_macip_acl_interface_add_del_t * mp)
 {
-  acl_main_t *sm = &acl_main;
+  acl_main_t *am = &acl_main;
   vl_api_macip_acl_interface_add_del_reply_t *rmp;
   int rv = -1;
-  vnet_interface_main_t *im = &sm->vnet_main->interface_main;
+  vnet_interface_main_t *im = &am->vnet_main->interface_main;
   u32 sw_if_index = ntohl (mp->sw_if_index);
 
   if (pool_is_free_index(im->sw_interfaces, sw_if_index))
@@ -1821,9 +1766,9 @@ vl_api_macip_acl_interface_get_t_handler (vl_api_macip_acl_interface_get_t *
 static clib_error_t *
 acl_plugin_api_hookup (vlib_main_t * vm)
 {
-  acl_main_t *sm = &acl_main;
+  acl_main_t *am = &acl_main;
 #define _(N,n)                                                  \
-    vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
+    vl_msg_api_set_handlers((VL_API_##N + am->msg_id_base),     \
                            #n,                                 \
                            vl_api_##n##_t_handler,              \
                            vl_noop_handler,                     \
@@ -1841,10 +1786,10 @@ acl_plugin_api_hookup (vlib_main_t * vm)
 #undef vl_msg_name_crc_list
 
 static void
-setup_message_id_table (acl_main_t * sm, api_main_t * am)
+setup_message_id_table (acl_main_t * am, api_main_t * apim)
 {
 #define _(id,n,crc) \
-  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
+  vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + am->msg_id_base);
   foreach_vl_msg_name_crc_acl;
 #undef _
 }
@@ -1854,11 +1799,11 @@ register_match_action_nexts (u32 next_in_ip4, u32 next_in_ip6,
                             u32 next_out_ip4, u32 next_out_ip6)
 {
   acl_main_t *am = &acl_main;
-  u32 act = am->n_match_actions;
   if (am->n_match_actions == 255)
     {
       return ~0;
     }
+  u32 act = am->n_match_actions;
   am->n_match_actions++;
   am->acl_in_ip4_match_next[act] = next_in_ip4;
   am->acl_in_ip6_match_next[act] = next_in_ip6;