github.com/cilium/cilium@v1.16.2/bpf/lib/eps.h (about)

     1  /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
     2  /* Copyright Authors of Cilium */
     3  
     4  #pragma once
     5  
     6  #include <linux/ip.h>
     7  #include <linux/ipv6.h>
     8  
     9  #include "maps.h"
    10  
    11  static __always_inline __maybe_unused struct endpoint_info *
    12  __lookup_ip6_endpoint(const union v6addr *ip6)
    13  {
    14  	struct endpoint_key key = {};
    15  
    16  	key.ip6 = *ip6;
    17  	key.family = ENDPOINT_KEY_IPV6;
    18  
    19  	return map_lookup_elem(&ENDPOINTS_MAP, &key);
    20  }
    21  
    22  static __always_inline __maybe_unused struct endpoint_info *
    23  lookup_ip6_endpoint(const struct ipv6hdr *ip6)
    24  {
    25  	return __lookup_ip6_endpoint((union v6addr *)&ip6->daddr);
    26  }
    27  
    28  static __always_inline __maybe_unused struct endpoint_info *
    29  __lookup_ip4_endpoint(__u32 ip)
    30  {
    31  	struct endpoint_key key = {};
    32  
    33  	key.ip4 = ip;
    34  	key.family = ENDPOINT_KEY_IPV4;
    35  
    36  	return map_lookup_elem(&ENDPOINTS_MAP, &key);
    37  }
    38  
    39  static __always_inline __maybe_unused struct endpoint_info *
    40  lookup_ip4_endpoint(const struct iphdr *ip4)
    41  {
    42  	return __lookup_ip4_endpoint(ip4->daddr);
    43  }
    44  
    45  /* IPCACHE_STATIC_PREFIX gets sizeof non-IP, non-prefix part of ipcache_key */
    46  #define IPCACHE_STATIC_PREFIX							\
    47  	(8 * (sizeof(struct ipcache_key) - sizeof(struct bpf_lpm_trie_key)	\
    48  	      - sizeof(union v6addr)))
    49  #define IPCACHE_PREFIX_LEN(PREFIX) (IPCACHE_STATIC_PREFIX + (PREFIX))
    50  
    51  #define V6_CACHE_KEY_LEN (sizeof(union v6addr)*8)
    52  
    53  static __always_inline __maybe_unused struct remote_endpoint_info *
    54  ipcache_lookup6(const void *map, const union v6addr *addr,
    55  		__u32 prefix, __u32 cluster_id)
    56  {
    57  	struct ipcache_key key = {
    58  		.lpm_key = { IPCACHE_PREFIX_LEN(prefix), {} },
    59  		.family = ENDPOINT_KEY_IPV6,
    60  		.ip6 = *addr,
    61  	};
    62  
    63  	/* Check overflow */
    64  	if (cluster_id > UINT16_MAX)
    65  		return NULL;
    66  
    67  	key.cluster_id = (__u16)cluster_id;
    68  
    69  	ipv6_addr_clear_suffix(&key.ip6, prefix);
    70  	return map_lookup_elem(map, &key);
    71  }
    72  
    73  #define V4_CACHE_KEY_LEN (sizeof(__u32)*8)
    74  
    75  static __always_inline __maybe_unused struct remote_endpoint_info *
    76  ipcache_lookup4(const void *map, __be32 addr, __u32 prefix, __u32 cluster_id)
    77  {
    78  	struct ipcache_key key = {
    79  		.lpm_key = { IPCACHE_PREFIX_LEN(prefix), {} },
    80  		.family = ENDPOINT_KEY_IPV4,
    81  		.ip4 = addr,
    82  	};
    83  
    84  	/* Check overflow */
    85  	if (cluster_id > UINT16_MAX)
    86  		return NULL;
    87  
    88  	key.cluster_id = (__u16)cluster_id;
    89  
    90  	key.ip4 &= GET_PREFIX(prefix);
    91  	return map_lookup_elem(map, &key);
    92  }
    93  
    94  #define lookup_ip6_remote_endpoint(addr, cluster_id) \
    95  	ipcache_lookup6(&IPCACHE_MAP, addr, V6_CACHE_KEY_LEN, cluster_id)
    96  #define lookup_ip4_remote_endpoint(addr, cluster_id) \
    97  	ipcache_lookup4(&IPCACHE_MAP, addr, V4_CACHE_KEY_LEN, cluster_id)