github.com/cilium/cilium@v1.16.2/bpf/lib/neigh.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 <bpf/ctx/ctx.h>
     7  #include <bpf/api.h>
     8  
     9  #include "common.h"
    10  #include "eth.h"
    11  
    12  #if defined(ENABLE_NODEPORT) && defined(ENABLE_IPV6)
    13  struct {
    14  	__uint(type, BPF_MAP_TYPE_LRU_HASH);
    15  	__type(key, union v6addr);	/* ipv6 addr */
    16  	__type(value, union macaddr);	/* hw addr */
    17  	__uint(pinning, LIBBPF_PIN_BY_NAME);
    18  	__uint(max_entries, NODEPORT_NEIGH6_SIZE);
    19  } NODEPORT_NEIGH6 __section_maps_btf;
    20  
    21  static __always_inline int neigh_record_ip6(struct __ctx_buff *ctx)
    22  {
    23  	union macaddr smac = {}, *mac;
    24  	void *data, *data_end;
    25  	struct ipv6hdr *ip6;
    26  
    27  	if (!revalidate_data(ctx, &data, &data_end, &ip6))
    28  		return DROP_INVALID;
    29  	if (eth_load_saddr(ctx, smac.addr, 0) < 0)
    30  		return DROP_INVALID;
    31  
    32  	mac = map_lookup_elem(&NODEPORT_NEIGH6, &ip6->saddr);
    33  	if (!mac || eth_addrcmp(mac, &smac)) {
    34  		int ret = map_update_elem(&NODEPORT_NEIGH6, &ip6->saddr,
    35  					  &smac, 0);
    36  		if (ret < 0)
    37  			return ret;
    38  	}
    39  
    40  	return 0;
    41  }
    42  
    43  static __always_inline union macaddr *neigh_lookup_ip6(const union v6addr *addr)
    44  {
    45  	return map_lookup_elem(&NODEPORT_NEIGH6, addr);
    46  }
    47  #else
    48  static __always_inline union macaddr *
    49  neigh_lookup_ip6(const union v6addr *addr __maybe_unused)
    50  {
    51  	return NULL;
    52  }
    53  #endif /* ENABLE_NODEPORT && ENABLE_IPV6 */
    54  
    55  #if defined(ENABLE_NODEPORT) && defined(ENABLE_IPV4)
    56  struct {
    57  	__uint(type, BPF_MAP_TYPE_LRU_HASH);
    58  	__type(key, __be32);		/* ipv4 addr */
    59  	__type(value, union macaddr);	/* hw addr */
    60  	__uint(pinning, LIBBPF_PIN_BY_NAME);
    61  	__uint(max_entries, NODEPORT_NEIGH4_SIZE);
    62  } NODEPORT_NEIGH4 __section_maps_btf;
    63  
    64  static __always_inline int neigh_record_ip4(struct __ctx_buff *ctx)
    65  {
    66  	union macaddr smac = {}, *mac;
    67  	void *data, *data_end;
    68  	struct iphdr *ip4;
    69  
    70  	if (!revalidate_data(ctx, &data, &data_end, &ip4))
    71  		return DROP_INVALID;
    72  	if (eth_load_saddr(ctx, smac.addr, 0) < 0)
    73  		return DROP_INVALID;
    74  
    75  	mac = map_lookup_elem(&NODEPORT_NEIGH4, &ip4->saddr);
    76  	if (!mac || eth_addrcmp(mac, &smac)) {
    77  		int ret = map_update_elem(&NODEPORT_NEIGH4, &ip4->saddr,
    78  					  &smac, 0);
    79  		if (ret < 0)
    80  			return ret;
    81  	}
    82  
    83  	return 0;
    84  }
    85  
    86  static __always_inline union macaddr *neigh_lookup_ip4(const __be32 *addr)
    87  {
    88  	return map_lookup_elem(&NODEPORT_NEIGH4, addr);
    89  }
    90  #else
    91  static __always_inline union macaddr *
    92  neigh_lookup_ip4(const __be32 *addr __maybe_unused)
    93  {
    94  	return NULL;
    95  }
    96  #endif /* ENABLE_NODEPORT && ENABLE_IPV4 */