cgroup_sockopt.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //! Cgroup socket option programs.
  2. use alloc::{borrow::ToOwned, string::String};
  3. use crate::{
  4. generated::bpf_attach_type,
  5. thiserror::{self, Error},
  6. };
  7. /// Defines where to attach a `CgroupSockopt` program.
  8. #[derive(Copy, Clone, Debug)]
  9. pub enum CgroupSockoptAttachType {
  10. /// Attach to GetSockopt.
  11. Get,
  12. /// Attach to SetSockopt.
  13. Set,
  14. }
  15. impl From<CgroupSockoptAttachType> for bpf_attach_type {
  16. fn from(s: CgroupSockoptAttachType) -> bpf_attach_type {
  17. match s {
  18. CgroupSockoptAttachType::Get => bpf_attach_type::BPF_CGROUP_GETSOCKOPT,
  19. CgroupSockoptAttachType::Set => bpf_attach_type::BPF_CGROUP_SETSOCKOPT,
  20. }
  21. }
  22. }
  23. #[derive(Debug, Error)]
  24. #[error("{0} is not a valid attach type for a CGROUP_SOCKOPT program")]
  25. pub(crate) struct InvalidAttachType(String);
  26. impl CgroupSockoptAttachType {
  27. pub(crate) fn try_from(value: &str) -> Result<CgroupSockoptAttachType, InvalidAttachType> {
  28. match value {
  29. "getsockopt" => Ok(CgroupSockoptAttachType::Get),
  30. "setsockopt" => Ok(CgroupSockoptAttachType::Set),
  31. _ => Err(InvalidAttachType(value.to_owned())),
  32. }
  33. }
  34. }