rbfrt 0.1.8

Rust library for interaction with Intel Tofino(TM) BFRT switch interface.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
/* Copyright 2023-present University of Tuebingen, Chair of Communication Networks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Steffen Lindner (steffen.lindner@uni-tuebingen.de)
 */

//! This crate implements an interface to a P4-programmable switch that can be controlled through the BFRuntime Interface.
//!
//! # Example
//!
//! ```no_run
//! use rbfrt::{SwitchConnection, table};
//! use rbfrt::table::{MatchValue, Request};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let switch = SwitchConnection::builder("localhost", 50052)
//!         .device_id(0)
//!         .client_id(1)
//!         .p4_name("my_p4_program")
//!         .connect()
//!         .await?;
//!
//!
//!     let requests = vec![
//!         Request::new("ingress.p4tg.frame_type.frame_type_monitor")
//!             .match_key("hdr.ipv4.dst_addr", MatchValue::lpm(vec![10u8, 0, 0, 2], 32))
//!             .match_key("ig_intr_md.ingress_port", MatchValue::exact(0)),
//!         Request::new("ingress.p4tg.tg_forward")
//!             .match_key("ig_intr_md.ingress_port", MatchValue::exact(10))
//!             .match_key("ig_md.rand_value", MatchValue::range(20, 30))
//!             .match_key("hdr.pkt_gen.app_id", MatchValue::exact(5))
//!         ];
//!
//!     for req in requests {
//!         let entries = switch.get_table_entries(req).await?;
//!
//!         for e in entries {
//!             println!("{:?}", e);
//!         }
//!     }
//!
//!     Ok(())
//! }
//! ```

mod bfrt;
mod core;
pub mod error;
mod protos;
pub mod register;
pub mod table;
pub mod util;

use crate::bfrt_proto::forwarding_pipeline_config::Profile;
use crate::bfrt_proto::set_forwarding_pipeline_config_request::{Action, DevInitMode};
use crate::bfrt_proto::{
    ForwardingPipelineConfig, ReadResponse, SetForwardingPipelineConfigRequest,
    StreamMessageRequest, StreamMessageResponse, WriteResponse,
};
use crate::error::RBFRTError;
use crate::error::RBFRTError::{
    ConnectionError, GRPCError, GetForwardingPipelineError, P4ProgramError, RequestEmpty,
    UnknownReadResult,
};
use crate::protos::bfrt_proto::data_field::Value;
use crate::protos::bfrt_proto::entity::Entity;
use crate::protos::bfrt_proto::stream_message_response::Update;
use crate::protos::bfrt_proto::{ReadRequest, WriteRequest};
use crate::register::Register;
use crate::table::MatchValue;
use crate::util::Digest;
use bfrt::BFRTInfo;
use bfrt_proto::bf_runtime_client::BfRuntimeClient;
use bfrt_proto::GetForwardingPipelineConfigRequest;
use bfrt_proto::TargetDevice;
use log::{debug, info, warn};
use protos::bfrt_proto;
use std::collections::HashMap;
use std::io::Read;
use std::{fs, str};
use table::{Request, RequestType, TableEntry};
use tokio::sync::Mutex;
use tokio_stream::wrappers::ReceiverStream;
use tonic::transport::Channel;
use tonic::{Response, Streaming};

/// Size of the internal digest queue
/// Up to 20k elements with back pressure
const DIGEST_QUEUE_SIZE: usize = 20000;

#[allow(dead_code)]
#[allow(clippy::large_enum_variant)]
enum DispatchResult {
    ReadResult {
        response: Response<Streaming<ReadResponse>>,
    },
    WriteResult {
        response: Response<WriteResponse>,
    },
}

/// A builder to create the [SwitchConnection] between the switch and the controller.
///
/// # Example
///
/// ```no_run
/// use rbfrt::SwitchConnection;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let builder = SwitchConnection::builder("localhost", 50052)
///         .device_id(0)
///         .client_id(1)
///         .p4_name("my_p4_program");
///
///     let switch = builder.connect().await?;
///
///     Ok(())
/// }
/// ```
pub struct SwitchConnectionBuilder {
    ip: String,
    port: u16,
    device_id: u32,
    client_id: u32,
    p4_name: Option<String>,
    config: Option<String>,
}

impl SwitchConnectionBuilder {
    /// Sets the `client id` of the controller for this [SwitchConnection].
    pub fn client_id(mut self, client_id: u32) -> SwitchConnectionBuilder {
        self.client_id = client_id;
        self
    }

    /// Sets the `device id` of the switch for this [SwitchConnection].
    ///
    /// Different `device ids` are used for multiple switches.
    pub fn device_id(mut self, device_id: u32) -> SwitchConnectionBuilder {
        self.device_id = device_id;
        self
    }

    /// Sets the `P4 program name` running on the switch.
    ///
    /// See [config](crate::SwitchConnectionBuilder::config) to load a program onto the switch.
    ///
    /// Either one of [p4_name](crate::SwitchConnectionBuilder::p4_name) or [config](crate::SwitchConnectionBuilder::config) needs to be used to configure the [SwitchConnection].
    pub fn p4_name(mut self, p4_name: &str) -> SwitchConnectionBuilder {
        self.p4_name = Some(p4_name.to_owned());
        self
    }

    /// Sets the path to the `config file` of the program to load onto the switch.
    ///
    /// See [p4_name](crate::SwitchConnectionBuilder::p4_name) to specify the `P4 program name` already running on the switch.
    ///
    /// Either one of [config](crate::SwitchConnectionBuilder::config) or [p4_name](crate::SwitchConnectionBuilder::p4_name) needs to be used to configure the [SwitchConnection].
    pub fn config(mut self, path: &str) -> SwitchConnectionBuilder {
        self.config = Some(path.to_owned());
        self
    }

    /// Creates the [SwitchConnection] between the switch and controller.
    pub async fn connect(self) -> Result<SwitchConnection, RBFRTError> {
        debug!(
            "Start switch connection to: http://{}:{}.",
            self.ip, self.port
        );

        match BfRuntimeClient::connect(format!("http://{}:{}", self.ip, self.port)).await {
            Ok(client) => {
                let bf_client = Mutex::new(
                    client
                        .max_decoding_message_size(16 * 1024 * 1024)
                        .max_encoding_message_size(16 * 1024 * 1024),
                );

                let (request_tx, request_rx) =
                    tokio::sync::mpsc::channel::<StreamMessageRequest>(DIGEST_QUEUE_SIZE);
                let (response_tx, mut response_rx) =
                    tokio::sync::mpsc::channel::<StreamMessageResponse>(DIGEST_QUEUE_SIZE);
                let (digest_sender, digest_receiver) =
                    crossbeam_channel::bounded(DIGEST_QUEUE_SIZE);
                let mut connection = SwitchConnection {
                    ip: self.ip,
                    port: self.port,
                    device_id: self.device_id,
                    client_id: self.client_id,
                    bf_client,
                    config: self.config,
                    bfrt_info: None,
                    target: TargetDevice {
                        device_id: self.device_id,
                        pipe_id: 0xffff,
                        direction: 0xff,
                        prsr_id: 0xff,
                    },
                    p4_name: self.p4_name,
                    send_channel: request_tx,
                    digest_queue: digest_receiver,
                };

                if connection.config.is_some() {
                    connection
                        .set_forwarding_pipeline(&connection.config.as_ref().unwrap().clone())
                        .await?;
                }

                if connection.p4_name.is_none() {
                    panic!("P4 name not set.")
                }

                connection
                    .subscribe(request_rx, response_tx, &mut response_rx)
                    .await?;
                connection.bind_forwarding_pipeline().await?;
                connection.bfrt_info = Some(connection.load_pipeline().await?);

                connection.start_notification_thread(response_rx, digest_sender);

                info!(
                    "Switch connection to {}:{} successful.",
                    connection.ip, connection.port
                );

                Ok(connection)
            }
            Err(e) => Err(ConnectionError {
                ip: self.ip,
                port: self.port,
                orig_e: Box::new(e),
            }),
        }
    }
}

/// Represents the connection between the switch and the controller.
///
/// # Example
///
/// ```no_run
/// use rbfrt::SwitchConnection;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let switch = SwitchConnection::builder("localhost", 50052)
///         .device_id(0)
///         .client_id(1)
///         .p4_name("my_p4_program")
///         .connect()
///         .await?;
///
///     Ok(())
/// }
/// ```
pub struct SwitchConnection {
    ip: String,
    port: u16,
    device_id: u32,
    client_id: u32,
    bf_client: Mutex<BfRuntimeClient<Channel>>,
    bfrt_info: Option<BFRTInfo>,
    target: TargetDevice,
    p4_name: Option<String>,
    send_channel: tokio::sync::mpsc::Sender<StreamMessageRequest>,
    /// Queue containing all digests of the connected switch.
    pub digest_queue: crossbeam_channel::Receiver<Digest>,
    config: Option<String>,
}

impl SwitchConnection {
    /// Creates a new [SwitchConnectionBuilder] to build the connection between the switch and controller.
    ///
    /// The switch is available at the `ip` address and runs a gRPC server listing on the specified `port`.
    pub fn builder(ip: &str, port: u16) -> SwitchConnectionBuilder {
        SwitchConnectionBuilder {
            ip: ip.to_owned(),
            port,
            device_id: 0,
            client_id: 1,
            p4_name: None,
            config: None,
        }
    }

    /// Opens a notification channel.
    /// This is needed to bind to the device and to get notifications from the switch.
    #[allow(deprecated)]
    async fn subscribe(
        &self,
        request_rx: tokio::sync::mpsc::Receiver<StreamMessageRequest>,
        response_tx: tokio::sync::mpsc::Sender<StreamMessageResponse>,
        response_rx: &mut tokio::sync::mpsc::Receiver<StreamMessageResponse>,
    ) -> Result<(), RBFRTError> {
        // subscription request
        let subscribe_req = StreamMessageRequest {
            client_id: self.client_id,
            update: Some(bfrt_proto::stream_message_request::Update::Subscribe(
                bfrt_proto::Subscribe {
                    is_master: true,
                    device_id: self.device_id,
                    notifications: Some(bfrt_proto::subscribe::Notifications {
                        enable_learn_notifications: true,
                        enable_idletimeout_notifications: true,
                        enable_port_status_change_notifications: true,
                        enable_entry_active_notifications: true,
                    }),
                    status: None,
                },
            )),
        };

        let stream = ReceiverStream::new(request_rx);
        let req = tonic::Request::new(stream);

        let mut clone = { self.bf_client.lock().await.clone() };

        // start thread to listen for notifications
        tokio::spawn(async move {
            let response_channel = match clone.stream_channel(req).await {
                Ok(res) => res,
                Err(e) => {
                    warn!("Failed to open stream_channel: {e}");
                    return;
                }
            };
            info!("Started stream_channel");
            let mut resp = response_channel.into_inner();

            loop {
                match resp.message().await {
                    Ok(Some(msg)) => match msg.clone().update.unwrap() {
                        Update::Subscribe(_) | Update::Digest(_) => {
                            if let Err(e) = response_tx.try_send(msg) {
                                warn!("Failed to send notification: {e}");
                            }
                        }
                        _ => {
                            warn!(
                                    "Got a notification that is currently not supported. Will be ignored."
                                );
                        }
                    },
                    Ok(None) => {
                        warn!("Stream was closed by sender.");
                        break;
                    }
                    Err(e) => {
                        warn!("Error receiving notification: {e}");
                        break;
                    }
                }
            }

            warn!("Notification channel closed.");
        });

        if self.send_channel.send(subscribe_req).await.is_err() {
            warn!("Notification endpoint hang.")
        }

        let msg = response_rx.recv().await.unwrap();

        match msg.update.unwrap() {
            Update::Subscribe(sub) => {
                if sub.status.unwrap().code != 0 {
                    panic!("Notification subscription failed.");
                } else {
                    info!("Notification subscription successful.")
                }
            }
            _ => {
                panic!("Notification subscription expected.");
            }
        }

        Ok(())
    }

    /// Loads the pipeline information from the switch
    async fn load_pipeline(&self) -> Result<BFRTInfo, RBFRTError> {
        debug!("Loading pipeline.");
        match self
            .bf_client
            .lock()
            .await
            .get_forwarding_pipeline_config(GetForwardingPipelineConfigRequest {
                device_id: self.device_id,
                client_id: self.client_id,
            })
            .await
        {
            Ok(pipeline) => {
                let msg = pipeline.into_inner();

                // tofino internal tables
                let non_p4_config = msg.non_p4_config.unwrap();
                let non_p4: BFRTInfo =
                    serde_json::from_slice(&non_p4_config.bfruntime_info).unwrap();
                let non_p4_tables = non_p4.tables();

                for v in msg.config {
                    if v.p4_name == self.p4_name.clone().unwrap() {
                        let mut tmp: BFRTInfo = serde_json::from_slice(&v.bfruntime_info).unwrap();
                        for t in &non_p4_tables {
                            tmp.add_table(t.clone());
                        }

                        return Ok(tmp);
                    }
                }

                Err(P4ProgramError {
                    name: self.p4_name.clone().unwrap(),
                })
            }
            Err(e) => Err(GetForwardingPipelineError {
                device_id: self.device_id,
                client_id: self.client_id,
                orig_e: Box::new(e),
            }),
        }
    }

    fn start_notification_thread(
        &self,
        mut response_rx: tokio::sync::mpsc::Receiver<StreamMessageResponse>,
        digest_queue: crossbeam_channel::Sender<Digest>,
    ) {
        let local_bfrt_info = self.bfrt_info.clone();

        // start receive channel thread
        tokio::spawn(async move {
            let bfrt_info = local_bfrt_info.unwrap();
            while let Some(msg) = response_rx.recv().await {
                match msg.update.unwrap() {
                    Update::Digest(digest) => {
                        let learn_filter = bfrt_info.learn_filter_get(digest.digest_id);

                        // this is really ugly but works at the moment
                        //TODO rewrite
                        match learn_filter {
                            Ok(filter) => {
                                for data in digest.data {
                                    let mut digest_fields = HashMap::new();

                                    for field in data.fields {
                                        let id = field.field_id;
                                        let field_name = filter.get_data_field_name_by_id(id);

                                        if let Ok(field_name) = field_name {
                                            let data = field.value;

                                            if let Some(data) = data {
                                                match data {
                                                    Value::Stream(data) => {
                                                        digest_fields.insert(field_name, data);
                                                    }
                                                    _ => {
                                                        warn!("Not supported digest field type received.");
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    let digest = Digest {
                                        name: filter.name.to_owned(),
                                        data: digest_fields,
                                    };

                                    let _ = digest_queue.try_send(digest);
                                }
                            }
                            Err(err) => {
                                warn!("Received an error while retrieving learn filter: {err}");
                            }
                        }
                    }
                    _ => {
                        warn!("Received not supported notification. Only Digests are currently supported.")
                    }
                }
            }

            warn!("Notification channel closed.");
        });
    }

    /// Reads file content and returns byte representation
    ///
    /// * `file_path` - Path to the file
    fn read_file_to_bytes(&self, file_path: &str) -> Vec<u8> {
        let mut file =
            fs::File::open(file_path).unwrap_or_else(|_| panic!("Unable to read: {file_path}"));

        let metadata = fs::metadata(file_path)
            .unwrap_or_else(|_| panic!("Unable to read metadata for {file_path}."));
        let mut file_buffer = vec![0; metadata.len() as usize];
        file.read_exact(&mut file_buffer).expect("buffer overflow");

        file_buffer
    }

    /// Load a P4 program onto the switch based on the information in the `config_file`
    ///
    /// * `config_file` - Path to the config file (output from P4 compiler)
    async fn set_forwarding_pipeline(&mut self, config_file: &str) -> Result<(), RBFRTError> {
        debug!("Set forwarding pipeline.");

        let file = fs::File::open(config_file)
            .unwrap_or_else(|_| panic!("config file: {config_file} not readable."));
        let config: core::Configuration =
            serde_json::from_reader(file).expect("config file has invalid json format.");

        let device = config.p4_devices.first().unwrap();

        let mut forwarding_configs: Vec<ForwardingPipelineConfig> = vec![];

        // generate GRPC message
        for program in &device.p4_programs {
            self.p4_name = Some(program.program_name.clone());

            let profiles: Vec<Profile> = program
                .p4_pipelines
                .iter()
                .map(|profile| Profile {
                    profile_name: profile.p4_pipeline_name.to_owned(),
                    context: self.read_file_to_bytes(&profile.context),
                    binary: self.read_file_to_bytes(&profile.config),
                    pipe_scope: profile.pipe_scope.clone(),
                })
                .collect();

            let forwarding_config = ForwardingPipelineConfig {
                p4_name: program.program_name.to_owned(),
                bfruntime_info: self.read_file_to_bytes(&program.bfrt_config),
                profiles,
            };

            forwarding_configs.push(forwarding_config);
        }

        let request = SetForwardingPipelineConfigRequest {
            device_id: self.device_id,
            client_id: self.client_id,
            action: Action::VerifyAndWarmInitBeginAndEnd.into(),
            dev_init_mode: DevInitMode::FastReconfig.into(),
            base_path: "".to_string(),
            config: forwarding_configs,
        };

        let req = self
            .bf_client
            .lock()
            .await
            .set_forwarding_pipeline_config(request)
            .await;

        match req {
            Ok(_) => Ok(()),
            Err(e) => Err(GRPCError {
                message: e.to_string(),
                details: format!("{:?}", e.details()),
            }),
        }
    }

    /// Binds to a P4 program
    async fn bind_forwarding_pipeline(&self) -> Result<(), RBFRTError> {
        debug!(
            "Bind forwarding pipeline: {}.",
            self.p4_name.as_ref().unwrap().to_owned()
        );

        let forwarding_config = ForwardingPipelineConfig {
            p4_name: self.p4_name.as_ref().unwrap().to_owned(),
            bfruntime_info: vec![],
            profiles: vec![],
        };

        let request = SetForwardingPipelineConfigRequest {
            device_id: self.device_id,
            client_id: self.client_id,
            action: Action::Bind.into(),
            dev_init_mode: DevInitMode::FastReconfig.into(),
            base_path: "".to_string(),
            config: vec![forwarding_config],
        };

        let req = self
            .bf_client
            .lock()
            .await
            .set_forwarding_pipeline_config(request)
            .await;

        match req {
            Ok(_) => {
                info!("Bind to forwarding pipeline successful.");
                Ok(())
            }
            Err(e) => {
                warn!("Bind forwarding pipeline failed.");
                Err(GRPCError {
                    message: e.to_string(),
                    details: format!("{:?}", e.details()),
                })
            }
        }
    }

    fn get_target_device(&self) -> TargetDevice {
        TargetDevice {
            device_id: self.target.device_id,
            pipe_id: self.target.pipe_id,
            direction: self.target.direction,
            prsr_id: self.target.prsr_id,
        }
    }

    /// Executes a table operation.
    ///
    /// See [TableOperation](crate::table::TableOperation) for supported operations, like synchronization of counters or registers.
    pub async fn execute_operation(&self, request: Request) -> Result<(), RBFRTError> {
        debug!("Execute operation {request:?}");
        let req = request.request_type(RequestType::Operation);

        let vec_req = vec![req];

        self.dispatch_request(&vec_req).await?;

        Ok(())
    }

    /// Checks if the switch has a table with the specified `name`.
    pub fn has_table(&self, name: &str) -> bool {
        let t = self.bfrt_info.as_ref().unwrap().table_get(name);

        t.is_ok()
    }

    /// Retrieves the entries of a single table.
    pub async fn get_table_entries(&self, request: Request) -> Result<Vec<TableEntry>, RBFRTError> {
        let entries = self.get_tables_entries(vec![request]).await?;

        Ok(entries)
    }

    /// Retrieves the entries of multiple tables.
    pub async fn get_tables_entries(
        &self,
        requests: Vec<Request>,
    ) -> Result<Vec<TableEntry>, RBFRTError> {
        let mut veq_req = vec![];
        let mut entries = vec![];

        for req in requests {
            veq_req.push(req.request_type(RequestType::Read));
        }

        match self.dispatch_request(&veq_req).await? {
            DispatchResult::ReadResult { response } => {
                let mut stream = response.into_inner();
                let message = stream.message().await?.unwrap();
                for entity in message.entities {
                    let entity = entity.entity.unwrap();

                    match &entity {
                        Entity::TableEntry(table_entry) => {
                            let table = self
                                .bfrt_info
                                .as_ref()
                                .unwrap()
                                .table_get_by_id(table_entry.table_id)?;

                            let entry = table.parse_read_request(entity, table.name())?;

                            entries.push(entry);
                        }
                        _ => {
                            return Err(UnknownReadResult {});
                        }
                    }
                }
                // This drains all remanining messages in the stream.
                // In the bfrt, we only get a single message, so this shouldnt be a problem.
                // However, without doing this, the stream will not be closed and the connection will hang.
                let _ = stream.trailers().await;
                Ok(entries)
            }
            _ => {
                panic!("Unreachable code.")
            }
        }
    }

    /// Writes a single entry into a table.
    ///
    /// The entry's key must not be present in the table to insert the new entry.
    /// See [update_table_entry](crate::SwitchConnection::update_table_entry) to update an existing entry.
    pub async fn write_table_entry(&self, request: Request) -> Result<(), RBFRTError> {
        debug!("Write table entry {request:?}");

        let req = request.request_type(RequestType::Write);
        let vec_req = vec![req];

        self.dispatch_request(&vec_req).await?;

        Ok(())
    }

    /// Writes a entries into one or multiple tables.
    ///
    /// The entries' keys must not be present in the table to insert the new entries.
    /// See [update_table_entries](crate::SwitchConnection::update_table_entries) to update existing entries.
    pub async fn write_table_entries(&self, requests: Vec<Request>) -> Result<(), RBFRTError> {
        debug!("Write table entry {requests:?}");
        let req = requests
            .iter()
            .map(|x| x.clone().request_type(RequestType::Write))
            .collect();
        self.dispatch_request(&req).await?;

        Ok(())
    }

    /// Updates a single entry in a table.
    ///
    /// The entry's key must be present in the table to update the entry.
    /// See [write_table_entry](crate::SwitchConnection::write_table_entry) to insert a new entry.
    pub async fn update_table_entry(&self, request: Request) -> Result<(), RBFRTError> {
        debug!("Update table entry {request:?}");
        let req = request.request_type(RequestType::Update);
        let vec_req = vec![req];
        self.dispatch_request(&vec_req).await?;

        Ok(())
    }

    /// Updates multiple entries in one or multiple tables.
    ///
    /// The entries' keys must be present in the tables to update the entries.
    /// See [write_table_entries](crate::SwitchConnection::write_table_entries) to insert new entries.
    pub async fn update_table_entries(&self, requests: Vec<Request>) -> Result<(), RBFRTError> {
        debug!("Update table entry {requests:?}");
        let req = requests
            .iter()
            .map(|x| x.clone().request_type(RequestType::Update))
            .collect();
        self.dispatch_request(&req).await?;

        Ok(())
    }

    /// Deletes a entry in a table.
    ///
    /// See [clear_table](crate::SwitchConnection::clear_table) to delete all entries inside the table.
    pub async fn delete_table_entry(&self, request: Request) -> Result<(), RBFRTError> {
        debug!("Delete table entry {request:?}");
        let req = request.request_type(RequestType::Delete);

        let vec_req = vec![req];

        self.dispatch_request(&vec_req).await?;

        Ok(())
    }

    /// Deletes multiple entries in one or multiple tables.
    ///
    /// See [clear_tables](crate::SwitchConnection::clear_tables) to delete all entries inside the tables.
    pub async fn delete_table_entries(&self, request: Vec<Request>) -> Result<(), RBFRTError> {
        debug!("Delete table entries {request:?}");
        let vec_req = request
            .iter()
            .map(|x| x.clone().request_type(RequestType::Delete))
            .collect();

        self.dispatch_request(&vec_req).await?;

        Ok(())
    }

    /// Deletes all entries in a table.
    ///
    /// See [delete_table_entry](crate::SwitchConnection::delete_table_entry) or [delete_table_entries](crate::SwitchConnection::delete_table_entries) to delete one or multiple entries inside the table.
    pub async fn clear_table(&self, name: &str) -> Result<(), RBFRTError> {
        debug!("Clear table : {name}");
        let req = Request::new(name);

        self.delete_table_entry(req).await?;

        Ok(())
    }

    /// Deletes all entries in multiple tables.
    ///
    /// See [delete_table_entry](crate::SwitchConnection::delete_table_entry) or [delete_table_entries](crate::SwitchConnection::delete_table_entries) to delete one or multiple entries inside the tables.
    pub async fn clear_tables(&self, name: Vec<&str>) -> Result<(), RBFRTError> {
        debug!("Clear tables : {name:?}");
        let reqs: Vec<Request> = name.iter().map(|x| Request::new(x)).collect();

        self.delete_table_entries(reqs).await?;

        Ok(())
    }

    /// Reads the value of a register.
    pub async fn get_register_entry(
        &self,
        request: register::Request,
    ) -> Result<Register, RBFRTError> {
        debug!("Read register {request:?}");
        let mut table_request = Request::new(request.get_name()).request_type(RequestType::Read);

        if request.get_index().is_some() {
            table_request = table_request.match_key(
                "$REGISTER_INDEX",
                MatchValue::exact(request.get_index().unwrap()),
            );
        }

        let entries = self.get_table_entries(table_request).await?;

        let name = request.get_name();

        Ok(Register::parse_register_entries(entries, name))
    }

    /// Reads the values of multiple registers or indices.
    pub async fn get_register_entries(
        &self,
        requests: Vec<register::Request>,
    ) -> Result<Register, RBFRTError> {
        debug!("Read register {requests:?}");

        let name = requests.first().as_ref().unwrap().get_name();

        let mut req = vec![];

        for request in &requests {
            let table_request = Request::new(request.get_name()).request_type(RequestType::Read);

            if request.get_index().is_some() {
                req.push(table_request.match_key(
                    "$REGISTER_INDEX",
                    MatchValue::exact(request.get_index().unwrap()),
                ));
            }
        }

        let entries = self.get_tables_entries(req).await?;

        Ok(Register::parse_register_entries(entries, name))
    }

    /// Writes a value into a register.
    pub async fn write_register_entry(&self, request: register::Request) -> Result<(), RBFRTError> {
        debug!("Write register {request:?}");
        let mut table_request = Request::new(request.get_name());

        if request.get_index().is_none() {
            return Err(RBFRTError::MissingRegisterIndex);
        }

        table_request = table_request.match_key(
            "$REGISTER_INDEX",
            MatchValue::exact(request.get_index().unwrap()),
        );

        for (name, value) in request.get_data() {
            table_request = table_request.action_data(name, value.clone());
        }

        self.write_table_entry(table_request).await?;

        Ok(())
    }

    /// Writes values into multiple registers or indices.
    pub async fn write_register_entries(
        &self,
        requests: Vec<register::Request>,
    ) -> Result<(), RBFRTError> {
        debug!("Write register {requests:?}");

        let mut write_req = vec![];

        for req in &requests {
            if req.get_index().is_none() {
                return Err(RBFRTError::MissingRegisterIndex);
            }

            let mut table_request = Request::new(req.get_name()).match_key(
                "$REGISTER_INDEX",
                MatchValue::exact(req.get_index().unwrap()),
            );

            for (name, value) in req.get_data() {
                table_request = table_request.action_data(name, value.clone());
            }

            write_req.push(table_request);
        }

        self.write_table_entries(write_req).await?;

        Ok(())
    }

    /// Dispatches the requests to the switch.
    ///
    /// # Note
    ///
    /// Assumes all request are of the same kind.
    /// You MUST NOT mix different request types, e.g., [Read](crate::table::table_entry::Read) and [Write](crate::table::table_entry::Write), in one dispatch!
    async fn dispatch_request(&self, request: &Vec<Request>) -> Result<DispatchResult, RBFRTError> {
        let bfrt_info = self.bfrt_info.as_ref().unwrap();

        if request.is_empty() {
            return Err(RequestEmpty {});
        }

        match request.first().as_ref().unwrap().get_type() {
            RequestType::Read => {
                let mut entities = vec![];

                for req in request {
                    let table = bfrt_info.table_get(req.get_table_name())?;
                    let entity = table.build_read_request(req, &self.target)?;
                    entities.push(entity);
                }

                let req = ReadRequest {
                    target: Some(self.get_target_device()),
                    client_id: self.client_id,
                    entities,
                    p4_name: self.p4_name.as_ref().unwrap().to_owned(),
                };

                let response = self.bf_client.lock().await.read(req).await?;

                Ok(DispatchResult::ReadResult { response })
            }
            RequestType::Write | RequestType::Update => {
                let mut updates = vec![];

                for req in request {
                    let table = bfrt_info.table_get(req.get_table_name())?;
                    let update = table.build_write_request(req, &self.target)?;
                    updates.push(update);
                }

                let req = WriteRequest {
                    target: Some(self.get_target_device()),
                    client_id: self.client_id,
                    updates,
                    p4_name: self.p4_name.as_ref().unwrap().to_owned(),
                    atomicity: 0,
                };

                let response = self.bf_client.lock().await.write(req).await?;

                Ok(DispatchResult::WriteResult { response })
            }
            RequestType::Operation => {
                let mut updates = vec![];

                for req in request {
                    let table = bfrt_info.table_get(req.get_table_name())?;
                    let update = table.build_operation_request(req)?;
                    updates.push(update);
                }

                let req = WriteRequest {
                    target: Some(self.get_target_device()),
                    client_id: self.client_id,
                    updates,
                    p4_name: self.p4_name.as_ref().unwrap().to_owned(),
                    atomicity: 0,
                };

                let response = self.bf_client.lock().await.write(req).await?;

                Ok(DispatchResult::WriteResult { response })
            }
            RequestType::Delete => {
                let mut updates = vec![];

                for req in request {
                    let table = bfrt_info.table_get(req.get_table_name())?;
                    let update = table.build_delete_request(req, &self.target)?;
                    updates.push(update);
                }

                let req = WriteRequest {
                    target: Some(self.get_target_device()),
                    client_id: self.client_id,
                    updates,
                    p4_name: self.p4_name.as_ref().unwrap().to_owned(),
                    atomicity: 0,
                };

                let response = self.bf_client.lock().await.write(req).await?;

                Ok(DispatchResult::WriteResult { response })
            }
        }
    }
}