Skip to content

Commit cb60aa4

Browse files
committed
events: ovs: Use Option instead of unit enum value.
OVS module currently makes no assumptions on the order of the event chunks and tries to build the event even if out-of-order pieces are received. This makes little sense in practice as these chuncks are sent in the same hook. Removing that unneeded requirement and assuming the base action event (OVS_DP_ACTION) will be received before specific action argument events (e.g: OVS_DP_ACTION_OUTPUT) makes decoding simpler. This also it avoids requiring a Default version of the event which is currently implemented using an "undefined" value for enums. This mechanism is not supported by pyo3. Also, create a dummy object to avoid having mixed complex unit variants in enums. [1] Upstream discussions: PyO3/pyo3#3582 (comment) PyO3/pyo3#3749 Signed-off-by: Adrian Moreno <[email protected]>
1 parent 33c5508 commit cb60aa4

File tree

3 files changed

+168
-202
lines changed

3 files changed

+168
-202
lines changed

retis-events/src/ovs.rs

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::*;
77
use crate::{event_section, event_type, Formatter};
88

99
///The OVS Event
10-
#[derive(Default, PartialEq)]
10+
#[derive(PartialEq)]
1111
#[event_section("ovs")]
1212
pub struct OvsEvent {
1313
/// Event data
@@ -23,7 +23,7 @@ impl EventFmt for OvsEvent {
2323

2424
#[event_type]
2525
#[serde(tag = "event_type")]
26-
#[derive(Default, PartialEq)]
26+
#[derive(PartialEq)]
2727
pub enum OvsEventType {
2828
/// Upcall event. It indicates the begining of an upcall. An upcall can have multiple enqueue
2929
/// events.
@@ -51,10 +51,6 @@ pub enum OvsEventType {
5151
/// Action execution event. It indicates the datapath has executed an action on a packet.
5252
#[serde(rename = "action_execute")]
5353
Action(ActionEvent),
54-
55-
#[serde(rename = "undefined")]
56-
#[default]
57-
Undefined,
5854
}
5955

6056
impl EventFmt for OvsEventType {
@@ -67,7 +63,6 @@ impl EventFmt for OvsEventType {
6763
RecvUpcall(e) => e,
6864
Operation(e) => e,
6965
Action(e) => e,
70-
Undefined => return write!(f, "?"),
7166
};
7267

7368
disp.event_fmt(f, format)
@@ -266,7 +261,7 @@ impl EventFmt for RecvUpcallEvent {
266261
pub struct ActionEvent {
267262
/// Action to be executed.
268263
#[serde(flatten)]
269-
pub action: OvsAction,
264+
pub action: Option<OvsAction>,
270265
/// Recirculation id.
271266
pub recirc_id: u32,
272267
/// Queue id used for tracking. None if not tracking or if the output event did not come from
@@ -284,19 +279,18 @@ impl EventFmt for ActionEvent {
284279
write!(f, "exec")?;
285280

286281
match &self.action {
287-
OvsAction::Unspecified => write!(f, " (unspecified)")?,
288-
OvsAction::Output(a) => write!(f, " oport {}", a.port)?,
289-
OvsAction::Userspace => write!(f, " userspace")?,
290-
OvsAction::Set => write!(f, " tunnel_set")?,
291-
OvsAction::PushVlan => write!(f, " push_vlan")?,
292-
OvsAction::PopVlan => write!(f, " pop_vlan")?,
293-
OvsAction::Sample => write!(f, " sample")?,
294-
OvsAction::Recirc(a) => write!(f, " recirc {:#x}", a.id)?,
295-
OvsAction::Hash => write!(f, " hash")?,
296-
OvsAction::PushMpls => write!(f, " push_mpls")?,
297-
OvsAction::PopMpls => write!(f, " pop_mpls")?,
298-
OvsAction::SetMasked => write!(f, " set_masked")?,
299-
OvsAction::Ct(ct) => {
282+
Some(OvsAction::Output(a)) => write!(f, " oport {}", a.port)?,
283+
Some(OvsAction::Userspace(_)) => write!(f, " userspace")?,
284+
Some(OvsAction::Set(_)) => write!(f, " tunnel_set")?,
285+
Some(OvsAction::PushVlan(_)) => write!(f, " push_vlan")?,
286+
Some(OvsAction::PopVlan(_)) => write!(f, " pop_vlan")?,
287+
Some(OvsAction::Sample(_)) => write!(f, " sample")?,
288+
Some(OvsAction::Recirc(a)) => write!(f, " recirc {:#x}", a.id)?,
289+
Some(OvsAction::Hash(_)) => write!(f, " hash")?,
290+
Some(OvsAction::PushMpls(_)) => write!(f, " push_mpls")?,
291+
Some(OvsAction::PopMpls(_)) => write!(f, " pop_mpls")?,
292+
Some(OvsAction::SetMasked(_)) => write!(f, " set_masked")?,
293+
Some(OvsAction::Ct(ct)) => {
300294
write!(f, " ct zone {}", ct.zone_id)?;
301295

302296
if let Some(nat) = &ct.nat {
@@ -358,17 +352,18 @@ impl EventFmt for ActionEvent {
358352
write!(f, " {}", flags.join(","))?;
359353
}
360354
}
361-
OvsAction::Trunc => write!(f, " trunc")?,
362-
OvsAction::PushEth => write!(f, " push_eth")?,
363-
OvsAction::PopEth => write!(f, " pop_eth")?,
364-
OvsAction::CtClear => write!(f, " ct_clear")?,
365-
OvsAction::PushNsh => write!(f, " push_nsh")?,
366-
OvsAction::PopNsh => write!(f, " pop_nsh")?,
367-
OvsAction::Meter => write!(f, " meter")?,
368-
OvsAction::Clone => write!(f, " clone")?,
369-
OvsAction::CheckPktLen => write!(f, " check_pkt_len")?,
370-
OvsAction::AddMpls => write!(f, " add_mpls")?,
371-
OvsAction::DecTtl => write!(f, " dec_ttl")?,
355+
Some(OvsAction::Trunc(_)) => write!(f, " trunc")?,
356+
Some(OvsAction::PushEth(_)) => write!(f, " push_eth")?,
357+
Some(OvsAction::PopEth(_)) => write!(f, " pop_eth")?,
358+
Some(OvsAction::CtClear(_)) => write!(f, " ct_clear")?,
359+
Some(OvsAction::PushNsh(_)) => write!(f, " push_nsh")?,
360+
Some(OvsAction::PopNsh(_)) => write!(f, " pop_nsh")?,
361+
Some(OvsAction::Meter(_)) => write!(f, " meter")?,
362+
Some(OvsAction::Clone(_)) => write!(f, " clone")?,
363+
Some(OvsAction::CheckPktLen(_)) => write!(f, " check_pkt_len")?,
364+
Some(OvsAction::AddMpls(_)) => write!(f, " add_mpls")?,
365+
Some(OvsAction::DecTtl(_)) => write!(f, " dec_ttl")?,
366+
None => write!(f, " unspec")?,
372367
}
373368

374369
if let Some(p) = self.queue_id {
@@ -379,59 +374,62 @@ impl EventFmt for ActionEvent {
379374
}
380375
}
381376

377+
// Adding unit values in an otherwise complex is not supported by pyo3.
378+
// FIXME: Remove when arguments from all actions are implemented.
379+
#[event_type]
380+
#[derive(PartialEq)]
381+
pub struct OvsDummyAction;
382+
382383
#[event_type]
383384
#[serde(tag = "action")]
384-
#[derive(Default, PartialEq)]
385+
#[derive(PartialEq)]
385386
pub enum OvsAction {
386-
#[serde(rename = "unspecified")]
387-
#[default]
388-
Unspecified,
389387
#[serde(rename = "output")]
390388
Output(OvsActionOutput),
391389
#[serde(rename = "userspace")]
392-
Userspace,
390+
Userspace(OvsDummyAction),
393391
#[serde(rename = "set")]
394-
Set,
392+
Set(OvsDummyAction),
395393
#[serde(rename = "push_vlan")]
396-
PushVlan,
394+
PushVlan(OvsDummyAction),
397395
#[serde(rename = "pop_vlan")]
398-
PopVlan,
396+
PopVlan(OvsDummyAction),
399397
#[serde(rename = "sample")]
400-
Sample,
398+
Sample(OvsDummyAction),
401399
#[serde(rename = "recirc")]
402400
Recirc(OvsActionRecirc),
403401
#[serde(rename = "hash")]
404-
Hash,
402+
Hash(OvsDummyAction),
405403
#[serde(rename = "push_mpls")]
406-
PushMpls,
404+
PushMpls(OvsDummyAction),
407405
#[serde(rename = "pop_mpls")]
408-
PopMpls,
406+
PopMpls(OvsDummyAction),
409407
#[serde(rename = "set_masked")]
410-
SetMasked,
408+
SetMasked(OvsDummyAction),
411409
#[serde(rename = "ct")]
412410
Ct(OvsActionCt),
413411
#[serde(rename = "trunc")]
414-
Trunc,
412+
Trunc(OvsDummyAction),
415413
#[serde(rename = "push_eth")]
416-
PushEth,
414+
PushEth(OvsDummyAction),
417415
#[serde(rename = "pop_eth")]
418-
PopEth,
416+
PopEth(OvsDummyAction),
419417
#[serde(rename = "ct_clear")]
420-
CtClear,
418+
CtClear(OvsDummyAction),
421419
#[serde(rename = "push_nsh")]
422-
PushNsh,
420+
PushNsh(OvsDummyAction),
423421
#[serde(rename = "pop_nsh")]
424-
PopNsh,
422+
PopNsh(OvsDummyAction),
425423
#[serde(rename = "meter")]
426-
Meter,
424+
Meter(OvsDummyAction),
427425
#[serde(rename = "clone")]
428-
Clone,
426+
Clone(OvsDummyAction),
429427
#[serde(rename = "check_pkt_len")]
430-
CheckPktLen,
428+
CheckPktLen(OvsDummyAction),
431429
#[serde(rename = "add_mpls")]
432-
AddMpls,
430+
AddMpls(OvsDummyAction),
433431
#[serde(rename = "dec_ttl")]
434-
DecTtl,
432+
DecTtl(OvsDummyAction),
435433
}
436434

437435
/// OVS output action data.
@@ -555,7 +553,7 @@ mod tests {
555553
r#"{"action":"output","event_type":"action_execute","port":2,"queue_id":1361394472,"recirc_id":0}"#,
556554
OvsEvent {
557555
event: OvsEventType::Action(ActionEvent {
558-
action: OvsAction::Output(OvsActionOutput { port: 2 }),
556+
action: Some(OvsAction::Output(OvsActionOutput { port: 2 })),
559557
recirc_id: 0,
560558
queue_id: Some(1361394472),
561559
}),
@@ -615,7 +613,7 @@ mod tests {
615613
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}"#,
616614
OvsEvent {
617615
event: OvsEventType::Action(ActionEvent {
618-
action: OvsAction::Ct(OvsActionCt {
616+
action: Some(OvsAction::Ct(OvsActionCt {
619617
zone_id: 20,
620618
flags: 485,
621619
nat: Some(OvsActionCtNat {
@@ -625,7 +623,7 @@ mod tests {
625623
min_port: Some(36895),
626624
max_port: Some(36900),
627625
}),
628-
}),
626+
})),
629627
recirc_id: 34,
630628
queue_id: None,
631629
}),

0 commit comments

Comments
 (0)