netlink_proto.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. use bitmap::{traits::BitMapOps, AllocBitmap};
  2. use core::intrinsics::unlikely;
  3. use system_error::SystemError;
  4. use crate::libs::lazy_init::Lazy;
  5. pub const PROTO_INUSE_NR: usize = 64;
  6. // pub static mut PROTO_INUSE_IDX: Lazy<AllocBitmap> = Lazy::new();
  7. // pub static PROTO_INUSE_IDX: Lazy<AllocBitmap> = Lazy::new(<AllocBitmap::new(PROTO_INUSE_NR));
  8. /// 协议操作集的trait
  9. pub trait Protocol {
  10. fn close(&self);
  11. // fn first_false_index(&self, proto_inuse_idx:usize, proto_inuse_nr:usize)->usize;
  12. }
  13. /// 协议操作集的结构体
  14. pub struct Proto<'a> {
  15. name: &'a str,
  16. // owner: THIS_MODULE,
  17. obj_size: usize,
  18. inuse_idx: Option<usize>,
  19. }
  20. impl Protocol for Proto<'_> {
  21. fn close(&self) {}
  22. }
  23. /// 静态变量,用于注册netlink协议,是一个操作集结构体的实例
  24. // https://code.dragonos.org.cn/xref/linux-6.1.9/net/netlink/af_netlink.c#634
  25. pub static mut NETLINK_PROTO: Proto = Proto {
  26. name: "NETLINK",
  27. // owner: THIS_MODULE,
  28. obj_size: core::mem::size_of::<Proto>(),
  29. // 运行时分配的索引
  30. inuse_idx: None,
  31. };
  32. // https://code.dragonos.org.cn/xref/linux-6.1.9/net/core/sock.c?fi=proto_register#3853
  33. /// 注册协议
  34. pub fn proto_register(proto: &mut Proto, alloc_slab: i32) -> Result<i32, SystemError> {
  35. let mut ret = Err(SystemError::ENOBUFS);
  36. if alloc_slab != 0 {
  37. log::info!("TODO: netlink_proto: slab allocation not supported\n");
  38. return ret;
  39. }
  40. ret = assign_proto_idx(proto);
  41. ret
  42. }
  43. // https://code.dragonos.org.cn/xref/linux-6.1.9/net/core/sock.c?fi=proto_register#3752
  44. /// 为协议分配一个索引
  45. pub fn assign_proto_idx(prot: &mut Proto) -> Result<i32, SystemError> {
  46. // prot.inuse_idx = unsafe { PROTO_INUSE_IDX.first_false_index() };
  47. // 如果没有找到空闲的索引
  48. if unlikely(prot.inuse_idx == Some(PROTO_INUSE_NR - 1)) {
  49. log::info!("PROTO_INUSE_NR exhausted\n");
  50. return Err(SystemError::ENOSPC);
  51. }
  52. // 为协议分配一个索引
  53. // unsafe { PROTO_INUSE_IDX.set((prot.inuse_idx).unwrap(), true) };
  54. return Ok(0);
  55. }