Skip to content

Commit

Permalink
events: ovs: flatten ovs event object
Browse files Browse the repository at this point in the history
Instead of nesting an enum inside a struct, just make the event_section
an enum. This requires a small change in the event_section marco.

This change does not modify the json representation but does improve
python user experience:

OLD:
```
>> In [6]: e["ovs"]
Out[6]: OvsEvent { event: Upcall { upcall: UpcallEvent { cmd: 1, port: 2271867293, cpu: 2 } } }

In [7]: e["ovs"].event
Out[7]: Upcall { upcall: UpcallEvent { cmd: 1, port: 2271867293, cpu: 2 } }

In [8]: e["ovs"].upcall
```
NEW:

```
In [5]: e["ovs"]
Out[5]: Upcall { upcall: UpcallEvent { cmd: 1, port: 2271867293, cpu: 2 } }

In [6]: e["ovs"].upcall
Out[6]: UpcallEvent { cmd: 1, port: 2271867293, cpu: 2 }
```

Signed-off-by: Adrian Moreno <[email protected]>
  • Loading branch information
amorenoz authored and atenart committed Dec 10, 2024
1 parent 8e48422 commit 2886fcb
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 177 deletions.
8 changes: 6 additions & 2 deletions retis-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ pub fn event_section(
args: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let input: ItemStruct = parse_macro_input!(item);
let ident = &input.ident;
let input: Item = parse_macro_input!(item);
let ident = match input {
Item::Struct(ref item) => item.ident.clone(),
Item::Enum(ref item) => item.ident.clone(),
_ => panic!("event types must be enums or structs"),
};

let id: syn::Expr = syn::parse(args).expect("Invalid event id");

Expand Down
142 changes: 57 additions & 85 deletions retis-events/src/ovs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,9 @@ use crate::{event_section, event_type, Formatter};

///The OVS Event
#[event_section(SectionId::Ovs)]
#[derive(PartialEq)]
pub struct OvsEvent {
/// Event data
#[serde(flatten)]
pub event: OvsEventType,
}

impl EventFmt for OvsEvent {
fn event_fmt(&self, f: &mut Formatter, format: &DisplayFormat) -> fmt::Result {
self.event.event_fmt(f, format)
}
}

#[event_type]
#[serde(tag = "event_type")]
#[derive(PartialEq)]
pub enum OvsEventType {
pub enum OvsEvent {
/// Upcall event. It indicates the begining of an upcall. An upcall can have multiple enqueue
/// events.
#[serde(rename = "upcall")]
Expand Down Expand Up @@ -71,9 +57,9 @@ pub enum OvsEventType {
},
}

impl EventFmt for OvsEventType {
impl EventFmt for OvsEvent {
fn event_fmt(&self, f: &mut Formatter, format: &DisplayFormat) -> fmt::Result {
use OvsEventType::*;
use OvsEvent::*;
let disp: &dyn EventFmt = match self {
Upcall { upcall } => upcall,
UpcallEnqueue { upcall_enqueue } => upcall_enqueue,
Expand Down Expand Up @@ -561,110 +547,96 @@ mod tests {
// Upcall event
(
r#"{"cmd":1,"cpu":0,"event_type":"upcall","port":4195744766}"#,
OvsEvent {
event: OvsEventType::Upcall {
upcall: UpcallEvent {
cmd: 1,
cpu: 0,
port: 4195744766,
},
OvsEvent::Upcall {
upcall: UpcallEvent {
cmd: 1,
cpu: 0,
port: 4195744766,
},
},
),
// Action event
(
r#"{"action":"output","event_type":"action_execute","port":2,"queue_id":1361394472,"recirc_id":0}"#,
OvsEvent {
event: OvsEventType::Action {
action_execute: ActionEvent {
action: Some(OvsAction::Output {
output: OvsActionOutput { port: 2 },
}),
recirc_id: 0,
queue_id: Some(1361394472),
},
OvsEvent::Action {
action_execute: ActionEvent {
action: Some(OvsAction::Output {
output: OvsActionOutput { port: 2 },
}),
recirc_id: 0,
queue_id: Some(1361394472),
},
},
),
// Upcall enqueue event
(
r#"{"cmd":1,"event_type":"upcall_enqueue","queue_id":3316322986,"ret":0,"upcall_cpu":0,"port":4195744766,"upcall_ts":61096236973661}"#,
OvsEvent {
event: OvsEventType::UpcallEnqueue {
upcall_enqueue: UpcallEnqueueEvent {
ret: 0,
cmd: 1,
port: 4195744766,
upcall_ts: 61096236973661,
upcall_cpu: 0,
queue_id: 3316322986,
},
OvsEvent::UpcallEnqueue {
upcall_enqueue: UpcallEnqueueEvent {
ret: 0,
cmd: 1,
port: 4195744766,
upcall_ts: 61096236973661,
upcall_cpu: 0,
queue_id: 3316322986,
},
},
),
// Upcall return event
(
r#"{"event_type":"upcall_return","ret":0,"upcall_cpu":0,"upcall_ts":61096236973661}"#,
OvsEvent {
event: OvsEventType::UpcallReturn {
upcall_return: UpcallReturnEvent {
ret: 0,
upcall_ts: 61096236973661,
upcall_cpu: 0,
},
OvsEvent::UpcallReturn {
upcall_return: UpcallReturnEvent {
ret: 0,
upcall_ts: 61096236973661,
upcall_cpu: 0,
},
},
),
// Operation event exec
(
r#"{"batch_idx":0,"batch_ts":61096237019698,"event_type":"flow_operation","op_type":"exec","queue_id":3316322986}"#,
OvsEvent {
event: OvsEventType::Operation {
flow_operation: OperationEvent {
op_type: 0,
queue_id: 3316322986,
batch_ts: 61096237019698,
batch_idx: 0,
},
OvsEvent::Operation {
flow_operation: OperationEvent {
op_type: 0,
queue_id: 3316322986,
batch_ts: 61096237019698,
batch_idx: 0,
},
},
),
// Operation event put
(
r#"{"batch_idx":0,"batch_ts":61096237019698,"event_type":"flow_operation","op_type":"put","queue_id":3316322986}"#,
OvsEvent {
event: OvsEventType::Operation {
flow_operation: OperationEvent {
op_type: 1,
queue_id: 3316322986,
batch_ts: 61096237019698,
batch_idx: 0,
},
OvsEvent::Operation {
flow_operation: OperationEvent {
op_type: 1,
queue_id: 3316322986,
batch_ts: 61096237019698,
batch_idx: 0,
},
},
),
// Conntrack action event
(
r#"{"action":"ct","event_type":"action_execute","flags":485,"nat":{"dir":"dst","max_addr":"10.244.1.30","max_port":36900,"min_addr":"10.244.1.3","min_port":36895},"recirc_id":34,"zone_id":20}"#,
OvsEvent {
event: OvsEventType::Action {
action_execute: ActionEvent {
action: Some(OvsAction::Ct {
ct: OvsActionCt {
zone_id: 20,
flags: 485,
nat: Some(OvsActionCtNat {
dir: Some(NatDirection::Dst),
min_addr: Some(String::from("10.244.1.3")),
max_addr: Some(String::from("10.244.1.30")),
min_port: Some(36895),
max_port: Some(36900),
}),
},
}),
recirc_id: 34,
queue_id: None,
},
OvsEvent::Action {
action_execute: ActionEvent {
action: Some(OvsAction::Ct {
ct: OvsActionCt {
zone_id: 20,
flags: 485,
nat: Some(OvsActionCtNat {
dir: Some(NatDirection::Dst),
min_addr: Some(String::from("10.244.1.3")),
max_addr: Some(String::from("10.244.1.30")),
min_port: Some(36895),
max_port: Some(36900),
}),
},
}),
recirc_id: 34,
queue_id: None,
},
},
),
Expand Down
Loading

0 comments on commit 2886fcb

Please sign in to comment.