/* * Copyright (c) 2016 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. */ #include #include #include #include #include #include #include #include #include #include #include #include typedef struct { u32 sw_if_index; u32 next_index; u32 session_index; u32 is_slow_path; } snat_in2out_trace_t; typedef struct { u32 next_worker_index; u8 do_handoff; } snat_in2out_worker_handoff_trace_t; /* packet trace format function */ static u8 * format_snat_in2out_trace (u8 * s, va_list * args) { CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *); char * tag; tag = t->is_slow_path ? "SNAT_IN2OUT_SLOW_PATH" : "SNAT_IN2OUT_FAST_PATH"; s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag, t->sw_if_index, t->next_index, t->session_index); return s; } static u8 * format_snat_in2out_fast_trace (u8 * s, va_list * args) { CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *); s = format (s, "SANT_IN2OUT_FAST: sw_if_index %d, next index %d", t->sw_if_index, t->next_index); return s; } static u8 * format_snat_in2out_worker_handoff_trace (u8 * s, va_list * args) { CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); snat_in2out_worker_handoff_trace_t * t = va_arg (*args, snat_in2out_worker_handoff_trace_t *); char * m; m = t->do_handoff ? "next worker" : "same worker"; s = format (s, "SNAT_IN2OUT_WORKER_HANDOFF: %s %d", m, t->next_worker_index); return s; } vlib_node_registration_t snat_in2out_node; vlib_node_registration_t snat_in2out_slowpath_node; vlib_node_registration_t snat_in2out_fast_node; vlib_node_registration_t snat_in2out_worker_handoff_node; #define foreach_snat_in2out_error \ _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \ _(IN2OUT_PACKETS, "Good in2out packets processed") \ _(OUT_OF_PORTS, "Out of ports") \ _(BAD_OUTSIDE_FIB, "Outside VRF ID not found") \ _(BAD_ICMP_TYPE, "icmp type not echo-request") \ _(NO_TRANSLATION, "No translation") typedef enum { #define _(sym,str) SNAT_IN2OUT_ERROR_##sym, foreach_snat_in2out_error #undef _ SNAT_IN2OUT_N_ERROR, } snat_in2out_error_t; static char * snat_in2out_error_strings[] = { #define _(sym,string) string, foreach_snat_in2out_error #undef _ }; typedef enum { SNAT_IN2OUT_NEXT_LOOKUP, SNAT_IN2OUT_NEXT_DROP, SNAT_IN2OUT_NEXT_SLOW_PATH, SNAT_IN2OUT_NEXT_ICMP_ERROR, SNAT_IN2OUT_N_NEXT, } snat_in2out_next_t; /** * @brief Check if packet should be translated * * Packets aimed at outside interface and external addresss with active session * should be translated. * * @param sm SNAT main * @param rt SNAT runtime data * @param sw_if_index0 index of the inside interface * @param ip0 IPv4 header * @param proto0 SNAT protocol * @param rx_fib_index0 RX FIB index * * @returns 0 if packet should be translated otherwise 1 */ static inline int snat_not_translate (snat_main_t * sm, snat_runtime_t * rt, u32 sw_if_index0, ip4_header_t * ip0, u32 proto0, u32 rx_fib_index0) { ip4_address_t * first_int_addr; udp_header_t * udp0 = ip4_next_header (ip0); snat_session_key_t key0, sm0; clib_bihash_kv_8_8_t kv0, value0; fib_node_index_t fei = FIB_NODE_INDEX_INVALID; fib_prefix_t pfx = { .fp_proto = FIB_PROTOCOL_IP4, .fp_len = 32, .fp_addr = { .ip4.as_u32 = ip0->dst_address.as_u32, }, }; if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0)) { first_int_addr = ip4_interface_first_address (sm->ip4_main, sw_if_index0, 0 /* just want the address */); rt->cached_sw_if_index = sw_if_index0; if (first_int_addr) rt->cached_ip4_address = first_int_addr->as_u32; else rt->cached_ip4_address = 0; } /* Don't NAT packet aimed at the intfc address */ if (PREDICT_FALSE(ip0->dst_address.as_u32 == rt->cached_ip4_address)) return 1; key0.addr = ip0->dst_address; key0.port = udp0->dst_port; key0.protocol = proto0; key0.fib_index = sm->outside_fib_index; kv0.key = key0.as_u64; /* NAT packet aimed at external address if */ /* has active sessions */ if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0)) { /* or is static mappings */ if (!snat_static_mapping_match(sm, key0, &sm0, 1)) return 0; } else return 0; fei = fib_table_lookup (rx_fib_index0, &pfx); if (FIB_NODE_INDEX_INVALID != fei) { u32 sw_if_index = fib_entry_get_resolving_interface (fei); if (sw_if_index == ~0) { fei = fib_table_lookup (sm->outside_fib_index, &pfx); if (FIB_NODE_INDEX_INVALID != fei) sw_if_index = fib_entry_get_resolving_interface (fei); } snat_interface_t *i; pool_foreach (i, sm->interfaces, ({ /* NAT packet aimed at outside interface */ if ((i->is_inside == 0) && (sw_if_index == i->sw_if_index)) return 0; })); } return 1; } static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t * ip0, u32 rx_fib_index0, snat_session_key_t * key0, snat_session_t ** sessionp, vlib_node_runtime_t * node, u32 next0, u32 cpu_index) { snat_user_t *u; snat_user_key_t user_key; snat_session_t *s; clib_bihash_kv_8_8_t kv0, value0; u32 oldest_per_user_translation_list_index; dlist_elt_t * oldest_per_user_translation_list_elt; dlist_elt_t * per_user_translation_list_elt; dlist_elt_t * per_user_list_head_elt; u32 session_index; snat_session_key_t key1; u32 address_index = ~0; u32 outside_fib_index; uword * p; snat_worker_key_t worker_by_out_key; p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id); if (! p) { b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB]; return SNAT_IN2OUT_NEXT_DROP; } outside_fib_index = p[0]; key1.protocol = key0->protocol; user_key.addr = ip0->src_address; user_key.fib_index = rx_fib_index0; kv0.key = user_key.as_u64; /* Ever heard of the "user" = src ip4 address before? */ if (clib_bihash_search_8_8 (&sm->user_hash, &kv0, &value0)) { /* no, make a new one */ pool_get (sm->per_thread_data[cpu_index].users, u); memset (u, 0, sizeof (*u)); u->addr = ip0->src_address; pool_get (sm->per_thread_data[cpu_index].list_pool, per_user_list_head_elt); u->sessions_per_user_list_head_index = per_user_list_head_elt - sm->per_thread_data[cpu_index].list_pool; clib_dlist_init (sm->per_thread_data[cpu_index].list_pool, u->sessions_per_user_list_head_index); kv0.value = u - sm->per_thread_data[cpu_index].users; /* add user */ clib_bihash_add_del_8_8 (&sm->user_hash, &kv0, 1 /* is_add */); } else { u = pool_elt_at_index (sm->per_thread_data[cpu_index].users, value0.value); } /* Over quota? Recycle the least recently used dynamic translation */ if (u->nsessions >= sm->max_translations_per_user) { /* Remove the oldest dynamic translation */ do { oldest_per_user_translation_list_index = clib_dlist_remove_head (sm->per_thread_data[cpu_index].list_pool, u->sessions_per_user_list_head_index); ASSERT (oldest_per_user_translation_list_index != ~0); /* add it back to the end of the LRU list */ clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool, u->sessions_per_user_list_head_index, oldest_per_user_translation_list_index); /* Get the list element */ oldest_per_user_translation_list_elt = pool_elt_at_index (sm->per_thread_data[cpu_index].list_pool, oldest_per_user_translation_list_index); /* Get the session index from the list element */ session_index = oldest_per_user_translation_list_elt->value; /* Get the session */ s = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions, session_index); } while (snat_is_session_static (s)); /* Remove in2out, out2in keys */ kv0.key = s->in2out.as_u64; if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 0 /* is_add */)) clib_warning ("in2out key delete failed"); kv0.key = s->out2in.as_u64; if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 0 /* is_add */)) clib_warning ("out2in key delete failed"); /* log NAT event */ snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32, s->out2in.addr.as_u32, s->in2out.protocol, s->in2out.port, s->out2in.port, s->in2out.fib_index); snat_free_outside_address_and_port (sm, &s->out2in, s->outside_address_index); s->outside_address_index = ~0; if (snat_alloc_outside_address_and_port (sm, &key1, &address_index)) { ASSERT(0); b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS]; return SNAT_IN2OUT_NEXT_DROP; } s->outside_address_index = address_index; } else { u8 static_mapping = 1; /* First try to match static mapping by local address and port */ if (snat_static_mapping_match (sm, *key0, &key1, 0)) { static_mapping = 0; /* Try to create dynamic translation */ if (snat_alloc_outside_address_and_port (sm, &key1, &address_index)) { b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS]; return SNAT_IN2OUT_NEXT_DROP; } } /* Create a new session */ pool_get (sm->per_thread_data[cpu_index].sessions, s); memset (s, 0, sizeof (*s)); s->outside_address_index = address_index; if (static_mapping) { u->nstaticsessions++; s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING; } else { u->nsessions++; } /* Create list elts */ pool_get (sm->per_thread_data[cpu_index].list_pool, per_user_translation_list_elt); clib_dlist_init (sm->per_thread_data[cpu_index].list_pool, per_user_translation_list_elt - sm->per_thread_data[cpu_index].list_pool); per_user_translation_list_elt->value = s - sm->per_thread_data[cpu_index].sessions; s->per_user_index = per_user_translation_list_elt - sm->per_thread_data[cpu_index].list_pool; s->per_user_list_head_index = u->sessions_per_user_list_head_index; clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool, s->per_user_list_head_index, per_user_translation_list_elt - sm->per_thread_data[cpu_index].list_pool); } s->in2out = *key0; s->out2in = key1; s->out2in.protocol = key0->protocol; s->out2in.fib_index = outside_fib_index; *sessionp = s; /* Add to translation hashes */ kv0.key = s->in2out.as_u64; kv0.value = s - sm->per_thread_data[cpu_index].sessions; if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 1 /* is_add */)) clib_warning ("in2out key add failed"); kv0.key = s->out2in.as_u64; kv0.value = s - sm->per_thread_data[cpu_index].sessions; if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 1 /* is_add */)) clib_warning ("out2in key add failed"); /* Add to translated packets worker lookup */ worker_by_out_key.addr = s->out2in.addr; worker_by_out_key.port = s->out2in.port; worker_by_out_key.fib_index = s->out2in.fib_index; kv0.key = worker_by_out_key.as_u64; kv0.value = cpu_index; clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1); /* log NAT event */ snat_ipfix_logging_nat44_ses_create(s->in2out.addr.as_u32, s->out2in.addr.as_u32, s->in2out.protocol, s->in2out.port, s->out2in.port, s->in2out.fib_index); return next0; } typedef struct { u16 src_port, dst_port; } tcp_udp_header_t; static inline u32 icmp_in2out_slow_path (snat_main_t *sm, vlib_buffer_t * b0, ip4_header_t * ip0, icmp46_header_t * icmp0, u32 sw_if_index0, u32 rx_fib_index0, vlib_node_runtime_t * node, u32 next0, f64 now, u32 cpu_index, snat_session_t ** p_s0) { snat_session_key_t key0; icmp_echo_header_t *echo0, *inner_echo0 = 0; ip4_header_t *inner_ip0; void *l4_header = 0; icmp46_header_t *inner_icmp0; clib_bihash_kv_8_8_t kv0, value0; snat_session_t * s0 = 0; u32 new_addr0, old_addr0; u16 old_id0, new_id0; ip_csum_t sum0; u16 checksum0; snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data; u8 is_error_message = 0; echo0 = (icmp_echo_header_t *)(icmp0+1); key0.addr = ip0->src_address; key0.fib_index = rx_fib_index0; switch(icmp0->type) { case ICMP4_destination_unreachable: case ICMP4_time_exceeded: case ICMP4_parameter_problem: case ICMP4_source_quench: case ICMP4_redirect: case ICMP4_alternate_host_address: is_error_message = 1; } if (!is_error_message) { if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request)) { b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE]; next0 = SNAT_IN2OUT_NEXT_DROP; goto out; } key0.protocol = SNAT_PROTOCOL_ICMP; key0.port = echo0->identifier; } else { inner_ip0 = (ip4_header_t *)(echo0+1); l4_header = ip4_next_header (inner_ip0); key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol); switch (key0.protocol) { case SNAT_PROTOCOL_ICMP: inner_icmp0 = (icmp46_header_t*)l4_header; inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1); key0.port = inner_echo0->identifier; break; case SNAT_PROTOCOL_UDP: case SNAT_PROTOCOL_TCP: key0.port = ((tcp_udp_header_t*)l4_header)->dst_port; break; default: b0->error = node->errors[SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL]; next0 = SNAT_IN2OUT_NEXT_DROP; goto out; } } kv0.key = key0.as_u64; if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0)) { if (PREDICT_FALSE(snat_not_translate(sm, rt, sw_if_index0, ip0, IP_PROTOCOL_ICMP, rx_fib_index0))) goto out; if (is_error_message) { next0 = SNAT_IN2OUT_NEXT_DROP; goto out; } next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0, &s0, node, next0, cpu_index); if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP)) goto out; } else s0 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions, value0.value); sum0 = ip_incremental_checksum (0, icmp0, ntohs(ip0->length) - ip4_header_bytes (ip0)); checksum0 = ~ip_csum_fold (sum0); if (PREDICT_FALSE(checksum0 != 0 && checksum0 != 0xffff)) { next0 = SNAT_IN2OUT_NEXT_DROP; goto out; } old_addr0 = ip0->src_address.as_u32; ip0->src_address = s0->out2in.addr; new_addr0 = ip0->src_address.as_u32; vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index; sum0 = ip0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, src_address /* changed member */); ip0->checksum = ip_csum_fold (sum0); if (!is_error_message) { old_id0 = echo0->identifier; new_id0 = s0->out2in.port; echo0->identifier = new_id0; sum0 = icmp0->checksum; sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t, identifier); icmp0->checksum = ip_csum_fold (sum0); } else { if (!ip4_header_checksum_is_valid (inner_ip0)) { next0 = SNAT_IN2OUT_NEXT_DROP; goto out; } old_addr0 = inner_ip0->dst_address.as_u32; inner_ip0->dst_address = s0->out2in.addr; new_addr0 = inner_ip0->src_address.as_u32; sum0 = icmp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */); icmp0->checksum = ip_csum_fold (sum0); switch (key0.protocol) { case SNAT_PROTOCOL_ICMP: old_id0 = inner_echo0->identifier; new_id0 = s0->out2in.port; inner_echo0->identifier = new_id0; sum0 = icmp0->checksum; sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t, identifier); icmp0->checksum = ip_csum_fold (sum0); break; case SNAT_PROTOCOL_UDP: case SNAT_PROTOCOL_TCP: old_id0 = ((tcp_udp_header_t*)l4_header)->dst_port; new_id0 = s0->out2in.port; ((tcp_udp_header_t*)l4_header)->dst_port = new_id0; sum0 = icmp0->checksum; sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t, dst_port); icmp0->checksum = ip_csum_fold (sum0); break; default: ASSERT(0); } } /* Accounting */ s0->last_heard = now; s0->total_pkts++; s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0); /* Per-user LRU list maintenance for dynamic translations */ if (!snat_is_session_static (s0)) { clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool, s0->per_user_index); clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool, s0->per_user_list_head_index, s0->per_user_index); } out: *p_s0 = s0; return next0; } /** * @brief Hairpinning * * Hairpinning allows two endpoints on the internal side of the NAT to * communicate even if they only use each other's external IP addresses * and ports. * * @param sm SNAT main. * @param b0 Vlib buffer. * @param ip0 IP header. * @param udp0 UDP header. * @param tcp0 TCP header. * @param proto0 SNAT protocol. */ static inline void snat_hairpinning (snat_main_t *sm, vlib_buffer_t * b0, ip4_header_t * ip0, udp_header_t * udp0, tcp_header_t * tcp0, u32 proto0) { snat_session_key_t key0, sm0; snat_worker_key_t k0; snat_session_t * s0; clib_bihash_kv_8_8_t kv0, value0; ip_csum_t sum0; u32 new_dst_addr0 = 0, old_dst_addr0, t
/*
 * Copyright (c) 2016 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.
 */

#ifndef included_features_h
#define included_features_h

#include <vnet/vnet.h>
#include <vnet/api_errno.h>
#include <vnet/devices/devices.h>

/** feature registration object */
typedef struct _vnet_feature_arc_registration
{
  /** next registration in list of all registrations*/
  struct _vnet_feature_arc_registration *next;
  /** Feature Arc name */
  char *arc_name;
  /** Start nodes */
  char **start_nodes;
  int n_start_nodes;
  /** End of the arc (optional, for consistency-checking) */
  char *last_in_arc;
  /* Feature arc index, assigned by init function */
  u8 feature_arc_index;
  u8 *arc_index_ptr;
} vnet_feature_arc_registration_t;

/* Enable feature callback. */
typedef clib_error_t *(vnet_feature_enable_disable_function_t)
  (u32 sw_if_index, int enable_disable);

/** feature registration object */
typedef struct _vnet_feature_registration
{
  /** next registration in list of all registrations*/
  struct _vnet_feature_registration *next, *next_in_arc;
  /** Feature arc name */
  char *arc_name;
  /** Graph node name */
  char *node_name;
  /** Pointer to this feature index, filled in by vnet_feature_arc_init */
  u32 *feature_index_ptr;
  u32 feature_index;
  /** Constraints of the form "this feature runs before X" */
  char **runs_before;
  /** Constraints of the form "this feature runs after Y" */
  char **runs_after;

  /** Function to enable/disable feature  **/
  vnet_feature_enable_disable_function_t *enable_disable_cb;
} vnet_feature_registration_t;

/** constraint registration object */
typedef struct _vnet_feature_constraint_registration
{
  /** next constraint set in list of all registrations*/
  struct _vnet_feature_constraint_registration *next, *next_in_arc;
  /** Feature arc name */
  char *arc_name;

  /** Feature arc index, assigned by init function */
  u8 feature_arc_index;

  /** Node names, to run in the specified order */
  char **node_names;
} vnet_feature_constraint_registration_t;

typedef struct vnet_feature_config_main_t_
{
  vnet_config_main_t config_main;
  u32 *config_index_by_sw_if_index;
} vnet_feature_config_main_t;

typedef struct
{
  /** feature arc configuration list */
  vnet_feature_arc_registration_t *next_arc;
  uword **arc_index_by_name;

  /** feature path configuration lists */
  vnet_feature_registration_t *next_feature;
  vnet_feature_registration_t **next_feature_by_arc;
  vnet_feature_constraint_registration_t *next_constraint;
  vnet_feature_constraint_registration_t **next_constraint_by_arc;
  uword **next_feature_by_name;

  /** feature config main objects */
  vnet_feature_config_main_t *feature_config_mains;

  /** Save partial order results for show command */
  char ***feature_nodes;

  /** bitmap of interfaces which have driver rx features configured */
  uword **sw_if_index_has_features;

  /** feature reference counts by interface */
  i16 **feature_count_by_sw_if_index;

  /** Feature arc index for device-input */
  u8 device_input_feature_arc_index;

  /** convenience */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;
} vnet_feature_main_t;

extern vnet_feature_main_t feature_main;

#ifndef CLIB_MARCH_VARIANT
#define VNET_FEATURE_ARC_INIT(x,...)				\
  __VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x;\
static void __vnet_add_feature_arc_registration_##x (void)	\
  __attribute__((__constructor__)) ;				\
static void __vnet_add_feature_arc_registration_##x (void)	\
{								\
  vnet_feature_main_t * fm = &feature_main;			\
  vnet_feat_arc_##x.next = fm->next_arc;			\
  fm->next_arc = & vnet_feat_arc_##x;				\
}								\
static void __vnet_rm_feature_arc_registration_##x (void)	\
  __attribute__((__destructor__)) ;				\
static void __vnet_rm_feature_arc_registration_##x (void)	\
{								\
  vnet_feature_main_t * fm = &feature_main;			\
  vnet_feature_arc_registration_t *r = &vnet_feat_arc_##x;	\
  VLIB_REMOVE_FROM_LINKED_LIST (fm->next_arc, r, next);		\
}								\
__VA_ARGS__ vnet_feature_arc_registration_t vnet_feat_arc_##x

#define VNET_FEATURE_INIT(x,...)				\
  __VA_ARGS__ vnet_feature_registration_t vnet_feat_##x;	\
static void __vnet_add_feature_registration_##x (void)		\
  __attribute__((__constructor__)) ;				\
static void __vnet_add_feature_registration_##x (void)		\
{								\
  vnet_feature_main_t * fm = &feature_main;			\
  vnet_feat_##x.next = fm->next_feature;			\
  fm->next_feature = & vnet_feat_##x;				\
}								\
static void __vnet_rm_feature_registration_##x (void)		\
  __attribute__((__destructor__)) ;				\
static void __vnet_rm_feature_registration_##x (void)		\
{								\
  vnet_feature_main_t * fm = &feature_main;			\
  vnet_feature_registration_t *r = &vnet_feat_##x;		\
  VLIB_REMOVE_FROM_LINKED_LIST (fm->next_feature, r, next);	\
}								\
__VA_ARGS__ vnet_feature_registration_t vnet_feat_##x

#define VNET_FEATURE_ARC_ORDER(x,...)                                   \
  __VA_ARGS__ vnet_feature_constraint_registration_t 			\
vnet_feature_constraint_##x;                                            \
static void __vnet_add_constraint_registration_##x (void)		\
  __attribute__((__constructor__)) ;                                    \
static void __vnet_add_constraint_registration_##x (void)		\
{                                                                       \
  vnet_feature_main_t * fm = &feature_main;                             \
  vnet_feature_constraint_##x.next = fm->next_constraint;               \
  fm->next_constraint = & vnet_feature_constraint_##x;                  \
}                                                                       \
static void __vnet_rm_constraint_registration_##x (void)		\
  __attribute__((__destructor__)) ;                                     \
static void __vnet_rm_constraint_registration_##x (void)		\
{                                                                       \
  vnet_feature_main_t * fm = &feature_main;                             \
  vnet_feature_constraint_registration_t *r = &vnet_feature_constraint_##x; \
  VLIB_REMOVE_FROM_LINKED_LIST (fm->next_constraint, r, next);          \
}                                                                       \
__VA_ARGS__ vnet_feature_constraint_registration_t vnet_feature_constraint_##x

#else
#define VNET_FEATURE_ARC_INIT(x,...)				\
extern vnet_feature_arc_registration_t __clib_unused vnet_feat_arc_##x; \
static vnet_feature_arc_registration_t __clib_unused __clib_unused_vnet_feat_arc_##x
#define VNET_FEATURE_INIT(x,...)				\
extern vnet_feature_registration_t __clib_unused vnet_feat_##x; \
static vnet_feature_registration_t __clib_unused __clib_unused_vnet_feat_##x

#define VNET_FEATURE_ARC_ORDER(x,...)                           \
extern vnet_feature_constraint_registration_t                   \
__clib_unused vnet_feature_constraint_##x;                      \
static vnet_feature_constraint_registration_t __clib_unused 	\
__clib_unused_vnet_feature_constraint_##x


#endif

void
vnet_config_update_feature_count (vnet_feature_main_t * fm, u8 arc,
				  u32 sw_if_index, int is_add);

u32 vnet_get_feature_index (u8 arc, const char *s);
u8 vnet_get_feature_arc_index (const char *s);
vnet_feature_registration_t *vnet_get_feature_reg (const char *arc_name,
						   const char *node_name);


int
vnet_feature_enable_disable_with_index (u8 arc_index, u32 feature_index,
					u32 sw_if_index, int enable_disable,
					void *feature_config,
					u32 n_feature_config_bytes);

int
vnet_feature_enable_disable (const char *arc_name, const char *node_name,
			     u32 sw_if_index, int enable_disable,
			     void *feature_config,
			     u32 n_feature_config_bytes);

static_always_inline u32
vnet_get_feature_count (u8 arc, u32 sw_if_index)
{
  vnet_feature_main_t *fm = &feature_main;
  return (fm->feature_count_by_sw_if_index[arc][sw_if_index]);
}

static inline vnet_feature_config_main_t *
vnet_get_feature_arc_config_main (u8 arc_index)
{
  vnet_feature_main_t *fm = &feature_main;

  if (arc_index == (u8) ~ 0)
    return 0;

  return &fm->feature_config_mains[arc_index];
}

static_always_inline vnet_feature_config_main_t *
vnet_feature_get_config_main (u16 arc)
{
  vnet_feature_main_t *fm = &feature_main;
  return &fm->feature_config_mains[arc];
}

static_always_inline int
vnet_have_features (u8 arc, u32 sw_if_index)
{
  vnet_feature_main_t *fm = &feature_main;
  return clib_bitmap_get (fm->sw_if_index_has_features[arc], sw_if_index);
}

static_always_inline u32
vnet_get_feature_config_index (u8 arc, u32 sw_if_index)
{
  vnet_feature_main_t *fm = &feature_main;
  vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];
  return vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
}

static_always_inline void *
vnet_feature_arc_start_with_data (u8 arc, u32 sw_if_index, u32 * next,
				  vlib_buffer_t * b, u32 n_data_bytes)
{
  vnet_feature_main_t *fm = &feature_main;
  vnet_feature_config_main_t *cm;
  cm = &fm->feature_config_mains[arc];

  if (PREDICT_FALSE (vnet_have_features (arc, sw_if_index)))
    {
      vnet_buffer (b)->feature_arc_index = arc;
      b->current_config_index =
	vec_elt (cm->config_index_by_sw_if_index, sw_if_index);
      return vnet_get_config_data (&cm->config_main, &b->current_config_index,
				   next, n_data_bytes);
    }
  return 0;
}

static_always_inline void
vnet_feature_arc_start (u8 arc, u32 sw_if_index, u32 * next0,
			vlib_buffer_t * b0)
{
  vnet_feature_arc_start_with_data (arc, sw_if_index, next0, b0, 0);
}

static_always_inline void *
vnet_feature_next_with_data (u32 * next0, vlib_buffer_t * b0,
			     u32 n_data_bytes)
{
  vnet_feature_main_t *fm = &feature_main;
  u8 arc = vnet_buffer (b0)->feature_arc_index;
  vnet_feature_config_main_t *cm = &fm->feature_config_mains[arc];

  return vnet_get_config_data (&cm->config_main,
			       &b0->current_config_index, next0,
			       n_data_bytes);
}

static_always_inline void
vnet_feature_next (u32 * next0, vlib_buffer_t * b0)