-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathovs-data.c
4813 lines (4208 loc) · 150 KB
/
ovs-data.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 Open Networking Foundation.
*
* 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.
*/
#define _GNU_SOURCE
#include <config.h>
#define _GNU_SOURCE
#include <assert.h>
#include <sys/socket.h>
#include <linux/ethtool.h>
#include <linux/if.h>
#include <linux/sockios.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
/* libovs */
#include <dynamic-string.h>
#include <ovsdb-idl-provider.h>
#include <vswitch-idl.h>
#include <dirs.h>
#include <dpif.h>
#include <vconn.h>
#include <socket-util.h>
#include <ofp-util.h>
#include <ofp-msgs.h>
#include <ofp-print.h>
#include <poll-loop.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libnetconf.h>
#include "data.h"
/* Tests bit of OpenFlow port config and returns "true"/"false" */
#define OFC_PORT_CONF_BIT(conf, bit) \
(conf & bit ? "true" : "false")
typedef struct {
struct ovsdb_idl *idl;
struct ovsdb_idl_txn *txn;
unsigned int seqno;
bool added_interface;
struct vconn *vconn;
} ovsdb_t;
ovsdb_t *ovsdb_handler = NULL;
int ioctlfd = -1;
/* locally stored data */
static xmlChar *cs_id = NULL; /* /capable-switch/id */
struct u32_str_map {
uint32_t value;
const char *str;
};
static const struct u32_str_map rates[] = {
{ADVERTISED_10baseT_Half, "10Mb-HD"},
{ADVERTISED_10baseT_Full, "10Mb-FD"},
{ADVERTISED_100baseT_Half, "100Mb-HD"},
{ADVERTISED_100baseT_Full, "100Mb-FD"},
{ADVERTISED_1000baseT_Half, "1Gb-HD"},
{ADVERTISED_1000baseT_Full, "1Gb-FD"},
{ADVERTISED_1000baseKX_Full, "1Gb-FD"},
// { ADVERTISED_2500baseX_Full, "2500baseX/Full" },
{ADVERTISED_10000baseT_Full, "10Gb"},
{ADVERTISED_10000baseKX4_Full, "10Gb"},
{ADVERTISED_10000baseKR_Full, "10Gb"},
// { ADVERTISED_20000baseMLD2_Full, "20000baseMLD2/Full" },
// { ADVERTISED_20000baseKR2_Full, "20000baseKR2/Full" },
{ADVERTISED_40000baseKR4_Full, "40Gb"},
{ADVERTISED_40000baseCR4_Full, "40Gb"},
{ADVERTISED_40000baseSR4_Full, "40Gb"},
{ADVERTISED_40000baseLR4_Full, "40Gb"},
};
static const struct u32_str_map medium[] = {
{ADVERTISED_TP, "copper"},
{ADVERTISED_FIBRE, "fiber"},
};
/* OpenFlow helpers to get information about interfaces */
/* Prepare vconnp - connection with bridge using OpenFlow. Bridge is
* identified by name (e.g. ofc-bridge). We use default OF versions
* (OFPUTIL_DEFAULT_VERSIONS).
*
* This function was copied from ovs-ofctl and modified.
*
* Function returns 0 on success, otherwise errno value. If successful,
* stores a pointer to the new connection in '*vconnp', otherwise a null
* pointer. */
static int
of_open_vconn_socket(const char *name, struct vconn **vconnp)
{
int error;
char *vconn_name;
if (asprintf(&vconn_name, "unix:%s", name) == -1) {
return ENOMEM;
}
error = vconn_open(vconn_name, OFPUTIL_DEFAULT_VERSIONS, DSCP_DEFAULT,
vconnp);
if (error && error != ENOENT) {
nc_verb_error("OpenFlow: %s: failed to open socket (%s)", name,
ovs_strerror(error));
}
free(vconn_name);
return error;
}
/* Creates connection via OpenFlow with the bridge identified by 'name'.
*
* Function was partially copied from ovs-ofctl and modified.
*
* Function returns true on success. If successful, it stores a pointer
* to the new connection in '*vconnp', otherwise a null pointer. */
static bool
of_open_vconn(const char *name, struct vconn **vconnp)
{
char *dp_name = NULL, *dp_type = NULL, *sock_name = NULL;
enum ofputil_protocol protocol;
char *bridge_path = NULL;
int ofp_version;
int error = 0, max_try;
bool ret = false;
const struct timespec timeout = {1, 0};
if (asprintf(&bridge_path, "%s/%s.mgmt", ovs_rundir(), name) == -1) {
return false;
}
/* changed to called function */
dp_parse_name(name, &dp_name, &dp_type);
if (asprintf(&sock_name, "%s/%s.mgmt", ovs_rundir(), dp_name) == -1) {
nc_verb_error("%s: asprintf() failed", __func__);
goto cleanup;
}
max_try = 30;
/* After creation of a new bridge, it lasts some time
* before the socket is ready -> repeat connection until
* the 'max_try' is 0 or the connection is successful. */
do {
if (strchr(name, ':')) {
vconn_open(name, OFPUTIL_DEFAULT_VERSIONS, DSCP_DEFAULT, vconnp);
} else if (!of_open_vconn_socket(name, vconnp)) {
/* Fall Through. */
} else if (!of_open_vconn_socket(bridge_path, vconnp)) {
/* Fall Through. */
} else if (!of_open_vconn_socket(sock_name, vconnp)) {
/* Fall Through. */
} else {
nc_verb_error("OpenFlow: %s is not a bridge or a socket.", name);
goto timeout;
}
nc_verb_verbose("OpenFlow: connecting to %s", vconn_get_name(*vconnp));
error = vconn_connect_block(*vconnp);
if (error) {
nc_verb_error("OpenFlow: %s: failed to connect to socket (%s).",
name, ovs_strerror(error));
timeout:
if (--max_try == 0) {
/* max_try elapsed, giving up... */
goto cleanup;
}
if (*vconnp) {
vconn_close(*vconnp);
*vconnp = NULL;
}
nanosleep(&timeout, NULL);
}
/* Repeat when we haven't successful connection. When the
* socket is not ready yet, OVS disconnects us (ECONNRESET). */
} while (!(*vconnp) || error == ECONNRESET);
if (*vconnp) {
nc_verb_verbose("OpenFlow: %s: successful connection.", name);
} else {
nc_verb_error("OpenFlow: %s: connection failed, giving up.", name);
goto cleanup;
}
ofp_version = vconn_get_version(*vconnp);
protocol = ofputil_protocol_from_ofp_version(ofp_version);
if (!protocol) {
nc_verb_error("OpenFlow: %s: unsupported OpenFlow version 0x%02x.",
name, ofp_version);
goto cleanup;
}
ret = true;
cleanup:
free(bridge_path);
free(dp_name);
free(dp_type);
free(sock_name);
return ret;
}
/* Gets information about interfaces using 'vconnp' connection. Function
* returns pointer to OpenFlow buffer (implemented by OVS) that is used
* to get results. */
static struct ofpbuf *
of_get_ports(struct vconn *vconnp)
{
struct ofpbuf *request;
struct ofpbuf *reply;
int ofp_version;
if (vconnp == NULL) {
return NULL;
}
/* existence of version was checked in of_open_vconn() */
ofp_version = vconn_get_version(vconnp);
request = ofputil_encode_port_desc_stats_request(ofp_version, OFPP_NONE);
vconn_transact(vconnp, request, &reply);
/* updates reply size */
ofputil_switch_features_has_ports(reply);
return reply;
}
/* Sets value of configuration bit of 'port_name' interface. It can be used
* to set: OFPUTIL_PC_NO_FWD, OFPUTIL_PC_NO_PACKET_IN, OFPUTIL_PC_NO_RECV,
* OFPUTIL_PC_PORT_DOWN given as 'bit'. If 'value' is 0, clear configuration
* bit. Otherwise, set 'bit' to 1. Function returns EXIT_SUCCESS on success.
* Otherwise it returns EXIT_FAILURE and sets *e. */
static int
of_mod_port_cfg_internal(struct vconn *vconnp, const char *port_name,
enum ofputil_port_config bit, char value,
struct nc_err **e)
{
enum ofptype type;
int ofp_version, ret;
enum ofputil_protocol protocol;
struct ofputil_phy_port pp;
struct ofputil_port_mod pm;
struct ofp_header *oh;
struct ofpbuf b, *request, *reply = of_get_ports(vconnp);
struct ofpbuf *mod_reply = NULL;
char *oferr = NULL;
if (reply == NULL) {
*e = nc_err_new(NC_ERR_DATA_MISSING);
nc_err_set(*e, NC_ERR_PARAM_MSG, "No port found via OpenFlow.");
return EXIT_FAILURE;
}
ofp_version = vconn_get_version(vconnp);
protocol = ofputil_protocol_from_ofp_version(ofp_version);
oh = ofpbuf_data(reply);
/* get the beginning of data */
ofpbuf_use_const(&b, oh, ntohs(oh->length));
if (ofptype_pull(&type, &b) || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
*e = nc_err_new(NC_ERR_OP_FAILED);
nc_err_set(*e, NC_ERR_PARAM_MSG, "Unexpected response via OpenFlow.");
goto error;
}
/* find port by name */
while (!ofputil_pull_phy_port(oh->version, &b, &pp)) {
/* modify port */
if (!strncmp(pp.name, port_name, strlen(pp.name) + 1)) {
/* prepare port for transaction */
pm.port_no = pp.port_no;
pm.config = 0;
pm.mask = 0;
pm.advertise = 0;
memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
pm.mask = bit;
/* set value to selected bit: */
pm.config = (value != 0 ? bit : 0);
request = ofputil_encode_port_mod(&pm, protocol);
ofpmsg_update_length(request);
ret = vconn_transact_noreply(vconnp, request, &mod_reply);
if (ret) {
/* got error code */
*e = nc_err_new(NC_ERR_OP_FAILED);
nc_err_set(*e, NC_ERR_PARAM_MSG, ovs_strerror(ret));
goto error;
} else {
if (mod_reply) {
oferr = ofp_to_string(ofpbuf_data(reply),
ofpbuf_size(reply), 2);
*e = nc_err_new(NC_ERR_OP_FAILED);
nc_err_set(*e, NC_ERR_PARAM_MSG, oferr);
free(oferr);
ofpbuf_delete(mod_reply);
goto error;
}
/* success - reply to port_mod was empty */
ofpbuf_delete(reply);
return EXIT_SUCCESS;
}
}
}
/* Port name was not found. */
*e = nc_err_new(NC_ERR_OP_FAILED);
nc_err_set(*e, NC_ERR_PARAM_MSG, "Modification of unknown port.");
error:
ofpbuf_delete(reply);
return EXIT_FAILURE;
}
static unsigned int
dev_get_flags(const char *ifname)
{
struct ifreq ethreq;
strncpy(ethreq.ifr_name, ifname, sizeof ethreq.ifr_name);
if (ioctl(ioctlfd, SIOCGIFFLAGS, ðreq)) {
nc_verb_error("ioctl %d on \"%s\" failed (%s)", SIOCGIFFLAGS, ifname,
strerror(errno));
return 0;
}
return ethreq.ifr_flags;
}
static int
dev_set_flags(const char *ifname, unsigned int flags)
{
struct ifreq ethreq;
strncpy(ethreq.ifr_name, ifname, sizeof ethreq.ifr_name);
ethreq.ifr_flags = flags;
if (ioctl(ioctlfd, SIOCSIFFLAGS, ðreq)) {
nc_verb_error("ioctl %d on \"%s\" failed (%s)", SIOCSIFFLAGS, ifname,
strerror(errno));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static int
dev_is_system(const char *ifname)
{
struct ethtool_drvinfo drvinfo;
struct ifreq ethreq;
memset(ðreq, 0, sizeof ethreq);
memset(&drvinfo, 0, sizeof drvinfo);
strncpy(ethreq.ifr_name, ifname, sizeof ethreq.ifr_name);
drvinfo.cmd = ETHTOOL_GDRVINFO;
ethreq.ifr_data = &drvinfo;
errno = 0;
ioctl(ioctlfd, SIOCETHTOOL, ðreq);
if (errno || !strcmp(drvinfo.driver, "openvswitch")) {
return 0;
} else {
return 1;
}
}
static struct ethtool_cmd *
dev_get_ethtool(const char *ifname)
{
static struct ethtool_cmd ecmd;
struct ifreq ethreq;
memset(ðreq, 0, sizeof ethreq);
memset(&ecmd, 0, sizeof ecmd);
strncpy(ethreq.ifr_name, ifname, sizeof ethreq.ifr_name);
ecmd.cmd = ETHTOOL_GSET;
ethreq.ifr_data = &ecmd;
ioctl(ioctlfd, SIOCETHTOOL, ðreq);
return &ecmd;
}
static int
dev_set_ethtool(const char *ifname, struct ethtool_cmd *ecmd)
{
struct ifreq ethreq;
memset(ðreq, 0, sizeof ethreq);
strncpy(ethreq.ifr_name, ifname, sizeof ethreq.ifr_name);
ecmd->cmd = ETHTOOL_SSET;
ethreq.ifr_data = ecmd;
return ioctl(ioctlfd, SIOCETHTOOL, ðreq);
}
static const xmlChar *
find_bridge_with_port(const xmlChar *port_name)
{
const struct ovsrec_bridge *bridge;
const struct ovsrec_port *port;
const struct ovsrec_interface *interface;
int port_i, inter_i;
OVSREC_BRIDGE_FOR_EACH(bridge, ovsdb_handler->idl) {
for (port_i = 0; port_i < bridge->n_ports; port_i++) {
port = bridge->ports[port_i];
for (inter_i = 0; inter_i < port->n_interfaces; inter_i++) {
interface = port->interfaces[inter_i];
if (!strncmp
(interface->name, (char *) port_name,
strlen(interface->name) + 1)) {
return BAD_CAST bridge->name;
}
}
}
}
return NULL;
}
int
of_mod_port_cfg(const xmlChar *port_name, const xmlChar *node_name,
const xmlChar *value, struct nc_err **e)
{
struct vconn *vconnp;
enum ofputil_port_config bit;
char val;
const xmlChar *br_name;
br_name = find_bridge_with_port(port_name);
if (!br_name) {
nc_verb_error("%s: the bridge with the port %s not found", __func__,
(char *) port_name);
*e = nc_err_new(NC_ERR_OP_FAILED);
nc_err_set(*e, NC_ERR_PARAM_MSG,
"The bridge with the port being modified not found.");
return EXIT_FAILURE;
}
if (xmlStrEqual(node_name, BAD_CAST "no-receive")) {
bit = OFPUTIL_PC_NO_RECV;
} else if (xmlStrEqual(node_name, BAD_CAST "no-forward")) {
bit = OFPUTIL_PC_NO_FWD;
} else if (xmlStrEqual(node_name, BAD_CAST "no-packet-in")) {
bit = OFPUTIL_PC_NO_PACKET_IN;
} else if (xmlStrEqual(node_name, BAD_CAST "admin-state")) {
if (dev_is_system((const char *) port_name)) {
/* set status if the system interface directly via ioctl() */
return txn_mod_port_admin_state(port_name, value, e);
}
/* it is internal interface, use OpenFlow */
bit = OFPUTIL_PC_PORT_DOWN;
} else {
*e = nc_err_new(NC_ERR_BAD_ELEM);
nc_err_set(*e, NC_ERR_PARAM_INFO_BADELEM, (char *) node_name);
nc_err_set(*e, NC_ERR_PARAM_MSG, "Invalid element name.");
return EXIT_FAILURE;
}
/* check value */
val = -1;
if (bit == OFPUTIL_PC_PORT_DOWN) {
/* admin-state */
if (!value || xmlStrEqual(value, BAD_CAST "up")) {
/* !value = delete, up is the default value */
val = 0;
} else if (xmlStrEqual(value, BAD_CAST "down")) {
val = 1;
}
} else {
/* no-receive, no-forward, no-packet-in */
if (!value || xmlStrEqual(value, BAD_CAST "false")) {
/* !value = delete, false is the default value */
val = 0;
} else if (xmlStrEqual(value, BAD_CAST "true")) {
val = 1;
}
}
if (val == -1) {
*e = nc_err_new(NC_ERR_BAD_ELEM);
nc_err_set(*e, NC_ERR_PARAM_INFO_BADELEM, (char *) node_name);
nc_err_set(*e, NC_ERR_PARAM_MSG, "Invalid element value.");
return EXIT_FAILURE;
}
/* prepare OpenFlow connection to the bridge where the port is used ... */
if (of_open_vconn((char *) br_name, &vconnp) != true) {
nc_verb_error("OpenFlow: could not connect to '%s' bridge.", br_name);
*e = nc_err_new(NC_ERR_OP_FAILED);
nc_err_set(*e, NC_ERR_PARAM_MSG,
"Unable to connect to the bridge via openFlow.");
return EXIT_FAILURE;
}
/* ... and apply change to the port */
if (of_mod_port_cfg_internal(vconnp, (char *) port_name, bit, val, e)) {
nc_verb_error("OpenFlow: modification of configuration failed.");
vconn_close(vconnp);
return EXIT_FAILURE;
}
vconn_close(vconnp);
return EXIT_SUCCESS;
}
/* Finds interface with 'name' in OpenFlow 'reply' and returns pointer to it.
* If interface is not found, function returns NULL. 'reply' should be
* prepared by ofc_of_get_ports(). Note that 'reply' still needs to be
* freed. */
static struct ofputil_phy_port *
of_get_port_byname(struct ofpbuf *reply, const char *name)
{
struct ofpbuf b;
static struct ofputil_phy_port pp;
struct ofp_header *oh;
enum ofptype type;
if (reply == NULL) {
return NULL;
}
/* get the beginning of data */
oh = ofpbuf_data(reply);
ofpbuf_use_const(&b, oh, ntohs(oh->length));
if (ofptype_pull(&type, &b) || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
nc_verb_error("OpenFlow: bad reply.");
return NULL;
}
while (!ofputil_pull_phy_port(oh->version, &b, &pp)) {
/* this is the point where we have information about state and
* configuration of interface */
if (!strncmp(pp.name, name, strlen(pp.name) + 1)) {
return &pp;
}
}
return NULL;
}
/*
* Locally stored data
*/
/*
* Set /capable-switch/id
*
* If this value not set, do not provide any access to the configuration data
*/
int
ofc_set_switchid(xmlNodePtr node)
{
xmlChar *id;
if (!node) {
/* delete id */
xmlFree(cs_id);
cs_id = NULL;
return EXIT_SUCCESS;
}
if (!node->children || node->children->type != XML_TEXT_NODE) {
nc_verb_error("%s: invalid id element", __func__);
return EXIT_FAILURE;
}
id = xmlStrdup(node->children->content);
if (!id) {
nc_verb_error("%s: invalid id element content", __func__);
return EXIT_FAILURE;
}
xmlFree(cs_id);
cs_id = id;
return EXIT_SUCCESS;
}
/*
* Read current /capable-switch/id
*/
const xmlChar *
ofc_get_switchid(void)
{
return cs_id;
}
/*
* If key is in the string map s, append the it's value into string,
* otherwise don't append anything.
*
* s string map with data
* key value of key in string map to find
* elem name of element to append into XML
* string dynamic string containing XML
*/
static void
find_and_append_smap_val(const struct smap *s, const char *key,
const char *elem, struct ds *string)
{
const char *value = smap_get(s, key);
if (value != NULL) {
ds_put_format(string, "<%s>%s</%s>", elem, value, elem);
}
}
/* Returns true and sets key of flow-table if found. Otherwise it
* returns false.
*/
static bool
find_flowtable_id(const struct ovsrec_flow_table *flowtable, int64_t *key)
{
const struct ovsrec_bridge *row;
size_t i;
int cmp;
OVSREC_BRIDGE_FOR_EACH(row, ovsdb_handler->idl) {
for (i = 0; i < row->n_flow_tables; i++) {
cmp = uuid_equals(&flowtable->header_.uuid,
&row->value_flow_tables[i]->header_.uuid);
if (cmp) {
/* found */
*key = row->key_flow_tables[i];
return true;
}
}
}
return false;
}
static char *
get_flow_tables_config(void)
{
const struct ovsrec_flow_table *row;
const char *resource_id;
struct ds string;
int64_t table_id;
ds_init(&string);
OVSREC_FLOW_TABLE_FOR_EACH(row, ovsdb_handler->idl) {
resource_id = smap_get(&row->external_ids, OFC_RESOURCE_ID);
if (!find_flowtable_id(row, &table_id)) {
continue;
}
ds_put_format(&string, "<flow-table><table-id>%" PRIi64 "</table-id>",
table_id);
if (resource_id != NULL && strcmp(resource_id, "")) {
ds_put_format(&string, "<resource-id>%s</resource-id>",
resource_id);
}
if (row->name) {
ds_put_format(&string, "<name>%s</name>", row->name);
}
ds_put_format(&string, "</flow-table>");
}
return string.length ? ds_steal_cstr(&string) : NULL;
}
static const struct ovsrec_port *
find_queue_port(const char *rid)
{
const struct ovsrec_port *port;
const char *aux;
int i;
OVSREC_PORT_FOR_EACH(port, ovsdb_handler->idl) {
if (!port->qos) {
continue;
}
for (i = 0; i < port->qos->n_queues; i++) {
aux = smap_get(&port->qos->value_queues[i]->external_ids,
OFC_RESOURCE_ID);
if (aux && !strcmp(aux, rid)) {
return port;
}
}
}
return NULL;
}
static char *
get_queues_config(void)
{
const struct ovsrec_queue *row;
const struct ovsrec_port *port;
struct ds string, aux;
const char *id, *rid;
ds_init(&string);
OVSREC_QUEUE_FOR_EACH(row, ovsdb_handler->idl) {
rid = smap_get(&row->external_ids, OFC_RESOURCE_ID);
if (!rid || (!strcmp(rid, ""))) {
continue;
}
ds_put_format(&string, "<queue><resource-id>%s</resource-id>", rid);
id = smap_get(&row->external_ids, "ofc_id");
if (id) {
ds_put_format(&string, "<id>%s</id>", id);
}
port = find_queue_port(rid);
if (port) {
ds_put_format(&string, "<port>%s</port>", port->name);
}
ds_init(&aux);
find_and_append_smap_val(&row->other_config, "min-rate", "min-rate",
&aux);
find_and_append_smap_val(&row->other_config, "max-rate", "max-rate",
&aux);
find_and_append_smap_val(&row->other_config, "experimenter-id",
"experimenter-id", &aux);
find_and_append_smap_val(&row->other_config, "experimenter-data",
"experimenter-data", &aux);
if (aux.length) {
ds_put_format(&string, "<properties>%s</properties></queue>",
ds_cstr(&aux));
ds_destroy(&aux);
} else {
ds_put_format(&string, "</queue>");
}
}
return string.length ? ds_steal_cstr(&string) : NULL;
}
static void
dump_port_features(struct ds *s, uint32_t mask)
{
int i;
assert(s);
/* dump rate elements */
for (i = 0; i < (sizeof rates) / (sizeof rates[0]); i++) {
if (rates[i].value & mask) {
ds_put_format(s, "<rate>%s</rate>", rates[i].str);
}
}
/* dump auto-negotiate element */
ds_put_format(s, "<auto-negotiate>%s</auto-negotiate>",
ADVERTISED_Autoneg & mask ? "true" : "false");
/* dump medium elements */
for (i = 0; i < (sizeof medium) / (sizeof medium[0]); i++) {
if (medium[i].value & mask) {
ds_put_format(s, "<medium>%s</medium>", medium[i].str);
}
}
/* dump pause element */
if (ADVERTISED_Asym_Pause & mask) {
ds_put_format(s, "<pause>asymmetric</pause>");
} else if (ADVERTISED_Pause & mask) {
ds_put_format(s, "<pause>symmetric</pause>");
} else {
ds_put_format(s, "<pause>unsupported</pause>");
}
}
static char *
get_ports_config(const struct ovsrec_bridge *bridge)
{
const struct ovsrec_interface *row;
size_t port_it, ifc;
struct ds string;
struct vconn *vconnp;
const char *bridge_name = bridge->name;
const char *tunnel_type;
struct ofpbuf *of_ports = NULL;
struct ofputil_phy_port *of_port = NULL;
enum ofputil_port_config c;
struct ethtool_cmd *ecmd;
if (of_open_vconn(bridge_name, &vconnp) == true) {
of_ports = of_get_ports(vconnp);
} else {
nc_verb_error("OpenFlow: could not connect to '%s' bridge.",
bridge_name);
}
ds_init(&string);
/* iterate over all interfaces of all ports */
for (port_it = 0; port_it < bridge->n_ports; port_it++) {
for (ifc = 0; ifc < bridge->ports[port_it]->n_interfaces; ifc++) {
row = bridge->ports[port_it]->interfaces[ifc];
ds_put_format(&string, "<port>");
ds_put_format(&string, "<name>%s</name>", row->name);
if (row->n_ofport_request) {
ds_put_format(&string,
"<requested-number>%"PRIu64"</requested-number>",
row->ofport_request[0]);
}
/* port/configuration/ */
of_port = of_get_port_byname(of_ports, row->name);
if (of_port != NULL) {
c = of_port->config;
ds_put_format(&string, "<configuration>"
"<admin-state>%s</admin-state>"
"<no-receive>%s</no-receive>"
"<no-forward>%s</no-forward>"
"<no-packet-in>%s</no-packet-in>"
"</configuration>",
(c & OFPUTIL_PC_PORT_DOWN ? "down" : "up"),
OFC_PORT_CONF_BIT(c, OFPUTIL_PC_NO_RECV),
OFC_PORT_CONF_BIT(c, OFPUTIL_PC_NO_FWD),
OFC_PORT_CONF_BIT(c, OFPUTIL_PC_NO_PACKET_IN));
} else if (row->admin_state) {
/* port was not found in OpenFlow reply, but admin-state can
* be get directly from OVSDB
*/
ds_put_format(&string, "<configuration>"
"<admin-state>%s</admin-state>"
"</configuration>", row->admin_state);
}
if (dev_is_system(row->name)) {
/* get port/features/ via ioctl() */
ecmd = dev_get_ethtool(row->name);
ds_put_format(&string, "<features><advertised>");
dump_port_features(&string, ecmd->advertising);
ds_put_format(&string, "</advertised></features>");
}
tunnel_type = smap_get(&row->external_ids, "tunnel_type");
if (tunnel_type) {
ds_put_format(&string, "<%s>", tunnel_type);
find_and_append_smap_val(&row->options, "local_ip",
"local-endpoint-ipv4-adress",
&string);
find_and_append_smap_val(&row->options, "remote_ip",
"remote-endpoint-ipv4-adress",
&string);
if (!strcmp(tunnel_type, "ipgre-tunnel")) {
find_and_append_smap_val(&row->options, "csum",
"checksum-present", &string);
find_and_append_smap_val(&row->options, "key", "key",
&string);
} else if (!strcmp(tunnel_type, "vxlan-tunnel")) {
find_and_append_smap_val(&row->options, "key", "vni",
&string);
}
ds_put_format(&string, "</%s>", tunnel_type);
}
ds_put_format(&string, "</port>");
}
}
ofpbuf_delete(of_ports);
vconn_close(vconnp);
return string.length ? ds_steal_cstr(&string) : NULL;
}
static char *
get_flow_tables_state(void)
{
struct ds string;
const struct ovsrec_flow_table *ft;
const char *tid;
ds_init(&string);
OVSREC_FLOW_TABLE_FOR_EACH(ft, ovsdb_handler->idl) {
if (ft->n_flow_limit > 0) {
tid = smap_get(&(ft->external_ids), "table_id");
if (!tid) {
continue;
}
ds_put_format(&string, "<flow-table><table-id>%s</table-id>"
"<max-entries>%ld</max-entries></flow-table>",
tid, ft->flow_limit[0]);
}
}
return string.length ? ds_steal_cstr(&string) : NULL;
}
static char *
get_ports_state(const struct ovsrec_bridge *bridge)
{
const struct ovsrec_interface *row;
size_t port_it, ifc;
struct ds string, aux;
struct vconn *vconnp;
const char *bridge_name = bridge->name;
struct ofpbuf *of_ports = NULL;
struct ofputil_phy_port *of_port = NULL;
const unsigned char norate = 0xff;
if (of_open_vconn(bridge_name, &vconnp) == true) {
of_ports = of_get_ports(vconnp);
} else {
nc_verb_error("OpenFlow: could not connect to '%s' bridge.",
bridge_name);
}
ds_init(&string);
/* iterate over all interfaces of all ports */
for (port_it = 0; port_it < bridge->n_ports; port_it++) {
for (ifc = 0; ifc < bridge->ports[port_it]->n_interfaces; ifc++) {
row = bridge->ports[port_it]->interfaces[ifc];
ds_put_format(&string, "<port>");
ds_put_format(&string, "<name>%s</name>", row->name);
if (row->n_ofport > 0) {
ds_put_format(&string, "<number>%" PRIu64 "</number>",
row->ofport[0]);
}
ds_init(&aux);
if (row->link_state) {
ds_put_format(&aux, "<oper-state>%s</oper-state>",
row->link_state);
}
find_and_append_smap_val(&row->other_config, "stp_state",
"blocked", &aux);
of_port = of_get_port_byname(of_ports, row->name);
if (of_port != NULL) {
ds_put_format(&aux, "<live>%s</live>",
OFC_PORT_CONF_BIT(of_port->state,
OFPUTIL_PS_LIVE));
}
if (aux.length) {
ds_put_format(&string, "<state>%s</state>", aux.string);
ds_destroy(&aux);
}
/* get port/features/ via ioctl() */
struct ethtool_cmd ecmd_;
struct ethtool_cmd *ecmd = &ecmd_;
if (!dev_is_system(row->name)) {
memset(ecmd, 0, sizeof *ecmd);
} else {
ecmd = dev_get_ethtool(row->name);
}
ds_put_format(&string, "<features><current>");
/* rate - get speed and convert it with duplex value to
* OFPortRateType */
switch ((ecmd->speed_hi << 16) | ecmd->speed) {
case 10:
ds_put_format(&string, "<rate>10Mb");
break;
case 100:
ds_put_format(&string, "<rate>100Mb");
break;
case 1000:
ds_put_format(&string, "<rate>1Gb");
break;
case 10000:
ds_put_format(&string, "<rate>10Gb");
/* do not print duplex suffix */
ecmd->duplex = DUPLEX_FULL + 1;
break;
case 40000:
ds_put_format(&string, "<rate>40Gb");
/* do not print duplex suffix */