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
use super::Connection;
use super::Context;
use crate::app::P4app;
use crate::entity::UpdateType;
use crate::error::{ContextError, ContextErrorKind};
use crate::event::{CommonEvents, CoreEvent, CoreRequest, Event, PacketReceived};
use crate::p4rt::bmv2::Bmv2SwitchConnection;
use crate::p4rt::pipeconf::{Pipeconf, PipeconfID};
use crate::p4rt::pure::{
    new_packet_out_request, new_set_entity_request, new_write_table_entry, table_entry_to_entity,
};
use crate::proto::p4runtime::P4RuntimeClient;
use crate::proto::p4runtime::{
    stream_message_response, Entity, Index, MeterEntry, PacketIn, StreamMessageRequest,
    StreamMessageResponse, Uint128, Update, WriteRequest, WriteResponse,
};
use crate::representation::{ConnectPoint, Device, DeviceID, DeviceType};
use crate::util::flow::Flow;
use byteorder::BigEndian;
use byteorder::ByteOrder;
use bytes::Bytes;
use failure::ResultExt;
use futures::channel::mpsc::{UnboundedReceiver, UnboundedSender};
use futures::compat::*;
use futures::future::FutureExt;
use futures::sink::SinkExt;
use futures::stream::StreamExt;
use grpcio::{StreamingCallSink, WriteFlags};
use log::{debug, error, info, trace, warn};
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::path::Path;
use std::sync::{Arc, Mutex, RwLock};
use tokio::runtime::current_thread::Handle;
use tokio::runtime::Runtime;

#[derive(Clone)]
pub struct ContextHandle<E> {
    pub sender: UnboundedSender<CoreRequest<E>>,
    connections: Arc<HashMap<DeviceID, Arc<Connection>>>,
    pipeconf: Arc<HashMap<PipeconfID, Pipeconf>>,
}

impl<E> ContextHandle<E>
where
    E: Debug,
{
    pub fn new(
        sender: UnboundedSender<CoreRequest<E>>,
        connections: Arc<HashMap<DeviceID, Arc<Connection>>>,
        pipeconf: Arc<HashMap<PipeconfID, Pipeconf>>,
    ) -> ContextHandle<E> {
        ContextHandle {
            sender,
            connections,
            pipeconf,
        }
    }

    pub fn insert_flow(&self, mut flow: Flow, device: DeviceID) -> Result<Flow, ContextError> {
        self.set_flow(flow, device, UpdateType::Insert)
    }

    pub fn set_flow(
        &self,
        mut flow: Flow,
        device: DeviceID,
        update: UpdateType,
    ) -> Result<Flow, ContextError> {
        let hash = crate::util::hash(&flow);
        let connection = self.connections.get(&device).ok_or(ContextError::from(
            ContextErrorKind::DeviceNotConnected { device },
        ))?;
        let table_entry = flow.to_table_entry(&connection.pipeconf, hash);
        if let Some(c) = self.connections.get(&device) {
            let request =
                new_set_entity_request(1, table_entry_to_entity(table_entry), update.into());
            match c.p4runtime_client.write(&request) {
                Ok(response) => {
                    debug!(target:"context","set entity response: {:?}",response);
                }
                Err(e) => {
                    error!(target:"context","grpc send error: {:?}",e);
                }
            }
        } else {
            error!(target:"context","SetEntity error: connection not found for device {:?}",&device);
        }
        flow.metadata = hash;
        Ok(flow)
    }

    pub fn add_device(&self, name: String, address: String, device_id: u64, pipeconf: &str) {
        let id = crate::util::hash(&name);
        let pipeconf = crate::util::hash(pipeconf);
        let device = Device {
            id: DeviceID(id),
            name,
            ports: Default::default(),
            typ: DeviceType::MASTER {
                socket_addr: address,
                device_id,
                pipeconf: PipeconfID(pipeconf),
            },
            device_id,
            index: 0,
        };
        self.sender
            .unbounded_send(CoreRequest::AddDevice {
                device,
                reply: None,
            })
            .unwrap()
    }

    pub fn add_device_object(&self, device: Device) {
        self.sender
            .unbounded_send(CoreRequest::AddDevice {
                device,
                reply: None,
            })
            .unwrap()
    }

    pub fn add_device_with_pipeconf_id(
        &self,
        name: String,
        address: String,
        device_id: u64,
        pipeconf: PipeconfID,
    ) {
        let id = crate::util::hash(&name);
        let device = Device {
            id: DeviceID(id),
            name,
            ports: Default::default(),
            typ: DeviceType::MASTER {
                socket_addr: address,
                device_id,
                pipeconf,
            },
            device_id,
            index: 0,
        };
        self.sender
            .unbounded_send(CoreRequest::AddDevice {
                device,
                reply: None,
            })
            .unwrap()
    }

    pub fn send_event(&self, event: E) {
        self.sender
            .unbounded_send(CoreRequest::Event(event))
            .unwrap();
    }

    pub fn send_request(&self, request: CoreRequest<E>) {
        self.sender.unbounded_send(request).unwrap();
    }

    pub fn send_packet(&self, to: ConnectPoint, packet: Bytes) {
        if let Some(c) = self.connections.get(&to.device) {
            let request = new_packet_out_request(&c.pipeconf, to.port, packet);
            let result = c.send_stream_request(request);
            if result.is_err() {
                error!(target:"context","packet out err {:?}", result.err().unwrap());
            }
        } else {
            // find device name
            error!(target:"context","PacketOut error: connection not found for device {:?}.", to.device);
        }
    }

    pub fn set_entity<T: crate::entity::ToEntity>(
        &self,
        device: DeviceID,
        update_type: UpdateType,
        entity: &T,
    ) -> Result<(), ContextError> {
        let connection = self.connections.get(&device).ok_or(ContextError::from(
            ContextErrorKind::DeviceNotConnected { device },
        ))?;
        if let Some(entity) = entity.to_proto_entity(&connection.pipeconf) {
            if let Some(c) = self.connections.get(&device) {
                let request = new_set_entity_request(1, entity, update_type.into());
                match c.p4runtime_client.write(&request) {
                    Ok(response) => {
                        debug!(target:"context","set entity response: {:?}",response);
                    }
                    Err(e) => {
                        error!(target:"context","grpc send error: {:?}",e);
                    }
                }
            } else {
                error!(target:"context","SetEntity error: connection not found for device {:?}",&device);
            }
            Ok(())
        } else {
            Err(ContextError::from(ContextErrorKind::EntityIsNone))
        }
    }

    pub fn remove_device(&self, device: DeviceID) {
        self.sender
            .unbounded_send(CoreRequest::RemoveDevice { device })
            .unwrap();
    }
}