generic.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. register the ops with register_pernet_gen_device to get the id
  13. * of your private pointer;
  14. * 2. call net_assign_generic() to put the private data on the struct
  15. * net (most preferably this should be done in the ->init callback
  16. * of the ops registered);
  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(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. BUG_ON(id == 0 || id > ng->len);
  35. ptr = ng->ptr[id - 1];
  36. rcu_read_unlock();
  37. return ptr;
  38. }
  39. extern int net_assign_generic(struct net *net, int id, void *data);
  40. #endif