forked from ovn-org/ovn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinctrl.c
5414 lines (4702 loc) · 180 KB
/
pinctrl.c
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) 2015, 2016, 2017 Red Hat, Inc.
* Copyright (c) 2017 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "pinctrl.h"
#include "coverage.h"
#include "csum.h"
#include "dirs.h"
#include "dp-packet.h"
#include "encaps.h"
#include "flow.h"
#include "ha-chassis.h"
#include "lport.h"
#include "nx-match.h"
#include "ovn-controller.h"
#include "latch.h"
#include "lib/packets.h"
#include "lib/sset.h"
#include "openvswitch/ofp-actions.h"
#include "openvswitch/ofp-msgs.h"
#include "openvswitch/ofp-packet.h"
#include "openvswitch/ofp-print.h"
#include "openvswitch/ofp-switch.h"
#include "openvswitch/ofp-util.h"
#include "openvswitch/vlog.h"
#include "lib/random.h"
#include "lib/dhcp.h"
#include "ovn-controller.h"
#include "ovn/actions.h"
#include "ovn/lex.h"
#include "lib/acl-log.h"
#include "lib/ip-mcast-index.h"
#include "lib/mcast-group-index.h"
#include "lib/ovn-l7.h"
#include "lib/ovn-util.h"
#include "ovn/logical-fields.h"
#include "openvswitch/poll-loop.h"
#include "openvswitch/rconn.h"
#include "socket-util.h"
#include "seq.h"
#include "timeval.h"
#include "vswitch-idl.h"
#include "lflow.h"
#include "ip-mcast.h"
VLOG_DEFINE_THIS_MODULE(pinctrl);
/* pinctrl module creates a thread - pinctrl_handler to handle
* the packet-ins from ovs-vswitchd. Some of the OVN actions
* are translated to OF 'controller' actions. See include/ovn/actions.h
* for more details.
*
* pinctrl_handler thread doesn't access the Southbound IDL object. But
* some of the OVN actions which gets translated to 'controller'
* OF action, require data from Southbound DB. Below are the details
* on how these actions are implemented.
*
* pinctrl_run() function is called by ovn-controller main thread.
* A Mutex - 'pinctrl_mutex' is used between the pinctrl_handler() thread
* and pinctrl_run().
*
* - dns_lookup - In order to do a DNS lookup, this action needs
* to access the 'DNS' table. pinctrl_run() builds a
* local DNS cache - 'dns_cache'. See sync_dns_cache()
* for more details.
* The function 'pinctrl_handle_dns_lookup()' (which is
* called with in the pinctrl_handler thread) looks into
* the local DNS cache to resolve the DNS requests.
*
* - put_arp/put_nd - These actions stores the IPv4/IPv6 and MAC addresses
* in the 'MAC_Binding' table.
* The function 'pinctrl_handle_put_mac_binding()' (which
* is called with in the pinctrl_handler thread), stores
* the IPv4/IPv6 and MAC addresses in the
* hmap - put_mac_bindings.
*
* pinctrl_run(), reads these mac bindings from the hmap
* 'put_mac_bindings' and writes to the 'MAC_Binding'
* table in the Southbound DB.
*
* - arp/nd_ns - These actions generate an ARP/IPv6 Neighbor solicit
* requests. The original packets are buffered and
* injected back when put_arp/put_nd resolves
* corresponding ARP/IPv6 Neighbor solicit requests.
* When pinctrl_run(), writes the mac bindings from the
* 'put_mac_bindings' hmap to the MAC_Binding table in
* SB DB, run_buffered_binding will add the buffered
* packets to buffered_mac_bindings and notify
* pinctrl_handler.
*
* The pinctrl_handler thread calls the function -
* send_mac_binding_buffered_pkts(), which uses
* the hmap - 'buffered_mac_bindings' and reinjects the
* buffered packets.
*
* - igmp - This action punts an IGMP packet to the controller
* which maintains multicast group information. The
* multicast groups (mcast_snoop_map) are synced to
* the 'IGMP_Group' table by ip_mcast_sync().
* ip_mcast_sync() also reads the 'IP_Multicast'
* (snooping and querier) configuration and builds a
* local configuration mcast_cfg_map.
* ip_mcast_snoop_run() which runs in the
* pinctrl_handler() thread configures the per datapath
* mcast_snoop_map entries according to mcast_cfg_map.
*
* pinctrl module also periodically sends IPv6 Router Solicitation requests
* and gARPs (for the router gateway IPs and configured NAT addresses).
*
* IPv6 RA handling - pinctrl_run() prepares the IPv6 RA information
* (see prepare_ipv6_ras()) in the shash 'ipv6_ras' by
* looking into the Southbound DB table - Port_Binding.
*
* pinctrl_handler thread sends the periodic IPv6 RAs using
* the shash - 'ipv6_ras'
*
* g/rARP handling - pinctrl_run() prepares the g/rARP information
* (see send_garp_rarp_prepare()) in the shash
* 'send_garp_rarp_data' by looking into the
* Southbound DB table Port_Binding.
* pinctrl_handler() thread sends these gARPs using the
* shash 'send_garp_rarp_data'.
*
* IGMP Queries - pinctrl_run() prepares the IGMP queries (at most one
* per local datapath) based on the mcast_snoop_map
* contents and stores them in mcast_query_list.
*
* pinctrl_handler thread sends the periodic IGMP queries
* by walking the mcast_query_list.
*
* Notification between pinctrl_handler() and pinctrl_run()
* -------------------------------------------------------
* 'struct seq' is used for notification between pinctrl_handler() thread
* and pinctrl_run().
* 'pinctrl_handler_seq' is used by pinctrl_run() to
* wake up pinctrl_handler thread from poll_block() if any changes happened
* in 'send_garp_rarp_data', 'ipv6_ras' and 'buffered_mac_bindings'
* structures.
*
* 'pinctrl_main_seq' is used by pinctrl_handler() thread to wake up
* the main thread from poll_block() when mac bindings/igmp groups need to
* be updated in the Southboubd DB.
* */
static struct ovs_mutex pinctrl_mutex = OVS_MUTEX_INITIALIZER;
static struct seq *pinctrl_handler_seq;
static struct seq *pinctrl_main_seq;
static void *pinctrl_handler(void *arg);
struct pinctrl {
char *br_int_name;
pthread_t pinctrl_thread;
/* Latch to destroy the 'pinctrl_thread' */
struct latch pinctrl_thread_exit;
};
static struct pinctrl pinctrl;
static void init_buffered_packets_map(void);
static void destroy_buffered_packets_map(void);
static void
run_buffered_binding(struct ovsdb_idl_index *sbrec_mac_binding_by_lport_ip,
const struct hmap *local_datapaths)
OVS_REQUIRES(pinctrl_mutex);
static void pinctrl_handle_put_mac_binding(const struct flow *md,
const struct flow *headers,
bool is_arp)
OVS_REQUIRES(pinctrl_mutex);
static void init_put_mac_bindings(void);
static void destroy_put_mac_bindings(void);
static void run_put_mac_bindings(
struct ovsdb_idl_txn *ovnsb_idl_txn,
struct ovsdb_idl_index *sbrec_datapath_binding_by_key,
struct ovsdb_idl_index *sbrec_port_binding_by_key,
struct ovsdb_idl_index *sbrec_mac_binding_by_lport_ip)
OVS_REQUIRES(pinctrl_mutex);
static void wait_put_mac_bindings(struct ovsdb_idl_txn *ovnsb_idl_txn);
static void flush_put_mac_bindings(void);
static void send_mac_binding_buffered_pkts(struct rconn *swconn)
OVS_REQUIRES(pinctrl_mutex);
static void init_send_garps_rarps(void);
static void destroy_send_garps_rarps(void);
static void send_garp_rarp_wait(long long int send_garp_rarp_time);
static void send_garp_rarp_prepare(
struct ovsdb_idl_index *sbrec_port_binding_by_datapath,
struct ovsdb_idl_index *sbrec_port_binding_by_name,
const struct ovsrec_bridge *,
const struct sbrec_chassis *,
const struct hmap *local_datapaths,
const struct sset *active_tunnels)
OVS_REQUIRES(pinctrl_mutex);
static void send_garp_rarp_run(struct rconn *swconn,
long long int *send_garp_rarp_time)
OVS_REQUIRES(pinctrl_mutex);
static void pinctrl_handle_nd_na(struct rconn *swconn,
const struct flow *ip_flow,
const struct match *md,
struct ofpbuf *userdata,
bool is_router);
static void reload_metadata(struct ofpbuf *ofpacts,
const struct match *md);
static void pinctrl_handle_put_nd_ra_opts(
struct rconn *swconn,
const struct flow *ip_flow, struct dp_packet *pkt_in,
struct ofputil_packet_in *pin, struct ofpbuf *userdata,
struct ofpbuf *continuation);
static void pinctrl_handle_nd_ns(struct rconn *swconn,
const struct flow *ip_flow,
struct dp_packet *pkt_in,
const struct match *md,
struct ofpbuf *userdata);
static void pinctrl_handle_put_icmp4_frag_mtu(struct rconn *swconn,
const struct flow *in_flow,
struct dp_packet *pkt_in,
struct ofputil_packet_in *pin,
struct ofpbuf *userdata,
struct ofpbuf *continuation);
static void
pinctrl_handle_event(struct ofpbuf *userdata)
OVS_REQUIRES(pinctrl_mutex);
static void wait_controller_event(struct ovsdb_idl_txn *ovnsb_idl_txn);
static void init_ipv6_ras(void);
static void destroy_ipv6_ras(void);
static void ipv6_ra_wait(long long int send_ipv6_ra_time);
static void prepare_ipv6_ras(const struct hmap *local_datapaths)
OVS_REQUIRES(pinctrl_mutex);
static void send_ipv6_ras(struct rconn *swconn,
long long int *send_ipv6_ra_time)
OVS_REQUIRES(pinctrl_mutex);
static void ip_mcast_snoop_init(void);
static void ip_mcast_snoop_destroy(void);
static void ip_mcast_snoop_run(void)
OVS_REQUIRES(pinctrl_mutex);
static void ip_mcast_querier_run(struct rconn *swconn,
long long int *query_time);
static void ip_mcast_querier_wait(long long int query_time);
static void ip_mcast_sync(
struct ovsdb_idl_txn *ovnsb_idl_txn,
const struct sbrec_chassis *chassis,
const struct hmap *local_datapaths,
struct ovsdb_idl_index *sbrec_datapath_binding_by_key,
struct ovsdb_idl_index *sbrec_port_binding_by_key,
struct ovsdb_idl_index *sbrec_igmp_groups,
struct ovsdb_idl_index *sbrec_ip_multicast)
OVS_REQUIRES(pinctrl_mutex);
static void pinctrl_ip_mcast_handle_igmp(
struct rconn *swconn,
const struct flow *ip_flow,
struct dp_packet *pkt_in,
const struct match *md,
struct ofpbuf *userdata);
static bool may_inject_pkts(void);
static void init_put_vport_bindings(void);
static void destroy_put_vport_bindings(void);
static void run_put_vport_bindings(
struct ovsdb_idl_txn *ovnsb_idl_txn,
struct ovsdb_idl_index *sbrec_datapath_binding_by_key,
struct ovsdb_idl_index *sbrec_port_binding_by_key,
const struct sbrec_chassis *chassis)
OVS_REQUIRES(pinctrl_mutex);
static void wait_put_vport_bindings(struct ovsdb_idl_txn *ovnsb_idl_txn);
static void pinctrl_handle_bind_vport(const struct flow *md,
struct ofpbuf *userdata);
static void pinctrl_handle_svc_check(struct rconn *swconn,
const struct flow *ip_flow,
struct dp_packet *pkt_in,
const struct match *md);
static void init_svc_monitors(void);
static void destroy_svc_monitors(void);
static void sync_svc_monitors(
struct ovsdb_idl_txn *ovnsb_idl_txn,
const struct sbrec_service_monitor_table *svc_mon_table,
struct ovsdb_idl_index *sbrec_port_binding_by_name,
const struct sbrec_chassis *our_chassis)
OVS_REQUIRES(pinctrl_mutex);
static void svc_monitors_run(struct rconn *swconn,
long long int *svc_monitors_next_run_time)
OVS_REQUIRES(pinctrl_mutex);
static void svc_monitors_wait(long long int svc_monitors_next_run_time);
COVERAGE_DEFINE(pinctrl_drop_put_mac_binding);
COVERAGE_DEFINE(pinctrl_drop_buffered_packets_map);
COVERAGE_DEFINE(pinctrl_drop_controller_event);
COVERAGE_DEFINE(pinctrl_drop_put_vport_binding);
struct empty_lb_backends_event {
struct hmap_node hmap_node;
long long int timestamp;
char *vip;
char *protocol;
char *load_balancer;
};
static struct hmap event_table[OVN_EVENT_MAX];
static int64_t event_seq_num;
static void
init_event_table(void)
{
for (size_t i = 0; i < OVN_EVENT_MAX; i++) {
hmap_init(&event_table[i]);
}
}
#define EVENT_TIMEOUT 10000
static void
empty_lb_backends_event_gc(bool flush)
{
struct empty_lb_backends_event *cur_ce, *next_ce;
long long int now = time_msec();
HMAP_FOR_EACH_SAFE (cur_ce, next_ce, hmap_node,
&event_table[OVN_EVENT_EMPTY_LB_BACKENDS]) {
if ((now < cur_ce->timestamp + EVENT_TIMEOUT) && !flush) {
continue;
}
free(cur_ce->vip);
free(cur_ce->protocol);
free(cur_ce->load_balancer);
hmap_remove(&event_table[OVN_EVENT_EMPTY_LB_BACKENDS],
&cur_ce->hmap_node);
free(cur_ce);
}
}
static void
event_table_gc(bool flush)
{
empty_lb_backends_event_gc(flush);
}
static void
event_table_destroy(void)
{
event_table_gc(true);
for (size_t i = 0; i < OVN_EVENT_MAX; i++) {
hmap_destroy(&event_table[i]);
}
}
static struct empty_lb_backends_event *
pinctrl_find_empty_lb_backends_event(char *vip, char *protocol,
char *load_balancer, uint32_t hash)
{
struct empty_lb_backends_event *ce;
HMAP_FOR_EACH_WITH_HASH (ce, hmap_node, hash,
&event_table[OVN_EVENT_EMPTY_LB_BACKENDS]) {
if (!strcmp(ce->vip, vip) &&
!strcmp(ce->protocol, protocol) &&
!strcmp(ce->load_balancer, load_balancer)) {
return ce;
}
}
return NULL;
}
static const struct sbrec_controller_event *
empty_lb_backends_lookup(struct empty_lb_backends_event *event,
const struct sbrec_controller_event_table *ce_table,
const struct sbrec_chassis *chassis)
{
const struct sbrec_controller_event *sbrec_event;
const char *event_type = event_to_string(OVN_EVENT_EMPTY_LB_BACKENDS);
char ref_uuid[UUID_LEN + 1];
sprintf(ref_uuid, UUID_FMT, UUID_ARGS(&chassis->header_.uuid));
SBREC_CONTROLLER_EVENT_TABLE_FOR_EACH (sbrec_event, ce_table) {
if (strcmp(sbrec_event->event_type, event_type)) {
continue;
}
char chassis_uuid[UUID_LEN + 1];
sprintf(chassis_uuid, UUID_FMT,
UUID_ARGS(&sbrec_event->chassis->header_.uuid));
if (strcmp(ref_uuid, chassis_uuid)) {
continue;
}
const char *vip = smap_get(&sbrec_event->event_info, "vip");
const char *protocol = smap_get(&sbrec_event->event_info, "protocol");
const char *load_balancer = smap_get(&sbrec_event->event_info,
"load_balancer");
if (!strcmp(event->vip, vip) &&
!strcmp(event->protocol, protocol) &&
!strcmp(event->load_balancer, load_balancer)) {
return sbrec_event;
}
}
return NULL;
}
static void
controller_event_run(struct ovsdb_idl_txn *ovnsb_idl_txn,
const struct sbrec_controller_event_table *ce_table,
const struct sbrec_chassis *chassis)
OVS_REQUIRES(pinctrl_mutex)
{
if (!ovnsb_idl_txn) {
goto out;
}
struct empty_lb_backends_event *empty_lbs;
HMAP_FOR_EACH (empty_lbs, hmap_node,
&event_table[OVN_EVENT_EMPTY_LB_BACKENDS]) {
const struct sbrec_controller_event *event;
event = empty_lb_backends_lookup(empty_lbs, ce_table, chassis);
if (!event) {
struct smap event_info = SMAP_INITIALIZER(&event_info);
smap_add(&event_info, "vip", empty_lbs->vip);
smap_add(&event_info, "protocol", empty_lbs->protocol);
smap_add(&event_info, "load_balancer", empty_lbs->load_balancer);
event = sbrec_controller_event_insert(ovnsb_idl_txn);
sbrec_controller_event_set_event_type(event,
event_to_string(OVN_EVENT_EMPTY_LB_BACKENDS));
sbrec_controller_event_set_seq_num(event, ++event_seq_num);
sbrec_controller_event_set_event_info(event, &event_info);
sbrec_controller_event_set_chassis(event, chassis);
}
}
out:
event_table_gc(!!ovnsb_idl_txn);
}
void
pinctrl_init(void)
{
init_put_mac_bindings();
init_send_garps_rarps();
init_ipv6_ras();
init_buffered_packets_map();
init_event_table();
ip_mcast_snoop_init();
init_put_vport_bindings();
init_svc_monitors();
pinctrl.br_int_name = NULL;
pinctrl_handler_seq = seq_create();
pinctrl_main_seq = seq_create();
latch_init(&pinctrl.pinctrl_thread_exit);
pinctrl.pinctrl_thread = ovs_thread_create("ovn_pinctrl", pinctrl_handler,
&pinctrl);
}
static ovs_be32
queue_msg(struct rconn *swconn, struct ofpbuf *msg)
{
const struct ofp_header *oh = msg->data;
ovs_be32 xid = oh->xid;
rconn_send(swconn, msg, NULL);
return xid;
}
/* Sets up 'swconn', a newly (re)connected connection to a switch. */
static void
pinctrl_setup(struct rconn *swconn)
{
/* Fetch the switch configuration. The response later will allow us to
* change the miss_send_len to UINT16_MAX, so that we can enable
* asynchronous messages. */
queue_msg(swconn, ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
rconn_get_version(swconn), 0));
/* Set a packet-in format that supports userdata. */
queue_msg(swconn,
ofputil_encode_set_packet_in_format(rconn_get_version(swconn),
OFPUTIL_PACKET_IN_NXT2));
}
static void
set_switch_config(struct rconn *swconn,
const struct ofputil_switch_config *config)
{
enum ofp_version version = rconn_get_version(swconn);
struct ofpbuf *request = ofputil_encode_set_config(config, version);
queue_msg(swconn, request);
}
static void
set_actions_and_enqueue_msg(struct rconn *swconn,
const struct dp_packet *packet,
const struct match *md,
struct ofpbuf *userdata)
{
/* Copy metadata from 'md' into the packet-out via "set_field"
* actions, then add actions from 'userdata'.
*/
uint64_t ofpacts_stub[4096 / 8];
struct ofpbuf ofpacts = OFPBUF_STUB_INITIALIZER(ofpacts_stub);
enum ofp_version version = rconn_get_version(swconn);
reload_metadata(&ofpacts, md);
enum ofperr error = ofpacts_pull_openflow_actions(userdata, userdata->size,
version, NULL, NULL,
&ofpacts);
if (error) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_WARN_RL(&rl, "failed to parse actions from userdata (%s)",
ofperr_to_string(error));
ofpbuf_uninit(&ofpacts);
return;
}
struct ofputil_packet_out po = {
.packet = dp_packet_data(packet),
.packet_len = dp_packet_size(packet),
.buffer_id = UINT32_MAX,
.ofpacts = ofpacts.data,
.ofpacts_len = ofpacts.size,
};
match_set_in_port(&po.flow_metadata, OFPP_CONTROLLER);
enum ofputil_protocol proto = ofputil_protocol_from_ofp_version(version);
queue_msg(swconn, ofputil_encode_packet_out(&po, proto));
ofpbuf_uninit(&ofpacts);
}
struct buffer_info {
struct ofpbuf ofpacts;
struct dp_packet *p;
};
#define BUFFER_QUEUE_DEPTH 4
struct buffered_packets {
struct hmap_node hmap_node;
struct ovs_list list;
/* key */
struct in6_addr ip;
struct eth_addr ea;
long long int timestamp;
struct buffer_info data[BUFFER_QUEUE_DEPTH];
uint32_t head, tail;
};
static struct hmap buffered_packets_map;
static struct ovs_list buffered_mac_bindings;
static void
init_buffered_packets_map(void)
{
hmap_init(&buffered_packets_map);
ovs_list_init(&buffered_mac_bindings);
}
static void
destroy_buffered_packets(struct buffered_packets *bp)
{
struct buffer_info *bi;
while (bp->head != bp->tail) {
bi = &bp->data[bp->head];
dp_packet_delete(bi->p);
ofpbuf_uninit(&bi->ofpacts);
bp->head = (bp->head + 1) % BUFFER_QUEUE_DEPTH;
}
}
static void
destroy_buffered_packets_map(void)
{
struct buffered_packets *bp, *next;
HMAP_FOR_EACH_SAFE (bp, next, hmap_node, &buffered_packets_map) {
destroy_buffered_packets(bp);
hmap_remove(&buffered_packets_map, &bp->hmap_node);
free(bp);
}
hmap_destroy(&buffered_packets_map);
LIST_FOR_EACH_POP (bp, list, &buffered_mac_bindings) {
destroy_buffered_packets(bp);
free(bp);
}
}
static void
buffered_push_packet(struct buffered_packets *bp,
struct dp_packet *packet,
const struct match *md)
{
uint32_t next = (bp->tail + 1) % BUFFER_QUEUE_DEPTH;
struct buffer_info *bi = &bp->data[bp->tail];
ofpbuf_init(&bi->ofpacts, 4096);
reload_metadata(&bi->ofpacts, md);
struct ofpact_resubmit *resubmit = ofpact_put_RESUBMIT(&bi->ofpacts);
resubmit->in_port = OFPP_CONTROLLER;
resubmit->table_id = OFTABLE_REMOTE_OUTPUT;
bi->p = packet;
if (next == bp->head) {
bi = &bp->data[bp->head];
dp_packet_delete(bi->p);
ofpbuf_uninit(&bi->ofpacts);
bp->head = (bp->head + 1) % BUFFER_QUEUE_DEPTH;
}
bp->tail = next;
}
static void
buffered_send_packets(struct rconn *swconn, struct buffered_packets *bp,
struct eth_addr *addr)
{
enum ofp_version version = rconn_get_version(swconn);
enum ofputil_protocol proto = ofputil_protocol_from_ofp_version(version);
while (bp->head != bp->tail) {
struct buffer_info *bi = &bp->data[bp->head];
struct eth_header *eth = dp_packet_data(bi->p);
eth->eth_dst = *addr;
struct ofputil_packet_out po = {
.packet = dp_packet_data(bi->p),
.packet_len = dp_packet_size(bi->p),
.buffer_id = UINT32_MAX,
.ofpacts = bi->ofpacts.data,
.ofpacts_len = bi->ofpacts.size,
};
match_set_in_port(&po.flow_metadata, OFPP_CONTROLLER);
queue_msg(swconn, ofputil_encode_packet_out(&po, proto));
ofpbuf_uninit(&bi->ofpacts);
dp_packet_delete(bi->p);
bp->head = (bp->head + 1) % BUFFER_QUEUE_DEPTH;
}
}
#define BUFFER_MAP_TIMEOUT 10000
static void
buffered_packets_map_gc(void)
{
struct buffered_packets *cur_qp, *next_qp;
long long int now = time_msec();
HMAP_FOR_EACH_SAFE (cur_qp, next_qp, hmap_node, &buffered_packets_map) {
if (now > cur_qp->timestamp + BUFFER_MAP_TIMEOUT) {
destroy_buffered_packets(cur_qp);
hmap_remove(&buffered_packets_map, &cur_qp->hmap_node);
free(cur_qp);
}
}
}
static struct buffered_packets *
pinctrl_find_buffered_packets(const struct in6_addr *ip, uint32_t hash)
{
struct buffered_packets *qp;
HMAP_FOR_EACH_WITH_HASH (qp, hmap_node, hash,
&buffered_packets_map) {
if (IN6_ARE_ADDR_EQUAL(&qp->ip, ip)) {
return qp;
}
}
return NULL;
}
/* Called with in the pinctrl_handler thread context. */
static int
pinctrl_handle_buffered_packets(const struct flow *ip_flow,
struct dp_packet *pkt_in,
const struct match *md, bool is_arp)
OVS_REQUIRES(pinctrl_mutex)
{
struct buffered_packets *bp;
struct dp_packet *clone;
struct in6_addr addr;
if (is_arp) {
addr = in6_addr_mapped_ipv4(ip_flow->nw_dst);
} else {
addr = ip_flow->ipv6_dst;
}
uint32_t hash = hash_bytes(&addr, sizeof addr, 0);
bp = pinctrl_find_buffered_packets(&addr, hash);
if (!bp) {
if (hmap_count(&buffered_packets_map) >= 1000) {
COVERAGE_INC(pinctrl_drop_buffered_packets_map);
return -ENOMEM;
}
bp = xmalloc(sizeof *bp);
hmap_insert(&buffered_packets_map, &bp->hmap_node, hash);
bp->head = bp->tail = 0;
bp->ip = addr;
}
bp->timestamp = time_msec();
/* clone the packet to send it later with correct L2 address */
clone = dp_packet_clone_data(dp_packet_data(pkt_in),
dp_packet_size(pkt_in));
buffered_push_packet(bp, clone, md);
return 0;
}
/* Called with in the pinctrl_handler thread context. */
static void
pinctrl_handle_arp(struct rconn *swconn, const struct flow *ip_flow,
struct dp_packet *pkt_in,
const struct match *md, struct ofpbuf *userdata)
{
/* This action only works for IP packets, and the switch should only send
* us IP packets this way, but check here just to be sure. */
if (ip_flow->dl_type != htons(ETH_TYPE_IP)) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_WARN_RL(&rl, "ARP action on non-IP packet (Ethertype %"PRIx16")",
ntohs(ip_flow->dl_type));
return;
}
ovs_mutex_lock(&pinctrl_mutex);
pinctrl_handle_buffered_packets(ip_flow, pkt_in, md, true);
ovs_mutex_unlock(&pinctrl_mutex);
/* Compose an ARP packet. */
uint64_t packet_stub[128 / 8];
struct dp_packet packet;
dp_packet_use_stub(&packet, packet_stub, sizeof packet_stub);
compose_arp__(&packet);
struct eth_header *eth = dp_packet_eth(&packet);
eth->eth_dst = ip_flow->dl_dst;
eth->eth_src = ip_flow->dl_src;
struct arp_eth_header *arp = dp_packet_l3(&packet);
arp->ar_op = htons(ARP_OP_REQUEST);
arp->ar_sha = ip_flow->dl_src;
put_16aligned_be32(&arp->ar_spa, ip_flow->nw_src);
arp->ar_tha = eth_addr_zero;
put_16aligned_be32(&arp->ar_tpa, ip_flow->nw_dst);
if (ip_flow->vlans[0].tci & htons(VLAN_CFI)) {
eth_push_vlan(&packet, htons(ETH_TYPE_VLAN_8021Q),
ip_flow->vlans[0].tci);
}
set_actions_and_enqueue_msg(swconn, &packet, md, userdata);
dp_packet_uninit(&packet);
}
/* Called with in the pinctrl_handler thread context. */
static void
pinctrl_handle_icmp(struct rconn *swconn, const struct flow *ip_flow,
struct dp_packet *pkt_in,
const struct match *md, struct ofpbuf *userdata,
bool include_orig_ip_datagram)
{
/* This action only works for IP packets, and the switch should only send
* us IP packets this way, but check here just to be sure. */
if (ip_flow->dl_type != htons(ETH_TYPE_IP) &&
ip_flow->dl_type != htons(ETH_TYPE_IPV6)) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_WARN_RL(&rl,
"ICMP action on non-IP packet (eth_type 0x%"PRIx16")",
ntohs(ip_flow->dl_type));
return;
}
uint64_t packet_stub[128 / 8];
struct dp_packet packet;
dp_packet_use_stub(&packet, packet_stub, sizeof packet_stub);
dp_packet_clear(&packet);
packet.packet_type = htonl(PT_ETH);
struct eth_header *eh = dp_packet_put_zeros(&packet, sizeof *eh);
eh->eth_dst = ip_flow->dl_dst;
eh->eth_src = ip_flow->dl_src;
if (get_dl_type(ip_flow) == htons(ETH_TYPE_IP)) {
struct ip_header *in_ip = dp_packet_l3(pkt_in);
uint16_t in_ip_len = ntohs(in_ip->ip_tot_len);
if (in_ip_len < IP_HEADER_LEN) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_WARN_RL(&rl,
"ICMP action on IP packet with invalid length (%u)",
in_ip_len);
return;
}
struct ip_header *nh = dp_packet_put_zeros(&packet, sizeof *nh);
eh->eth_type = htons(ETH_TYPE_IP);
dp_packet_set_l3(&packet, nh);
nh->ip_ihl_ver = IP_IHL_VER(5, 4);
nh->ip_tot_len = htons(sizeof(struct ip_header) +
sizeof(struct icmp_header));
nh->ip_proto = IPPROTO_ICMP;
nh->ip_frag_off = htons(IP_DF);
packet_set_ipv4(&packet, ip_flow->nw_src, ip_flow->nw_dst,
ip_flow->nw_tos, 255);
struct icmp_header *ih = dp_packet_put_zeros(&packet, sizeof *ih);
dp_packet_set_l4(&packet, ih);
packet_set_icmp(&packet, ICMP4_DST_UNREACH, 1);
if (include_orig_ip_datagram) {
/* RFC 1122: 3.2.2 MUST send at least the IP header and 8 bytes
* of header. MAY send more.
* RFC says return as much as we can without exceeding 576
* bytes.
* So, lets return as much as we can. */
/* Calculate available room to include the original IP + data. */
nh = dp_packet_l3(&packet);
uint16_t room = 576 - (sizeof *eh + ntohs(nh->ip_tot_len));
if (in_ip_len > room) {
in_ip_len = room;
}
dp_packet_put(&packet, in_ip, in_ip_len);
/* dp_packet_put may reallocate the buffer. Get the l3 and l4
* header pointers again. */
nh = dp_packet_l3(&packet);
ih = dp_packet_l4(&packet);
uint16_t ip_total_len = ntohs(nh->ip_tot_len) + in_ip_len;
nh->ip_tot_len = htons(ip_total_len);
ih->icmp_csum = 0;
ih->icmp_csum = csum(ih, sizeof *ih + in_ip_len);
nh->ip_csum = 0;
nh->ip_csum = csum(nh, sizeof *nh);
}
} else {
struct ip6_hdr *nh = dp_packet_put_zeros(&packet, sizeof *nh);
struct icmp6_data_header *ih;
uint32_t icmpv6_csum;
eh->eth_type = htons(ETH_TYPE_IPV6);
dp_packet_set_l3(&packet, nh);
nh->ip6_vfc = 0x60;
nh->ip6_nxt = IPPROTO_ICMPV6;
nh->ip6_plen = htons(sizeof(*nh) + ICMP6_DATA_HEADER_LEN);
packet_set_ipv6(&packet, &ip_flow->ipv6_src, &ip_flow->ipv6_dst,
ip_flow->nw_tos, ip_flow->ipv6_label, 255);
ih = dp_packet_put_zeros(&packet, sizeof *ih);
dp_packet_set_l4(&packet, ih);
ih->icmp6_base.icmp6_type = ICMP6_DST_UNREACH;
ih->icmp6_base.icmp6_code = 1;
ih->icmp6_base.icmp6_cksum = 0;
uint8_t *data = dp_packet_put_zeros(&packet, sizeof *nh);
memcpy(data, dp_packet_l3(pkt_in), sizeof(*nh));
icmpv6_csum = packet_csum_pseudoheader6(dp_packet_l3(&packet));
ih->icmp6_base.icmp6_cksum = csum_finish(
csum_continue(icmpv6_csum, ih,
sizeof(*nh) + ICMP6_DATA_HEADER_LEN));
}
if (ip_flow->vlans[0].tci & htons(VLAN_CFI)) {
eth_push_vlan(&packet, htons(ETH_TYPE_VLAN_8021Q),
ip_flow->vlans[0].tci);
}
set_actions_and_enqueue_msg(swconn, &packet, md, userdata);
dp_packet_uninit(&packet);
}
/* Called with in the pinctrl_handler thread context. */
static void
pinctrl_handle_tcp_reset(struct rconn *swconn, const struct flow *ip_flow,
struct dp_packet *pkt_in,
const struct match *md, struct ofpbuf *userdata)
{
/* This action only works for TCP segments, and the switch should only send
* us TCP segments this way, but check here just to be sure. */
if (ip_flow->nw_proto != IPPROTO_TCP) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_WARN_RL(&rl, "TCP_RESET action on non-TCP packet");
return;
}
uint64_t packet_stub[128 / 8];
struct dp_packet packet;
dp_packet_use_stub(&packet, packet_stub, sizeof packet_stub);
dp_packet_clear(&packet);
packet.packet_type = htonl(PT_ETH);
struct eth_header *eh = dp_packet_put_zeros(&packet, sizeof *eh);
eh->eth_dst = ip_flow->dl_dst;
eh->eth_src = ip_flow->dl_src;
if (get_dl_type(ip_flow) == htons(ETH_TYPE_IPV6)) {
struct ip6_hdr *nh = dp_packet_put_zeros(&packet, sizeof *nh);
eh->eth_type = htons(ETH_TYPE_IPV6);
dp_packet_set_l3(&packet, nh);
nh->ip6_vfc = 0x60;
nh->ip6_nxt = IPPROTO_TCP;
nh->ip6_plen = htons(TCP_HEADER_LEN);
packet_set_ipv6(&packet, &ip_flow->ipv6_src, &ip_flow->ipv6_dst,
ip_flow->nw_tos, ip_flow->ipv6_label, 255);
} else {
struct ip_header *nh = dp_packet_put_zeros(&packet, sizeof *nh);
eh->eth_type = htons(ETH_TYPE_IP);
dp_packet_set_l3(&packet, nh);
nh->ip_ihl_ver = IP_IHL_VER(5, 4);
nh->ip_tot_len = htons(IP_HEADER_LEN + TCP_HEADER_LEN);
nh->ip_proto = IPPROTO_TCP;
nh->ip_frag_off = htons(IP_DF);
packet_set_ipv4(&packet, ip_flow->nw_src, ip_flow->nw_dst,
ip_flow->nw_tos, 255);
}
struct tcp_header *th = dp_packet_put_zeros(&packet, sizeof *th);
struct tcp_header *tcp_in = dp_packet_l4(pkt_in);
dp_packet_set_l4(&packet, th);
th->tcp_ctl = TCP_CTL(TCP_RST, 5);
if (ip_flow->tcp_flags & htons(TCP_ACK)) {
th->tcp_seq = tcp_in->tcp_ack;
} else {
uint32_t tcp_seq, ack_seq, tcp_len;
tcp_seq = ntohl(get_16aligned_be32(&tcp_in->tcp_seq));
tcp_len = TCP_OFFSET(tcp_in->tcp_ctl) * 4;
ack_seq = tcp_seq + dp_packet_l4_size(pkt_in) - tcp_len;
put_16aligned_be32(&th->tcp_ack, htonl(ack_seq));
put_16aligned_be32(&th->tcp_seq, 0);
}
packet_set_tcp_port(&packet, ip_flow->tp_dst, ip_flow->tp_src);
if (ip_flow->vlans[0].tci & htons(VLAN_CFI)) {
eth_push_vlan(&packet, htons(ETH_TYPE_VLAN_8021Q),
ip_flow->vlans[0].tci);
}
set_actions_and_enqueue_msg(swconn, &packet, md, userdata);
dp_packet_uninit(&packet);
}
/* Called with in the pinctrl_handler thread context. */
static void
pinctrl_handle_put_dhcp_opts(
struct rconn *swconn,
struct dp_packet *pkt_in, struct ofputil_packet_in *pin,
struct ofpbuf *userdata, struct ofpbuf *continuation)
{
enum ofp_version version = rconn_get_version(swconn);
enum ofputil_protocol proto = ofputil_protocol_from_ofp_version(version);
struct dp_packet *pkt_out_ptr = NULL;
uint32_t success = 0;
/* Parse result field. */
const struct mf_field *f;
enum ofperr ofperr = nx_pull_header(userdata, NULL, &f, NULL);
if (ofperr) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_WARN_RL(&rl, "bad result OXM (%s)", ofperr_to_string(ofperr));
goto exit;
}
/* Parse result offset and offer IP. */
ovs_be32 *ofsp = ofpbuf_try_pull(userdata, sizeof *ofsp);
ovs_be32 *offer_ip = ofpbuf_try_pull(userdata, sizeof *offer_ip);
if (!ofsp || !offer_ip) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_WARN_RL(&rl, "offset or offer_ip not present in the userdata");
goto exit;
}
/* Check that the result is valid and writable. */