-
Notifications
You must be signed in to change notification settings - Fork 5
adding client and nas architecture #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pranav-kumar-hex
wants to merge
1
commit into
master
Choose a base branch
from
wip
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| pub mod app_context; | ||
| mod gnb_context; | ||
| mod ngap_context; | ||
| mod state; | ||
| mod ue_context; | ||
| mod nas_context; | ||
|
|
||
| pub use app_context::AppContext; | ||
| pub use gnb_context::{GnbContext, SupportedTai}; | ||
| pub use ngap_context::NgapContext; | ||
| pub use state::{AtomicGmmState, GmmState}; | ||
| pub use ue_context::UeContext; | ||
| pub use nas_context::NasContext; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| use std::{cell::UnsafeCell, num::NonZeroU32}; | ||
|
|
||
| use derive_new::new; | ||
| use ngap_models::RrcEstablishmentCause; | ||
| use non_empty_string::NonEmptyString; | ||
|
|
||
| use crate::utils::models::FiveGSTmsi; | ||
|
|
||
| #[derive(new)] | ||
| pub struct NasContext { | ||
| pub rrc_establishment_cause: RrcEstablishmentCause, | ||
| pub five_g_s_tmsi: Option<FiveGSTmsi>, | ||
| #[new(default)] | ||
| pub tmsi: Option<NonZeroU32>, | ||
| #[new(default)] | ||
| pub guti: Option<NonEmptyString>, | ||
| #[new(default)] | ||
| pub suci: Option<NonEmptyString>, | ||
| #[new(default)] | ||
| pub pei: Option<NonEmptyString>, | ||
| #[new(default)] | ||
| pub mac_addr: Option<NonEmptyString>, | ||
| #[new(default)] | ||
| pub plmn_id: Option<NonEmptyString>, | ||
| } |
218 changes: 218 additions & 0 deletions
218
lightning-nf/omnipath/app/src/context/state/atomic_gmm_state.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| use std::{ | ||
| fmt, ops::Deref, sync::atomic::{AtomicUsize, Ordering} | ||
| }; | ||
|
|
||
| use super::GmmState; | ||
|
|
||
| /// A custom atomic wrapper for GmmState that performs operations directly on | ||
| /// the enum's discriminant to avoid the potential overhead of `match` | ||
| /// statements. | ||
| /// | ||
| /// The safety of the `unsafe` transmutation relies on the invariant that this | ||
| /// struct only ever stores values that are valid `GmmState` discriminants. | ||
| #[derive(Debug)] | ||
| pub struct AtomicGmmState { | ||
| state: AtomicUsize, | ||
| } | ||
|
|
||
| impl AtomicGmmState { | ||
| /// Creates a new AtomicGmmState with the given initial state. | ||
| pub const fn new(initial_state: GmmState) -> Self { | ||
| Self { | ||
| state: AtomicUsize::new(initial_state as usize), | ||
| } | ||
| } | ||
|
|
||
| /// Loads the current state using `std::mem::transmute`. | ||
| #[inline] | ||
| pub fn load( | ||
| &self, | ||
| ordering: Ordering, | ||
| ) -> GmmState { | ||
| let discriminant = self.state.load(ordering); | ||
| // This assertion ensures that, in debug builds, we panic if the state | ||
| // ever holds an invalid discriminant, which would be undefined behavior. | ||
| debug_assert!( | ||
| discriminant <= GmmState::MAX_DISCRIMINANT as usize, | ||
| "Invalid GmmState discriminant: {}", | ||
| discriminant | ||
| ); | ||
| // Safety: The `debug_assert` and disciplined use of the `store` methods | ||
| // ensure that `state` only contains valid discriminants for `GmmState`. | ||
| // `GmmState` is `#[repr(u8)]`, so transmutation from its discriminant is sound. | ||
| unsafe { std::mem::transmute(discriminant as u8) } | ||
| } | ||
|
|
||
| /// Stores a new state. | ||
| #[inline] | ||
| pub fn store( | ||
| &self, | ||
| new_state: GmmState, | ||
| ordering: Ordering, | ||
| ) { | ||
| self.state.store(new_state as usize, ordering); | ||
| } | ||
|
|
||
| /// Atomically swaps the state and returns the previous state. | ||
| #[inline] | ||
| pub fn swap( | ||
| &self, | ||
| new_state: GmmState, | ||
| ordering: Ordering, | ||
| ) -> GmmState { | ||
| let old_discriminant = self.state.swap(new_state as usize, ordering); | ||
| debug_assert!( | ||
| old_discriminant <= GmmState::MAX_DISCRIMINANT as usize, | ||
| "Invalid GmmState discriminant: {}", | ||
| old_discriminant | ||
| ); | ||
| // Safety: Same justification as `load`. | ||
| unsafe { std::mem::transmute(old_discriminant as u8) } | ||
| } | ||
|
|
||
| /// A helper function to reduce code duplication in `compare_exchange` | ||
| /// methods. | ||
| #[inline] | ||
| fn transmute_result(result: Result<usize, usize>) -> Result<GmmState, GmmState> { | ||
| match result { | ||
| Ok(prev) => { | ||
| debug_assert!(prev <= GmmState::MAX_DISCRIMINANT as usize); | ||
| Ok(unsafe { std::mem::transmute(prev as u8) }) | ||
| } | ||
| Err(actual) => { | ||
| debug_assert!(actual <= GmmState::MAX_DISCRIMINANT as usize); | ||
| Err(unsafe { std::mem::transmute(actual as u8) }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Atomically compares the current state with `current` and, if they match, | ||
| /// replaces it with `new`. | ||
| #[inline] | ||
| pub fn compare_exchange( | ||
| &self, | ||
| current: GmmState, | ||
| new: GmmState, | ||
| success: Ordering, | ||
| failure: Ordering, | ||
| ) -> Result<GmmState, GmmState> { | ||
| let result = self | ||
| .state | ||
| .compare_exchange(current as usize, new as usize, success, failure); | ||
| Self::transmute_result(result) | ||
| } | ||
|
|
||
| /// Performs a weak compare-and-exchange operation. | ||
| #[inline] | ||
| pub fn compare_exchange_weak( | ||
| &self, | ||
| current: GmmState, | ||
| new: GmmState, | ||
| success: Ordering, | ||
| failure: Ordering, | ||
| ) -> Result<GmmState, GmmState> { | ||
| let result = | ||
| self.state | ||
| .compare_exchange_weak(current as usize, new as usize, success, failure); | ||
| Self::transmute_result(result) | ||
| } | ||
|
|
||
| /// Atomically modifies the state with a given function. | ||
| #[inline] | ||
| pub fn fetch_update<F>( | ||
| &self, | ||
| set_order: Ordering, | ||
| fetch_order: Ordering, | ||
| mut f: F, | ||
| ) -> Result<GmmState, GmmState> | ||
| where | ||
| F: FnMut(GmmState) -> Option<GmmState>, | ||
| { | ||
| let result = self | ||
| .state | ||
| .fetch_update(set_order, fetch_order, |discriminant| { | ||
| debug_assert!(discriminant <= GmmState::MAX_DISCRIMINANT as usize); | ||
| // Safety: Same justification as `load`. | ||
| let current_state = unsafe { std::mem::transmute(discriminant as u8) }; | ||
| f(current_state).map(|new_state| new_state as usize) | ||
| }); | ||
| Self::transmute_result(result) | ||
| } | ||
|
|
||
| // --- Convenience Methods --- | ||
|
|
||
| /// Gets the current state using `Acquire` ordering. | ||
| #[inline] | ||
| pub fn get(&self) -> GmmState { | ||
| self.load(Ordering::Acquire) | ||
| } | ||
|
|
||
| /// Sets the current state using `Release` ordering. | ||
| #[inline] | ||
| pub fn set( | ||
| &self, | ||
| state: GmmState, | ||
| ) { | ||
| self.store(state, Ordering::Release); | ||
| } | ||
|
|
||
| /// Checks if the current state matches the given state. | ||
| #[inline] | ||
| pub fn is( | ||
| &self, | ||
| expected: GmmState, | ||
| ) -> bool { | ||
| self.get() == expected | ||
| } | ||
|
|
||
| /// Checks if the UE can access services in the current state. | ||
| #[inline] | ||
| pub fn can_access_services(&self) -> bool { | ||
| self.get().can_access_services() | ||
| } | ||
|
|
||
| /// Checks if the current state is transitional. | ||
| #[inline] | ||
| pub fn is_transitional(&self) -> bool { | ||
| self.get().is_transitional() | ||
| } | ||
|
|
||
| } | ||
|
|
||
| impl Default for AtomicGmmState { | ||
| fn default() -> Self { | ||
| Self::new(GmmState::default()) | ||
| } | ||
| } | ||
|
|
||
| impl Deref for AtomicGmmState { | ||
| type Target = AtomicUsize; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.state | ||
| } | ||
| } | ||
|
|
||
| impl Clone for AtomicGmmState { | ||
| /// Clones the `AtomicGmmState` by creating a new atomic variable | ||
| /// initialized with the current state's value. | ||
| fn clone(&self) -> Self { | ||
| Self::new(self.get()) | ||
| } | ||
| } | ||
|
|
||
| impl From<GmmState> for AtomicGmmState { | ||
| fn from(state: GmmState) -> Self { | ||
| Self::new(state) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for AtomicGmmState { | ||
| fn fmt( | ||
| &self, | ||
| f: &mut fmt::Formatter<'_>, | ||
| ) -> fmt::Result { | ||
| // Delegate formatting to the underlying GmmState's Debug or Display impl. | ||
| write!(f, "{:?}", self.get()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Derefleaks the rawAtomicUsizeand breaks your invariantsBy exposing
&AtomicUsize, callers can invoke operations likefetch_addorstorewith arbitrary values, producing invalid discriminants and triggering UB onceload()transmutes them. This defeats the entire safety contract ofAtomicGmmState. Please drop theDerefimpl (or expose only constrained helpers) so the raw atomic ops remain encapsulated.📝 Committable suggestion
🤖 Prompt for AI Agents