|
| 1 | +// Copyright (C) 2025 Cisco Systems Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | +// implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package felix |
| 17 | + |
| 18 | +import ( |
| 19 | + "net" |
| 20 | + "syscall" |
| 21 | + |
| 22 | + "github.com/pkg/errors" |
| 23 | + "github.com/projectcalico/calico/felix/proto" |
| 24 | + "github.com/sirupsen/logrus" |
| 25 | + "github.com/vishvananda/netlink" |
| 26 | + |
| 27 | + "github.com/projectcalico/vpp-dataplane/v3/calico-vpp-agent/common" |
| 28 | + "github.com/projectcalico/vpp-dataplane/v3/calico-vpp-agent/watchers" |
| 29 | +) |
| 30 | + |
| 31 | +// RouteHandler handles network and IPAM events by updating VPP routing configuration |
| 32 | +type RouteHandler struct { |
| 33 | + log *logrus.Entry |
| 34 | + routeWatcher *watchers.RouteWatcher |
| 35 | +} |
| 36 | + |
| 37 | +// NewRouteHandler creates a new RouteHandler instance |
| 38 | +func NewRouteHandler(log *logrus.Entry) *RouteHandler { |
| 39 | + return &RouteHandler{ |
| 40 | + log: log, |
| 41 | + routeWatcher: nil, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// SetRouteWatcher sets the route watcher for performing route operations |
| 46 | +func (h *RouteHandler) SetRouteWatcher(routeWatcher *watchers.RouteWatcher) { |
| 47 | + h.routeWatcher = routeWatcher |
| 48 | +} |
| 49 | + |
| 50 | +// OnNetDeleted handles network deletion events |
| 51 | +func (h *RouteHandler) OnNetDeleted(netDef *common.NetworkDefinition) error { |
| 52 | + key := netDef.Range |
| 53 | + routes, err := h.getNetworkRoute(key, netDef.PhysicalNetworkName) |
| 54 | + if err != nil { |
| 55 | + h.log.Errorf("Error getting route from network deletion: %v", err) |
| 56 | + return err |
| 57 | + } |
| 58 | + for _, route := range routes { |
| 59 | + err = h.routeWatcher.DelRoute(route) |
| 60 | + if err != nil { |
| 61 | + h.log.Errorf("Cannot delete pool route %s through vpp tap: %v", key, err) |
| 62 | + return err |
| 63 | + } |
| 64 | + } |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// OnNetAddedOrUpdated handles network addition/update events |
| 69 | +func (h *RouteHandler) OnNetAddedOrUpdated(netDef *common.NetworkDefinition) error { |
| 70 | + key := netDef.Range |
| 71 | + routes, err := h.getNetworkRoute(key, netDef.PhysicalNetworkName) |
| 72 | + if err != nil { |
| 73 | + h.log.Errorf("Error getting route from network addition/update: %v", err) |
| 74 | + return err |
| 75 | + } |
| 76 | + for _, route := range routes { |
| 77 | + err = h.routeWatcher.AddRoute(route) |
| 78 | + if err != nil { |
| 79 | + h.log.Errorf("Cannot add pool route %s through vpp tap: %v", key, err) |
| 80 | + return err |
| 81 | + } |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// OnIpamConfChanged handles IPAM configuration changes |
| 87 | +func (h *RouteHandler) OnIpamConfChanged(oldPool, newPool *proto.IPAMPool) error { |
| 88 | + h.log.Debugf("Received IPAM config update in route handler old:%+v new:%+v", oldPool, newPool) |
| 89 | + if newPool == nil && oldPool != nil { |
| 90 | + routes, err := h.getNetworkRoute(oldPool.Cidr, "") |
| 91 | + if err != nil { |
| 92 | + h.log.Errorf("Error getting route from ipam update: %v", err) |
| 93 | + return err |
| 94 | + } |
| 95 | + for _, route := range routes { |
| 96 | + err = h.routeWatcher.DelRoute(route) |
| 97 | + if err != nil { |
| 98 | + h.log.Errorf("Cannot delete pool route %s through vpp tap: %v", oldPool.Cidr, err) |
| 99 | + return err |
| 100 | + } |
| 101 | + } |
| 102 | + } else if newPool != nil { |
| 103 | + routes, err := h.getNetworkRoute(newPool.Cidr, "") |
| 104 | + if err != nil { |
| 105 | + h.log.Errorf("Error getting route from ipam update: %v", err) |
| 106 | + return err |
| 107 | + } |
| 108 | + for _, route := range routes { |
| 109 | + err = h.routeWatcher.AddRoute(route) |
| 110 | + if err != nil { |
| 111 | + h.log.Errorf("Cannot add pool route %s through vpp tap: %v", newPool.Cidr, err) |
| 112 | + return err |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func (h *RouteHandler) getNetworkRoute(network string, physicalNet string) (route []*netlink.Route, err error) { |
| 120 | + _, cidr, err := net.ParseCIDR(network) |
| 121 | + if err != nil { |
| 122 | + return nil, errors.Wrapf(err, "error parsing %s", network) |
| 123 | + } |
| 124 | + var routes []*netlink.Route |
| 125 | + var order int |
| 126 | + for _, uplinkStatus := range common.VppManagerInfo.UplinkStatuses { |
| 127 | + if uplinkStatus.PhysicalNetworkName == physicalNet { |
| 128 | + gw := uplinkStatus.FakeNextHopIP4 |
| 129 | + if cidr.IP.To4() == nil { |
| 130 | + gw = uplinkStatus.FakeNextHopIP6 |
| 131 | + } |
| 132 | + var priority int |
| 133 | + if uplinkStatus.IsMain { |
| 134 | + priority = 0 |
| 135 | + } else { |
| 136 | + order += 1 |
| 137 | + priority = order |
| 138 | + } |
| 139 | + routes = append(routes, &netlink.Route{ |
| 140 | + Dst: cidr, |
| 141 | + Gw: gw, |
| 142 | + Protocol: syscall.RTPROT_STATIC, |
| 143 | + MTU: watchers.GetUplinkMtu(), |
| 144 | + Priority: priority, |
| 145 | + }) |
| 146 | + } |
| 147 | + } |
| 148 | + return routes, nil |
| 149 | +} |
0 commit comments