OpenSDN source code
flow_handler.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 #include <base/address.h>
6 #include <base/address_util.h>
7 #include "pkt/proto.h"
8 #include "pkt/proto_handler.h"
9 #include "pkt/pkt_init.h"
10 #include "pkt/pkt_handler.h"
11 #include "pkt/flow_table.h"
12 #include "pkt/flow_proto.h"
13 #include "pkt/flow_handler.h"
14 
16 
17 static const VmEntry *InterfaceToVm(const Interface *intf) {
18  if (intf->type() != Interface::VM_INTERFACE)
19  return NULL;
20 
21  const VmInterface *vm_port = static_cast<const VmInterface *>(intf);
22  return vm_port->vm();
23 }
24 
25 // Compute L2/L3 forwarding mode for pacekt.
26 // Forwarding mode is L3 if,
27 // - Packet uses L3 label
28 // - Packet uses L2 Label and DMAC hits a route with L2-Receive NH
29 // Else forwarding mode is L2
31  const Interface *intf =
32  agent_->interface_table()->FindInterface(pkt_info_->agent_hdr.ifindex);
33  // here return true/false does not make any difference as flow processing
34  // will be aborted due to invalid interface in subsequent processing.
35  if (intf == NULL) {
36  return false;
37  }
38  if (intf->type() == Interface::INET) {
39  return true;
40  }
41 
42  if (intf->type() == Interface::VM_INTERFACE) {
43  const VmInterface *vm_intf =
44  static_cast<const VmInterface *>(intf);
45  if (vm_intf == agent_->vhost_interface()) {
46  return true;
47  }
48  }
49 
50  if (pkt_info_->l3_label) {
51  return true;
52  }
53 
54  VrfTable *table = static_cast<VrfTable *>(agent_->vrf_table());
55  VrfEntry *vrf = table->FindVrfFromId(pkt_info_->agent_hdr.vrf);
56  if (vrf == NULL) {
57  return false;
58  }
59  BridgeAgentRouteTable *l2_table = static_cast<BridgeAgentRouteTable *>
60  (vrf->GetBridgeRouteTable());
61  AgentRoute *rt = static_cast<AgentRoute *>
62  (l2_table->FindRouteNoLock(pkt_info_->dmac));
63  if (rt == NULL) {
64  return false;
65  }
66 
67  const NextHop *nh = rt->GetActiveNextHop();
68  if (nh == NULL) {
69  return false;
70  }
71 
72  if (nh->GetType() == NextHop::L2_RECEIVE) {
73  return true;
74  }
75  return false;
76 }
77 
79  PktControlInfo in;
80  PktControlInfo out;
83  std::unique_ptr<FlowTaskMsg> ipc;
84  bool allow_reentrant = true;
85  if (pkt_info_->type == PktType::INVALID) {
86  info.SetPktInfo(pkt_info_);
87  info.l3_flow = IsL3ModeFlow();
88  } else if (pkt_info_->type == PktType::MESSAGE) {
89  // we don't allow reentrancy to different partition if it is
90  // a reevaluation for an existing flow which will only exist
91  // in this partition
92  allow_reentrant = false;
93  ipc = std::unique_ptr<FlowTaskMsg>(static_cast<FlowTaskMsg *>(pkt_info_->ipc));
94  pkt_info_->ipc = NULL;
95  FlowEntry *fe = ipc->fe_ptr.get();
96  // take lock on flow entry before accessing it, since we need to read
97  // forward flow only take lock only on forward flow
98  std::scoped_lock lock1(fe->mutex());
99  assert(flow_table_index_ == fe->flow_table()->table_index());
100 
101  if (fe->deleted() || fe->is_flags_set(FlowEntry::ShortFlow)) {
102  return true;
103  }
104 
105  // We dont support revaluation of linklocal flows
107  return true;
108  }
109 
110  info.flow_entry = fe;
111  pkt_info_->agent_hdr.cmd = AGENT_TRAP_FLOW_MISS;
112  pkt_info_->agent_hdr.cmd_param = fe->flow_handle();
113  pkt_info_->agent_hdr.ifindex = fe->data().if_index_info;
114  pkt_info_->tunnel = fe->data().tunnel_info;
115  pkt_info_->agent_hdr.nh = fe->key().nh;
116  pkt_info_->agent_hdr.vrf = fe->data().vrf;
117  pkt_info_->family =
118  fe->key().src_addr.is_v4() ? Address::INET : Address::INET6;
119  pkt_info_->smac = fe->data().smac;
120  pkt_info_->dmac = fe->data().dmac;
121  pkt_info_->ip_saddr = fe->key().src_addr;
122  pkt_info_->ip_daddr = fe->key().dst_addr;
123  pkt_info_->ip_proto = fe->key().protocol;
124  pkt_info_->sport = fe->key().src_port;
125  pkt_info_->dport = fe->key().dst_port;
127  pkt_info_->vrf = fe->data().vrf;
128  pkt_info_->ttl = fe->data().ttl;
129  info.l3_flow = fe->l3_flow();
131  }
132 
133  if (info.Process(pkt_info_.get(), &in, &out) == false) {
134  info.short_flow = true;
135  }
136 
137  // Flows that change port-numbers are always processed in thread-0.
138  // Identify flows that change port and enqueue to thread-0
139  if (allow_reentrant && ((pkt_info_->sport != info.nat_sport) ||
140  (pkt_info_->dport != info.nat_dport))) {
141  if ((info.nat_sport != 0 || info.nat_dport != 0)) {
143  // Enqueue flow evaluation to
144  // FlowTable::kPortNatFlowTableInstance instance.
147  return true;
148  }
149  }
150  }
151 
152  if (in.intf_ && ((in.intf_->type() != Interface::VM_INTERFACE) &&
153  (in.intf_->type() != Interface::INET))) {
154  in.intf_ = NULL;
155  }
156 
157  if (in.intf_ && out.intf_) {
158  info.local_flow = true;
159  }
160 
161  if (in.intf_) {
162  in.vm_ = InterfaceToVm(in.intf_);
163  }
164 
165  if (out.intf_) {
166  out.vm_ = InterfaceToVm(out.intf_);
167  }
168 
169  info.Add(pkt_info_.get(), &in, &out);
170  return true;
171 }
@ INET
Definition: address.h:27
@ INET6
Definition: address.h:28
Base class for all Route entries in agent.
Definition: agent_route.h:225
const NextHop * GetActiveNextHop() const
Definition: agent_route.cc:883
InterfaceTable * interface_table() const
Definition: agent.h:468
VrfTable * vrf_table() const
Definition: agent.h:488
const Interface * vhost_interface() const
Definition: agent.h:938
BridgeRouteEntry * FindRouteNoLock(const MacAddress &mac)
Definition: bridge_route.cc:78
const FlowKey & key() const
Definition: flow_entry.h:595
FlowData & data()
Definition: flow_entry.h:596
bool is_flags_set(const FlowEntryFlags &flags) const
Definition: flow_entry.h:611
uint32_t flow_handle() const
Definition: flow_entry.h:601
FlowTable * flow_table() const
Definition: flow_entry.h:598
bool deleted()
Definition: flow_entry.h:681
std::mutex & mutex()
Definition: flow_entry.h:651
bool l3_flow() const
Definition: flow_entry.h:599
@ LinkLocalFlow
Definition: flow_entry.h:540
uint16_t flow_table_index_
Definition: flow_handler.h:40
FlowProto * flow_proto_
Definition: flow_handler.h:39
bool IsL3ModeFlow() const
Definition: flow_handler.cc:30
FlowTable * GetTable(uint16_t index) const
Definition: flow_proto.cc:262
bool EnqueueReentrant(boost::shared_ptr< PktInfo > msg, uint8_t table_index)
Definition: flow_proto.cc:670
uint16_t table_index() const
Definition: flow_table.h:209
static const uint32_t kPortNatFlowTableInstance
Definition: flow_table.h:165
const Interface * FindInterface(size_t index) const
Definition: interface.cc:323
@ VM_INTERFACE
Definition: interface.h:38
Type type() const
Definition: interface.h:115
@ L2_RECEIVE
Definition: nexthop.h:347
Type GetType() const
Definition: nexthop.h:406
void Add(const PktInfo *pkt, PktControlInfo *in, PktControlInfo *out)
bool Process(const PktInfo *pkt, PktControlInfo *in, PktControlInfo *out)
FlowEntry * flow_entry
uint32_t out_component_nh_idx
void SetPktInfo(boost::shared_ptr< PktInfo > info)
uint32_t nat_sport
uint32_t nat_dport
Agent * agent_
Definition: proto_handler.h:92
boost::shared_ptr< PktInfo > pkt_info_
Definition: proto_handler.h:93
Definition: vm.h:35
const VmEntry * vm() const
Definition: vrf.h:90
AgentRouteTable * GetBridgeRouteTable() const
Definition: vrf.cc:335
Definition: vrf.h:272
VrfEntry * FindVrfFromId(size_t index)
Definition: vrf.cc:885
static const VmEntry * InterfaceToVm(const Interface *intf)
Definition: flow_handler.cc:17
SandeshTraceBufferPtr PktFlowTraceBuf(SandeshTraceBufferCreate("FlowHandler", 5000))
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:19
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)
Definition: sandesh_trace.h:47
MacAddress smac
Definition: flow_entry.h:293
uint32_t component_nh_idx
Definition: flow_entry.h:319
TunnelInfo tunnel_info
Definition: flow_entry.h:339
uint32_t ttl
Definition: flow_entry.h:323
MacAddress dmac
Definition: flow_entry.h:294
uint32_t if_index_info
Definition: flow_entry.h:338
uint32_t vrf
Definition: flow_entry.h:316
uint8_t protocol
Definition: flow_entry.h:216
IpAddress src_addr
Definition: flow_entry.h:214
IpAddress dst_addr
Definition: flow_entry.h:215
uint16_t src_port
Definition: flow_entry.h:217
uint32_t nh
Definition: flow_entry.h:213
uint16_t dst_port
Definition: flow_entry.h:218
const Interface * intf_
Definition: pkt_flow_info.h:28
const VmEntry * vm_
Definition: pkt_flow_info.h:31