nfp_app.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (C) 2017 Netronome Systems, Inc.
  3. *
  4. * This software is dual 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 or the BSD 2-Clause License provided below. You have the
  7. * option to license this software under the complete terms of either license.
  8. *
  9. * The BSD 2-Clause License:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #ifndef _NFP_APP_H
  34. #define _NFP_APP_H 1
  35. #include <net/devlink.h>
  36. #include <trace/events/devlink.h>
  37. #include "nfp_net_repr.h"
  38. struct bpf_prog;
  39. struct net_device;
  40. struct netdev_bpf;
  41. struct netlink_ext_ack;
  42. struct pci_dev;
  43. struct sk_buff;
  44. struct sk_buff;
  45. struct nfp_app;
  46. struct nfp_cpp;
  47. struct nfp_pf;
  48. struct nfp_repr;
  49. struct nfp_net;
  50. enum nfp_app_id {
  51. NFP_APP_CORE_NIC = 0x1,
  52. NFP_APP_BPF_NIC = 0x2,
  53. NFP_APP_FLOWER_NIC = 0x3,
  54. };
  55. extern const struct nfp_app_type app_nic;
  56. extern const struct nfp_app_type app_bpf;
  57. extern const struct nfp_app_type app_flower;
  58. /**
  59. * struct nfp_app_type - application definition
  60. * @id: application ID
  61. * @name: application name
  62. * @ctrl_cap_mask: ctrl vNIC capability mask, allows disabling features like
  63. * IRQMOD which are on by default but counter-productive for
  64. * control messages which are often latency-sensitive
  65. * @ctrl_has_meta: control messages have prepend of type:5/port:CTRL
  66. *
  67. * Callbacks
  68. * @init: perform basic app checks and init
  69. * @clean: clean app state
  70. * @extra_cap: extra capabilities string
  71. * @vnic_alloc: allocate vNICs (assign port types, etc.)
  72. * @vnic_free: free up app's vNIC state
  73. * @vnic_init: vNIC netdev was registered
  74. * @vnic_clean: vNIC netdev about to be unregistered
  75. * @repr_init: representor about to be registered
  76. * @repr_preclean: representor about to unregistered, executed before app
  77. * reference to the it is removed
  78. * @repr_clean: representor about to be unregistered
  79. * @repr_open: representor netdev open callback
  80. * @repr_stop: representor netdev stop callback
  81. * @check_mtu: MTU change request on a netdev (verify it is valid)
  82. * @repr_change_mtu: MTU change request on repr (make and verify change)
  83. * @start: start application logic
  84. * @stop: stop application logic
  85. * @ctrl_msg_rx: control message handler
  86. * @setup_tc: setup TC ndo
  87. * @bpf: BPF ndo offload-related calls
  88. * @xdp_offload: offload an XDP program
  89. * @eswitch_mode_get: get SR-IOV eswitch mode
  90. * @sriov_enable: app-specific sriov initialisation
  91. * @sriov_disable: app-specific sriov clean-up
  92. * @repr_get: get representor netdev
  93. */
  94. struct nfp_app_type {
  95. enum nfp_app_id id;
  96. const char *name;
  97. u32 ctrl_cap_mask;
  98. bool ctrl_has_meta;
  99. int (*init)(struct nfp_app *app);
  100. void (*clean)(struct nfp_app *app);
  101. const char *(*extra_cap)(struct nfp_app *app, struct nfp_net *nn);
  102. int (*vnic_alloc)(struct nfp_app *app, struct nfp_net *nn,
  103. unsigned int id);
  104. void (*vnic_free)(struct nfp_app *app, struct nfp_net *nn);
  105. int (*vnic_init)(struct nfp_app *app, struct nfp_net *nn);
  106. void (*vnic_clean)(struct nfp_app *app, struct nfp_net *nn);
  107. int (*repr_init)(struct nfp_app *app, struct net_device *netdev);
  108. void (*repr_preclean)(struct nfp_app *app, struct net_device *netdev);
  109. void (*repr_clean)(struct nfp_app *app, struct net_device *netdev);
  110. int (*repr_open)(struct nfp_app *app, struct nfp_repr *repr);
  111. int (*repr_stop)(struct nfp_app *app, struct nfp_repr *repr);
  112. int (*check_mtu)(struct nfp_app *app, struct net_device *netdev,
  113. int new_mtu);
  114. int (*repr_change_mtu)(struct nfp_app *app, struct net_device *netdev,
  115. int new_mtu);
  116. int (*start)(struct nfp_app *app);
  117. void (*stop)(struct nfp_app *app);
  118. void (*ctrl_msg_rx)(struct nfp_app *app, struct sk_buff *skb);
  119. int (*setup_tc)(struct nfp_app *app, struct net_device *netdev,
  120. enum tc_setup_type type, void *type_data);
  121. int (*bpf)(struct nfp_app *app, struct nfp_net *nn,
  122. struct netdev_bpf *xdp);
  123. int (*xdp_offload)(struct nfp_app *app, struct nfp_net *nn,
  124. struct bpf_prog *prog,
  125. struct netlink_ext_ack *extack);
  126. int (*sriov_enable)(struct nfp_app *app, int num_vfs);
  127. void (*sriov_disable)(struct nfp_app *app);
  128. enum devlink_eswitch_mode (*eswitch_mode_get)(struct nfp_app *app);
  129. struct net_device *(*repr_get)(struct nfp_app *app, u32 id);
  130. };
  131. /**
  132. * struct nfp_app - NFP application container
  133. * @pdev: backpointer to PCI device
  134. * @pf: backpointer to NFP PF structure
  135. * @cpp: pointer to the CPP handle
  136. * @ctrl: pointer to ctrl vNIC struct
  137. * @reprs: array of pointers to representors
  138. * @type: pointer to const application ops and info
  139. * @priv: app-specific priv data
  140. */
  141. struct nfp_app {
  142. struct pci_dev *pdev;
  143. struct nfp_pf *pf;
  144. struct nfp_cpp *cpp;
  145. struct nfp_net *ctrl;
  146. struct nfp_reprs __rcu *reprs[NFP_REPR_TYPE_MAX + 1];
  147. const struct nfp_app_type *type;
  148. void *priv;
  149. };
  150. bool __nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
  151. bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
  152. static inline int nfp_app_init(struct nfp_app *app)
  153. {
  154. if (!app->type->init)
  155. return 0;
  156. return app->type->init(app);
  157. }
  158. static inline void nfp_app_clean(struct nfp_app *app)
  159. {
  160. if (app->type->clean)
  161. app->type->clean(app);
  162. }
  163. static inline int nfp_app_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
  164. unsigned int id)
  165. {
  166. return app->type->vnic_alloc(app, nn, id);
  167. }
  168. static inline void nfp_app_vnic_free(struct nfp_app *app, struct nfp_net *nn)
  169. {
  170. if (app->type->vnic_free)
  171. app->type->vnic_free(app, nn);
  172. }
  173. static inline int nfp_app_vnic_init(struct nfp_app *app, struct nfp_net *nn)
  174. {
  175. if (!app->type->vnic_init)
  176. return 0;
  177. return app->type->vnic_init(app, nn);
  178. }
  179. static inline void nfp_app_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
  180. {
  181. if (app->type->vnic_clean)
  182. app->type->vnic_clean(app, nn);
  183. }
  184. static inline int nfp_app_repr_open(struct nfp_app *app, struct nfp_repr *repr)
  185. {
  186. if (!app->type->repr_open)
  187. return -EINVAL;
  188. return app->type->repr_open(app, repr);
  189. }
  190. static inline int nfp_app_repr_stop(struct nfp_app *app, struct nfp_repr *repr)
  191. {
  192. if (!app->type->repr_stop)
  193. return -EINVAL;
  194. return app->type->repr_stop(app, repr);
  195. }
  196. static inline int
  197. nfp_app_repr_init(struct nfp_app *app, struct net_device *netdev)
  198. {
  199. if (!app->type->repr_init)
  200. return 0;
  201. return app->type->repr_init(app, netdev);
  202. }
  203. static inline void
  204. nfp_app_repr_preclean(struct nfp_app *app, struct net_device *netdev)
  205. {
  206. if (app->type->repr_preclean)
  207. app->type->repr_preclean(app, netdev);
  208. }
  209. static inline void
  210. nfp_app_repr_clean(struct nfp_app *app, struct net_device *netdev)
  211. {
  212. if (app->type->repr_clean)
  213. app->type->repr_clean(app, netdev);
  214. }
  215. static inline int
  216. nfp_app_check_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
  217. {
  218. if (!app || !app->type->check_mtu)
  219. return 0;
  220. return app->type->check_mtu(app, netdev, new_mtu);
  221. }
  222. static inline int
  223. nfp_app_repr_change_mtu(struct nfp_app *app, struct net_device *netdev,
  224. int new_mtu)
  225. {
  226. if (!app || !app->type->repr_change_mtu)
  227. return 0;
  228. return app->type->repr_change_mtu(app, netdev, new_mtu);
  229. }
  230. static inline int nfp_app_start(struct nfp_app *app, struct nfp_net *ctrl)
  231. {
  232. app->ctrl = ctrl;
  233. if (!app->type->start)
  234. return 0;
  235. return app->type->start(app);
  236. }
  237. static inline void nfp_app_stop(struct nfp_app *app)
  238. {
  239. if (!app->type->stop)
  240. return;
  241. app->type->stop(app);
  242. }
  243. static inline const char *nfp_app_name(struct nfp_app *app)
  244. {
  245. if (!app)
  246. return "";
  247. return app->type->name;
  248. }
  249. static inline bool nfp_app_needs_ctrl_vnic(struct nfp_app *app)
  250. {
  251. return app && app->type->ctrl_msg_rx;
  252. }
  253. static inline bool nfp_app_ctrl_has_meta(struct nfp_app *app)
  254. {
  255. return app->type->ctrl_has_meta;
  256. }
  257. static inline const char *nfp_app_extra_cap(struct nfp_app *app,
  258. struct nfp_net *nn)
  259. {
  260. if (!app || !app->type->extra_cap)
  261. return "";
  262. return app->type->extra_cap(app, nn);
  263. }
  264. static inline bool nfp_app_has_tc(struct nfp_app *app)
  265. {
  266. return app && app->type->setup_tc;
  267. }
  268. static inline int nfp_app_setup_tc(struct nfp_app *app,
  269. struct net_device *netdev,
  270. enum tc_setup_type type, void *type_data)
  271. {
  272. if (!app || !app->type->setup_tc)
  273. return -EOPNOTSUPP;
  274. return app->type->setup_tc(app, netdev, type, type_data);
  275. }
  276. static inline int nfp_app_bpf(struct nfp_app *app, struct nfp_net *nn,
  277. struct netdev_bpf *bpf)
  278. {
  279. if (!app || !app->type->bpf)
  280. return -EINVAL;
  281. return app->type->bpf(app, nn, bpf);
  282. }
  283. static inline int nfp_app_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
  284. struct bpf_prog *prog,
  285. struct netlink_ext_ack *extack)
  286. {
  287. if (!app || !app->type->xdp_offload)
  288. return -EOPNOTSUPP;
  289. return app->type->xdp_offload(app, nn, prog, extack);
  290. }
  291. static inline bool __nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
  292. {
  293. trace_devlink_hwmsg(priv_to_devlink(app->pf), false, 0,
  294. skb->data, skb->len);
  295. return __nfp_ctrl_tx(app->ctrl, skb);
  296. }
  297. static inline bool nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
  298. {
  299. trace_devlink_hwmsg(priv_to_devlink(app->pf), false, 0,
  300. skb->data, skb->len);
  301. return nfp_ctrl_tx(app->ctrl, skb);
  302. }
  303. static inline void nfp_app_ctrl_rx(struct nfp_app *app, struct sk_buff *skb)
  304. {
  305. trace_devlink_hwmsg(priv_to_devlink(app->pf), true, 0,
  306. skb->data, skb->len);
  307. app->type->ctrl_msg_rx(app, skb);
  308. }
  309. static inline int nfp_app_eswitch_mode_get(struct nfp_app *app, u16 *mode)
  310. {
  311. if (!app->type->eswitch_mode_get)
  312. return -EOPNOTSUPP;
  313. *mode = app->type->eswitch_mode_get(app);
  314. return 0;
  315. }
  316. static inline int nfp_app_sriov_enable(struct nfp_app *app, int num_vfs)
  317. {
  318. if (!app || !app->type->sriov_enable)
  319. return -EOPNOTSUPP;
  320. return app->type->sriov_enable(app, num_vfs);
  321. }
  322. static inline void nfp_app_sriov_disable(struct nfp_app *app)
  323. {
  324. if (app && app->type->sriov_disable)
  325. app->type->sriov_disable(app);
  326. }
  327. static inline struct net_device *nfp_app_repr_get(struct nfp_app *app, u32 id)
  328. {
  329. if (unlikely(!app || !app->type->repr_get))
  330. return NULL;
  331. return app->type->repr_get(app, id);
  332. }
  333. struct nfp_app *nfp_app_from_netdev(struct net_device *netdev);
  334. struct nfp_reprs *
  335. nfp_reprs_get_locked(struct nfp_app *app, enum nfp_repr_type type);
  336. struct nfp_reprs *
  337. nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
  338. struct nfp_reprs *reprs);
  339. const char *nfp_app_mip_name(struct nfp_app *app);
  340. struct sk_buff *
  341. nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority);
  342. struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id);
  343. void nfp_app_free(struct nfp_app *app);
  344. /* Callbacks shared between apps */
  345. int nfp_app_nic_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
  346. unsigned int id);
  347. #endif