Dave Barach | 6545716 | 2017-10-10 17:53:14 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco 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 agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 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 | #ifndef included_dns_h |
| 17 | #define included_dns_h |
| 18 | |
| 19 | #include <vppinfra/time.h> |
| 20 | #include <vppinfra/cache.h> |
| 21 | #include <vppinfra/error.h> |
| 22 | |
| 23 | #include <vppinfra/hash.h> |
| 24 | #include <vnet/dns/dns_packet.h> |
| 25 | #include <vnet/ip/ip.h> |
| 26 | |
| 27 | typedef struct |
| 28 | { |
| 29 | /** flags */ |
| 30 | volatile u8 flags; |
| 31 | |
| 32 | /** The name in "normal human being" notation, e.g. www.foobar.com */ |
| 33 | u8 *name; |
| 34 | |
| 35 | /** Expiration time */ |
| 36 | f64 expiration_time; |
| 37 | |
| 38 | /** Cached dns request, for sending retries */ |
| 39 | u8 *dns_request; |
| 40 | |
| 41 | /** Retry parameters */ |
| 42 | int retry_count; |
| 43 | int server_rotor; |
| 44 | int server_af; |
| 45 | f64 retry_timer; |
| 46 | |
| 47 | /** Cached dns response */ |
| 48 | u8 *dns_response; |
| 49 | |
| 50 | /** Clients awaiting responses */ |
| 51 | u32 *api_clients_to_notify; |
| 52 | u32 *api_client_contexts; |
| 53 | ip4_address_t *ip4_peers_to_notify; |
| 54 | ip6_address_t *ip6_peers_to_notify; |
| 55 | } dns_cache_entry_t; |
| 56 | |
| 57 | #define DNS_CACHE_ENTRY_FLAG_VALID (1<<0) /**< we have Actual Data */ |
| 58 | #define DNS_CACHE_ENTRY_FLAG_STATIC (1<<1) /**< static entry */ |
| 59 | |
| 60 | #define DNS_RETRIES_PER_SERVER 3 |
| 61 | |
| 62 | #define DNS_RESOLVER_EVENT_RESOLVED 1 |
| 63 | #define DNS_RESOLVER_EVENT_PENDING 2 |
| 64 | |
| 65 | |
| 66 | typedef struct |
| 67 | { |
| 68 | /** Pool of cache entries */ |
| 69 | dns_cache_entry_t *entries; |
| 70 | |
| 71 | /** Pool indices of unresolved entries */ |
| 72 | u32 *unresolved_entries; |
| 73 | |
| 74 | /** Find cached record by name */ |
| 75 | uword *cache_entry_by_name; |
| 76 | uword *cache_lock; |
| 77 | |
| 78 | /** enable / disable flag */ |
| 79 | int is_enabled; |
| 80 | |
| 81 | /** upstream name servers, e.g. 8.8.8.8 */ |
| 82 | ip4_address_t *ip4_name_servers; |
| 83 | ip6_address_t *ip6_name_servers; |
| 84 | |
| 85 | /** config parameters */ |
| 86 | u32 name_cache_size; |
| 87 | u32 max_ttl_in_seconds; |
| 88 | u32 random_seed; |
| 89 | |
| 90 | /* convenience */ |
| 91 | vlib_main_t *vlib_main; |
| 92 | vnet_main_t *vnet_main; |
| 93 | } dns_main_t; |
| 94 | |
| 95 | extern dns_main_t dns_main; |
| 96 | |
| 97 | extern vlib_node_registration_t dns46_reply_node; |
| 98 | extern vlib_node_registration_t dns_resolver_node; |
| 99 | |
| 100 | #define foreach_dns46_reply_error \ |
| 101 | _(PROCESSED, "DNS reply pkts processed") \ |
| 102 | _(NO_ELT, "No DNS pool element") \ |
| 103 | _(FORMAT_ERROR, "DNS format errors") \ |
| 104 | _(TEST_DROP, "DNS reply pkt dropped for test purposes") |
| 105 | |
| 106 | typedef enum |
| 107 | { |
| 108 | #define _(sym,str) DNS46_REPLY_ERROR_##sym, |
| 109 | foreach_dns46_reply_error |
| 110 | #undef _ |
| 111 | DNS46_REPLY_N_ERROR, |
| 112 | } dns46_reply_error_t; |
| 113 | |
| 114 | void vnet_send_dns_request (dns_main_t * dm, dns_cache_entry_t * ep); |
| 115 | int vnet_dns_cname_indirection_nolock (dns_main_t * dm, |
| 116 | dns_cache_entry_t * ep, u8 * reply); |
| 117 | int vnet_dns_delete_entry_by_index_nolock (dns_main_t * dm, u32 index); |
| 118 | |
| 119 | format_function_t format_dns_reply; |
| 120 | |
| 121 | static inline void |
| 122 | dns_cache_lock (dns_main_t * dm) |
| 123 | { |
| 124 | if (dm->cache_lock) |
| 125 | { |
| 126 | while (__sync_lock_test_and_set (dm->cache_lock, 1)) |
| 127 | ; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static inline void |
| 132 | dns_cache_unlock (dns_main_t * dm) |
| 133 | { |
| 134 | if (dm->cache_lock) |
| 135 | { |
| 136 | CLIB_MEMORY_BARRIER (); |
| 137 | *dm->cache_lock = 0; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #endif /* included_dns_h */ |
| 142 | |
| 143 | /* |
| 144 | * fd.io coding-style-patch-verification: ON |
| 145 | * |
| 146 | * Local Variables: |
| 147 | * eval: (c-set-style "gnu") |
| 148 | * End: |
| 149 | */ |