generic.h 1.1 KB

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