generic.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * generic net pointers
  4. */
  5. #ifndef __NET_GENERIC_H__
  6. #define __NET_GENERIC_H__
  7. #include <linux/bug.h>
  8. #include <linux/rcupdate.h>
  9. /*
  10. * Generic net pointers are to be used by modules to put some private
  11. * stuff on the struct net without explicit struct net modification
  12. *
  13. * The rules are simple:
  14. * 1. set pernet_operations->id. After register_pernet_device you
  15. * will have the id of your private pointer.
  16. * 2. set pernet_operations->size to have the code allocate and free
  17. * a private structure pointed to from struct net.
  18. * 3. do not change this pointer while the net is alive;
  19. * 4. do not try to have any private reference on the net_generic object.
  20. *
  21. * After accomplishing all of the above, the private pointer can be
  22. * accessed with the net_generic() call.
  23. */
  24. struct net_generic {
  25. union {
  26. struct {
  27. unsigned int len;
  28. struct rcu_head rcu;
  29. } s;
  30. void *ptr[0];
  31. };
  32. };
  33. static inline void *net_generic(const struct net *net, unsigned int id)
  34. {
  35. struct net_generic *ng;
  36. void *ptr;
  37. rcu_read_lock();
  38. ng = rcu_dereference(net->gen);
  39. ptr = ng->ptr[id];
  40. rcu_read_unlock();
  41. return ptr;
  42. }
  43. #endif