projectcalico / calico

Cloud native networking and network security
https://docs.tigera.io/calico/latest/about/
Apache License 2.0
6.04k stars 1.35k forks source link

How to learn the principle of Calico BGP #9500

Closed asskss closed 1 day ago

asskss commented 2 days ago

I hope to learn the principles of Calico BGP through source code

  1. How is pod ip allocated using BGP mode
  2. How to generate BGP routes from allocated IP addresses
  3. Why can a route of/32 be configured when the blocksize is set to 26

ippool

apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
  creationTimestamp: "2023-06-26T01:33:39Z"
  name: ippool-121-128
  resourceVersion: "94611962"
  uid: f91c2f6c-701b-4fec-9add-03e2e2c97be1
spec:
  allowedUses:
  - Workload
  blockSize: 27
  cidr: 10.128.121.128/25
  ipipMode: Never
  natOutgoing: true
  nodeSelector: all()
  vxlanMode: Never

birdcl -s /var/run/calico/bird.ctl show route | grep 10.128.121 | grep 122.48

10.128.121.154/32  via 10.128.122.48 on bond0 [Node_10_128_122_26 08:12:30 from 10.128.122.26] * (100/0) [i]
10.128.121.145/32  via 10.128.122.48 on bond0 [Node_10_128_122_26 07:16:26 from 10.128.122.26] * (100/0) [i]
10.128.121.192/27  via 10.128.122.48 on bond0 [Node_10_128_122_26 2023-10-08 from 10.128.122.26] * (100/0) [i]
10.128.121.255/32  via 10.128.122.48 on bond0 [Node_10_128_122_26 2024-09-05 from 10.128.122.26] * (100/0) [i]
10.128.121.250/32  via 10.128.122.48 on bond0 [Node_10_128_122_26 2024-10-22 from 10.128.122.26] * (100/0) [i]
10.128.121.244/32  via 10.128.122.48 on bond0 [Node_10_128_122_26 2024-10-14 from 10.128.122.26] * (100/0) [i]
10.128.121.177/32  via 10.128.122.48 on bond0 [Node_10_128_122_26 2024-07-30 from 10.128.122.26] * (100/0) [i]
10.128.121.241/32  via 10.128.122.48 on bond0 [Node_10_128_122_26 2024-11-13 from 10.128.122.26] * (100/0) [i]
10.128.121.233/32  via 10.128.122.48 on bond0 [Node_10_128_122_26 2024-07-05 from 10.128.122.26] * (100/0) [i]
10.128.121.234/32  via 10.128.122.48 on bond0 [Node_10_128_122_26 10:00:01 from 10.128.122.26] * (100/0) [i]
10.128.121.97/32   via 10.128.122.48 on bond0 [Node_10_128_122_26 2024-06-18 from 10.128.122.26] * (100/0) [i]
10.128.121.163/32  via 10.128.122.48 on bond0 [Node_10_128_122_26 2024-10-23 from 10.128.122.26] * (100/0) [i]

From the above example, it can be seen that the block size is/27, but the BGP routing can allocate/32. Why is this? If we start learning this from the source code, where should we start?

mazdakn commented 2 days ago

@asskss, IPAM allocation is no different in BGP setup. Calico CNI is responsible to IP allocation in all networking options. CNI source: https://github.com/projectcalico/calico/tree/master/cni-plugin

In BGP case, ConfD is responsible for watching for resources like nodes, allocations, and also for generating BIRD configs from predefined templates. BIRD templates: https://github.com/projectcalico/calico/tree/master/confd/etc/calico/confd/templates ConfD: https://github.com/projectcalico/calico/tree/master/confd

It's hard to get into the detail of your question in a GH issue. I suggest to join Calico slack channel (https://calicousers.slack.com/) and ask specific questions about this matter there.

asskss commented 2 days ago

@asskss, IPAM allocation is no different in BGP setup. Calico CNI is responsible to IP allocation in all networking options. CNI source: https://github.com/projectcalico/calico/tree/master/cni-plugin

In BGP case, ConfD is responsible for watching for resources like nodes, allocations, and also for generating BIRD configs from predefined templates. BIRD templates: https://github.com/projectcalico/calico/tree/master/confd/etc/calico/confd/templates ConfD: https://github.com/projectcalico/calico/tree/master/confd

It's hard to get into the detail of your question in a GH issue. I suggest to join Calico slack channel (https://calicousers.slack.com/) and ask specific questions about this matter there.

thanks