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

     1  /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
     2  /* Copyright Authors of Cilium */
     3  
     4  #ifdef ENABLE_IPV4
     5  static __always_inline void
     6  lb_v4_upsert_service(__be32 addr, __be16 port, __u16 backend_count, __u16 rev_nat_index)
     7  {
     8  	struct lb4_key svc_key = {
     9  		.address = addr,
    10  		.dport = port,
    11  		.scope = LB_LOOKUP_SCOPE_EXT,
    12  	};
    13  	struct lb4_service svc_value = {
    14  		.count = backend_count,
    15  		.flags = SVC_FLAG_ROUTABLE,
    16  		.rev_nat_index = rev_nat_index,
    17  	};
    18  	map_update_elem(&LB4_SERVICES_MAP_V2, &svc_key, &svc_value, BPF_ANY);
    19  	/* Register with both scopes: */
    20  	svc_key.scope = LB_LOOKUP_SCOPE_INT;
    21  	map_update_elem(&LB4_SERVICES_MAP_V2, &svc_key, &svc_value, BPF_ANY);
    22  }
    23  
    24  static __always_inline void
    25  lb_v4_add_service(__be32 addr, __be16 port, __u16 backend_count, __u16 rev_nat_index)
    26  {
    27  	/* Register with both scopes: */
    28  	lb_v4_upsert_service(addr, port, backend_count, rev_nat_index);
    29  
    30  	/* Insert a reverse NAT entry for the above service */
    31  	struct lb4_reverse_nat revnat_value = {
    32  		.address = addr,
    33  		.port = port,
    34  	};
    35  	map_update_elem(&LB4_REVERSE_NAT_MAP, &rev_nat_index, &revnat_value, BPF_ANY);
    36  }
    37  
    38  static __always_inline void
    39  lb_v4_add_service_with_flags(__be32 addr, __be16 port, __u16 backend_count, __u16 rev_nat_index,
    40  			     __u8 flags, __u8 flags2)
    41  {
    42  	struct lb4_key svc_key = {
    43  		.address = addr,
    44  		.dport = port,
    45  		.scope = LB_LOOKUP_SCOPE_EXT,
    46  	};
    47  	struct lb4_service svc_value = {
    48  		.count = backend_count,
    49  		.flags = flags,
    50  		.flags2 = flags2,
    51  		.rev_nat_index = rev_nat_index,
    52  	};
    53  	map_update_elem(&LB4_SERVICES_MAP_V2, &svc_key, &svc_value, BPF_ANY);
    54  	/* Register with both scopes: */
    55  	svc_key.scope = LB_LOOKUP_SCOPE_INT;
    56  	map_update_elem(&LB4_SERVICES_MAP_V2, &svc_key, &svc_value, BPF_ANY);
    57  }
    58  
    59  static __always_inline void
    60  lb_v4_upsert_backend(__u32 backend_id, __be32 backend_addr, __be16 backend_port,
    61  		     __u8 backend_proto, __u8 flags, __u8 cluster_id)
    62  {
    63  	struct lb4_backend backend = {
    64  		.address = backend_addr,
    65  		.port = backend_port,
    66  		.proto = backend_proto,
    67  		.flags = flags,
    68  		.cluster_id = cluster_id,
    69  	};
    70  
    71  	map_update_elem(&LB4_BACKEND_MAP, &backend_id, &backend, BPF_ANY);
    72  }
    73  
    74  static __always_inline void
    75  lb_v4_add_backend(__be32 svc_addr, __be16 svc_port, __u16 backend_slot,
    76  		  __u32 backend_id, __be32 backend_addr, __be16 backend_port,
    77  		  __u8 backend_proto, __u8 cluster_id)
    78  {
    79  	/* Create the actual backend: */
    80  	lb_v4_upsert_backend(backend_id, backend_addr, backend_port,
    81  			     backend_proto, BE_STATE_ACTIVE, cluster_id);
    82  
    83  	struct lb4_key svc_key = {
    84  		.address = svc_addr,
    85  		.dport = svc_port,
    86  		.backend_slot = backend_slot,
    87  		.scope = LB_LOOKUP_SCOPE_EXT,
    88  	};
    89  	struct lb4_service svc_value = {
    90  		.backend_id = backend_id,
    91  		.flags = SVC_FLAG_ROUTABLE,
    92  	};
    93  	/* Point the service's backend_slot at the created backend: */
    94  	map_update_elem(&LB4_SERVICES_MAP_V2, &svc_key, &svc_value, BPF_ANY);
    95  }
    96  #endif
    97  
    98  #ifdef ENABLE_IPV6
    99  static __always_inline void
   100  __lb_v6_add_service(const union v6addr *addr, __be16 port, __u16 backend_count, __u16 rev_nat_index,
   101  		    __u8 flags, __u8 flags2)
   102  {
   103  	struct lb6_key svc_key __align_stack_8 = {
   104  		.dport = port,
   105  		.scope = LB_LOOKUP_SCOPE_EXT,
   106  	};
   107  	struct lb6_service svc_value = {
   108  		.count = backend_count,
   109  		.flags = flags,
   110  		.flags2 = flags2,
   111  		.rev_nat_index = rev_nat_index,
   112  	};
   113  
   114  	memcpy(&svc_key.address, addr, sizeof(*addr));
   115  	map_update_elem(&LB6_SERVICES_MAP_V2, &svc_key, &svc_value, BPF_ANY);
   116  	svc_key.scope = LB_LOOKUP_SCOPE_INT;
   117  	map_update_elem(&LB6_SERVICES_MAP_V2, &svc_key, &svc_value, BPF_ANY);
   118  
   119  	/* Insert a reverse NAT entry for the above service */
   120  	struct lb6_reverse_nat revnat_value __align_stack_8 = {
   121  		.port = port,
   122  	};
   123  
   124  	memcpy(&revnat_value.address, addr, sizeof(*addr));
   125  	map_update_elem(&LB6_REVERSE_NAT_MAP, &rev_nat_index, &revnat_value, BPF_ANY);
   126  }
   127  
   128  static __always_inline void
   129  lb_v6_add_service(const union v6addr *addr, __be16 port, __u16 backend_count,
   130  		  __u16 rev_nat_index)
   131  {
   132  	__lb_v6_add_service(addr, port, backend_count, rev_nat_index, SVC_FLAG_ROUTABLE, 0);
   133  }
   134  
   135  static __always_inline void
   136  lb_v6_add_service_with_flags(const union v6addr *addr, __be16 port, __u16 backend_count,
   137  			     __u16 rev_nat_index, __u8 flags, __u8 flags2)
   138  {
   139  	__lb_v6_add_service(addr, port, backend_count, rev_nat_index, flags, flags2);
   140  }
   141  
   142  static __always_inline void
   143  lb_v6_add_backend(const union v6addr *svc_addr, __be16 svc_port, __u16 backend_slot,
   144  		  __u32 backend_id, const union v6addr *backend_addr,
   145  		  __be16 backend_port, __u8 backend_proto, __u8 cluster_id)
   146  {
   147  	struct lb6_key svc_key __align_stack_8 = {
   148  		.dport = svc_port,
   149  		.backend_slot = backend_slot,
   150  		.scope = LB_LOOKUP_SCOPE_EXT,
   151  	};
   152  	struct lb6_service svc_value = {
   153  		.backend_id = backend_id,
   154  		.flags = SVC_FLAG_ROUTABLE,
   155  	};
   156  
   157  	memcpy(&svc_key.address, svc_addr, sizeof(*svc_addr));
   158  	map_update_elem(&LB6_SERVICES_MAP_V2, &svc_key, &svc_value, BPF_ANY);
   159  
   160  	struct lb6_backend backend __align_stack_8 = {
   161  		.port = backend_port,
   162  		.proto = backend_proto,
   163  		.flags = BE_STATE_ACTIVE,
   164  		.cluster_id = cluster_id,
   165  	};
   166  
   167  	memcpy(&backend.address, backend_addr, sizeof(*backend_addr));
   168  	map_update_elem(&LB6_BACKEND_MAP, &backend_id, &backend, BPF_ANY);
   169  }
   170  #endif