generic.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. Either set pernet_operations->size (to have the code allocate and
  15. * free a private structure pointed to from struct net ) or
  16. * call net_assign_generic() to put the private data on the struct
  17. * net (most preferably this should be done in the ->init callback
  18. * of the ops registered);
  19. * 3. do not change this pointer while the net is alive;
  20. * 4. do not try to have any private reference on the net_generic object.
  21. *
  22. * After accomplishing all of the above, the private pointer can be
  23. * accessed with the net_generic() call.
  24. */
  25. struct net_generic {
  26. unsigned int len;
  27. struct rcu_head rcu;
  28. void *ptr[0];
  29. };
  30. static inline void *net_generic(struct net *net, int id)
  31. {
  32. struct net_generic *ng;
  33. void *ptr;
  34. rcu_read_lock();
  35. ng = rcu_dereference(net->gen);
  36. BUG_ON(id == 0 || id > ng->len);
  37. ptr = ng->ptr[id - 1];
  38. rcu_read_unlock();
  39. return ptr;
  40. }
  41. extern int net_assign_generic(struct net *net, int id, void *data);
  42. #endif