forked from oxidecomputer/p4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.rs
1087 lines (951 loc) · 34.7 KB
/
pipeline.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022 Oxide Computer Company
use crate::{
qualified_table_function_name, qualified_table_name, rust_type,
type_size_bytes, Context, Settings,
};
use p4::ast::{
Control, Direction, MatchKind, PackageInstance, Parser, Table, Type, AST,
};
use p4::hlir::Hlir;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
pub(crate) struct PipelineGenerator<'a> {
ast: &'a AST,
ctx: &'a mut Context,
hlir: &'a Hlir,
settings: &'a Settings,
}
impl<'a> PipelineGenerator<'a> {
pub(crate) fn new(
ast: &'a AST,
hlir: &'a Hlir,
ctx: &'a mut Context,
settings: &'a Settings,
) -> Self {
Self {
ast,
hlir,
ctx,
settings,
}
}
pub(crate) fn generate(&mut self) {
if let Some(ref inst) = self.ast.package_instance {
self.generate_pipeline(inst);
}
}
pub(crate) fn generate_pipeline(&mut self, inst: &PackageInstance) {
//TODO check instance against package definition instead of hardcoding
//SoftNPU in below. This begs the question, will the Rust back end
//support something besides SoftNPU. Probably not for the forseeable
//future. However it could be interesting to support different package
//structures to emulate different types of hardware. For example the
//Tofino package instance has a relatively complex pipeline structure.
//And it could be interesting/useful to evaluate workloads running
//within SoftNPU that exploit that structure.
if inst.instance_type != "SoftNPU" {
//TODO check this in the checker for a nicer failure mode.
panic!("Only the SoftNPU package is supported");
}
if inst.parameters.len() != 3 {
//TODO check this in the checker for a nicer failure mode.
panic!("SoftNPU instances take exactly 3 parameters");
}
let parser = match self.ast.get_parser(&inst.parameters[0]) {
Some(p) => p,
None => {
//TODO check this in the checker for a nicer failure mode.
panic!("First argument to SoftNPU must be a parser");
}
};
let ingress = match self.ast.get_control(&inst.parameters[1]) {
Some(c) => c,
None => {
//TODO check this in the checker for a nicer failure mode.
panic!("Second argument to SoftNPU must be a control block");
}
};
let egress = match self.ast.get_control(&inst.parameters[2]) {
Some(c) => c,
None => {
//TODO check this in the checker for a nicer failure mode.
panic!("Third argument to SoftNPU must be a control block");
}
};
let pipeline_name = format_ident!("{}_pipeline", inst.name);
//
// get table members and initializers from both the ingress and egress
// controllers.
//
let (mut table_members, mut table_initializers) =
self.table_members(ingress);
let (egress_table_members, egress_table_initializers) =
self.table_members(egress);
table_members.extend_from_slice(&egress_table_members);
table_initializers.extend_from_slice(&egress_table_initializers);
//
// parser, ingress and egress function members
//
let (parse_member, parser_initializer) = self.parse_entrypoint(parser);
let (ingress_member, ingress_initializer) =
self.control_entrypoint("ingress", ingress);
let (egress_member, egress_initializer) =
self.control_entrypoint("egress", egress);
let (pipeline_impl_process_packet, process_packet_headers) =
self.pipeline_impl_process_packet(parser, ingress, egress);
let add_table_entry_method =
self.add_table_entry_method(ingress, egress);
let remove_table_entry_method =
self.remove_table_entry_method(ingress, egress);
let get_table_entries_method =
self.get_table_entries_method(ingress, egress);
let get_table_ids_method = self.get_table_ids_method(ingress, egress);
let table_modifiers = self.table_modifiers(ingress, egress);
let c_create_fn =
format_ident!("_{}_pipeline_create", self.settings.pipeline_name);
let pipeline = quote! {
pub struct #pipeline_name {
#(#table_members,)*
#parse_member,
#ingress_member,
#egress_member,
radix: u16,
}
impl #pipeline_name {
pub fn new(radix: u16) -> Self {
usdt::register_probes().unwrap();
Self {
#(#table_initializers,)*
#parser_initializer,
#ingress_initializer,
#egress_initializer,
radix,
}
}
#process_packet_headers
#table_modifiers
}
impl p4rs::Pipeline for #pipeline_name {
#pipeline_impl_process_packet
#add_table_entry_method
#remove_table_entry_method
#get_table_entries_method
#get_table_ids_method
}
unsafe impl Send for #pipeline_name { }
#[no_mangle]
pub extern "C" fn #c_create_fn(radix: u16)
-> *mut dyn p4rs::Pipeline{
let pipeline = main_pipeline::new(radix);
let boxpipe: Box<dyn p4rs::Pipeline> = Box::new(pipeline);
Box::into_raw(boxpipe)
}
};
self.ctx.pipelines.insert(inst.name.clone(), pipeline);
}
fn pipeline_impl_process_packet(
&mut self,
parser: &Parser,
ingress: &Control,
egress: &Control,
) -> (TokenStream, TokenStream) {
let parsed_type = rust_type(&parser.parameters[1].ty);
// determine table arguments
let ingress_tables = ingress.tables(self.ast);
//TODO(dry)
let mut ingress_tbl_args = Vec::new();
for (cs, t) in ingress_tables {
let qtfn = qualified_table_function_name(Some(ingress), &cs, t);
let name = format_ident!("{}", qtfn);
ingress_tbl_args.push(quote! {
&self.#name
});
}
let egress_tables = egress.tables(self.ast);
let mut egress_tbl_args = Vec::new();
for (cs, t) in egress_tables {
let qtfn = qualified_table_function_name(Some(egress), &cs, t);
let name = format_ident!("{}", qtfn);
egress_tbl_args.push(quote! {
&self.#name
});
}
let process_packet = quote! {
fn process_packet<'a>(
&mut self,
port: u16,
pkt: &mut packet_in<'a>,
) -> Vec<(packet_out<'a>, u16)> {
//
// Instantiate the parser out type
//
let mut parsed = #parsed_type::default();
//
// Instantiate ingress/egress metadata
//
let mut ingress_metadata = ingress_metadata_t{
port: {
let mut x = bitvec![mut u8, Msb0; 0; 16];
x.store_le(port);
x
},
..Default::default()
};
let mut egress_metadata = egress_metadata_t::default();
//
// Run the parser block
//
let accept = (self.parse)(pkt, &mut parsed, &mut ingress_metadata);
if !accept {
// drop the packet
softnpu_provider::parser_dropped!(||());
return Vec::new();
}
let dump = format!("\n{}", parsed.dump());
softnpu_provider::parser_accepted!(||(&dump));
//
// Calculate parsed header size
//
let parsed_size = parsed.valid_header_size() >> 3;
//
// Run the ingress block
//
(self.ingress)(
&mut parsed,
&mut ingress_metadata,
&mut egress_metadata,
#(#ingress_tbl_args),*
);
//
// Determine egress ports
//
let ports = if egress_metadata.broadcast {
let mut ports = Vec::new();
for p in 0..self.radix {
if p == port {
continue;
}
ports.push(p);
}
ports
} else {
if egress_metadata.port.is_empty() || egress_metadata.drop {
Vec::new()
} else {
vec![egress_metadata.port.load_le()]
}
};
let dump = parsed.dump();
if ports.is_empty() {
softnpu_provider::ingress_dropped!(||(&dump));
return Vec::new();
}
let dump = format!("\n{}", parsed.dump());
softnpu_provider::ingress_accepted!(||(&dump));
//
// Run output of ingress block through egress block on each
// egress port.
//
let mut result = Vec::new();
for eport in ports {
let mut egm = egress_metadata.clone();
let mut parsed_ = parsed.clone();
//
// Run the egress block
//
egm.port = {
let mut x = bitvec![mut u8, Msb0; 0; 16];
x.store_le(eport);
x
};
(self.egress)(
&mut parsed_,
&mut ingress_metadata,
&mut egm,
#(#egress_tbl_args),*
);
if egm.drop {
continue;
}
//
// Create the packet output.
//
let bv = parsed_.to_bitvec();
let buf = bv.as_raw_slice();
let out = packet_out{
header_data: buf.to_owned(),
payload_data: &pkt.data[parsed_size..],
};
result.push((out, eport))
}
result
}
};
//TODO factor out commonalities with process_packet
let process_packet_headers = quote! {
fn process_packet_headers<'a>(
&mut self,
port: u16,
pkt: &mut packet_in<'a>,
) -> Vec<(#parsed_type, u16)> {
//
// Instantiate the parser out type
//
let mut parsed = #parsed_type::default();
//
// Instantiate ingress/egress metadata
//
let mut ingress_metadata = ingress_metadata_t{
port: {
let mut x = bitvec![mut u8, Msb0; 0; 16];
x.store_le(port);
x
},
..Default::default()
};
let mut egress_metadata = egress_metadata_t::default();
//
// Run the parser block
//
let accept = (self.parse)(pkt, &mut parsed, &mut ingress_metadata);
if !accept {
// drop the packet
softnpu_provider::parser_dropped!(||());
return Vec::new();
}
let dump = format!("\n{}", parsed.dump());
softnpu_provider::parser_accepted!(||(&dump));
//
// Calculate parsed header size
//
let parsed_size = parsed.valid_header_size() >> 3;
//
// Run the ingress block
//
(self.ingress)(
&mut parsed,
&mut ingress_metadata,
&mut egress_metadata,
#(#ingress_tbl_args),*
);
//
// Determine egress ports
//
let ports = if egress_metadata.broadcast {
let mut ports = Vec::new();
for p in 0..self.radix {
if p == port {
continue;
}
ports.push(p);
}
ports
} else {
if egress_metadata.port.is_empty() || egress_metadata.drop {
Vec::new()
} else {
vec![egress_metadata.port.load_le()]
}
};
let dump = parsed.dump();
if ports.is_empty() {
softnpu_provider::ingress_dropped!(||(&dump));
return Vec::new();
}
let dump = format!("\n{}", parsed.dump());
softnpu_provider::ingress_accepted!(||(&dump));
//
// Run output of ingress block through egress block on each
// egress port.
//
let mut result = Vec::new();
for eport in ports {
let mut egm = egress_metadata.clone();
let mut parsed_ = parsed.clone();
//
// Run the egress block
//
egm.port = {
let mut x = bitvec![mut u8, Msb0; 0; 16];
x.store_le(eport);
x
};
(self.egress)(
&mut parsed_,
&mut ingress_metadata,
&mut egm,
#(#egress_tbl_args),*
);
if egm.drop {
continue;
}
//
// Create the packet output.
//
result.push((parsed_, eport))
}
result
}
};
(process_packet, process_packet_headers)
}
pub(crate) fn table_members(
&mut self,
control: &Control,
) -> (Vec<TokenStream>, Vec<TokenStream>) {
let mut members = Vec::new();
let mut initializers = Vec::new();
let mut cg =
crate::ControlGenerator::new(self.ast, self.hlir, self.ctx);
let tables = control.tables(self.ast);
for (cs, table) in tables {
let table_control = cs.last().unwrap().1;
let qtn = qualified_table_function_name(Some(control), &cs, table);
let fqtn = qualified_table_function_name(Some(control), &cs, table);
let (_, mut param_types) = cg.control_parameters(table_control);
for var in &table_control.variables {
if let Type::UserDefined(typename) = &var.ty {
if self.ast.get_extern(typename).is_some() {
let extern_type = format_ident!("{}", typename);
param_types.push(quote! {
&p4rs::externs::#extern_type
})
}
}
}
let n = table.key.len();
let table_type = quote! {
p4rs::table::Table::<
#n,
std::sync::Arc<dyn Fn(#(#param_types),*)>
>
};
let qtn = format_ident!("{}", qtn);
let fqtn = format_ident!("{}", fqtn);
members.push(quote! {
pub #qtn: #table_type
});
initializers.push(quote! {
#qtn: #fqtn()
})
}
(members, initializers)
}
fn add_table_entry_method(
&mut self,
ingress: &Control,
egress: &Control,
) -> TokenStream {
let mut body = TokenStream::new();
for control in &[ingress, egress] {
let tables = control.tables(self.ast);
for (cs, table) in tables.iter() {
let qtn = qualified_table_name(Some(control), cs, table);
let qtfn =
qualified_table_function_name(Some(control), cs, table);
let call = format_ident!("add_{}_entry", qtfn);
body.extend(quote! {
#qtn => self.#call(
action_id,
keyset_data,
parameter_data,
priority,
),
});
}
}
body.extend(quote! {
x => println!("add table entry: unknown table id {}, ignoring", x),
});
quote! {
fn add_table_entry(
&mut self,
table_id: &str,
action_id: &str,
keyset_data: &[u8],
parameter_data: &[u8],
priority: u32,
) {
match table_id {
#body
}
}
}
}
fn remove_table_entry_method(
&mut self,
ingress: &Control,
egress: &Control,
) -> TokenStream {
let mut body = TokenStream::new();
for control in &[ingress, egress] {
let tables = control.tables(self.ast);
for (cs, table) in tables.iter() {
let qtn = qualified_table_name(Some(control), cs, table);
let qftn =
qualified_table_function_name(Some(control), cs, table);
let call = format_ident!("remove_{}_entry", qftn);
body.extend(quote! {
#qtn => self.#call(keyset_data),
});
}
}
body.extend(quote!{
x => println!("remove table entry: unknown table id {}, ignoring", x),
});
quote! {
fn remove_table_entry(
&mut self,
table_id: &str,
keyset_data: &[u8],
) {
match table_id {
#body
}
}
}
}
fn get_table_ids_method(
&mut self,
ingress: &Control,
egress: &Control,
) -> TokenStream {
let mut names = Vec::new();
for control in &[ingress, egress] {
let tables = control.tables(self.ast);
for (cs, table) in &tables {
names.push(qualified_table_name(Some(control), cs, table));
}
}
quote! {
fn get_table_ids(&self) -> Vec<&str> {
vec![#(#names),*]
}
}
}
fn get_table_entries_method(
&mut self,
ingress: &Control,
egress: &Control,
) -> TokenStream {
let mut body = TokenStream::new();
for control in &[ingress, egress] {
let tables = control.tables(self.ast);
for (cs, table) in tables.iter() {
let qtn = qualified_table_name(Some(control), cs, table);
let qtfn =
qualified_table_function_name(Some(control), cs, table);
let call = format_ident!("get_{}_entries", qtfn);
body.extend(quote! {
#qtn => Some(self.#call()),
});
}
}
body.extend(quote! {
x => None,
});
quote! {
fn get_table_entries(
&self,
table_id: &str,
) -> Option<Vec<p4rs::TableEntry>> {
match table_id {
#body
}
}
}
}
fn table_modifiers(
&mut self,
ingress: &Control,
egress: &Control,
) -> TokenStream {
let mut tokens = TokenStream::new();
self.table_modifiers_for_control(&mut tokens, ingress);
self.table_modifiers_for_control(&mut tokens, egress);
tokens
}
fn table_modifiers_for_control(
&mut self,
tokens: &mut TokenStream,
control: &Control,
) {
let tables = control.tables(self.ast);
for (cs, table) in tables {
let table_control = cs.last().unwrap().1;
let qtfn = qualified_table_function_name(Some(control), &cs, table);
tokens.extend(self.add_table_entry_function(
table,
table_control,
&qtfn,
));
tokens.extend(self.remove_table_entry_function(
table,
table_control,
&qtfn,
));
tokens.extend(self.get_table_entries_function(
table,
table_control,
&qtfn,
));
}
}
fn table_entry_keys(&mut self, table: &Table) -> Vec<TokenStream> {
let mut keys = Vec::new();
let mut offset: usize = 0;
for (lval, match_kind) in &table.key {
let name_info =
self.hlir.lvalue_decls.get(lval).unwrap_or_else(|| {
panic!("declaration info for {:#?}", lval,)
});
let sz = type_size_bytes(&name_info.ty, self.ast);
match match_kind {
MatchKind::Exact => keys.push(quote! {
p4rs::extract_exact_key(
keyset_data,
#offset,
#sz,
)
}),
MatchKind::Ternary => {
keys.push(quote! {
p4rs::extract_ternary_key(
keyset_data,
#offset,
#sz,
)
});
offset += 1; // for care/dontcare indicator
}
MatchKind::LongestPrefixMatch => keys.push(quote! {
p4rs::extract_lpm_key(
keyset_data,
#offset,
#sz,
)
}),
MatchKind::Range => keys.push(quote! {
p4rs::extract_range_key(
keyset_data,
#offset,
#sz,
)
}),
}
offset += sz;
}
keys
}
fn add_table_entry_function(
&mut self,
table: &Table,
control: &Control,
qtfn: &str,
) -> TokenStream {
let keys = self.table_entry_keys(table);
let mut action_match_body = TokenStream::new();
for action in table.actions.iter() {
let call =
format_ident!("{}_action_{}", control.name, &action.name);
let n = table.key.len();
//XXX hack
if &action.name == "NoAction" {
continue;
}
let a = control.get_action(&action.name).unwrap_or_else(|| {
panic!(
"control {} must have action {}",
control.name, &action.name,
)
});
let mut parameter_tokens = Vec::new();
let mut parameter_refs = Vec::new();
let mut offset: usize = 0;
for p in &a.parameters {
let pname = format_ident!("{}", p.name);
match &p.ty {
Type::Bool => {
parameter_tokens.push(quote! {
let #pname = p4rs::extract_bool_action_parameter(
parameter_data,
#offset,
);
});
offset += 1;
}
Type::Error => {
todo!();
}
Type::State => {
todo!();
}
Type::Action => {
todo!();
}
Type::Bit(n) => {
parameter_tokens.push(quote! {
let #pname = p4rs::extract_bit_action_parameter(
parameter_data,
#offset,
#n,
);
});
parameter_refs.push(quote! { #pname.clone() });
offset += n >> 3;
}
Type::Varbit(_n) => {
todo!();
}
Type::Int(_n) => {
todo!();
}
Type::String => {
todo!();
}
Type::UserDefined(_s) => {
todo!();
}
Type::ExternFunction => {
todo!();
}
Type::HeaderMethod => {
todo!();
}
Type::Table => {
todo!();
}
Type::Void => {
todo!();
}
Type::List(_) => {
todo!();
}
}
}
let mut control_params = Vec::new();
let mut control_param_types = Vec::new();
let mut action_params = Vec::new();
let mut action_param_types = Vec::new();
for p in &control.parameters {
let name = format_ident!("{}", p.name);
control_params.push(quote! { #name });
let ty = rust_type(&p.ty);
match p.direction {
Direction::Out | Direction::InOut => {
control_param_types.push(quote! { &mut #ty });
}
_ => {
control_param_types.push(quote! { &#ty });
}
}
}
for p in &a.parameters {
let name = format_ident!("{}", p.name);
action_params.push(quote! { #name });
let ty = rust_type(&p.ty);
action_param_types.push(quote! { #ty });
}
for var in &control.variables {
let name = format_ident!("{}", var.name);
if let Type::UserDefined(typename) = &var.ty {
if self.ast.get_extern(typename).is_some() {
control_params.push(quote! { #name });
let extern_type = format_ident!("{}", typename);
control_param_types.push(quote! {
&p4rs::externs::#extern_type
});
}
}
}
let aname = &action.name;
let tname = format_ident!("{}", qtfn);
action_match_body.extend(quote! {
#aname => {
#(#parameter_tokens)*
let action: std::sync::Arc<dyn Fn(
#(#control_param_types),*
)>
= std::sync::Arc::new(move |
#(#control_params),*
| {
#call(
#(#control_params),*,
#(#parameter_refs),*
)
});
self.#tname
.entries
.insert(p4rs::table::TableEntry::<
#n,
std::sync::Arc<dyn Fn(
#(#control_param_types),*
)>,
> {
key,
priority,
name: "your name here".into(), //TODO
action,
action_id: #aname.to_owned(),
parameter_data: parameter_data.to_owned(),
});
}
});
}
let name = &control.name;
action_match_body.extend(quote! {
x => panic!("unknown {} action id {}", #name, x),
});
let name = format_ident!("add_{}_entry", qtfn);
quote! {
// lifetime is due to
// https://github.com/rust-lang/rust/issues/96771#issuecomment-1119886703
pub fn #name<'a>(
&mut self,
action_id: &str,
keyset_data: &'a [u8],
parameter_data: &'a [u8],
priority: u32,
) {
let key = [#(#keys),*];
match action_id {
#action_match_body
}
}
}
}
fn remove_table_entry_function(
&mut self,
table: &Table,
control: &Control,
qtfn: &str,
) -> TokenStream {
let keys = self.table_entry_keys(table);
let n = table.key.len();
let tname = format_ident!("{}", qtfn);
let name = format_ident!("remove_{}_entry", qtfn);
let mut control_params = Vec::new();
let mut control_param_types = Vec::new();
for p in &control.parameters {
let name = format_ident!("{}", p.name);
control_params.push(quote! { #name });
let ty = rust_type(&p.ty);
match p.direction {
Direction::Out | Direction::InOut => {
control_param_types.push(quote! { &mut #ty });
}
_ => {
control_param_types.push(quote! { &#ty });
}
}
}
for var in &control.variables {
let name = format_ident!("{}", var.name);
if let Type::UserDefined(typename) = &var.ty {
if self.ast.get_extern(typename).is_some() {
control_params.push(quote! { #name });
let extern_type = format_ident!("{}", typename);
control_param_types.push(quote! {
&p4rs::externs::#extern_type
});
}
}
}
quote! {
// lifetime is due to
// https://github.com/rust-lang/rust/issues/96771#issuecomment-1119886703
pub fn #name<'a>(
&mut self,
keyset_data: &'a [u8],
) {
let key = [#(#keys),*];
let action: std::sync::Arc<dyn Fn(
#(#control_param_types),*
)>
= std::sync::Arc::new(move |
#(#control_params),*
| { });
self.#tname
.entries
.remove(
&p4rs::table::TableEntry::<
#n,
std::sync::Arc<dyn Fn(
#(#control_param_types),*
)>,
> {
key,
priority: 0, //TODO
name: "your name here".into(), //TODO
action,
action_id: String::new(),