Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Netronome Systems, Inc. |
| 3 | * |
| 4 | * This software is licensed under the GNU General License Version 2, |
| 5 | * June 1991 as shown in the file COPYING in the top-level directory of this |
| 6 | * source tree. |
| 7 | * |
| 8 | * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" |
| 9 | * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, |
| 10 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 11 | * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE |
| 12 | * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME |
| 13 | * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. |
| 14 | */ |
| 15 | |
Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 16 | #include <linux/device.h> |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 17 | #include <linux/kernel.h> |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 18 | #include <linux/list.h> |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/u64_stats_sync.h> |
| 21 | |
| 22 | #define DRV_NAME "netdevsim" |
| 23 | |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 24 | #define NSIM_XDP_MAX_MTU 4000 |
| 25 | |
| 26 | #define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg) |
| 27 | |
| 28 | struct bpf_prog; |
| 29 | struct dentry; |
Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 30 | struct nsim_vf_config; |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 31 | |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 32 | struct netdevsim { |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 33 | struct net_device *netdev; |
| 34 | |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 35 | u64 tx_packets; |
| 36 | u64 tx_bytes; |
| 37 | struct u64_stats_sync syncp; |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 38 | |
Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 39 | struct device dev; |
| 40 | |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 41 | struct dentry *ddir; |
| 42 | |
Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 43 | unsigned int num_vfs; |
| 44 | struct nsim_vf_config *vfconfigs; |
| 45 | |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 46 | struct bpf_prog *bpf_offloaded; |
| 47 | u32 bpf_offloaded_id; |
| 48 | |
| 49 | u32 xdp_flags; |
| 50 | int xdp_prog_mode; |
| 51 | struct bpf_prog *xdp_prog; |
| 52 | |
| 53 | u32 prog_id_gen; |
| 54 | |
| 55 | bool bpf_bind_accept; |
| 56 | u32 bpf_bind_verifier_delay; |
| 57 | struct dentry *ddir_bpf_bound_progs; |
| 58 | struct list_head bpf_bound_progs; |
| 59 | |
| 60 | bool bpf_tc_accept; |
| 61 | bool bpf_tc_non_bound_accept; |
| 62 | bool bpf_xdpdrv_accept; |
| 63 | bool bpf_xdpoffload_accept; |
Jakub Kicinski | 395cacb | 2018-01-17 19:13:30 -0800 | [diff] [blame] | 64 | |
| 65 | bool bpf_map_accept; |
| 66 | struct list_head bpf_bound_maps; |
David Ahern | 37923ed | 2018-03-27 18:22:00 -0700 | [diff] [blame] | 67 | #if IS_ENABLED(CONFIG_NET_DEVLINK) |
| 68 | struct devlink *devlink; |
| 69 | #endif |
Jakub Kicinski | 83c9e13 | 2017-12-01 15:08:58 -0800 | [diff] [blame] | 70 | }; |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 71 | |
| 72 | extern struct dentry *nsim_ddir; |
| 73 | |
Jakub Kicinski | 7c5db7e | 2018-01-23 11:22:54 -0800 | [diff] [blame] | 74 | #ifdef CONFIG_BPF_SYSCALL |
Jakub Kicinski | 31d3ad8 | 2017-12-01 15:08:59 -0800 | [diff] [blame] | 75 | int nsim_bpf_init(struct netdevsim *ns); |
| 76 | void nsim_bpf_uninit(struct netdevsim *ns); |
| 77 | int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf); |
| 78 | int nsim_bpf_disable_tc(struct netdevsim *ns); |
| 79 | int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, |
| 80 | void *type_data, void *cb_priv); |
Jakub Kicinski | 7c5db7e | 2018-01-23 11:22:54 -0800 | [diff] [blame] | 81 | #else |
| 82 | static inline int nsim_bpf_init(struct netdevsim *ns) |
| 83 | { |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static inline void nsim_bpf_uninit(struct netdevsim *ns) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | static inline int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf) |
| 92 | { |
| 93 | return bpf->command == XDP_QUERY_PROG ? 0 : -EOPNOTSUPP; |
| 94 | } |
| 95 | |
| 96 | static inline int nsim_bpf_disable_tc(struct netdevsim *ns) |
| 97 | { |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static inline int |
| 102 | nsim_bpf_setup_tc_block_cb(enum tc_setup_type type, void *type_data, |
| 103 | void *cb_priv) |
| 104 | { |
| 105 | return -EOPNOTSUPP; |
| 106 | } |
| 107 | #endif |
Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 108 | |
David Ahern | 37923ed | 2018-03-27 18:22:00 -0700 | [diff] [blame] | 109 | #if IS_ENABLED(CONFIG_NET_DEVLINK) |
| 110 | enum nsim_resource_id { |
| 111 | NSIM_RESOURCE_NONE, /* DEVLINK_RESOURCE_ID_PARENT_TOP */ |
| 112 | NSIM_RESOURCE_IPV4, |
| 113 | NSIM_RESOURCE_IPV4_FIB, |
| 114 | NSIM_RESOURCE_IPV4_FIB_RULES, |
| 115 | NSIM_RESOURCE_IPV6, |
| 116 | NSIM_RESOURCE_IPV6_FIB, |
| 117 | NSIM_RESOURCE_IPV6_FIB_RULES, |
| 118 | }; |
| 119 | |
David Ahern | ef81710 | 2018-03-30 09:28:51 -0700 | [diff] [blame] | 120 | int nsim_devlink_setup(struct netdevsim *ns); |
David Ahern | 37923ed | 2018-03-27 18:22:00 -0700 | [diff] [blame] | 121 | void nsim_devlink_teardown(struct netdevsim *ns); |
| 122 | |
| 123 | int nsim_devlink_init(void); |
| 124 | void nsim_devlink_exit(void); |
| 125 | |
| 126 | int nsim_fib_init(void); |
| 127 | void nsim_fib_exit(void); |
| 128 | u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max); |
| 129 | int nsim_fib_set_max(struct net *net, enum nsim_resource_id res_id, u64 val); |
| 130 | #else |
David Ahern | ef81710 | 2018-03-30 09:28:51 -0700 | [diff] [blame] | 131 | static inline int nsim_devlink_setup(struct netdevsim *ns) |
David Ahern | 37923ed | 2018-03-27 18:22:00 -0700 | [diff] [blame] | 132 | { |
David Ahern | ef81710 | 2018-03-30 09:28:51 -0700 | [diff] [blame] | 133 | return 0; |
David Ahern | 37923ed | 2018-03-27 18:22:00 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static inline void nsim_devlink_teardown(struct netdevsim *ns) |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | static inline int nsim_devlink_init(void) |
| 141 | { |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static inline void nsim_devlink_exit(void) |
| 146 | { |
| 147 | } |
| 148 | #endif |
| 149 | |
Jakub Kicinski | 7957922 | 2017-12-01 15:09:01 -0800 | [diff] [blame] | 150 | static inline struct netdevsim *to_nsim(struct device *ptr) |
| 151 | { |
| 152 | return container_of(ptr, struct netdevsim, dev); |
| 153 | } |