github.com/cilium/cilium@v1.16.2/pkg/bgpv1/types/bgp.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package types 5 6 import ( 7 "context" 8 "net/netip" 9 10 "github.com/osrg/gobgp/v3/pkg/packet/bgp" 11 12 "github.com/cilium/cilium/api/v1/models" 13 v2alpha1api "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1" 14 ) 15 16 // BGPGlobal contains high level BGP configuration for given instance. 17 type BGPGlobal struct { 18 ASN uint32 19 RouterID string 20 ListenPort int32 // When -1 gobgp won't listen on tcp:179 21 RouteSelectionOptions *RouteSelectionOptions 22 } 23 24 // RouteSelectionOptions contains generic BGP route selection tuning parameters 25 type RouteSelectionOptions struct { 26 // AdvertiseInactiveRoutes when set will advertise route even if it is not present in RIB 27 AdvertiseInactiveRoutes bool 28 } 29 30 // StateNotificationCh is a channel used to notify the state of the BGP instance has changed 31 type StateNotificationCh chan struct{} 32 33 // Path is an object representing a single routing Path. It is an analogue of GoBGP's Path object, 34 // but only contains minimal fields required for Cilium usecases. 35 type Path struct { 36 // read/write 37 NLRI bgp.AddrPrefixInterface 38 PathAttributes []bgp.PathAttributeInterface 39 Family Family // can be empty, in which case it will be inferred from NLRI 40 41 // readonly 42 AgeNanoseconds int64 // time duration in nanoseconds since the Path was created 43 Best bool 44 UUID []byte // path identifier in underlying implementation 45 } 46 47 // NeighborRequest contains neighbor parameters used when enabling or disabling peer 48 type NeighborRequest struct { 49 // Deprecated: field kept for backward compatibility. 50 // 51 // Both Neighbor and Peer should not be used at the same time. 52 // Neighbor field is used in BGPv1 and Peer, PeerConfig fields are used in BGPv2. 53 Neighbor *v2alpha1api.CiliumBGPNeighbor 54 55 Peer *v2alpha1api.CiliumBGPNodePeer 56 PeerConfig *v2alpha1api.CiliumBGPPeerConfigSpec 57 // Password is the "AuthSecret" in the Neighbor, fetched from a secret 58 Password string 59 } 60 61 // SoftResetDirection defines the direction in which a BGP soft reset should be performed 62 type SoftResetDirection int 63 64 const ( 65 SoftResetDirectionIn SoftResetDirection = iota 66 SoftResetDirectionOut 67 SoftResetDirectionBoth 68 ) 69 70 // ResetNeighborRequest contains parameters used when resetting a BGP peer 71 type ResetNeighborRequest struct { 72 PeerAddress string 73 Soft bool 74 SoftResetDirection SoftResetDirection 75 AdminCommunication string 76 } 77 78 // PathRequest contains parameters for advertising or withdrawing a Path 79 type PathRequest struct { 80 Path *Path 81 } 82 83 // PathResponse contains response after advertising the Path, the returned Path can be used 84 // for withdrawing the Path (based on UUID set by the underlying implementation) 85 type PathResponse struct { 86 Path *Path 87 } 88 89 // RoutePolicyPrefixMatch can be used to match a CIDR prefix in a routing policy. 90 // It can be used to perform exact prefix length matches (if CIDR.Bits() == PrefixLenMin == PrefixLenMax), 91 // or variable prefix length matches. 92 // 93 // +deepequal-gen=true 94 // +deepequal-gen:private-method=true 95 type RoutePolicyPrefixMatch struct { 96 // CIDR is a prefix to match with. 97 // +deepequal-gen=false 98 CIDR netip.Prefix 99 // PrefixLenMin is the minimal prefix length that will match if it falls under CIDR. 100 PrefixLenMin int 101 // PrefixLenMax is the maximal prefix length that will match if it falls under CIDR. 102 PrefixLenMax int 103 } 104 105 // RoutePolicyConditions represent conditions of a policy statement. 106 // 107 // +deepequal-gen=true 108 type RoutePolicyConditions struct { 109 // MatchNeighbors matches ANY of the provided BGP neighbor IP addresses. If empty matches all neighbors. 110 MatchNeighbors []string 111 // MatchPrefixes matches ANY of the provided prefixes. If empty matches all prefixes. 112 MatchPrefixes []*RoutePolicyPrefixMatch 113 } 114 115 // RoutePolicyAction defines the action taken on a route matched by a routing policy. 116 type RoutePolicyAction int 117 118 const ( 119 // RoutePolicyActionNone does not affect processing of a route. 120 // The policy evaluation continues with the next policy statements / other policies. 121 RoutePolicyActionNone RoutePolicyAction = iota 122 // RoutePolicyActionAccept accepts a route into the RIB / adjacency RIB. 123 // No further policy statements / policies are evaluated for the route. 124 RoutePolicyActionAccept 125 // RoutePolicyActionReject rejects a route from the RIB / adjacency RIB. 126 // No further policy statements / policies are evaluated for the route. 127 RoutePolicyActionReject 128 ) 129 130 // RoutePolicyActions define policy actions taken on route matched by a routing policy. 131 // 132 // +deepequal-gen=true 133 type RoutePolicyActions struct { 134 // RouteAction defines an action taken on the matched route. 135 RouteAction RoutePolicyAction 136 // AddCommunities defines a list of BGP standard community values to be added to the matched route. 137 // If empty, no communities will be added. 138 AddCommunities []string 139 // AddCommunities defines a list of BGP large community values to be added to the matched route. 140 // If empty, no communities will be added. 141 AddLargeCommunities []string 142 // SetLocalPreference define a BGP local preference value to be set on the matched route. 143 // If nil, no local preference is set. 144 SetLocalPreference *int64 145 } 146 147 // RoutePolicyStatement represents a single statement of a routing RoutePolicy. It contains conditions for 148 // matching a route and actions taken if a route matches the conditions. 149 // 150 // +deepequal-gen=true 151 type RoutePolicyStatement struct { 152 // Conditions of the statement. If ALL of them match a route, the Actions are taken on the route. 153 Conditions RoutePolicyConditions 154 // Actions define actions taken on a matched route. 155 Actions RoutePolicyActions 156 } 157 158 // RoutePolicyType defines the type of routing policy. 159 type RoutePolicyType int 160 161 const ( 162 // RoutePolicyTypeExport represents export routing policy type (affecting how the routes from RIB are advertised to peers). 163 RoutePolicyTypeExport RoutePolicyType = iota 164 // RoutePolicyTypeImport represents import routing policy type (affecting how the routes are imported into RIB). 165 RoutePolicyTypeImport 166 ) 167 168 // RoutePolicy represents a BGP routing policy, also called "route map" in some BGP implementations. 169 // It can contain multiple Statements that are evaluated in the given order. Each Statement 170 // contains conditions for matching a route and actions taken if a route matches the conditions. 171 // Whenever a Statement matches a route and the action taken on it is to either accept or reject the route, 172 // the policy evaluation for the given route stops, and no further Statements nor other RoutePolicies are evaluated. 173 // 174 // +deepequal-gen=true 175 type RoutePolicy struct { 176 // Name is a unique string identifier of the policy for the given router. 177 Name string 178 // RoutePolicyType is the type of the policy. 179 Type RoutePolicyType 180 // Statements is an ordered list of policy statements. 181 Statements []*RoutePolicyStatement 182 } 183 184 // RoutePolicyRequest contains parameters for adding or removing a routing policy. 185 type RoutePolicyRequest struct { 186 DefaultExportAction RoutePolicyAction 187 Policy *RoutePolicy 188 } 189 190 // GetPeerStateResponse contains state of peers configured in given instance 191 type GetPeerStateResponse struct { 192 Peers []*models.BgpPeer 193 } 194 195 // GetBGPResponse contains BGP global parameters 196 type GetBGPResponse struct { 197 Global BGPGlobal 198 } 199 200 // ServerParameters contains information for underlying bgp implementation layer to initializing BGP process. 201 type ServerParameters struct { 202 Global BGPGlobal 203 StateNotification StateNotificationCh 204 } 205 206 // Family holds Address Family Indicator (AFI) and Subsequent Address Family Indicator for Multi-Protocol BGP 207 type Family struct { 208 Afi Afi 209 Safi Safi 210 } 211 212 func (f Family) String() string { 213 return f.Afi.String() + "-" + f.Safi.String() 214 } 215 216 // Route represents a single route in the RIB of underlying router 217 type Route struct { 218 Prefix string 219 Paths []*Path 220 } 221 222 // TableType specifies the routing table type of underlying router 223 type TableType int 224 225 const ( 226 TableTypeUnknown TableType = iota 227 TableTypeLocRIB 228 TableTypeAdjRIBIn 229 TableTypeAdjRIBOut 230 ) 231 232 // ParseTableType parses s as a routing table type. If s is unknown, 233 // TableTypeUnknown is returned. 234 func ParseTableType(s string) TableType { 235 switch s { 236 case "loc-rib": 237 return TableTypeLocRIB 238 case "adj-rib-in": 239 return TableTypeAdjRIBIn 240 case "adj-rib-out": 241 return TableTypeAdjRIBOut 242 default: 243 return TableTypeUnknown 244 } 245 } 246 247 // GetRoutesRequest contains parameters for retrieving routes from the RIB of underlying router 248 type GetRoutesRequest struct { 249 // TableType specifies a table type to retrieve 250 TableType TableType 251 252 // Family specifies an address family of the table 253 Family Family 254 255 // Neighbor specifies which neighbor's table to retrieve. Must be 256 // specified when TableTypeAdjRIBIn/Out is specified in TableType. 257 Neighbor netip.Addr 258 } 259 260 // GetRoutesResponse contains routes retrieved from the RIB of underlying router 261 type GetRoutesResponse struct { 262 Routes []*Route 263 } 264 265 // GetRoutePoliciesResponse contains route policies retrieved from the underlying router 266 type GetRoutePoliciesResponse struct { 267 Policies []*RoutePolicy 268 } 269 270 // Router is vendor-agnostic cilium bgp configuration layer. Parameters of this layer 271 // are standard BGP RFC complaint and not specific to any underlying implementation. 272 type Router interface { 273 Stop() 274 275 // AddNeighbor configures BGP peer 276 AddNeighbor(ctx context.Context, n NeighborRequest) error 277 278 // UpdateNeighbor updates BGP peer 279 UpdateNeighbor(ctx context.Context, n NeighborRequest) error 280 281 // RemoveNeighbor removes BGP peer 282 RemoveNeighbor(ctx context.Context, n NeighborRequest) error 283 284 // ResetNeighbor resets BGP peering with the provided neighbor address 285 ResetNeighbor(ctx context.Context, r ResetNeighborRequest) error 286 287 // AdvertisePath advertises BGP Path to all configured peers 288 AdvertisePath(ctx context.Context, p PathRequest) (PathResponse, error) 289 290 // WithdrawPath removes BGP Path from all peers 291 WithdrawPath(ctx context.Context, p PathRequest) error 292 293 // AddRoutePolicy adds a new routing policy into the underlying router. 294 AddRoutePolicy(ctx context.Context, p RoutePolicyRequest) error 295 296 // RemoveRoutePolicy removes a routing policy from the underlying router. 297 RemoveRoutePolicy(ctx context.Context, p RoutePolicyRequest) error 298 299 // GetPeerState returns status of BGP peers 300 GetPeerState(ctx context.Context) (GetPeerStateResponse, error) 301 302 // GetRoutes retrieves routes from the RIB of underlying router 303 GetRoutes(ctx context.Context, r *GetRoutesRequest) (*GetRoutesResponse, error) 304 305 // GetRoutePolicies retrieves route policies from the underlying router 306 GetRoutePolicies(ctx context.Context) (*GetRoutePoliciesResponse, error) 307 308 // GetBGP returns configured BGP global parameters 309 GetBGP(ctx context.Context) (GetBGPResponse, error) 310 }