generic.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. union {
  25. struct {
  26. unsigned int len;
  27. struct rcu_head rcu;
  28. } s;
  29. void *ptr[0];
  30. };
  31. };
  32. static inline void *net_generic(const struct net *net, unsigned int id)
  33. {
  34. struct net_generic *ng;
  35. void *ptr;
  36. rcu_read_lock();
  37. ng = rcu_dereference(net->gen);
  38. ptr = ng->ptr[id];
  39. rcu_read_unlock();
  40. return ptr;
  41. }
  42. #endif