forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_vppcalls.go
150 lines (131 loc) · 4.34 KB
/
route_vppcalls.go
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
// Copyright (c) 2019 Cisco and/or its affiliates.
//
// 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.
package vpp2001_324
import (
"net"
"github.com/ligato/vpp-agent/api/models/netalloc"
l3 "github.com/ligato/vpp-agent/api/models/vpp/l3"
vpp_ip "github.com/ligato/vpp-agent/plugins/vpp/binapi/vpp2001_324/ip"
"github.com/pkg/errors"
)
const (
// NextHopViaLabelUnset constant has to be assigned into the field next hop
// via label in ip_add_del_route binary message if next hop via label is not defined.
// Equals to MPLS_LABEL_INVALID defined in VPP
NextHopViaLabelUnset uint32 = 0xfffff + 1
// ClassifyTableIndexUnset is a default value for field classify_table_index in ip_add_del_route binary message.
ClassifyTableIndexUnset = ^uint32(0)
// NextHopOutgoingIfUnset constant has to be assigned into the field next_hop_outgoing_interface
// in ip_add_del_route binary message if outgoing interface for next hop is not defined.
NextHopOutgoingIfUnset = ^uint32(0)
)
// vppAddDelRoute adds or removes route, according to provided input. Every route has to contain VRF ID (default is 0).
func (h *RouteHandler) vppAddDelRoute(route *l3.Route, rtIfIdx uint32, delete bool) error {
req := &vpp_ip.IPRouteAddDel{
// Multi path is always true
IsMultipath: 1,
}
if delete {
req.IsAdd = 0
} else {
req.IsAdd = 1
}
// Common route parameters
fibPath := vpp_ip.FibPath{
Weight: uint8(route.Weight),
Preference: uint8(route.Preference),
}
if route.NextHopAddr != "" {
nextHop, err := h.addrAlloc.GetOrParseIPAddress(route.NextHopAddr,
route.OutgoingInterface, netalloc.IPAddressForm_ADDR_ONLY)
if err != nil {
return err
}
fibPath.Nh, fibPath.Proto = setFibPathNhAndProto(nextHop.IP)
}
// VRF/Other route parameters based on type
if route.Type == l3.Route_INTER_VRF {
fibPath.SwIfIndex = rtIfIdx
fibPath.TableID = route.ViaVrfId
} else if route.Type == l3.Route_DROP {
fibPath.Type = vpp_ip.FIB_API_PATH_TYPE_DROP
} else {
fibPath.SwIfIndex = rtIfIdx
fibPath.TableID = route.VrfId
}
// Destination address
dstNet, err := h.addrAlloc.GetOrParseIPAddress(route.DstNetwork,
"", netalloc.IPAddressForm_ADDR_NET)
if err != nil {
return err
}
prefix := networkToPrefix(dstNet)
req.Route = vpp_ip.IPRoute{
TableID: route.VrfId,
Prefix: prefix,
NPaths: 1,
Paths: []vpp_ip.FibPath{fibPath},
}
reply := &vpp_ip.IPRouteAddDelReply{}
if err := h.callsChannel.SendRequest(req).ReceiveReply(reply); err != nil {
return err
}
return nil
}
// VppAddRoute implements route handler.
func (h *RouteHandler) VppAddRoute(route *l3.Route) error {
swIfIdx, err := h.getRouteSwIfIndex(route.OutgoingInterface)
if err != nil {
return err
}
return h.vppAddDelRoute(route, swIfIdx, false)
}
// VppDelRoute implements route handler.
func (h *RouteHandler) VppDelRoute(route *l3.Route) error {
swIfIdx, err := h.getRouteSwIfIndex(route.OutgoingInterface)
if err != nil {
return err
}
return h.vppAddDelRoute(route, swIfIdx, true)
}
func setFibPathNhAndProto(netIP net.IP) (nh vpp_ip.FibPathNh, proto vpp_ip.FibPathNhProto) {
var addrUnion vpp_ip.AddressUnion
if netIP.To4() == nil {
proto = vpp_ip.FIB_API_PATH_NH_PROTO_IP6
var ip6addr vpp_ip.IP6Address
copy(ip6addr[:], netIP.To16())
addrUnion.SetIP6(ip6addr)
} else {
proto = vpp_ip.FIB_API_PATH_NH_PROTO_IP4
var ip4addr vpp_ip.IP4Address
copy(ip4addr[:], netIP.To4())
addrUnion.SetIP4(ip4addr)
}
return vpp_ip.FibPathNh{
Address: addrUnion,
ViaLabel: NextHopViaLabelUnset,
ClassifyTableIndex: ClassifyTableIndexUnset,
}, proto
}
func (h *RouteHandler) getRouteSwIfIndex(ifName string) (swIfIdx uint32, err error) {
swIfIdx = NextHopOutgoingIfUnset
if ifName != "" {
meta, found := h.ifIndexes.LookupByName(ifName)
if !found {
return 0, errors.Errorf("interface %s not found", ifName)
}
swIfIdx = meta.SwIfIndex
}
return
}