@@ -25,15 +25,10 @@ const (
2525 // MapNameSnat6Global represents global IPv6 NAT table.
2626 MapNameSnat6Global = "cilium_snat_v6_external"
2727
28- // MinPortSnatDefault represents default min port from range.
29- MinPortSnatDefault = 1024
30- // MaxPortSnatDefault represents default max port from range.
31- MaxPortSnatDefault = 65535
32-
33- // MapNameSnat4AllocRetries represents the histogram of IPv4 NAT port allocation retries.
34- MapNameSnat4AllocRetries = "cilium_snat_v4_alloc_retries"
35- // MapNameSnat6AllocRetries represents the histogram of IPv6 NAT port allocation retries.
36- MapNameSnat6AllocRetries = "cilium_snat_v6_alloc_retries"
28+ // mapNameSnat4AllocRetries represents the histogram of IPv4 NAT port allocation retries.
29+ mapNameSnat4AllocRetries = "cilium_snat_v4_alloc_retries"
30+ // mapNameSnat6AllocRetries represents the histogram of IPv6 NAT port allocation retries.
31+ mapNameSnat6AllocRetries = "cilium_snat_v6_alloc_retries"
3732
3833 // SnatCollisionRetries represents the maximum number of port allocation retries.
3934 SnatCollisionRetries = 32
@@ -117,7 +112,8 @@ type RetriesKey struct {
117112 Key uint32
118113}
119114
120- func (k * RetriesKey ) String () string { return fmt .Sprintf ("%d" , k .Key ) }
115+ func (k * RetriesKey ) String () string { return fmt .Sprintf ("%d" , k .Key ) }
116+
121117func (k * RetriesKey ) New () bpf.MapKey { return & RetriesKey {} }
122118
123119type RetriesValue struct {
@@ -126,26 +122,17 @@ type RetriesValue struct {
126122
127123type RetriesValues []RetriesValue
128124
129- func (k * RetriesValue ) String () string { return fmt .Sprintf ("%d" , k .Value ) }
125+ func (k * RetriesValue ) String () string { return fmt .Sprintf ("%d" , k .Value ) }
126+
130127func (k * RetriesValue ) New () bpf.MapValue { return & RetriesValue {} }
131- func (k * RetriesValue ) NewSlice () any { return & RetriesValues {} }
128+
129+ func (k * RetriesValue ) NewSlice () any { return & RetriesValues {} }
132130
133131type RetriesMapRecord struct {
134132 Key * RetriesKey
135133 Value * RetriesValue
136134}
137135
138- func NewRetriesMap (name string ) * bpf.Map {
139- return bpf .NewMap (
140- name ,
141- ebpf .PerCPUArray ,
142- & RetriesKey {},
143- & RetriesValue {},
144- SnatCollisionRetries + 1 ,
145- 0 ,
146- )
147- }
148-
149136// DumpBatch4 uses batch iteration to walk the map and applies fn for each batch of entries.
150137func (m * Map ) DumpBatch4 (fn func (* tuple.TupleKey4 , * NatEntry4 )) (count int , err error ) {
151138 if m .family != IPv4 {
@@ -425,32 +412,64 @@ func maxEntries() int {
425412 return option .LimitTableMax
426413}
427414
415+ type natRetriesMap struct {
416+ bpfMapV4 * bpf.Map
417+ bpfMapV6 * bpf.Map
418+ }
419+
420+ func newNATRetriesMap (ipv4Enabled bool , ipv6Enabled bool ) * natRetriesMap {
421+ m := & natRetriesMap {}
422+
423+ if ipv4Enabled {
424+ m .bpfMapV4 = newRetriesMap (mapNameSnat4AllocRetries )
425+ }
426+
427+ if ipv6Enabled {
428+ m .bpfMapV6 = newRetriesMap (mapNameSnat6AllocRetries )
429+ }
430+
431+ return m
432+ }
433+
434+ func (m * natRetriesMap ) init () error {
435+ if m .bpfMapV4 != nil {
436+ if err := m .bpfMapV4 .Create (); err != nil {
437+ return fmt .Errorf ("failed to create nat retries v4 bpf map: %w" , err )
438+ }
439+ }
440+
441+ if m .bpfMapV6 != nil {
442+ if err := m .bpfMapV6 .Create (); err != nil {
443+ return fmt .Errorf ("failed to create nat retries v6 bpf map: %w" , err )
444+ }
445+ }
446+
447+ return nil
448+ }
449+
450+ func newRetriesMap (name string ) * bpf.Map {
451+ return bpf .NewMap (
452+ name ,
453+ ebpf .PerCPUArray ,
454+ & RetriesKey {},
455+ & RetriesValue {},
456+ SnatCollisionRetries + 1 ,
457+ 0 ,
458+ )
459+ }
460+
428461// RetriesMaps returns the maps that contain the histograms of the number of retries.
462+ // This should only be used from components which aren't capable of using hive - mainly the cilium-dbg.
463+ // It needs to initialized beforehand via the Cilium Agent.
429464func RetriesMaps (ipv4 , ipv6 , natRequired bool ) (ipv4RetriesMap , ipv6RetriesMap RetriesMap ) {
430465 if ! natRequired {
431466 return
432467 }
433468 if ipv4 {
434- ipv4RetriesMap = NewRetriesMap ( MapNameSnat4AllocRetries )
469+ ipv4RetriesMap = newRetriesMap ( mapNameSnat4AllocRetries )
435470 }
436471 if ipv6 {
437- ipv6RetriesMap = NewRetriesMap ( MapNameSnat6AllocRetries )
472+ ipv6RetriesMap = newRetriesMap ( mapNameSnat6AllocRetries )
438473 }
439474 return
440475}
441-
442- func CreateRetriesMaps (ipv4 , ipv6 bool ) error {
443- if ipv4 {
444- ipv4Map := NewRetriesMap (MapNameSnat4AllocRetries )
445- if err := ipv4Map .OpenOrCreate (); err != nil {
446- return err
447- }
448- }
449- if ipv6 {
450- ipv6Map := NewRetriesMap (MapNameSnat6AllocRetries )
451- if err := ipv6Map .OpenOrCreate (); err != nil {
452- return err
453- }
454- }
455- return nil
456- }
0 commit comments