-
Notifications
You must be signed in to change notification settings - Fork 1
/
elsa_pa.lua
1620 lines (1399 loc) · 46.6 KB
/
elsa_pa.lua
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
#!/usr/bin/env lua
-- -*-lua-*-
--
-- $Id: elsa_pa.lua $
--
-- Author: Markus Stenberg <[email protected]>
--
-- Copyright (c) 2012 cisco Systems, Inc.
--
-- Created: Wed Oct 3 11:47:19 2012 mstenber
-- Last modified: Tue Nov 5 09:40:56 2013 mstenber
-- Edit time: 1145 min
--
-- the main logic around with prefix assignment within e.g. BIRD works
--
-- elsa_pa is given skv instance, elsa instance, and should roll on
-- it's way.
--
-- the main difference is that this code assumes that there are LSAs;
-- pa code just deals with rid, asp, usp, if abstractions
-- #define LSA_T_AC 0xBFF0 /* Auto-Configuration LSA */
-- /* function code 8176(0x1FF0): experimental, U-bit=1, Area Scope */
-- TODO - document the API between elsa (wrapper), elsa_pa
-- => lsa_changed(lsa)
-- => lsa_deleting(lsa)
-- <= ???
require 'mst'
require 'mst_skiplist'
require 'ospf_codec'
require 'ssloop'
require 'dns_db' -- for name2ll
local pa = require 'pa'
module(..., package.seeall)
-- LSA type used for storing the auto-configuration LSA
-- Benjamin
--AC_TYPE=0xBFF0
-- 0xAC0F Jari
AC_TYPE=0xAC0F
FORCE_SKV_AC_CHECK_INTERVAL=60
-- New scheme for encoding the received PD/6RD/DHCPv4 in the SKV is as
-- follows:
-- <source>.<ifname> = { {key1=value1, key2=value2}, {key3=value3, key4=value4, ..} }
PD_SKVPREFIX='pd.'
DHCPV4_SKVPREFIX='dhcp.'
TUNNEL_SKVPREFIX='tunnel.'
SKVPREFIXES={PD_SKVPREFIX, DHCPV4_SKVPREFIX, TUNNEL_SKVPREFIX}
-- these keys are used within the objects to describe found information
PREFIX_KEY='prefix'
DNS_KEY='dns'
DNS_SEARCH_KEY='dns_search'
NH_KEY='nh'
IFLIST_KEY='iflist' -- allow overriding of active interfaces for source type
-- extra info fields not used directly, but used in e.g. pm handlers
PREFIX_CLASS_KEY='pclass'
PREFERRED_KEY='pref' -- both of these are absolute timestamps
VALID_KEY='valid'
-- list of keys which are passed verbatim from
-- IF-specific prefix SKV [=> JSON_USP_INFO_KEY] => LAP/USP SKV lists
PREFIX_INFO_SKV_KEYS={PREFIX_CLASS_KEY}
-- locally as-is passed fields
PREFIX_INFO_LOCAL_SKV_KEYS={PREFERRED_KEY, VALID_KEY}
-- used to indicate that interface shouldn't be assigned to (nor used
-- in general - this includes starting any daemon on it)
DISABLE_SKVPREFIX='disable.'
-- used to indicate that no IPv4 prefix assignment on the interface
DISABLE_V4_SKVPREFIX='disable-pa-v4.'
-- SKV 'singleton' keys
OSPF_RID_KEY='ospf-rid' -- OSPF router ID
OSPF_RNAME_KEY='ospf-rname' -- (home-wide unique) router name
OSPF_LAP_KEY='ospf-lap' -- PA alg locally assigned prefixes (local)
OSPF_USP_KEY='ospf-usp' -- usable prefixes from PA alg (across whole home)
OSPF_IFLIST_KEY='ospf-iflist' -- active set of interfaces
-- IPv6 DNS
OSPF_DNS_KEY='ospf-dns'
OSPF_DNS_SEARCH_KEY='ospf-dns-search'
-- IPv4 DNS
OSPF_IPV4_DNS_KEY='ospf-v4-dns'
OSPF_IPV4_DNS_SEARCH_KEY='ospf-v4-dns-search'
-- allow for configuration of prefix assignment algorithm
-- via skv too
PA_CONFIG_SKV_KEY='pa-config'
-- JSON fields within jsonblob AC TLV
JSON_ASA_KEY='asa' -- assigned IPv4 address
JSON_DNS_KEY='dns'
JSON_DNS_SEARCH_KEY='dns_search'
JSON_IPV4_DNS_KEY='ipv4_dns'
JSON_IPV4_DNS_SEARCH_KEY='ipv4_dns_search'
-- extra USP information
JSON_USP_INFO_KEY='usp_info'
-- Hybrid proxy specific things
-- Zones consist of:
-- name=<name>, ip=<ip>[, browse=<something][, search=<something>]
-- note that name is UTF-8 string ('foo.bar.com'). this could be done
-- with label lists if we cared enough..
-- ip is where the responsible name server can be reached within (or
-- without) home
-- browse being set indicates that the zone is ~local, and it should
-- be added to the DNS-SD browse path
-- search being set indicates that the zone is ~remote, and it should
-- be added to the DHCP{v4,v6} and RA search list
-- these two are set by user (and come through elsa_pa to hybrid
-- proxy)
STATIC_HP_DOMAIN_KEY='static-domain' -- <name>
STATIC_HP_ZONES_KEY='static-zones' -- manually added extra remote zones
-- this is provided by hybrid proxy _to_ OSPF
HP_MDNS_ZONES_KEY='hp-mdns-zones' -- local autodiscovered mdns zones
-- (populated by hp_ospf)
-- and this to PM for local DHCP/RA server usage
HP_SEARCH_LIST_KEY='hp-search' -- to be published via DHCP*/RA
-- these are provided by OSPF _to_ hybrid proxy
OSPF_HP_DOMAIN_KEY='ospf-hp-domain' -- <name>
OSPF_HP_ZONES_KEY='ospf-hp-zones' -- non-local hybrid proxy zones
-- from the draft; time from boot to wait iff no other routers around
-- before starting new assignments
NEW_PREFIX_ASSIGNMENT=20
-- from the draft; time from boot to wait iff no other routers around
-- before generating ULA
NEW_ULA_PREFIX=20
-- =~ TERMINATE_PREFIX_ASSIGNMENT in the draft
LAP_DEPRACATE_TIMEOUT=240
-- not in the draft; the amount we keep deprecated prefixes around (to
-- e.g. advertise via radvd with zero prefix lifetime, and to reuse
-- first if need be)
LAP_EXPIRE_TIMEOUT=300
ORIGINATE_MIN_INTERVAL=4 -- up to this point, we hold on spamming
ORIGINATE_MAX_INTERVAL=300 -- even without changes
-- TODO - TERMINATE_ULA_PREFIX timeout is a 'SHOULD', but we ignore it
-- for simplicity's sake; getting rid of floating prefixes ASAP is
-- probably good thing (and the individual interface-assigned prefixes
-- will be depracated => will disappear soon anyway)
-- what do we mirror ~directly to OSPF?
skv_to_ospf_set = mst.array_to_table{
STATIC_HP_DOMAIN_KEY,
STATIC_HP_ZONES_KEY,
HP_SEARCH_LIST_KEY,
}
-- elsa specific lap subclass
elsa_lap = pa.lap:new_subclass{class='elsa_lap',
}
local json_sources={
[JSON_DNS_SEARCH_KEY]={prefix=PD_SKVPREFIX,
key=DNS_SEARCH_KEY,
ospf=OSPF_DNS_SEARCH_KEY},
[JSON_IPV4_DNS_SEARCH_KEY]={prefix=DHCPV4_SKVPREFIX,
key=DNS_SEARCH_KEY,
ospf=OSPF_IPV4_DNS_SEARCH_KEY},
}
function elsa_lap:start_depracate_timeout()
self:d('start_depracate_timeout')
self:a(not self.timeout)
self.timeout = self.pa.time() + LAP_DEPRACATE_TIMEOUT
self.pa.timeouts:insert(self)
end
function elsa_lap:stop_depracate_timeout()
self:d('stop_depracate_timeout')
self:a(self.timeout)
self.pa.timeouts:remove_if_present(self)
self.timeout = nil
end
function elsa_lap:start_expire_timeout()
self:d('start_expire_timeout')
self:a(not self.timeout)
self.timeout = self.pa.time() + LAP_EXPIRE_TIMEOUT
self.pa.timeouts:insert(self)
end
function elsa_lap:stop_expire_timeout()
self:d('stop_expire_timeout')
self:a(self.timeout)
self.pa.timeouts:remove_if_present(self)
self.timeout = nil
end
-- actual elsa_pa itself, which controls pa (and interfaces with
-- skv/elsa-wrapper
elsa_pa = mst.create_class{class='elsa_pa',
mandatory={'skv', 'elsa', 'if_table'},
time=ssloop.time,
originate_min_interval=ORIGINATE_MIN_INTERVAL,
}
function elsa_pa:init()
-- set of _all_ interface names we've _ever_ seen (used for
-- checking SKV for tidbits). initialized only here so that it
-- won't be screwed if pa reconfigure is called.
self.all_seen_if_names = mst.set:new{}
self.f = function (k, v) self:kv_changed(k, v) end
self.skv:add_change_observer(self.f)
-- overridable fields either using arguments to this class,
-- or using the 'o' dict (priority-wise, o > class > defaults)
local args = {new_prefix_assignment=NEW_PREFIX_ASSIGNMENT,
new_ula_prefix=NEW_ULA_PREFIX,
}
for i, v in ipairs(pa.CONFIGS)
do
args[v] = false
end
-- check if class has updates on any of the keys..
for k, v in pairs(args)
do
local v2 = self[k]
if v2
then
args[k] = v2
end
end
self.pa_args = args
-- this should not be done before we actually have pa_config from skv
-- however, someone may have supplied us pa_config as argument
self.pa_config = self.pa_config or self.skv:get(PA_CONFIG_SKV_KEY)
self:reconfigure_pa()
end
function elsa_pa:reconfigure_pa(v)
self:d('reconfigure_pa')
v = v or self.pa_config
self.pa_config = v
self:init_own()
if not v
then
self:d(' skipped, no config yet')
return
end
self:init_pa()
end
function elsa_pa:init_own()
-- set various things to their default values
self.ac_changes = 0
self.lsa_changes = 0
self.skv_changes = 0
-- when did we consider originate/publish last
self.last_publish = 0
-- when did we last actually originate AC LSA
self.last_originate = 0
-- and what did it contain?
self.last_body = ''
end
local function timeout_is_less(o1, o2)
return o1.timeout < o2.timeout
end
function elsa_pa:init_pa()
local args = mst.table_copy(self.pa_args)
-- update with whatever we have in pa_config
mst.table_copy(self.pa_config, args)
-- these are always hardcoded - nobody should be able to change them
args.rid=self.rid
args.client = self
args.lap_class = elsa_lap
args.time = self.time
-- create the actual abstract prefix algorithm object we wrap
-- (create shallow copy of args, so that we don't wind up re-using
-- the object)
self.pa = pa.pa:new(mst.table_copy(args))
self.pa.timeouts = mst_skiplist.ipi_skiplist:new{p=2,
lt=timeout_is_less}
end
function elsa_pa:uninit()
self.skv:remove_change_observer(self.f)
if self.pa
then
-- we don't 'own' skv or 'elsa', so we don't do anything here,
-- except clean up our own state, which is basically the pa object
self.pa:done()
end
end
function elsa_pa:kv_changed(k, v)
-- handle configuration changes explicitly here
if k == PA_CONFIG_SKV_KEY
then
self:reconfigure_pa(v)
return
end
if skv_to_ospf_set[k]
then
self.skv_changes = self.skv_changes + 1
end
-- implicitly externally sourced information to the
-- all_seen_if_names (someone plays with stuff that starts with
-- one of the skvprefixes -> stuff happens)
for i, p in ipairs(SKVPREFIXES)
do
local r = mst.string_startswith(k, p)
if r
then
if r ~= IFLIST_KEY
then
self:add_seen_if(r)
end
break
end
end
-- TODO - determine which cases should actually do this?
-- invalidate caches that have if info
self.skvp = nil
self.ext_set = nil
end
function elsa_pa:lsa_changed(lsa)
local lsatype = lsa.type
if lsa.rid == self.rid
then
-- ignore us, if BIRD calls us about it.. we don't
-- 'see' our own changes
return
end
self.ac_tlv_cache = nil
if lsatype == AC_TYPE
then
self:d('ac lsa changed at', lsa.rid)
self.ac_changes = self.ac_changes + 1
else
self:d('other lsa changed at', lsa.rid, lsatype)
self.lsa_changes = self.lsa_changes + 1
end
end
function elsa_pa:lsa_deleting(lsa)
-- for the time being, we don't note a difference between the two
self:lsa_changed(lsa)
end
function elsa_pa:ospf_changed()
-- emulate to get the old behavior.. shouldn't be called!
self:d('deprecated ospf_changed called')
self:lsa_changed{type=AC_TYPE}
self:lsa_changed{type=(AC_TYPE-1)}
end
function elsa_pa:repr_data()
return '-'
end
function elsa_pa:get_rname_base()
local n = mst.read_filename_to_string('/proc/sys/kernel/hostname') or 'r'
n = mst.string_strip(n)
self:d('get_rname_base', n)
return n
end
function elsa_pa:get_hwaddr(rid, ifname)
local ifo = self.if_table:get_if(ifname)
if ifo
then
return ifo:get_hwaddr()
end
end
function elsa_pa:get_hwf(rid)
rid = rid or self.rid
local hwf = self.elsa:get_hwf(rid)
mst.a(hwf)
return hwf
end
function elsa_pa:get_padded_hwf(rid)
local hwf = self:get_hwf(rid)
mst.a(hwf, 'unable to get hwf')
local d = ospf_codec.MINIMUM_AC_TLV_RHF_LENGTH
if #hwf < d
then
hwf = hwf .. string.rep('1', d - #hwf)
end
mst.a(#hwf >= d)
return hwf
end
function elsa_pa:check_conflict(bonus_lsa)
local my_hwf = self:get_padded_hwf()
local other_hwf = nil
local lsas = 0
local tlvs = 0
local function consider_lsa(lsa)
lsas = lsas + 1
if lsa.rid ~= self.rid then return end
local found = nil
for i, tlv in ipairs(ospf_codec.decode_ac_tlvs(lsa.body))
do
tlvs = tlvs + 1
if tlv.type == ospf_codec.AC_TLV_RHF
then
found = tlv.body
end
end
if found and found ~= my_hwf
then
other_hwf = found
end
end
if bonus_lsa then consider_lsa(bonus_lsa) end
self:iterate_ac_lsa(consider_lsa)
self:d('check_conflict considered', lsas, tlvs)
if not other_hwf then return end
self:d('found conflict', my_hwf, other_hwf)
-- we have conflict; depending on what the hwf looks like,
-- we either have to change our rid.. or not.
-- if our hwf is greater, we don't need to change, but the other does
if my_hwf > other_hwf
then
self:d('we have precedence, wait for other to renumber')
return
end
self:d('trying to change local rid, as we lack precedence')
-- uh oh, our hwf < other hwf -> have to change
self.elsa:change_rid(self.rid)
self.ac_changes = 0
self.lsa_changes = 0
self.had_conflict = true
return true
end
-- API to check from outside if run() should be called yet (strict
-- test runner won't call it unless it has to; however, in case of
-- elsa stuff, we typically call it in tick() functions or so so this
-- is mostly useful for unit testing)
function elsa_pa:should_run()
-- no pa? no point
if not self.pa
then
return
end
local lap = self.pa.timeouts:get_first()
if lap and lap.timeout <= self.time()
then
self:d('should run due to lap.timeout')
return true
end
if self:should_run_pa()
then
return true
end
return self:should_publish{ac_changes=self.ac_changes,
lsa_changes=self.lsa_changes}
end
function elsa_pa:should_run_pa()
-- first local reasons we know of
if self.ac_changes > 0
then
self:d('should_run_pa, ac_changes > 0')
return true
end
-- skvp change indicates we should also run PA
local _, skvp_repr = self:get_skvp()
if skvp_repr ~= self.pa_skvp_repr
then
self:d('should_run_pa, skvp changed')
return true
end
-- then the pa itself (second argument is checked_should)
if self.pa:should_run()
then
return true, true
end
end
function elsa_pa:next_time()
if not self.pa
then
return
end
if self:should_run()
then
return 0
end
-- there are two cases:
-- - either delayed publish (self.last_publish == 0)
-- or
-- - timeout
local lap = self.pa.timeouts:get_first()
local nt
if lap then nt = lap.timeout end
if self.last_publish == 0
then
local next = self.last_originate + self.originate_min_interval
if not nt or nt > next
then
nt = next
end
end
return nt
end
function elsa_pa:should_publish(d)
local r
-- if pa.run() said there's changes, yep, we should
if d.r
then
self:d('should publish due to d.r')
r = r or {}
end
if self.ridr_repr ~= self.publish_ridr_repr
then
r = r or {}
r.publish_ridr_repr = self.ridr_repr
self:d('should publish, ridr_repr changed')
end
local _, skvp_repr = self:get_skvp()
if skvp_repr ~= self.publish_skvp_repr
then
r = r or {}
r.publish_skvp_repr = skvp_repr
self:d('should publish, skvp_repr changed')
end
-- if ac LSA changed we should
if d.ac_changes and d.ac_changes > 0
then
self:d('should publish state due to ac_changes > 0')
r = r or {}
end
-- also if non-ac LSA changes, we should
if d.lsa_changes and d.lsa_changes > 0
then
self:d('should publish state due to lsa_changes > 0')
r = r or {}
end
if self.skv_changes > 0
then
self:d('should publish state due to skv_changes > 0')
r = r or {}
r.skv_changes = 0
end
-- finally, if the FORCE_SKV_AC_CHECK_INTERVAL was passed, we do
-- this (but this is paranoia, shouldn't be necessary)
if (self.time() - self.last_publish) > FORCE_SKV_AC_CHECK_INTERVAL
then
self:d(' should publish state due to FORCE_SKV_AC_CHECK_INTERVAL exceeded', self.time(), self.last_publish)
r = r or {}
end
if r
then
local now = self.time()
local delta = now - self.last_originate
-- don't spam, but ensure we publish as soon as interval is done
-- by setting the last_publish to 0
if delta < self.originate_min_interval
then
if self.last_publish and self.last_publish > 0
then
self:d(' .. but avoidin publish due to spam limitations')
self.last_publish = 0
end
r = nil
end
end
return r
end
function elsa_pa:run(calcrt)
self:d('run starting')
-- clear the route cache if calcrt > 0
self:a(not calcrt or type(calcrt) == 'number', 'weird calcrt', calcrt)
if calcrt and calcrt > 0
then
self:d('clearing rid2ro due to calcrt', calcrt)
self.rid2ro = nil
self.ac_changes = self.ac_changes + 1
end
-- without pa, there is no point
if not self.pa
then
return
end
local now = self.time()
while true
do
local lap = self.pa.timeouts:get_first()
if not lap or lap.timeout > now then break end
-- run the timeout (should remove itself, hopefully?)
lap.sm:Timeout()
end
-- let's check first that there is no conflict; that is,
-- nobody else with different hw fingerprint, but same rid
--
-- if someone like that exists, either we (or they) have to change
-- their router id..
if self.ac_changes == 0
then
if self.had_conflict
then
self:d('had conflict, no changes => still have conflict')
return
end
else
if self:check_conflict() then return end
end
local run_pa, checked_should = self:should_run_pa()
local ac_changes = self.ac_changes
local lsa_changes = self.lsa_changes
self.ac_changes = 0
self.lsa_changes = 0
-- our rid may have changed -> change that of the pa too, just in case
self.pa.rid = self.rid
-- consider if either ospf change occured (we got callback), pa
-- itself is in turbulent state, or the if state changed
local r
if run_pa
then
r = self.pa:run{checked_should=checked_should}
self:d('pa.run result', r)
self.ridr_repr = mst.repr(self.pa.ridr)
local _, skvp_repr = self:get_skvp()
self.pa_skvp_repr = skvp_repr
end
local now = self.time()
local sp = self:should_publish{r=r, ac_changes=ac_changes, lsa_changes=lsa_changes}
if sp
then
self.last_publish = self.time()
self:d('run doing skv/lsa update', r)
-- store the changed local state
for k, v in pairs(sp)
do
self[k] = v
end
self:run_handle_new_lsa()
-- store the domain; by default, static one, but also one from OSPF
-- if we don't have static
local domain = self.skv:get(STATIC_HP_DOMAIN_KEY)
local domainrid
if not domain
then
self:iterate_ac_lsa_tlv(function (o, lsa)
self:d('dn', o)
if not domain or domainrid < lsa.rid
then
domain = o.domain
domainrid = lsa.rid
end
end, {type=ospf_codec.AC_TLV_DN})
self:d('remote domain', domain)
else
self:d('found local domain', domain)
end
self.hp_domain = domain
self:run_handle_skv_publish()
end
self:d('run done')
end
function elsa_pa:run_handle_new_lsa()
-- originate LSA (or try to, there's duplicate prevention, or should be)
local body = self:generate_ac_lsa(false)
mst.a(body and #body, 'empty generated LSA?!?')
local now = self.time()
-- send duplicate if and only if we haven't sent anything in a long
-- while
if body == self.last_body
then
local delta = now - self.last_originate
if delta < ORIGINATE_MAX_INTERVAL
then
return
end
end
-- store the old 'reference' body for further use
-- (the new body is generated with relative timestamps, and is _always_
-- different, so not worth storing..)
self.last_body = body
local body = self:generate_ac_lsa(true)
self:d('originating ac lsa for real')
self.last_originate = now
self.elsa:originate_lsa{type=AC_TYPE,
rid=self.rid,
body=body}
end
local function non_empty(x)
if not x then return end
local t = type(x)
if t == 'number' then return x end
mst.a(t == 'string', 'non-string', t, x)
if #x == 0 then return end
return x
end
function relative_to_absolute(v, o_lsa, now)
mst.a(now, 'no now')
if not v then return end
v = v + now - (o_lsa and o_lsa.age or 0)
return math.floor(v)
end
function absolute_to_relative(v, now)
mst.a(now, 'no now')
if not v then return end
v = v - now
return math.floor(v)
end
-- gather local and remote prefix information
-- local == what's in skvprefix - prefix string -> object mapping
-- remote == what's in JSON_USP_INFO_KEY jsonblobs of LSAs
function elsa_pa:gather_prefix_info()
local i1 = {}
local i2 = {}
self:iterate_skv_prefix(function (p)
if p.prefix
then
i1[p.prefix] = p
end
end)
self:iterate_ac_lsa_tlv(function (json, lsa)
local t = json.table
local h = t[JSON_USP_INFO_KEY]
if not h then return end
for p, v in pairs(h)
do
i2[p] = v
end
end,
{type=ospf_codec.AC_TLV_JSONBLOB})
return {i1, i2}
end
function elsa_pa:copy_prefix_info_to_o(pi, prefix, dst)
self:a(type(prefix) == 'string', 'non-string prefix', prefix)
self:d('copy_prefix_info_to_o', prefix)
-- given ascii USP prefix p, we have to find the 'extra'
-- information about it, and dump it to object o
-- two options:
-- - local skv prefix
-- - 'some' jsonblob AC TLV with the information we want
local o
local o_lsa
local v = pi[1][prefix]
if v
then
o = v
else
local v = pi[2][prefix]
if v
then
o = v
o_lsa = v
end
end
if not o then return end
for _, key in ipairs(PREFIX_INFO_SKV_KEYS)
do
dst[key] = o[key]
end
if not o_lsa
then
-- this is local information, copy it verbatim
for _, key in ipairs(PREFIX_INFO_LOCAL_SKV_KEYS)
do
dst[key] = o[key]
end
else
-- we have an LSA => it's remote one. for the time being, we
-- mainly deal with timestamps, which should be _relative_ in
-- OSPF, but _locally_ they're absolute. convert them at this
-- point in time.
local now = self.time()
dst[PREFERRED_KEY] = relative_to_absolute(o[PREFERRED_KEY], o_lsa, now)
dst[VALID_KEY] = relative_to_absolute(o[VALID_KEY], o_lsa, now)
end
end
function elsa_pa:find_usp_for_ascii_prefix(p, iid)
local asp = self.pa:get_asp(p, iid, self.rid)
if asp and asp.usp
then
return asp.usp
end
-- failure.. look at all usp's instead, and see which one this
-- prefix belongs to (this is brute-force, but oh well)
local o
p = ipv6s.new_prefix_from_ascii(p)
self.pa.usp:foreach(function (rid, usp)
self:a(usp.prefix, 'no prefix?', usp)
if usp.prefix:contains(p)
then
o = usp
end
end)
return o
end
function elsa_pa:run_handle_skv_publish()
-- store the rid to SKV too
self.skv:set(OSPF_RID_KEY, self.rid)
-- store own router name
self.skv:set(OSPF_RNAME_KEY, self.pa.rname)
-- store the hp domain (if any)
self.skv:set(OSPF_HP_DOMAIN_KEY, self.hp_domain)
-- set up the locally assigned prefix field
local t = mst.array:new()
local dumped_if_ipv4 = {}
local pi = self:gather_prefix_info()
for i, lap in ipairs(self.pa.lap:values())
do
local iid = lap.iid
local ifo = self.pa.ifs[iid]
if not ifo
then
self:d('zombie interface', lap)
ifo = {}
end
if lap.ipv4 and lap.address
then
self:a(not dumped_if_ipv4[lap.ifname],
'system state somehow screwed up [>1 v4 address per if] ',
self.pa.usp, self.pa.asp, self.pa.lap)
dumped_if_ipv4[lap.ifname] = true
end
local p = lap.ascii_prefix
local o = {ifname=lap.ifname,
prefix=p,
iid=iid,
depracate=lap.depracated and 1 or nil,
owner=lap.owner,
address=lap.address and mst.string_split(lap.address:get_ascii(), '/')[1] or nil,
external=ifo.external,
}
local usp = self:find_usp_for_ascii_prefix(p, iid)
if usp
then
local p2 = usp.ascii_prefix
self:a(p2, 'no ascii_prefix in usp')
self:copy_prefix_info_to_o(pi, p2, o)
else
self:d('no usp?', lap)
end
t:insert(o)
end
self.skv:set(OSPF_LAP_KEY, t)
-- set up the interface list
local t = mst.array:new{}
for iid, ifo in pairs(self.pa.ifs)
do
-- if it's disabled interface, don't let pm know about it either
if not ifo.disable
then
t:insert(ifo.name)
end
end
self.skv:set(OSPF_IFLIST_KEY, t)
-- handle assorted 'gather info across the net' fields
for jsonkey, o in pairs(json_sources)
do
local l = self:get_local_field_array(o.prefix, o.key)
self.skv:set(o.ospf, self:get_field_array(jsonkey, l))
end
-- handle name servers
-- V4+V6
local l4, l6
l6 = self:get_local_field_array(PD_SKVPREFIX, DNS_KEY, l6)
l6 = self:get_local_field_array(TUNNEL_SKVPREFIX, DNS_KEY, l6)
l4 = self:get_local_field_array(DHCPV4_SKVPREFIX, DNS_KEY)
self:iterate_ac_lsa_tlv(function (o, lsa)
local is_ipv4 = ipv6s.address_is_ipv4(o.address)
--self:d('see ds', o, is_ipv4)
if is_ipv4
then
l4 = l4 or {}
table.insert(l4, o.address)
else
l6 = l6 or {}
table.insert(l6, o.address)
end
end, {type=ospf_codec.AC_TLV_DS})
l4 = mst.array_unique(l4)
l6 = mst.array_unique(l6)
self.skv:set(OSPF_DNS_KEY, l6)
self.skv:set(OSPF_IPV4_DNS_KEY, l4)
-- XXX - handle search domains for real (we fallback to
-- json_sources for now for search domain)
-- toss in the usp's too