-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathzedroutertypes.go
1374 lines (1205 loc) · 45.7 KB
/
zedroutertypes.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017-2021 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package types
import (
"fmt"
"net"
"strings"
"time"
"github.com/google/go-cmp/cmp"
"github.com/lf-edge/eve/pkg/kube/cnirpc"
"github.com/lf-edge/eve/pkg/pillar/base"
"github.com/lf-edge/eve/pkg/pillar/objtonum"
"github.com/lf-edge/eve/pkg/pillar/utils/netutils"
uuid "github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
)
// AppNetworkConfig : network configuration for a given application.
type AppNetworkConfig struct {
UUIDandVersion UUIDandVersion
DisplayName string
Activate bool
GetStatsIPAddr net.IP
AppNetAdapterList []AppNetAdapterConfig
CloudInitUserData *string `json:"pubsub-large-CloudInitUserData"`
CipherBlockStatus CipherBlockStatus
MetaDataType MetaDataType
}
// Key :
func (config AppNetworkConfig) Key() string {
return config.UUIDandVersion.UUID.String()
}
// LogCreate :
func (config AppNetworkConfig) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.AppNetworkConfigLogType, config.DisplayName,
config.UUIDandVersion.UUID, config.LogKey())
if logObject == nil {
return
}
logObject.CloneAndAddField("activate", config.Activate).
Noticef("App network config create")
}
// LogModify :
func (config AppNetworkConfig) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.AppNetworkConfigLogType, config.DisplayName,
config.UUIDandVersion.UUID, config.LogKey())
oldConfig, ok := old.(AppNetworkConfig)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of AppNetworkConfig type")
}
if oldConfig.Activate != config.Activate {
logObject.CloneAndAddField("activate", config.Activate).
AddField("old-activate", oldConfig.Activate).
Noticef("App network config modify")
} else {
// Log at Function level
logObject.CloneAndAddField("diff", cmp.Diff(oldConfig, config)).
Functionf("App network config modify other change")
}
}
// LogDelete :
func (config AppNetworkConfig) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.AppNetworkConfigLogType, config.DisplayName,
config.UUIDandVersion.UUID, config.LogKey())
logObject.CloneAndAddField("activate", config.Activate).
Noticef("App network config delete")
base.DeleteLogObject(logBase, config.LogKey())
}
// LogKey :
func (config AppNetworkConfig) LogKey() string {
return string(base.AppNetworkConfigLogType) + "-" + config.Key()
}
func (config *AppNetworkConfig) getAppNetAdapterConfig(
network uuid.UUID) *AppNetAdapterConfig {
for i := range config.AppNetAdapterList {
adapterConfig := &config.AppNetAdapterList[i]
if adapterConfig.Network == network {
return adapterConfig
}
}
return nil
}
// IsNetworkUsed returns true if the given network instance is used by this app.
func (config *AppNetworkConfig) IsNetworkUsed(network uuid.UUID) bool {
return config.getAppNetAdapterConfig(network) != nil
}
// AppNetworkStatus : status of app connectivity.
type AppNetworkStatus struct {
UUIDandVersion UUIDandVersion
AppNum int
Activated bool
PendingAdd bool
PendingModify bool
PendingDelete bool
ConfigInSync bool
DisplayName string
// AppPod is only valid in Kubernetes mode.
AppPod cnirpc.AppPod
// Copy from the AppNetworkConfig; used to delete when config is gone.
GetStatsIPAddr net.IP
AppNetAdapterList []AppNetAdapterStatus
AwaitNetworkInstance bool // If any Missing flag is set in the networks
// ID of the MAC generator variant that was used to generate MAC addresses for this app.
MACGenerator int
// Any errors from provisioning the network
// ErrorAndTime provides SetErrorNow() and ClearError()
ErrorAndTime
}
// Key :
func (status AppNetworkStatus) Key() string {
return status.UUIDandVersion.UUID.String()
}
// LogCreate :
func (status AppNetworkStatus) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.AppNetworkStatusLogType, status.DisplayName,
status.UUIDandVersion.UUID, status.LogKey())
if logObject == nil {
return
}
logObject.CloneAndAddField("activated", status.Activated).
Noticef("App network status create")
}
// LogModify :
func (status AppNetworkStatus) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.AppNetworkStatusLogType, status.DisplayName,
status.UUIDandVersion.UUID, status.LogKey())
oldStatus, ok := old.(AppNetworkStatus)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of AppNetworkStatus type")
}
if oldStatus.Activated != status.Activated {
logObject.CloneAndAddField("activated", status.Activated).
AddField("old-activated", oldStatus.Activated).
Noticef("App network status modify")
} else {
// Log at Function level
logObject.CloneAndAddField("diff", cmp.Diff(oldStatus, status)).
Functionf("App network status modify other change")
}
if status.HasError() {
errAndTime := status.ErrorAndTime
logObject.CloneAndAddField("activated", status.Activated).
AddField("error", errAndTime.Error).
AddField("error-time", errAndTime.ErrorTime).
Noticef("App network status modify")
}
}
// LogDelete :
func (status AppNetworkStatus) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.AppNetworkStatusLogType, status.DisplayName,
status.UUIDandVersion.UUID, status.LogKey())
logObject.CloneAndAddField("activated", status.Activated).
Noticef("App network status delete")
base.DeleteLogObject(logBase, status.LogKey())
}
// LogKey :
func (status AppNetworkStatus) LogKey() string {
return string(base.AppNetworkStatusLogType) + "-" + status.Key()
}
// Pending returns true if the last configuration operation is still pending
// and not processed yet.
func (status AppNetworkStatus) Pending() bool {
return status.PendingAdd || status.PendingModify || status.PendingDelete
}
// AwaitingNetwork - Is the app waiting for network?
func (status AppNetworkStatus) AwaitingNetwork() bool {
return status.AwaitNetworkInstance
}
// GetAdaptersStatusForNI returns AppNetAdapterStatus for every application VIF
// connected to the given network instance (there can be multiple interfaces connected
// to the same network instance).
func (status AppNetworkStatus) GetAdaptersStatusForNI(netUUID uuid.UUID) []*AppNetAdapterStatus {
var adapters []*AppNetAdapterStatus
for i := range status.AppNetAdapterList {
adapter := &status.AppNetAdapterList[i]
if adapter.Network == netUUID {
adapters = append(adapters, adapter)
}
}
return adapters
}
// AppContainerMetrics - App Container Metrics
type AppContainerMetrics struct {
UUIDandVersion UUIDandVersion // App UUID
// Stats Collection time for uploading stats to cloud
CollectTime time.Time
StatsList []AppContainerStats
}
// AppContainerStats - for App Container Stats
type AppContainerStats struct {
ContainerName string // unique under an App
Status string // uptime, pause, stop status
Pids uint32 // number of PIDs within the container
// CPU stats
Uptime int64 // unix.nano, time since container starts
CPUTotal uint64 // container CPU since starts in nanosec
SystemCPUTotal uint64 // total system, user, idle in nanosec
// Memory stats
UsedMem uint32 // in MBytes
AllocatedMem uint32 // in MBytes
// Network stats
TxBytes uint64 // in Bytes
RxBytes uint64 // in Bytes
// Disk stats
ReadBytes uint64 // in MBytes
WriteBytes uint64 // in MBytes
}
// Key - key for AppContainerMetrics
func (acMetric AppContainerMetrics) Key() string {
return acMetric.UUIDandVersion.UUID.String()
}
// LogCreate :
func (acMetric AppContainerMetrics) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.AppContainerMetricsLogType, "",
acMetric.UUIDandVersion.UUID, acMetric.LogKey())
if logObject == nil {
return
}
logObject.Metricf("App container metric create")
}
// LogModify :
func (acMetric AppContainerMetrics) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.AppContainerMetricsLogType, "",
acMetric.UUIDandVersion.UUID, acMetric.LogKey())
oldAcMetric, ok := old.(AppContainerMetrics)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of AppContainerMetrics type")
}
// XXX remove? XXX huge?
logObject.CloneAndAddField("diff", cmp.Diff(oldAcMetric, acMetric)).
Metricf("App container metric modify")
}
// LogDelete :
func (acMetric AppContainerMetrics) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.AppContainerMetricsLogType, "",
acMetric.UUIDandVersion.UUID, acMetric.LogKey())
logObject.Metricf("App container metric delete")
base.DeleteLogObject(logBase, acMetric.LogKey())
}
// LogKey :
func (acMetric AppContainerMetrics) LogKey() string {
return string(base.AppContainerMetricsLogType) + "-" + acMetric.Key()
}
// AppNetAdapterConfig : configuration for one application network adapter.
type AppNetAdapterConfig struct {
Name string // From proto message
AppMacAddr net.HardwareAddr // If set use it for vif
AppIPAddr net.IP // If set use DHCP to assign to app
IntfOrder uint32 // Order wrt. other virtual and also directly assigned network adapters
// XXX Shouldn't we use ErrorAndTime here
// Error
// If there is a parsing error and this AppNetAdapterNetwork config cannot be
// processed, set the error here. This allows the error to be propagated
// back to zedcloud
Error string
Network uuid.UUID // Points to a NetworkInstance.
ACLs []ACE
AccessVlanID uint32
IfIdx uint32 // If we have multiple interfaces on that network, we will increase the index
AllowToDiscover bool
}
// ACEDirection determines rule direction.
type ACEDirection uint8
const (
// AceDirBoth : Rule applies in both directions
AceDirBoth ACEDirection = iota
// AceDirIngress : Rules applies in Ingress direction (from internet to app)
AceDirIngress ACEDirection = 1
// AceDirEgress : Rules applies in Egress direction (from app to internet)
AceDirEgress ACEDirection = 2
)
// ACE definition is very similar to draft-ietf-netmod-acl-model
type ACE struct {
Matches []ACEMatch
Actions []ACEAction
Name string
RuleID int32
Dir ACEDirection
}
// ACEMatch determines which traffic is matched by a given ACE.
// The Type can be "ip" or "host" (aka domain name), "eidset", "protocol",
// "fport", "lport" or "adapter" for now. The "ip" and "host" matches the remote IP/hostname.
// The host matching is suffix-matching thus zededa.net matches *.zededa.net.
// "adapter" matches devices ports by user-configured or EVE-assigned shared port
// labels and applies the ACE only to flows transmitted through them.
// For now the matches are bidirectional.
// XXX Add directionality? Different rate limits in different directions?
// Value is always a string.
// There is an implicit reject rule at the end.
// The "eidset" type is special for the overlay. Matches all the IPs which
// are part of the DNSNameToIPList.
type ACEMatch struct {
Type string
Value string
}
// ACEAction decides what to do with traffic matched by a given ACE.
type ACEAction struct {
Drop bool // Otherwise accept
Limit bool // Is limiter enabled?
LimitRate int // Packets per unit
LimitUnit string // "s", "m", "h", for second, minute, hour
LimitBurst int // Packets
PortMap bool // Is port mapping part of action?
TargetPort int // Internal port
}
// AppNetAdapterStatus : status of application network adapter.
type AppNetAdapterStatus struct {
AppNetAdapterConfig
VifInfo
BridgeMac net.HardwareAddr
BridgeIPAddr net.IP // The address for DNS/DHCP service in zedrouter
AssignedAddresses AssignedAddrs // IPv4 and IPv6 addresses assigned to domU
IPv4Assigned bool // Set to true once DHCP has assigned it to domU
IPAddrMisMatch bool
HostName string
}
// NetworkInstanceInfo : info about created Network instance.
type NetworkInstanceInfo struct {
BridgeNum int
BridgeName string
BridgeIPAddr net.IP
BridgeMac net.HardwareAddr
BridgeIfindex int
// Name of a (dummy) interface where ICMP, ARP, DNS and DHCP packets
// are mirrored from the bridge and can be used for monitoring purposes.
// Empty if mirroring is not available.
MirrorIfName string
// Collection of address assignments; from MAC address to IP address
IPAssignments map[string]AssignedAddrs
// Set of vifs on this bridge
Vifs []VifNameMac
// Maintain a map of all access vlan ids to their counts, used by apps
// connected to this network instance.
VlanMap map[uint32]uint32
// Counts the number of trunk ports attached to this network instance
NumTrunkPorts uint32
}
// AssignedAddrs : IP addresses assigned to application network adapter.
type AssignedAddrs struct {
IPv4Addrs []AssignedAddr
IPv6Addrs []AssignedAddr
}
// GetInternallyLeasedIPv4Addr returns IPv4 address leased by EVE using
// an internally run DHCP server.
func (aa AssignedAddrs) GetInternallyLeasedIPv4Addr() net.IP {
for _, addr := range aa.IPv4Addrs {
if addr.AssignedBy == AddressSourceInternalDHCP {
return addr.Address
}
}
return nil
}
// AddressSource determines the source of an IP address assigned to an app VIF.
// Values are power of two and therefore can be used with a bit mask.
type AddressSource uint8
const (
// AddressSourceUndefined : IP address source is not defined
AddressSourceUndefined AddressSource = 0
// AddressSourceEVEInternal : IP address is used only internally by EVE
// (i.e. inside dom0).
AddressSourceEVEInternal AddressSource = 1 << iota
// AddressSourceInternalDHCP : IP address is leased to an app by an internal DHCP server
// run by EVE.
AddressSourceInternalDHCP
// AddressSourceExternalDHCP : IP address is leased to an app by an external DHCP server.
AddressSourceExternalDHCP
// AddressSourceSLAAC : Stateless Address Autoconfiguration (SLAAC) was used by the client
// to generate a unique IPv6 address.
AddressSourceSLAAC
// AddressSourceStatic : IP address is assigned to an app statically
// (using e.g. cloud-init).
AddressSourceStatic
)
// AssignedAddr : IP address assigned to an application interface (on the guest side).
type AssignedAddr struct {
Address net.IP
AssignedBy AddressSource
}
// VifNameMac : name and MAC address assigned to app VIF.
type VifNameMac struct {
Name string
MacAddr net.HardwareAddr
AppID uuid.UUID
}
// IsVifInBridge checks if network instance already contains VIF with the given name.
func (instanceInfo *NetworkInstanceInfo) IsVifInBridge(
vifName string) bool {
for _, vif := range instanceInfo.Vifs {
if vif.Name == vifName {
return true
}
}
return false
}
// RemoveVif : remove VIF record from network instance info.
func (instanceInfo *NetworkInstanceInfo) RemoveVif(log *base.LogObject,
vifName string) {
log.Functionf("RemoveVif(%s, %s)", instanceInfo.BridgeName, vifName)
found := false
var vifs []VifNameMac
for _, vif := range instanceInfo.Vifs {
if vif.Name != vifName {
vifs = append(vifs, vif)
} else {
found = true
}
}
if !found {
log.Errorf("RemoveVif(%x, %x) not found",
instanceInfo.BridgeName, vifName)
}
instanceInfo.Vifs = vifs
}
// AddVif : add VIF record into network instance info.
func (instanceInfo *NetworkInstanceInfo) AddVif(log *base.LogObject,
vifName string, appMac net.HardwareAddr, appID uuid.UUID) {
log.Functionf("AddVif(%s, %s, %s, %s)",
instanceInfo.BridgeName, vifName, appMac, appID.String())
// XXX Should we just overwrite it? There is a lookup function
// anyways if the caller wants "check and add" semantics
if instanceInfo.IsVifInBridge(vifName) {
log.Errorf("AddVif(%s, %s) exists",
instanceInfo.BridgeName, vifName)
return
}
info := VifNameMac{
Name: vifName,
MacAddr: appMac,
AppID: appID,
}
instanceInfo.Vifs = append(instanceInfo.Vifs, info)
}
// NetworkInstanceMetrics : metrics for a given network instance.
type NetworkInstanceMetrics struct {
UUIDandVersion UUIDandVersion
DisplayName string
Type NetworkInstanceType
BridgeName string
NetworkMetrics NetworkMetrics
ProbeMetrics []ProbeMetrics
VlanMetrics VlanMetrics
}
// VlanMetrics : VLAN metrics for a given NI.
type VlanMetrics struct {
NumTrunkPorts uint32
VlanCounts map[uint32]uint32
}
// ProbeMetrics - metrics published for a NI multipath route with probing-based
// selection of the output port.
type ProbeMetrics struct {
// Address of the destination network for which probing is used to select
// the output port.
DstNetwork string
// Logical label of the currently selected output port for the route.
SelectedPort string
SelectedPortIfName string // interface name of the port that probing picked
RemoteEndpoints []string // remote IP/URL addresses used for probing
LocalPingIntvl uint32 // local ping interval in seconds
RemotePingIntvl uint32 // remote probing interval in seconds
PortCount uint32 // number of ports included in probing
IntfProbeStats []ProbeIntfMetrics // metrics for all ports included in probing
}
// ProbeIntfMetrics - probe metrics for a device port (reported for a given NI)
type ProbeIntfMetrics struct {
IntfName string // interface name of the probed device port
NexthopIPs []net.IP // interface local next-hop address(es) used for probing
NexthopUP bool // Is local next-hop in UP status
RemoteUP bool // Is remote endpoint in UP status
NexthopUPCnt uint32 // local ping UP count
NexthopDownCnt uint32 // local ping DOWN count
RemoteUPCnt uint32 // remote probe UP count
RemoteDownCnt uint32 // remote probe DOWN count
LatencyToRemote uint32 // probe latency to remote in msec
}
// Key :
func (metrics NetworkInstanceMetrics) Key() string {
return metrics.UUIDandVersion.UUID.String()
}
// LogCreate :
func (metrics NetworkInstanceMetrics) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.NetworkInstanceMetricsLogType, "",
metrics.UUIDandVersion.UUID, metrics.LogKey())
if logObject == nil {
return
}
logObject.Metricf("Network instance metrics create")
}
// LogModify :
func (metrics NetworkInstanceMetrics) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.NetworkInstanceMetricsLogType, "",
metrics.UUIDandVersion.UUID, metrics.LogKey())
oldMetrics, ok := old.(NetworkInstanceMetrics)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of NetworkInstanceMetrics type")
}
// XXX remove?
logObject.CloneAndAddField("diff", cmp.Diff(oldMetrics, metrics)).
Metricf("Network instance metrics modify")
}
// LogDelete :
func (metrics NetworkInstanceMetrics) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.NetworkInstanceMetricsLogType, "",
metrics.UUIDandVersion.UUID, metrics.LogKey())
logObject.Metricf("Network instance metrics delete")
base.DeleteLogObject(logBase, metrics.LogKey())
}
// LogKey :
func (metrics NetworkInstanceMetrics) LogKey() string {
return string(base.NetworkInstanceMetricsLogType) + "-" + metrics.Key()
}
// NetworkMetrics are for all adapters
// Matches networkMetrics protobuf message.
type NetworkMetrics struct {
MetricList []NetworkMetric
TotalRuleCount uint64
}
// Key is used for pubsub
func (nms NetworkMetrics) Key() string {
return "global"
}
// LogCreate :
func (nms NetworkMetrics) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.NetworkMetricsLogType, "",
nilUUID, nms.LogKey())
if logObject == nil {
return
}
logObject.Metricf("Network metrics create")
}
// LogModify :
func (nms NetworkMetrics) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.NetworkMetricsLogType, "",
nilUUID, nms.LogKey())
oldNms, ok := old.(NetworkMetrics)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of NetworkMetrics type")
}
// XXX remove?
logObject.CloneAndAddField("diff", cmp.Diff(oldNms, nms)).
Metricf("Network metrics modify")
}
// LogDelete :
func (nms NetworkMetrics) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.NetworkMetricsLogType, "",
nilUUID, nms.LogKey())
logObject.Metricf("Network metrics delete")
base.DeleteLogObject(logBase, nms.LogKey())
}
// LogKey :
func (nms NetworkMetrics) LogKey() string {
return string(base.NetworkMetricsLogType) + "-" + nms.Key()
}
// LookupNetworkMetrics : get metrics collected for a given interface.
func (nms *NetworkMetrics) LookupNetworkMetrics(ifName string) (NetworkMetric, bool) {
for _, metric := range nms.MetricList {
if ifName == metric.IfName {
return metric, true
}
}
return NetworkMetric{}, false
}
// NetworkMetric : metrics for a given network interface.
type NetworkMetric struct {
IfName string
TxBytes uint64
RxBytes uint64
TxDrops uint64
RxDrops uint64
TxPkts uint64
RxPkts uint64
TxErrors uint64
RxErrors uint64
TxAclDrops uint64 // For implicit deny/drop at end
RxAclDrops uint64 // For implicit deny/drop at end
TxAclRateLimitDrops uint64 // For all rate limited rules
RxAclRateLimitDrops uint64 // For all rate limited rules
}
// NetworkInstanceType : type of network instance.
type NetworkInstanceType int32
// These values should be same as the ones defined in zconfig.ZNetworkInstType
const (
NetworkInstanceTypeFirst NetworkInstanceType = 0
NetworkInstanceTypeSwitch NetworkInstanceType = 1
NetworkInstanceTypeLocal NetworkInstanceType = 2
NetworkInstanceTypeCloud NetworkInstanceType = 3
NetworkInstanceTypeHoneyPot NetworkInstanceType = 5
NetworkInstanceTypeTransparent NetworkInstanceType = 6
NetworkInstanceTypeLast NetworkInstanceType = 255
)
// AddressType : type of network address.
type AddressType int32
// The values here should be same as the ones defined in zconfig.AddressType
const (
AddressTypeNone AddressType = 0 // For switch networks
AddressTypeIPV4 AddressType = 1
AddressTypeIPV6 AddressType = 2
AddressTypeCryptoIPV4 AddressType = 3
AddressTypeCryptoIPV6 AddressType = 4
AddressTypeLast AddressType = 255
)
// NetworkInstanceConfig
//
// Config Object for NetworkInstance
// Extracted from the protobuf NetworkInstanceConfig
type NetworkInstanceConfig struct {
UUIDandVersion
DisplayName string
Type NetworkInstanceType
// Activate - Activate the config.
Activate bool
// PortLabel references port(s) from DevicePortConfig to use for external
// connectivity.
// Can be a specific logicallabel matching a single port, or a shared label,
// such as "uplink", potentially matching multiple device ports.
PortLabel string
// IP configuration for the Application
IpType AddressType
Subnet net.IPNet
Gateway net.IP
DomainName string
NtpServers []string
DnsServers []net.IP // If not set we use Gateway as DNS server
DhcpRange IPRange
DnsNameToIPList []DNSNameToIP // Used for DNS and ACL ipset
MTU uint16 // IP MTU
// Route configuration
PropagateConnRoutes bool
StaticRoutes []IPRouteConfig
// Enable flow logging for this network instance.
// If enabled, EVE periodically captures metadata about all application TCP and UDP
// flows, as well DNS queries.
// It is recommended to disable flow logging by default. This is because it may
// potentially produce a large amount of data, which is then uploaded to
// the controller. Another drawback of flow-logging is that the iptables rules
// that EVE installs for network instances are considerably more complicated
// because of this feature and thus introduce additional packet processing overhead.
EnableFlowlog bool
// Spanning Tree Protocol configuration.
// Only applied for Switch NI with multiple ports.
STPConfig STPConfig
// VLAN access ports configured for a switch network instance.
// For other types of network instances, this option is ignored.
// This setting applies to physical network ports attached to the network instance.
// VLAN configuration for application interfaces is applied separately via AppNetAdapterConfig
// (see AppNetAdapterConfig.AccessVlanID).
VlanAccessPorts []VlanAccessPort
// Any errors from the parser
// ErrorAndTime provides SetErrorNow() and ClearError()
ErrorAndTime
}
// IPRouteConfig : single IP route config entry.
type IPRouteConfig struct {
// Destination network.
// Guaranteed by zedagent not to be nil.
DstNetwork *net.IPNet
// Gateway IP address.
// Can be nil.
Gateway net.IP
// Output device port for the routed traffic.
// Either a single NI port referenced by its name (SystemAdapter.Name, aka logical label)
// or an adapter shared-label matching zero or more NI ports (multipath routing).
// Not used when gateway references one of the applications connected to the NI.
OutputPortLabel string
// Probe remote endpoint to determine connectivity status of each port and pick one
// with a working connectivity (and known gateway IP) for the route (preferring
// the currently used one if any).
// Provides automatic fail-over between ports.
// If OutputPortLabel is not defined or references only a single port (e.g. directly
// by the logical label), probing is skipped (nothing to fail-over to anyway).
PortProbe NIPortProbe
// When EVE is deciding which port to use for multipath route and multiple ports have
// working connectivity (or probing is disabled), port can be selected based on the cost
// If this option is enabled, EVE will prefer ports with lower costs.
PreferLowerCost bool
// When EVE is deciding which port to use for multipath route and there are multiple
// candidates among cellular modems, it might make sense to consider the current
// cellular network signal strength. If this option is enabled, EVE will prefer
// cellular ports with better signal (only among cellular ports).
PreferStrongerWwanSignal bool
}
// STPConfig : Spanning Tree Protocol configuration.
// Only applied for Switch NI with multiple ports.
type STPConfig struct {
// Either a single NI port referenced by its name (SystemAdapter.Name, aka logical label)
// or an adapter shared-label matching zero or more NI ports.
PortsWithBpduGuard string
}
// VlanAccessPort : config applied to physical port(s) attached to a Switch NI.
type VlanAccessPort struct {
VlanID uint16
// Either a single NI port referenced by its name (SystemAdapter.Name, aka logical label)
// or an adapter shared-label matching zero or more NI ports.
PortLabel string
}
// ConnectivityProbeMethod - method to use to determine the connectivity status of a port.
type ConnectivityProbeMethod uint8
const (
// ConnectivityProbeMethodNone : connectivity probing is disabled.
ConnectivityProbeMethodNone ConnectivityProbeMethod = iota
// ConnectivityProbeMethodICMP : use ICMP ping against the probed endpoint to determine
// the connectivity status.
ConnectivityProbeMethodICMP
// ConnectivityProbeMethodTCP : try to establish TCP connection with the probed endpoint
// to determine the connectivity status.
ConnectivityProbeMethodTCP
)
// ConnectivityProbe : configuration for user-defined connectivity-testing probe.
type ConnectivityProbe struct {
// Method to use to determine the connectivity status.
Method ConnectivityProbeMethod
// ProbeHost is either IP or hostname.
ProbeHost string
// ProbePort is required for L4 probing methods (e.g. ConnectivityProbeMethodTCP).
ProbePort uint16
}
// String returns human-readable description of the probe.
func (r ConnectivityProbe) String() string {
probeStr := "<none>"
switch r.Method {
case ConnectivityProbeMethodICMP:
probeStr = fmt.Sprintf("icmp://%s", r.ProbeHost)
case ConnectivityProbeMethodTCP:
probeStr = fmt.Sprintf("tcp://%s:%d", r.ProbeHost,
r.ProbePort)
}
return probeStr
}
// NIPortProbe is used to determine connectivity status of a port to decide if it is suitable
// for the default route of a network instance.
type NIPortProbe struct {
// EVE uses ICMP ping against the port's gateway IP to determine connectivity status.
// User can disable this probe method. This is typically needed when the gateway router
// is configured to drop/ignore ICMP pings and therefore this probe would return false
// negatives.
EnabledGwPing bool
// Ports exceeding this cost will have the gateway probing disabled to reduce
// traffic generated by probing (only less-frequent user-defined probe will be performed).
GwPingMaxCost uint8
// User-defined method to use to determine the port connectivity status.
// Zedrouter runs this additionally to gateway pings (unless EnabledGwPing is false).
UserDefinedProbe ConnectivityProbe
}
// String returns human-readable description of the route.
// Format does not matter, we use curly brackets just for the readability sake.
func (r IPRouteConfig) String() string {
probe := fmt.Sprintf("{enabledGwPing=%t, gwPingMaxCost=%d, userProbe=%s}",
r.PortProbe.EnabledGwPing, r.PortProbe.GwPingMaxCost,
r.PortProbe.UserDefinedProbe.String())
return fmt.Sprintf("IP Route: {dst=%s, gw=%s, port=%s, probe=%s, "+
"preferLowerCost=%t, preferStrongerWwanSignal=%t}",
r.DstNetwork.String(), r.Gateway.String(), r.OutputPortLabel, probe,
r.PreferLowerCost, r.PreferStrongerWwanSignal)
}
// IsDefaultRoute returns true if this is a default route, i.e. matches all destinations.
func (r IPRouteConfig) IsDefaultRoute() bool {
if r.DstNetwork == nil {
return true
}
ones, _ := r.DstNetwork.Mask.Size()
return r.DstNetwork.IP.IsUnspecified() && ones == 0
}
// Equal compares two IP routes for equality.
func (r IPRouteConfig) Equal(r2 IPRouteConfig) bool {
return netutils.EqualIPs(r.Gateway, r2.Gateway) &&
netutils.EqualIPNets(r.DstNetwork, r2.DstNetwork) &&
r.OutputPortLabel == r2.OutputPortLabel &&
r.PortProbe == r2.PortProbe &&
r.PreferLowerCost == r2.PreferLowerCost &&
r.PreferStrongerWwanSignal && r2.PreferStrongerWwanSignal
}
// Key :
func (config *NetworkInstanceConfig) Key() string {
return config.UUID.String()
}
// LogCreate :
func (config NetworkInstanceConfig) LogCreate(logBase *base.LogObject) {
logObject := base.NewLogObject(logBase, base.NetworkInstanceConfigLogType, "",
config.UUIDandVersion.UUID, config.LogKey())
if logObject == nil {
return
}
logObject.Noticef("Network instance config create")
}
// LogModify :
func (config NetworkInstanceConfig) LogModify(logBase *base.LogObject, old interface{}) {
logObject := base.EnsureLogObject(logBase, base.NetworkInstanceConfigLogType, "",
config.UUIDandVersion.UUID, config.LogKey())
oldConfig, ok := old.(NetworkInstanceConfig)
if !ok {
logObject.Clone().Fatalf("LogModify: Old object interface passed is not of NetworkInstanceConfig type")
}
// XXX remove?
logObject.CloneAndAddField("diff", cmp.Diff(oldConfig, config)).
Noticef("Network instance config modify")
}
// LogDelete :
func (config NetworkInstanceConfig) LogDelete(logBase *base.LogObject) {
logObject := base.EnsureLogObject(logBase, base.NetworkInstanceConfigLogType, "",
config.UUIDandVersion.UUID, config.LogKey())
logObject.Noticef("Network instance config delete")
base.DeleteLogObject(logBase, config.LogKey())
}
// LogKey :
func (config NetworkInstanceConfig) LogKey() string {
return string(base.NetworkInstanceConfigLogType) + "-" + config.Key()
}
// IsIPv6 returns true if the address is IP version 6.
func (config *NetworkInstanceConfig) IsIPv6() bool {
switch config.IpType {
case AddressTypeIPV6:
return true
case AddressTypeCryptoIPV6:
return true
}
return false
}
type ChangeInProgressType int32
const (
ChangeInProgressTypeNone ChangeInProgressType = 0
ChangeInProgressTypeCreate ChangeInProgressType = 1
ChangeInProgressTypeModify ChangeInProgressType = 2
ChangeInProgressTypeDelete ChangeInProgressType = 3
ChangeInProgressTypeLast ChangeInProgressType = 255
)
// NetworkInstanceStatus
//
// Config Object for NetworkInstance
// Extracted from the protobuf NetworkInstanceConfig
type NetworkInstanceStatus struct {
NetworkInstanceConfig
NetworkInstanceInfo
// Error set when NI has invalid config.
ValidationErr ErrorAndTime
// Errors set when there are not enough resources to create the NI.
AllocationErr ErrorAndTime
// Make sure the Activate from the config isn't exposed as a boolean
Activate uint64
// Activated is true if the network instance has been created in the network stack.
Activated bool
// ChangeInProgress is used to make sure that other microservices do not read
// NI status until the latest Create/Modify/Delete operation completes.
ChangeInProgress ChangeInProgressType
// Error set when NI IP subnet overlaps with the subnet of one the device ports
// or another NI.
IPConflictErr ErrorAndTime
// MTU configured for the network instance and app interfaces connected to it.
// This can differ from the user-requested MTU in case it is invalid or conflicts
// with the device port MTU.
MTU uint16
// Error set when the MTU configured for NI is in conflict with the MTU configured
// for the associated port (e.g. NI MTU is higher than MTU of an associated device
// port).
MTUConflictErr ErrorAndTime
// Labels of device ports used for external connectivity.
// The list is empty for air-gapped network instances.
Ports []string
// List of NTP servers published to applications connected to this network instance.
// This includes the NTP server from the NI config (if any) and all NTP servers
// associated with ports used by the network instance for external connectivity.
NTPServers []string
// The intended state of the routing table.
// Includes user-configured static routes and potentially also automatically
// generated default route.
IntendedRoutes []IPRouteStatus
// The actual state of the routing table.
// This includes connected routes (for ports, not bridge), DHCP-received routes,
// user-defined static routes (NetworkInstanceConfig.static_routes) and the default
// route (if any). Note that some user-defined static routes might not be applied
// (and thus not reported here) if they do not match IP config of currently used
// device ports.
// Additionally, static routes with shared port labels (matching multiple ports)
// are reported here each with the logical label of the (single) port, currently
// selected for the route (selected based on connectivity status, port costs, wwan
// signal strength, etc.).
CurrentRoutes []IPRouteInfo
// Error set when NI fails to use the configured device port(s) for whatever reason.