generic.h 1.1 KB

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