1  /* Copyright (C) 2019-2023 Free Software Foundation, Inc.
       2  
       3     This file is part of GCC.
       4  
       5     GCC is free software; you can redistribute it and/or modify
       6     it under the terms of the GNU General Public License as published by
       7     the Free Software Foundation; either version 3, or (at your option)
       8     any later version.
       9  
      10     GCC is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU General Public License for more details.
      14  
      15     Under Section 7 of GPL version 3, you are granted additional
      16     permissions described in the GCC Runtime Library Exception, version
      17     3.1, as published by the Free Software Foundation.
      18  
      19     You should have received a copy of the GNU General Public License and
      20     a copy of the GCC Runtime Library Exception along with this program;
      21     see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      22     <http://www.gnu.org/licenses/>.  */
      23  
      24  /* The purpose of this file is to provide a compatiblity layer with
      25     the Linux kernel bpf_helpers.h header that is located in
      26     linux/tools/testing/selftests/bpf/bpf_helpers.h.  That file is
      27     currently llvm-specific.  */
      28  
      29  #ifndef __BPF_HELPERS_H
      30  #define __BPF_HELPERS_H
      31  
      32  #define SEC(NAME) __attribute__((section(NAME), used))
      33  #define KERNEL_HELPER(NUM) __attribute__((kernel_helper(NUM)))
      34  
      35  /* Flags used in some kernel helpers.  */
      36  
      37  #define BPF_ANY     0
      38  #define BPF_NOEXIST 1
      39  #define BPF_EXIST   2
      40  
      41  #define BPF_F_LOCK 4
      42  #define BPF_F_NO_COMMON_LRU (1U << 1)
      43  #define BPF_F_NUMA_NODE (1U << 2)
      44  
      45  /* Prototypes of functions to call kernel helpers.
      46     Please keep these protoypes sorted by helper number.  */
      47  
      48  void *bpf_map_lookup_elem (void *map, const void *key)
      49    KERNEL_HELPER (1);
      50  
      51  int bpf_map_update_elem (void *map, const void *key, const void *value,
      52  			 unsigned long long flags)
      53    KERNEL_HELPER (2);
      54  
      55  int bpf_map_delete_elem (void *map, const void *key)
      56    KERNEL_HELPER (3);
      57  
      58  int bpf_probe_read (void *dst, int size, const void *unsafe_ptr)
      59    KERNEL_HELPER (4);
      60  
      61  unsigned long long bpf_ktime_get_ns (void)
      62    KERNEL_HELPER (5);
      63  
      64  int bpf_trace_printk (const char *fmt, int fmt_size, ...)
      65    KERNEL_HELPER (6);
      66  
      67  unsigned long long bpf_get_prandom_u32 (void)
      68    KERNEL_HELPER (7);
      69  
      70  unsigned long long bpf_get_smp_processor_id (void)
      71    KERNEL_HELPER (8);
      72  
      73  int bpf_skb_store_bytes (void *ctx, int off, void *from, int len,
      74  			 unsigned int start_header)
      75    KERNEL_HELPER (9);
      76  
      77  int bpf_l3_csum_replace (void *ctx, int off, int from, int to, int flags)
      78    KERNEL_HELPER (10);
      79  
      80  int bpf_l4_csum_replace (void *ctx, int off, int from, int to, int flags)
      81    KERNEL_HELPER (11);
      82  
      83  int bpf_tail_call (void *ctx, void *map, unsigned int index)
      84    KERNEL_HELPER (12);
      85  
      86  int bpf_clone_redirect (void *ctx, int ifindex, int flags)
      87    KERNEL_HELPER (13);
      88  
      89  unsigned long long bpf_get_current_pid_tgid (void)
      90    KERNEL_HELPER (14);
      91  
      92  unsigned long long bpf_get_current_uid_gid (void)
      93    KERNEL_HELPER (15);
      94  
      95  int bpf_get_current_comm (void *buf, int buf_size)
      96    KERNEL_HELPER (16);
      97  
      98  unsigned int bpf_get_cgroup_classid (void *ctx)
      99    KERNEL_HELPER (17);
     100  
     101  int bpf_skb_vlan_push (void *ctx, short vlan_proto,
     102  		       unsigned short vlan_tci)
     103    KERNEL_HELPER (18);
     104  
     105  int bpf_skb_vlan_pop (void *ctx)
     106    KERNEL_HELPER (19);
     107  
     108  int bpf_skb_get_tunnel_key (void *ctx, void *key, int size, int flags)
     109    KERNEL_HELPER (20);
     110  
     111  int bpf_skb_set_tunnel_key (void *ctx, void *key, int size, int flags)
     112    KERNEL_HELPER (21);
     113  
     114  unsigned long long bpf_perf_event_read (void *map, unsigned long long flags)
     115    KERNEL_HELPER (22);
     116  
     117  int bpf_redirect (int ifindex, int flags)
     118    KERNEL_HELPER (23);
     119  
     120  unsigned int bpf_get_route_realm (void *ctx)
     121    KERNEL_HELPER (24);
     122  
     123  int bpf_perf_event_output (void *ctx, void *map, unsigned long long flags,
     124  			   void *data, int size)
     125    KERNEL_HELPER (25);
     126  
     127  int bpf_skb_load_bytes (void *ctx, int off, void *to, int len)
     128    KERNEL_HELPER (26);
     129  
     130  int bpf_get_stackid (void *ctx, void *map, int flags)
     131    KERNEL_HELPER (27);
     132  
     133  int bpf_csum_diff (void *from, int from_size, void *to, int to_size, int seed)
     134    KERNEL_HELPER (28);
     135  
     136  int bpf_skb_get_tunnel_opt (void *ctx, void *md, int size)
     137    KERNEL_HELPER (29);
     138  
     139  int bpf_skb_set_tunnel_opt (void *ctx, void *md, int size)
     140    KERNEL_HELPER (30);
     141  
     142  int bpf_skb_change_proto (void *ctx, short proto, unsigned long flags)
     143    KERNEL_HELPER (31);
     144  
     145  int bpf_skb_change_type (void *ctx, unsigned int type)
     146    KERNEL_HELPER (32);
     147  
     148  int bpf_skb_under_cgroup (void *ctx, void *map, int index)
     149    KERNEL_HELPER (33);
     150  
     151  unsigned int bpf_get_hash_recalc (void *ctx)
     152    KERNEL_HELPER (34);
     153  
     154  unsigned long long bpf_get_current_task (void)
     155    KERNEL_HELPER (35);
     156  
     157  int bpf_probe_write_user (void *dst, const void *src, int size)
     158    KERNEL_HELPER (36);
     159  
     160  int bpf_current_task_under_cgroup (void *map, int index)
     161    KERNEL_HELPER (37);
     162  
     163  int bpf_skb_change_tail (void *ctx, unsigned int len, unsigned long flags)
     164    KERNEL_HELPER (38);
     165  
     166  int bpf_skb_pull_data (void *, int len)
     167    KERNEL_HELPER (39);
     168  
     169  long long bpf_csum_update (void *ctx, unsigned int csum)
     170    KERNEL_HELPER (40);
     171  
     172  void bpf_set_hash_invalid (void *ctx)
     173    KERNEL_HELPER (41);
     174  
     175  int bpf_get_numa_node_id (void)
     176    KERNEL_HELPER (42);
     177  
     178  int bpf_skb_change_head (void *, int len, int flags)
     179    KERNEL_HELPER (43);
     180  
     181  int bpf_xdp_adjust_head (void *ctx, int offset)
     182    KERNEL_HELPER (44);
     183  
     184  int bpf_probe_read_str (void *ctx, unsigned int size, const void *unsafe_ptr)
     185    KERNEL_HELPER (45);
     186  
     187  int bpf_get_socket_cookie (void *ctx)
     188    KERNEL_HELPER (46);
     189  
     190  unsigned int bpf_get_socket_uid (void *ctx)
     191    KERNEL_HELPER (47);
     192  
     193  unsigned int bpf_set_hash (void *ctx, unsigned int hash)
     194    KERNEL_HELPER (48);
     195  
     196  int bpf_setsockopt (void *ctx, int level, int optname, void *optval, int optlen)
     197    KERNEL_HELPER (49);
     198  
     199  int bpf_skb_adjust_room (void *ctx, int len_diff, unsigned int mode,
     200  			 unsigned long long flags)
     201    KERNEL_HELPER (50);
     202  
     203  int bpf_redirect_map (void *map, int key, int flags)
     204    KERNEL_HELPER (51);
     205  
     206  int bpf_sk_redirect_map (void *ctx, void *map, int key, int flags)
     207    KERNEL_HELPER (52);
     208  
     209  int bpf_sock_map_update (void *map, void *key, void *value,
     210  			 unsigned long long flags)
     211    KERNEL_HELPER (53);
     212  
     213  int bpf_xdp_adjust_meta (void *ctx, int offset)
     214    KERNEL_HELPER (54);
     215  
     216  int bpf_perf_event_read_value (void *map, unsigned long long flags,
     217  			       void *buf, unsigned int buf_size)
     218    KERNEL_HELPER (55);
     219  
     220  int bpf_perf_prog_read_value (void *ctx, void *buf, unsigned int buf_size)
     221    KERNEL_HELPER (56);
     222  
     223  int bpf_getsockopt (void *ctx, int level, int optname, void *optval,
     224  		    int optlen)
     225    KERNEL_HELPER (57);
     226  
     227  int bpf_override_return (void *ctx, unsigned long rc)
     228    KERNEL_HELPER (58);
     229  
     230  int bpf_sock_ops_cb_flags_set (void *ctx, int flags)
     231    KERNEL_HELPER (59);
     232  
     233  int bpf_msg_redirect_map (void *ctx, void *map, int key, int flags)
     234    KERNEL_HELPER (60);
     235  
     236  int bpf_msg_apply_bytes (void *ctx, int len)
     237    KERNEL_HELPER (61);
     238  
     239  int bpf_msg_cork_bytes (void *ctx, int len)
     240    KERNEL_HELPER (62);
     241  
     242  int bpf_msg_pull_data (void *, int len)
     243    KERNEL_HELPER (63);
     244  
     245  int bpf_bind (void *ctx, void *addr, int addr_len)
     246    KERNEL_HELPER (64);
     247  
     248  int bpf_xdp_adjust_tail (struct xdp_md *xdp_md, int delta)
     249    KERNEL_HELPER (65);
     250  
     251  int bpf_skb_get_xfrm_state (void *ctx, int index, void *state,
     252  			    int size, int flags)
     253    KERNEL_HELPER (66);
     254  
     255  int bpf_get_stack (void *ctx, void *buf, int size, int flags)
     256    KERNEL_HELPER (67);
     257  
     258  int bpf_skb_load_bytes_relative (void *ctx, int off, void *to, int len,
     259  				 unsigned int start_header)
     260    KERNEL_HELPER (68);
     261  
     262  int bpf_fib_lookup (void *ctx, struct bpf_fib_lookup *params,
     263  		    int plen, unsigned int flags)
     264    KERNEL_HELPER (69);
     265  
     266  int bpf_sock_hash_update (void *map, void *key, void *value,
     267  			  unsigned long long flags)
     268    KERNEL_HELPER (70);
     269  
     270  int bpf_msg_redirect_hash (void *ctx, void *map, void *key, int flags)
     271    KERNEL_HELPER (71);
     272  
     273  int bpf_sk_redirect_hash (void *ctx, void *map, void *key, int flags)
     274    KERNEL_HELPER (72);
     275  
     276  int bpf_lwt_push_encap (void *ctx, unsigned int type, void *hdr,
     277  			unsigned int len)
     278    KERNEL_HELPER (73);
     279  
     280  int bpf_lwt_seg6_store_bytes (void *ctx, unsigned int offset,
     281  			      void *from, unsigned int len)
     282    KERNEL_HELPER (74);
     283  
     284  int bpf_lwt_seg6_adjust_srh (void *ctx, unsigned int offset,
     285  			     unsigned int len)
     286    KERNEL_HELPER (75);
     287  
     288  int bpf_lwt_seg6_action (void *ctx, unsigned int action, void *param,
     289  			 unsigned int param_len)
     290    KERNEL_HELPER (76);
     291  
     292  int bpf_rc_repeat (void *ctx)
     293    KERNEL_HELPER (77);
     294  
     295  int bpf_rc_keydown (void *ctx, unsigned int protocol,
     296  		    unsigned long long scancode, unsigned int toggle)
     297    KERNEL_HELPER (78);
     298  
     299  unsigned bpf_skb_cgroup_id (void *ctx)
     300    KERNEL_HELPER (79);
     301  
     302  unsigned long long bpf_get_current_cgroup_id (void)
     303    KERNEL_HELPER (80);
     304  
     305  void *bpf_get_local_storage (void *map, unsigned long long flags)
     306    KERNEL_HELPER (81);
     307  
     308  int bpf_sk_select_reuseport (void *ctx, void *map, void *key, unsigned int flags)
     309    KERNEL_HELPER (82);
     310  
     311  unsigned long long bpf_skb_ancestor_cgroup_id (void *ctx, int level)
     312    KERNEL_HELPER (83);
     313  
     314  struct bpf_sock *bpf_sk_lookup_tcp (void *ctx, struct bpf_sock_tuple *tuple,
     315  				    int size, unsigned long long netns_id,
     316  				    unsigned long long flags)
     317    KERNEL_HELPER (84);
     318  
     319  struct bpf_sock *bpf_sk_lookup_udp (void *ctx, struct bpf_sock_tuple *tuple,
     320  				    int size, unsigned long long netns_id,
     321  				    unsigned long long flags)
     322    KERNEL_HELPER (85);
     323  
     324  int bpf_sk_release (struct bpf_sock *sk)
     325    KERNEL_HELPER (86);
     326  
     327  int bpf_map_push_elem (void *map, const void *value, unsigned long long flags)
     328    KERNEL_HELPER (87);
     329  
     330  int bpf_map_pop_elem (void *map, void *value)
     331    KERNEL_HELPER (88);
     332  
     333  int bpf_map_peek_elem (void *map, void *value)
     334    KERNEL_HELPER (89);
     335  
     336  int bpf_msg_push_data (void *ctx, int start, int cut, int flags)
     337    KERNEL_HELPER (90);
     338  
     339  int bpf_msg_pop_data (void *ctx, int start, int cut, int flags)
     340    KERNEL_HELPER (91);
     341  
     342  int bpf_rc_pointer_rel (void *ctx, int rel_x, int rel_y)
     343    KERNEL_HELPER (92);
     344  
     345  void bpf_spin_lock (struct bpf_spin_lock *lock)
     346    KERNEL_HELPER (93);
     347  
     348  void bpf_spin_unlock (struct bpf_spin_lock *lock)
     349    KERNEL_HELPER (94);
     350  
     351  struct bpf_sock *bpf_sk_fullsock (struct bpf_sock *sk)
     352    KERNEL_HELPER (95);
     353  
     354  struct bpf_sock *bpf_tcp_sock (struct bpf_sock *sk)
     355    KERNEL_HELPER (96);
     356  
     357  int bpf_skb_ecn_set_ce (void *ctx)
     358    KERNEL_HELPER (97);
     359  
     360  struct bpf_sock *bpf_get_listener_sock (struct bpf_sock *sk)
     361    KERNEL_HELPER (98);
     362  
     363  struct bpf_sock *bpf_skc_lookup_tcp (void *ctx,
     364  				     struct bpf_sock_tuple *tuple,
     365  				     unsigned int tuple_size,
     366  				     unsigned long netns,
     367  				     unsigned long flags)
     368    KERNEL_HELPER (99);
     369  
     370  int bpf_tcp_check_syncookie (struct bpf_sock *sk, void *iph,
     371  			     unsigned int iph_len,
     372  			     struct tcp_hdr *th,
     373  			     unsigned int th_len)
     374    KERNEL_HELPER (100);
     375  
     376  int bpf_sysctl_get_name (struct bpf_sysctl *ctx,
     377  			 char *buf, unsigned long buf_len,
     378  			 unsigned long flags)
     379    KERNEL_HELPER (101);
     380  
     381  int bpf_sysctl_get_current_value (struct bpf_sysctl *ctx,
     382  				  char *buf, unsigned long buf_len)
     383    KERNEL_HELPER (102);
     384  
     385  int bpf_sysctl_get_new_value (struct bpf_sysctl *ctx, char *buf,
     386  			      unsigned long buf_len)
     387    KERNEL_HELPER (103);
     388  
     389  int bpf_sysctl_set_new_value (struct bpf_sysctl *ctx, const char *buf,
     390  			      unsigned long buf_len)
     391    KERNEL_HELPER (104);
     392  
     393  int bpf_strtol (const char *buf, unsigned long buf_len,
     394  		unsigned long flags, long *res)
     395    KERNEL_HELPER (105);
     396  
     397  int bpf_strtoul (const char *buf, unsigned long buf_len,
     398  		 unsigned long flags, unsigned long *res)
     399    KERNEL_HELPER (106);
     400  
     401  void *bpf_sk_storage_get (void *map, struct bpf_sock *sk,
     402  			  void *value, long flags)
     403    KERNEL_HELPER (107);
     404  
     405  int bpf_sk_storage_delete (void *map, struct bpf_sock *sk)
     406    KERNEL_HELPER (108);
     407  
     408  /* Functions to emit BPF_LD_ABS and BPF_LD_IND instructions.  We
     409     provide the "standard" names as synonyms of the corresponding GCC
     410     builtins.  Note how the SKB argument is ignored.  */
     411  
     412  #define load_byte(SKB,OFF) __builtin_bpf_load_byte ((OFF))
     413  #define load_half(SKB,OFF) __builtin_bpf_load_half ((OFF))
     414  #define load_word(SKB,OFF) __builtin_bpf_load_word ((OFF))
     415  
     416  struct bpf_map_def
     417  {
     418    unsigned int type;
     419    unsigned int key_size;
     420    unsigned int value_size;
     421    unsigned int max_entries;
     422    unsigned int map_flags;
     423    unsigned int inner_map_idx;
     424    unsigned int numa_node;
     425  };
     426  
     427  #endif /* ! __BPF_HELPERS_H */