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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
|
/*
* Copyright (c) 2009, 2010 Nicira Networks.
*
* 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 <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <getopt.h>
#include <inttypes.h>
#include <regex.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "command-line.h"
#include "compiler.h"
#include "dirs.h"
#include "dynamic-string.h"
#include "json.h"
#include "ovsdb-data.h"
#include "ovsdb-idl.h"
#include "poll-loop.h"
#include "process.h"
#include "svec.h"
#include "vswitchd/vswitch-idl.h"
#include "timeval.h"
#include "util.h"
#include "vlog.h"
#define THIS_MODULE VLM_vsctl
/* vsctl_fatal() also logs the error, so it is preferred in this file. */
#define ovs_fatal please_use_vsctl_fatal_instead_of_ovs_fatal
struct vsctl_context;
typedef void vsctl_handler_func(struct vsctl_context *);
struct vsctl_command_syntax {
const char *name;
int min_args;
int max_args;
vsctl_handler_func *run;
vsctl_handler_func *postprocess;
const char *options;
};
struct vsctl_command {
/* Data that remains constant after initialization. */
const struct vsctl_command_syntax *syntax;
int argc;
char **argv;
struct shash options;
/* Data modified by commands. */
struct ds output;
};
/* --db: The database server to contact. */
static const char *db;
/* --oneline: Write each command's output as a single line? */
static bool oneline;
/* --dry-run: Do not commit any changes. */
static bool dry_run;
/* --no-wait: Wait for ovs-vswitchd to reload its configuration? */
static bool wait_for_reload = true;
/* --timeout: Time to wait for a connection to 'db'. */
static int timeout = 5;
/* All supported commands. */
static const struct vsctl_command_syntax all_commands[];
static void vsctl_fatal(const char *, ...) PRINTF_FORMAT(1, 2) NO_RETURN;
static char *default_db(void);
static void usage(void) NO_RETURN;
static void parse_options(int argc, char *argv[]);
static struct vsctl_command *parse_commands(int argc, char *argv[],
size_t *n_commandsp);
static void parse_command(int argc, char *argv[], struct vsctl_command *);
static void do_vsctl(const char *args,
struct vsctl_command *, size_t n_commands,
struct ovsdb_idl *);
int
main(int argc, char *argv[])
{
struct ovsdb_idl *idl;
unsigned int seqno;
struct vsctl_command *commands;
size_t n_commands;
char *args;
int trials;
set_program_name(argv[0]);
signal(SIGPIPE, SIG_IGN);
time_init();
vlog_init();
vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
/* Log our arguments. This is often valuable for debugging systems. */
args = process_escape_args(argv);
VLOG_INFO("Called as %s", args);
/* Parse command line. */
parse_options(argc, argv);
commands = parse_commands(argc - optind, argv + optind, &n_commands);
if (timeout) {
time_alarm(timeout);
}
/* Do basic command syntax checking. */
/* Now execute the commands. */
idl = ovsdb_idl_create(db, &ovsrec_idl_class);
seqno = ovsdb_idl_get_seqno(idl);
trials = 0;
for (;;) {
unsigned int new_seqno;
ovsdb_idl_run(idl);
new_seqno = ovsdb_idl_get_seqno(idl);
if (new_seqno != seqno) {
if (++trials > 5) {
vsctl_fatal("too many database inconsistency failures");
}
do_vsctl(args, commands, n_commands, idl);
seqno = new_seqno;
}
ovsdb_idl_wait(idl);
poll_block();
}
}
static void
parse_options(int argc, char *argv[])
{
enum {
OPT_DB = UCHAR_MAX + 1,
OPT_ONELINE,
OPT_NO_SYSLOG,
OPT_NO_WAIT,
OPT_DRY_RUN,
VLOG_OPTION_ENUMS
};
static struct option long_options[] = {
{"db", required_argument, 0, OPT_DB},
{"no-syslog", no_argument, 0, OPT_NO_SYSLOG},
{"no-wait", no_argument, 0, OPT_NO_WAIT},
{"dry-run", no_argument, 0, OPT_DRY_RUN},
{"oneline", no_argument, 0, OPT_ONELINE},
{"timeout", required_argument, 0, 't'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
VLOG_LONG_OPTIONS,
{0, 0, 0, 0},
};
for (;;) {
int c;
c = getopt_long(argc, argv, "+v::hVt:", long_options, NULL);
if (c == -1) {
break;
}
switch (c) {
case OPT_DB:
db = optarg;
break;
case OPT_ONELINE:
oneline = true;
break;
case OPT_NO_SYSLOG:
vlog_set_levels(VLM_vsctl, VLF_SYSLOG, VLL_WARN);
break;
case OPT_NO_WAIT:
wait_for_reload = false;
break;
case OPT_DRY_RUN:
dry_run = true;
break;
case 'h':
usage();
case 'V':
OVS_PRINT_VERSION(0, 0);
exit(EXIT_SUCCESS);
case 't':
timeout = strtoul(optarg, NULL, 10);
if (timeout < 0) {
vsctl_fatal("value %s on -t or --timeout is invalid",
optarg);
}
break;
VLOG_OPTION_HANDLERS
case '?':
exit(EXIT_FAILURE);
default:
abort();
}
}
if (!db) {
db = default_db();
}
}
static struct vsctl_command *
parse_commands(int argc, char *argv[], size_t *n_commandsp)
{
struct vsctl_command *commands;
size_t n_commands, allocated_commands;
int i, start;
commands = NULL;
n_commands = allocated_commands = 0;
for (start = i = 0; i <= argc; i++) {
if (i == argc || !strcmp(argv[i], "--")) {
if (i > start) {
if (n_commands >= allocated_commands) {
struct vsctl_command *c;
commands = x2nrealloc(commands, &allocated_commands,
sizeof *commands);
for (c = commands; c < &commands[n_commands]; c++) {
shash_moved(&c->options);
}
}
parse_command(i - start, &argv[start],
&commands[n_commands++]);
}
start = i + 1;
}
}
if (!n_commands) {
vsctl_fatal("missing command name (use --help for help)");
}
*n_commandsp = n_commands;
return commands;
}
static void
parse_command(int argc, char *argv[], struct vsctl_command *command)
{
const struct vsctl_command_syntax *p;
int i;
shash_init(&command->options);
for (i = 0; i < argc; i++) {
if (argv[i][0] != '-') {
break;
}
if (!shash_add_once(&command->options, argv[i], NULL)) {
vsctl_fatal("'%s' option specified multiple times", argv[i]);
}
}
if (i == argc) {
vsctl_fatal("missing command name");
}
for (p = all_commands; p->name; p++) {
if (!strcmp(p->name, argv[i])) {
struct shash_node *node;
int n_arg;
SHASH_FOR_EACH (node, &command->options) {
const char *s = strstr(p->options, node->name);
int end = s ? s[strlen(node->name)] : EOF;
if (end != ',' && end != ' ' && end != '\0') {
vsctl_fatal("'%s' command has no '%s' option",
argv[i], node->name);
}
}
n_arg = argc - i - 1;
if (n_arg < p->min_args) {
vsctl_fatal("'%s' command requires at least %d arguments",
p->name, p->min_args);
} else if (n_arg > p->max_args) {
vsctl_fatal("'%s' command takes at most %d arguments",
p->name, p->max_args);
} else {
command->syntax = p;
command->argc = n_arg + 1;
command->argv = &argv[i];
return;
}
}
}
vsctl_fatal("unknown command '%s'; use --help for help", argv[i]);
}
static void
vsctl_fatal(const char *format, ...)
{
char *message;
va_list args;
va_start(args, format);
message = xvasprintf(format, args);
va_end(args);
vlog_set_levels(VLM_vsctl, VLF_CONSOLE, VLL_EMER);
VLOG_ERR("%s", message);
ovs_error(0, "%s", message);
exit(EXIT_FAILURE);
}
static void
usage(void)
{
printf("\
%s: ovs-vswitchd management utility\n\
usage: %s [OPTIONS] COMMAND [ARG...]\n\
\n\
Bridge commands:\n\
add-br BRIDGE create a new bridge named BRIDGE\n\
add-br BRIDGE PARENT VLAN create new fake BRIDGE in PARENT on VLAN\n\
del-br BRIDGE delete BRIDGE and all of its ports\n\
list-br print the names of all the bridges\n\
br-exists BRIDGE test whether BRIDGE exists\n\
br-to-vlan BRIDGE print the VLAN which BRIDGE is on\n\
br-to-parent BRIDGE print the parent of BRIDGE\n\
br-set-external-id BRIDGE KEY VALUE set KEY on BRIDGE to VALUE\n\
br-set-external-id BRIDGE KEY unset KEY on BRIDGE\n\
br-get-external-id BRIDGE KEY print value of KEY on BRIDGE\n\
br-get-external-id BRIDGE list key-value pairs on BRIDGE\n\
\n\
Port commands:\n\
list-ports BRIDGE print the names of all the ports on BRIDGE\n\
add-port BRIDGE PORT add network device PORT to BRIDGE\n\
add-bond BRIDGE PORT IFACE... add bonded port PORT in BRIDGE from IFACES\n\
del-port [BRIDGE] PORT delete PORT (which may be bonded) from BRIDGE\n\
port-to-br PORT print name of bridge that contains PORT\n\
A bond is considered to be a single port.\n\
\n\
Interface commands (a bond consists of multiple interfaces):\n\
list-ifaces BRIDGE print the names of all interfaces on BRIDGE\n\
iface-to-br IFACE print name of bridge that contains IFACE\n\
\n\
Controller commands:\n\
get-controller [BRIDGE] print the controller for BRIDGE\n\
del-controller [BRIDGE] delete the controller for BRIDGE\n\
set-controller [BRIDGE] TARGET set the controller for BRIDGE to TARGET\n\
get-fail-mode [BRIDGE] print the fail-mode for BRIDGE\n\
del-fail-mode [BRIDGE] delete the fail-mode for BRIDGE\n\
set-fail-mode [BRIDGE] MODE set the fail-mode for BRIDGE to MODE\n\
\n\
SSL commands:\n\
get-ssl print the SSL configuration\n\
del-ssl delete the SSL configuration\n\
set-ssl PRIV-KEY CERT CA-CERT set the SSL configuration\n\
\n\
Database commands:\n\
list TBL [REC] list RECord (or all records) in TBL\n\
get TBL REC COL[:KEY] print values of COLumns in RECORD in TBL\n\
set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
remove TBL REC COL [KEY=]VALUE remove (KEY=)VALUE from COLumn\n\
clear TBL REC COL clear values from COLumn in RECord in TBL\n\
create TBL COL[:KEY]=VALUE create and initialize new record\n\
destroy TBL REC delete REC from TBL\n\
Potentially unsafe database commands require --force option.\n\
\n\
Options:\n\
--db=DATABASE connect to DATABASE\n\
(default: %s)\n\
--oneline print exactly one line of output per command\n",
program_name, program_name, default_db());
vlog_usage();
printf("\n\
Other options:\n\
-h, --help display this help message\n\
-V, --version display version information\n");
exit(EXIT_SUCCESS);
}
static char *
default_db(void)
{
static char *def;
if (!def) {
def = xasprintf("unix:%s/ovsdb-server", ovs_rundir);
}
return def;
}
struct vsctl_context {
/* Read-only. */
int argc;
char **argv;
struct shash options;
/* Modifiable state. */
struct ds output;
struct ovsdb_idl *idl;
struct ovsdb_idl_txn *txn;
const struct ovsrec_open_vswitch *ovs;
};
struct vsctl_bridge {
struct ovsrec_bridge *br_cfg;
char *name;
struct ovsrec_controller *ctrl;
struct vsctl_bridge *parent;
int vlan;
};
struct vsctl_port {
struct ovsrec_port *port_cfg;
struct vsctl_bridge *bridge;
};
struct vsctl_iface {
struct ovsrec_interface *iface_cfg;
struct vsctl_port *port;
};
struct vsctl_info {
struct shash bridges;
struct shash ports;
struct shash ifaces;
struct ovsrec_controller *ctrl;
};
static struct vsctl_bridge *
add_bridge(struct vsctl_info *b,
struct ovsrec_bridge *br_cfg, const char *name,
struct vsctl_bridge *parent, int vlan)
{
struct vsctl_bridge *br = xmalloc(sizeof *br);
br->br_cfg = br_cfg;
br->name = xstrdup(name);
br->parent = parent;
br->vlan = vlan;
br->ctrl = parent ? parent->br_cfg->controller : br_cfg->controller;
shash_add(&b->bridges, br->name, br);
return br;
}
static bool
port_is_fake_bridge(const struct ovsrec_port *port_cfg)
{
return (port_cfg->fake_bridge
&& port_cfg->tag
&& *port_cfg->tag >= 1 && *port_cfg->tag <= 4095);
}
static struct vsctl_bridge *
find_vlan_bridge(struct vsctl_info *info,
struct vsctl_bridge *parent, int vlan)
{
struct shash_node *node;
SHASH_FOR_EACH (node, &info->bridges) {
struct vsctl_bridge *br = node->data;
if (br->parent == parent && br->vlan == vlan) {
return br;
}
}
return NULL;
}
static void
free_info(struct vsctl_info *info)
{
struct shash_node *node;
SHASH_FOR_EACH (node, &info->bridges) {
struct vsctl_bridge *bridge = node->data;
free(bridge->name);
free(bridge);
}
shash_destroy(&info->bridges);
SHASH_FOR_EACH (node, &info->ports) {
struct vsctl_port *port = node->data;
free(port);
}
shash_destroy(&info->ports);
SHASH_FOR_EACH (node, &info->ifaces) {
struct vsctl_iface *iface = node->data;
free(iface);
}
shash_destroy(&info->ifaces);
}
static void
get_info(const struct ovsrec_open_vswitch *ovs, struct vsctl_info *info)
{
struct shash bridges, ports;
size_t i;
shash_init(&info->bridges);
shash_init(&info->ports);
shash_init(&info->ifaces);
info->ctrl = ovs->controller;
shash_init(&bridges);
shash_init(&ports);
for (i = 0; i < ovs->n_bridges; i++) {
struct ovsrec_bridge *br_cfg = ovs->bridges[i];
struct vsctl_bridge *br;
size_t j;
if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
VLOG_WARN("%s: database contains duplicate bridge name",
br_cfg->name);
continue;
}
br = add_bridge(info, br_cfg, br_cfg->name, NULL, 0);
if (!br) {
continue;
}
for (j = 0; j < br_cfg->n_ports; j++) {
struct ovsrec_port *port_cfg = br_cfg->ports[j];
if (!shash_add_once(&ports, port_cfg->name, NULL)) {
VLOG_WARN("%s: database contains duplicate port name",
port_cfg->name);
continue;
}
if (port_is_fake_bridge(port_cfg)
&& shash_add_once(&bridges, port_cfg->name, NULL)) {
add_bridge(info, NULL, port_cfg->name, br, *port_cfg->tag);
}
}
}
shash_destroy(&bridges);
shash_destroy(&ports);
shash_init(&bridges);
shash_init(&ports);
for (i = 0; i < ovs->n_bridges; i++) {
struct ovsrec_bridge *br_cfg = ovs->bridges[i];
struct vsctl_bridge *br;
size_t j;
if (!shash_add_once(&bridges, br_cfg->name, NULL)) {
continue;
}
br = shash_find_data(&info->bridges, br_cfg->name);
for (j = 0; j < br_cfg->n_ports; j++) {
struct ovsrec_port *port_cfg = br_cfg->ports[j];
struct vsctl_port *port;
size_t k;
if (!shash_add_once(&ports, port_cfg->name, NULL)) {
continue;
}
if (port_is_fake_bridge(port_cfg)
&& !shash_add_once(&bridges, port_cfg->name, NULL)) {
continue;
}
port = xmalloc(sizeof *port);
port->port_cfg = port_cfg;
if (port_cfg->tag
&& *port_cfg->tag >= 1 && *port_cfg->tag <= 4095) {
port->bridge = find_vlan_bridge(info, br, *port_cfg->tag);
if (!port->bridge) {
port->bridge = br;
}
} else {
port->bridge = br;
}
shash_add(&info->ports, port_cfg->name, port);
for (k = 0; k < port_cfg->n_interfaces; k++) {
struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
struct vsctl_iface *iface;
if (shash_find(&info->ifaces, iface_cfg->name)) {
VLOG_WARN("%s: database contains duplicate interface name",
iface_cfg->name);
continue;
}
iface = xmalloc(sizeof *iface);
iface->iface_cfg = iface_cfg;
iface->port = port;
shash_add(&info->ifaces, iface_cfg->name, iface);
}
}
}
shash_destroy(&bridges);
shash_destroy(&ports);
}
static void
check_conflicts(struct vsctl_info *info, const char *name,
char *msg)
{
struct vsctl_iface *iface;
struct vsctl_port *port;
if (shash_find(&info->bridges, name)) {
vsctl_fatal("%s because a bridge named %s already exists",
msg, name);
}
port = shash_find_data(&info->ports, name);
if (port) {
vsctl_fatal("%s because a port named %s already exists on "
"bridge %s", msg, name, port->bridge->name);
}
iface = shash_find_data(&info->ifaces, name);
if (iface) {
vsctl_fatal("%s because an interface named %s already exists "
"on bridge %s", msg, name, iface->port->bridge->name);
}
free(msg);
}
static struct vsctl_bridge *
find_bridge(struct vsctl_info *info, const char *name, bool must_exist)
{
struct vsctl_bridge *br = shash_find_data(&info->bridges, name);
if (must_exist && !br) {
vsctl_fatal("no bridge named %s", name);
}
return br;
}
static struct vsctl_bridge *
find_real_bridge(struct vsctl_info *info, const char *name, bool must_exist)
{
struct vsctl_bridge *br = find_bridge(info, name, must_exist);
if (br && br->parent) {
vsctl_fatal("%s is a fake bridge", name);
}
return br;
}
static struct vsctl_port *
find_port(struct vsctl_info *info, const char *name, bool must_exist)
{
struct vsctl_port *port = shash_find_data(&info->ports, name);
if (port && !strcmp(name, port->bridge->name)) {
port = NULL;
}
if (must_exist && !port) {
vsctl_fatal("no port named %s", name);
}
return port;
}
static struct vsctl_iface *
find_iface(struct vsctl_info *info, const char *name, bool must_exist)
{
struct vsctl_iface *iface = shash_find_data(&info->ifaces, name);
if (iface && !strcmp(name, iface->port->bridge->name)) {
iface = NULL;
}
if (must_exist && !iface) {
vsctl_fatal("no interface named %s", name);
}
return iface;
}
static void
bridge_insert_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
{
struct ovsrec_port **ports;
size_t i;
ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
for (i = 0; i < br->n_ports; i++) {
ports[i] = br->ports[i];
}
ports[br->n_ports] = port;
ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
free(ports);
}
static void
bridge_delete_port(struct ovsrec_bridge *br, struct ovsrec_port *port)
{
struct ovsrec_port **ports;
size_t i, n;
ports = xmalloc(sizeof *br->ports * br->n_ports);
for (i = n = 0; i < br->n_ports; i++) {
if (br->ports[i] != port) {
ports[n++] = br->ports[i];
}
}
ovsrec_bridge_set_ports(br, ports, n);
free(ports);
}
static void
ovs_insert_bridge(const struct ovsrec_open_vswitch *ovs,
struct ovsrec_bridge *bridge)
{
struct ovsrec_bridge **bridges;
size_t i;
bridges = xmalloc(sizeof *ovs->bridges * (ovs->n_bridges + 1));
for (i = 0; i < ovs->n_bridges; i++) {
bridges[i] = ovs->bridges[i];
}
bridges[ovs->n_bridges] = bridge;
ovsrec_open_vswitch_set_bridges(ovs, bridges, ovs->n_bridges + 1);
free(bridges);
}
static void
ovs_delete_bridge(const struct ovsrec_open_vswitch *ovs,
struct ovsrec_bridge *bridge)
{
struct ovsrec_bridge **bridges;
size_t i, n;
bridges = xmalloc(sizeof *ovs->bridges * ovs->n_bridges);
for (i = n = 0; i < ovs->n_bridges; i++) {
if (ovs->bridges[i] != bridge) {
bridges[n++] = ovs->bridges[i];
}
}
ovsrec_open_vswitch_set_bridges(ovs, bridges, n);
free(bridges);
}
static void
cmd_init(struct vsctl_context *ctx UNUSED)
{
}
static void
cmd_add_br(struct vsctl_context *ctx)
{
const char *br_name = ctx->argv[1];
struct vsctl_info info;
get_info(ctx->ovs, &info);
check_conflicts(&info, br_name,
xasprintf("cannot create a bridge named %s", br_name));
if (ctx->argc == 2) {
struct ovsrec_bridge *br;
struct ovsrec_port *port;
struct ovsrec_interface *iface;
iface = ovsrec_interface_insert(ctx->txn);
ovsrec_interface_set_name(iface, br_name);
port = ovsrec_port_insert(ctx->txn);
ovsrec_port_set_name(port, br_name);
ovsrec_port_set_interfaces(port, &iface, 1);
br = ovsrec_bridge_insert(ctx->txn);
ovsrec_bridge_set_name(br, br_name);
ovsrec_bridge_set_ports(br, &port, 1);
ovs_insert_bridge(ctx->ovs, br);
} else if (ctx->argc == 3) {
vsctl_fatal("'%s' command takes exactly 1 or 3 arguments",
ctx->argv[0]);
} else if (ctx->argc == 4) {
const char *parent_name = ctx->argv[2];
int vlan = atoi(ctx->argv[3]);
struct ovsrec_bridge *br;
struct vsctl_bridge *parent;
struct ovsrec_port *port;
struct ovsrec_interface *iface;
int64_t tag = vlan;
if (vlan < 1 || vlan > 4095) {
vsctl_fatal("%s: vlan must be between 1 and 4095", ctx->argv[0]);
}
parent = find_bridge(&info, parent_name, false);
if (parent && parent->vlan) {
vsctl_fatal("cannot create bridge with fake bridge as parent");
}
if (!parent) {
vsctl_fatal("parent bridge %s does not exist", parent_name);
}
br = parent->br_cfg;
iface = ovsrec_interface_insert(ctx->txn);
ovsrec_interface_set_name(iface, br_name);
ovsrec_interface_set_type(iface, "internal");
port = ovsrec_port_insert(ctx->txn);
ovsrec_port_set_name(port, br_name);
ovsrec_port_set_interfaces(port, &iface, 1);
ovsrec_port_set_fake_bridge(port, true);
ovsrec_port_set_tag(port, &tag, 1);
bridge_insert_port(br, port);
} else {
NOT_REACHED();
}
free_info(&info);
}
static void
del_port(struct vsctl_info *info, struct vsctl_port *port)
{
struct shash_node *node;
SHASH_FOR_EACH (node, &info->ifaces) {
struct vsctl_iface *iface = node->data;
if (iface->port == port) {
ovsrec_interface_delete(iface->iface_cfg);
}
}
ovsrec_port_delete(port->port_cfg);
bridge_delete_port((port->bridge->parent
? port->bridge->parent->br_cfg
: port->bridge->br_cfg), port->port_cfg);
}
static void
cmd_del_br(struct vsctl_context *ctx)
{
bool must_exist = !shash_find(&ctx->options, "--if-exists");
struct vsctl_bridge *bridge;
struct vsctl_info info;
get_info(ctx->ovs, &info);
bridge = find_bridge(&info, ctx->argv[1], must_exist);
if (bridge) {
struct shash_node *node;
SHASH_FOR_EACH (node, &info.ports) {
struct vsctl_port *port = node->data;
if (port->bridge == bridge
|| !strcmp(port->port_cfg->name, bridge->name)) {
del_port(&info, port);
}
}
if (bridge->br_cfg) {
ovsrec_bridge_delete(bridge->br_cfg);
ovs_delete_bridge(ctx->ovs, bridge->br_cfg);
}
}
free_info(&info);
}
static void
output_sorted(struct svec *svec, struct ds *output)
{
const char *name;
size_t i;
svec_sort(svec);
SVEC_FOR_EACH (i, name, svec) {
ds_put_format(output, "%s\n", name);
}
}
static void
cmd_list_br(struct vsctl_context *ctx)
{
struct shash_node *node;
struct vsctl_info info;
struct svec bridges;
get_info(ctx->ovs, &info);
svec_init(&bridges);
SHASH_FOR_EACH (node, &info.bridges) {
struct vsctl_bridge *br = node->data;
svec_add(&bridges, br->name);
}
output_sorted(&bridges, &ctx->output);
svec_destroy(&bridges);
free_info(&info);
}
static void
cmd_br_exists(struct vsctl_context *ctx)
{
struct vsctl_info info;
get_info(ctx->ovs, &info);
if (!find_bridge(&info, ctx->argv[1], false)) {
exit(2);
}
free_info(&info);
}
/* Returns true if 'b_prefix' (of length 'b_prefix_len') concatenated with 'b'
* equals 'a', false otherwise. */
static bool
key_matches(const char *a,
const char *b_prefix, size_t b_prefix_len, const char *b)
{
return !strncmp(a, b_prefix, b_prefix_len) && !strcmp(a + b_prefix_len, b);
}
static void
set_external_id(char **old_keys, char **old_values, size_t old_n,
char *key, char *value,
char ***new_keysp, char ***new_valuesp, size_t *new_np)
{
char **new_keys;
char **new_values;
size_t new_n;
size_t i;
new_keys = xmalloc(sizeof *new_keys * (old_n + 1));
new_values = xmalloc(sizeof *new_values * (old_n + 1));
new_n = 0;
for (i = 0; i < old_n; i++) {
if (strcmp(key, old_keys[i])) {
new_keys[new_n] = old_keys[i];
new_values[new_n] = old_values[i];
new_n++;
}
}
if (value) {
new_keys[new_n] = key;
new_values[new_n] = value;
new_n++;
}
*new_keysp = new_keys;
*new_valuesp = new_values;
*new_np = new_n;
}
static void
cmd_br_set_external_id(struct vsctl_context *ctx)
{
struct vsctl_info info;
struct vsctl_bridge *bridge;
char **keys, **values;
size_t n;
get_info(ctx->ovs, &info);
bridge = find_bridge(&info, ctx->argv[1], true);
if (bridge->br_cfg) {
set_external_id(bridge->br_cfg->key_external_ids,
bridge->br_cfg->value_external_ids,
bridge->br_cfg->n_external_ids,
ctx->argv[2], ctx->argc >= 4 ? ctx->argv[3] : NULL,
&keys, &values, &n);
ovsrec_bridge_set_external_ids(bridge->br_cfg, keys, values, n);
} else {
char *key = xasprintf("fake-bridge-%s", ctx->argv[2]);
struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
set_external_id(port->port_cfg->key_external_ids,
port->port_cfg->value_external_ids,
port->port_cfg->n_external_ids,
key, ctx->argc >= 4 ? ctx->argv[3] : NULL,
&keys, &values, &n);
ovsrec_port_set_external_ids(port->port_cfg, keys, values, n);
free(key);
}
free(keys);
free(values);
free_info(&info);
}
static void
get_external_id(char **keys, char **values, size_t n,
const char *prefix, const char *key,
struct ds *output)
{
size_t prefix_len = strlen(prefix);
struct svec svec;
size_t i;
svec_init(&svec);
for (i = 0; i < n; i++) {
if (!key && !strncmp(keys[i], prefix, prefix_len)) {
svec_add_nocopy(&svec, xasprintf("%s=%s",
keys[i] + prefix_len, values[i]));
} else if (key_matches(keys[i], prefix, prefix_len, key)) {
svec_add(&svec, values[i]);
break;
}
}
output_sorted(&svec, output);
svec_destroy(&svec);
}
static void
cmd_br_get_external_id(struct vsctl_context *ctx)
{
struct vsctl_info info;
struct vsctl_bridge *bridge;
get_info(ctx->ovs, &info);
bridge = find_bridge(&info, ctx->argv[1], true);
if (bridge->br_cfg) {
get_external_id(bridge->br_cfg->key_external_ids,
bridge->br_cfg->value_external_ids,
bridge->br_cfg->n_external_ids,
"", ctx->argc >= 3 ? ctx->argv[2] : NULL,
&ctx->output);
} else {
struct vsctl_port *port = shash_find_data(&info.ports, ctx->argv[1]);
get_external_id(port->port_cfg->key_external_ids,
port->port_cfg->value_external_ids,
port->port_cfg->n_external_ids,
"fake-bridge-", ctx->argc >= 3 ? ctx->argv[2] : NULL, &ctx->output);
}
free_info(&info);
}
static void
cmd_list_ports(struct vsctl_context *ctx)
{
struct vsctl_bridge *br;
struct shash_node *node;
struct vsctl_info info;
struct svec ports;
get_info(ctx->ovs, &info);
br = find_bridge(&info, ctx->argv[1], true);
svec_init(&ports);
SHASH_FOR_EACH (node, &info.ports) {
struct vsctl_port *port = node->data;
if (strcmp(port->port_cfg->name, br->name) && br == port->bridge) {
svec_add(&ports, port->port_cfg->name);
}
}
output_sorted(&ports, &ctx->output);
svec_destroy(&ports);
free_info(&info);
}
static void
add_port(struct vsctl_context *ctx,
const char *br_name, const char *port_name, bool fake_iface,
char *iface_names[], int n_ifaces)
{
struct vsctl_info info;
struct vsctl_bridge *bridge;
struct ovsrec_interface **ifaces;
struct ovsrec_port *port;
size_t i;
get_info(ctx->ovs, &info);
check_conflicts(&info, port_name,
xasprintf("cannot create a port named %s", port_name));
/* XXX need to check for conflicts on interfaces too */
bridge = find_bridge(&info, br_name, true);
ifaces = xmalloc(n_ifaces * sizeof *ifaces);
for (i = 0; i < n_ifaces; i++) {
ifaces[i] = ovsrec_interface_insert(ctx->txn);
ovsrec_interface_set_name(ifaces[i], iface_names[i]);
}
port = ovsrec_port_insert(ctx->txn);
ovsrec_port_set_name(port, port_name);
ovsrec_port_set_interfaces(port, ifaces, n_ifaces);
ovsrec_port_set_bond_fake_iface(port, fake_iface);
free(ifaces);
if (bridge->vlan) {
int64_t tag = bridge->vlan;
ovsrec_port_set_tag(port, &tag, 1);
}
bridge_insert_port((bridge->parent ? bridge->parent->br_cfg
: bridge->br_cfg), port);
free_info(&info);
}
static void
cmd_add_port(struct vsctl_context *ctx)
{
add_port(ctx, ctx->argv[1], ctx->argv[2], false, &ctx->argv[2], 1);
}
static void
cmd_add_bond(struct vsctl_context *ctx)
{
bool fake_iface = shash_find(&ctx->options, "--fake-iface");
add_port(ctx, ctx->argv[1], ctx->argv[2], fake_iface,
&ctx->argv[3], ctx->argc - 3);
}
static void
cmd_del_port(struct vsctl_context *ctx)
{
bool must_exist = !shash_find(&ctx->options, "--if-exists");
struct vsctl_info info;
get_info(ctx->ovs, &info);
if (ctx->argc == 2) {
struct vsctl_port *port = find_port(&info, ctx->argv[1], must_exist);
if (port) {
del_port(&info, port);
}
} else if (ctx->argc == 3) {
struct vsctl_bridge *bridge = find_bridge(&info, ctx->argv[1], true);
struct vsctl_port *port = find_port(&info, ctx->argv[2], must_exist);
if (port) {
if (port->bridge == bridge) {
del_port(&info, port);
} else if (port->bridge->parent == bridge) {
vsctl_fatal("bridge %s does not have a port %s (although its "
"parent bridge %s does)",
ctx->argv[1], ctx->argv[2], bridge->parent->name);
} else {
vsctl_fatal("bridge %s does not have a port %s",
ctx->argv[1], ctx->argv[2]);
}
}
}
free_info(&info);
}
static void
cmd_port_to_br(struct vsctl_context *ctx)
{
struct vsctl_port *port;
struct vsctl_info info;
get_info(ctx->ovs, &info);
port = find_port(&info, ctx->argv[1], true);
ds_put_format(&ctx->output, "%s\n", port->bridge->name);
free_info(&info);
}
static void
cmd_br_to_vlan(struct vsctl_context *ctx)
{
struct vsctl_bridge *bridge;
struct vsctl_info info;
get_info(ctx->ovs, &info);
bridge = find_bridge(&info, ctx->argv[1], true);
ds_put_format(&ctx->output, "%d\n", bridge->vlan);
free_info(&info);
}
static void
cmd_br_to_parent(struct vsctl_context *ctx)
{
struct vsctl_bridge *bridge;
struct vsctl_info info;
get_info(ctx->ovs, &info);
bridge = find_bridge(&info, ctx->argv[1], true);
if (bridge->parent) {
bridge = bridge->parent;
}
ds_put_format(&ctx->output, "%s\n", bridge->name);
free_info(&info);
}
static void
cmd_list_ifaces(struct vsctl_context *ctx)
{
struct vsctl_bridge *br;
struct shash_node *node;
struct vsctl_info info;
struct svec ifaces;
get_info(ctx->ovs, &info);
br = find_bridge(&info, ctx->argv[1], true);
svec_init(&ifaces);
SHASH_FOR_EACH (node, &info.ifaces) {
struct vsctl_iface *iface = node->data;
if (strcmp(iface->iface_cfg->name, br->name)
&& br == iface->port->bridge) {
svec_add(&ifaces, iface->iface_cfg->name);
}
}
output_sorted(&ifaces, &ctx->output);
svec_destroy(&ifaces);
free_info(&info);
}
static void
cmd_iface_to_br(struct vsctl_context *ctx)
{
struct vsctl_iface *iface;
struct vsctl_info info;
get_info(ctx->ovs, &info);
iface = find_iface(&info, ctx->argv[1], true);
ds_put_format(&ctx->output, "%s\n", iface->port->bridge->name);
free_info(&info);
}
static void
cmd_get_controller(struct vsctl_context *ctx)
{
struct vsctl_info info;
get_info(ctx->ovs, &info);
if (ctx->argc == 1) {
/* Return the controller from the "Open_vSwitch" table */
if (info.ctrl) {
ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
}
} else {
/* Return the controller for a particular bridge. */
struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
/* If no controller is explicitly defined for the requested
* bridge, fallback to the "Open_vSwitch" table's controller. */
if (br->ctrl) {
ds_put_format(&ctx->output, "%s\n", br->ctrl->target);
} else if (info.ctrl) {
ds_put_format(&ctx->output, "%s\n", info.ctrl->target);
}
}
free_info(&info);
}
static void
cmd_del_controller(struct vsctl_context *ctx)
{
struct vsctl_info info;
get_info(ctx->ovs, &info);
if (ctx->argc == 1) {
if (info.ctrl) {
ovsrec_controller_delete(info.ctrl);
ovsrec_open_vswitch_set_controller(ctx->ovs, NULL);
}
} else {
struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
if (br->ctrl) {
ovsrec_controller_delete(br->ctrl);
ovsrec_bridge_set_controller(br->br_cfg, NULL);
}
}
free_info(&info);
}
static void
cmd_set_controller(struct vsctl_context *ctx)
{
struct vsctl_info info;
struct ovsrec_controller *ctrl;
get_info(ctx->ovs, &info);
if (ctx->argc == 2) {
/* Set the controller in the "Open_vSwitch" table. */
if (info.ctrl) {
ovsrec_controller_delete(info.ctrl);
}
ctrl = ovsrec_controller_insert(ctx->txn);
ovsrec_controller_set_target(ctrl, ctx->argv[1]);
ovsrec_open_vswitch_set_controller(ctx->ovs, ctrl);
} else {
/* Set the controller for a particular bridge. */
struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
if (br->ctrl) {
ovsrec_controller_delete(br->ctrl);
}
ctrl = ovsrec_controller_insert(ctx->txn);
ovsrec_controller_set_target(ctrl, ctx->argv[2]);
ovsrec_bridge_set_controller(br->br_cfg, ctrl);
}
free_info(&info);
}
static void
cmd_get_fail_mode(struct vsctl_context *ctx)
{
struct vsctl_info info;
const char *fail_mode = NULL;
get_info(ctx->ovs, &info);
if (ctx->argc == 1) {
/* Return the fail-mode from the "Open_vSwitch" table */
if (info.ctrl && info.ctrl->fail_mode) {
fail_mode = info.ctrl->fail_mode;
}
} else {
/* Return the fail-mode for a particular bridge. */
struct vsctl_bridge *br = find_bridge(&info, ctx->argv[1], true);
/* If no controller or fail-mode is explicitly defined for the
* requested bridge, fallback to the "Open_vSwitch" table's
* setting. */
if (br->ctrl && br->ctrl->fail_mode) {
fail_mode = br->ctrl->fail_mode;
} else if (info.ctrl && info.ctrl->fail_mode) {
fail_mode = info.ctrl->fail_mode;
}
}
if (fail_mode && strlen(fail_mode)) {
ds_put_format(&ctx->output, "%s\n", fail_mode);
}
free_info(&info);
}
static void
cmd_del_fail_mode(struct vsctl_context *ctx)
{
struct vsctl_info info;
get_info(ctx->ovs, &info);
if (ctx->argc == 1) {
if (info.ctrl && info.ctrl->fail_mode) {
ovsrec_controller_set_fail_mode(info.ctrl, NULL);
}
} else {
struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
if (br->ctrl && br->ctrl->fail_mode) {
ovsrec_controller_set_fail_mode(br->ctrl, NULL);
}
}
free_info(&info);
}
static void
cmd_set_fail_mode(struct vsctl_context *ctx)
{
struct vsctl_info info;
const char *fail_mode;
get_info(ctx->ovs, &info);
fail_mode = (ctx->argc == 2) ? ctx->argv[1] : ctx->argv[2];
if (strcmp(fail_mode, "standalone") && strcmp(fail_mode, "secure")) {
vsctl_fatal("fail-mode must be \"standalone\" or \"secure\"");
}
if (ctx->argc == 2) {
/* Set the fail-mode in the "Open_vSwitch" table. */
if (!info.ctrl) {
vsctl_fatal("no controller declared");
}
ovsrec_controller_set_fail_mode(info.ctrl, fail_mode);
} else {
struct vsctl_bridge *br = find_real_bridge(&info, ctx->argv[1], true);
if (!br->ctrl) {
vsctl_fatal("no controller declared for %s", br->name);
}
ovsrec_controller_set_fail_mode(br->ctrl, fail_mode);
}
free_info(&info);
}
static void
cmd_get_ssl(struct vsctl_context *ctx)
{
struct ovsrec_ssl *ssl = ctx->ovs->ssl;
if (ssl) {
ds_put_format(&ctx->output, "Private key: %s\n", ssl->private_key);
ds_put_format(&ctx->output, "Certificate: %s\n", ssl->certificate);
ds_put_format(&ctx->output, "CA Certificate: %s\n", ssl->ca_cert);
ds_put_format(&ctx->output, "Bootstrap: %s\n",
ssl->bootstrap_ca_cert ? "true" : "false");
}
}
static void
cmd_del_ssl(struct vsctl_context *ctx)
{
struct ovsrec_ssl *ssl = ctx->ovs->ssl;
if (ssl) {
ovsrec_ssl_delete(ssl);
ovsrec_open_vswitch_set_ssl(ctx->ovs, NULL);
}
}
static void
cmd_set_ssl(struct vsctl_context *ctx)
{
bool bootstrap = shash_find(&ctx->options, "--bootstrap");
struct ovsrec_ssl *ssl = ctx->ovs->ssl;
if (ssl) {
ovsrec_ssl_delete(ssl);
}
ssl = ovsrec_ssl_insert(ctx->txn);
ovsrec_ssl_set_private_key(ssl, ctx->argv[1]);
ovsrec_ssl_set_certificate(ssl, ctx->argv[2]);
ovsrec_ssl_set_ca_cert(ssl, ctx->argv[3]);
ovsrec_ssl_set_bootstrap_ca_cert(ssl, bootstrap);
ovsrec_open_vswitch_set_ssl(ctx->ovs, ssl);
}
/* Parameter commands. */
/* POSIX extended regular expression for an 8-bit unsigned decimal integer. */
#define OCTET_RE "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
/* POSIX extended regular expression for an IP address. */
#define IP_RE "("OCTET_RE"\\."OCTET_RE"\\."OCTET_RE"\\."OCTET_RE")"
/* POSIX extended regular expression for a netmask. */
#define NETMASK_RE \
"255.255.255."NETMASK_END_RE"|" \
"255.255."NETMASK_END_RE".0|" \
"255."NETMASK_END_RE".0.0|" \
NETMASK_END_RE".0.0.0"
#define NETMASK_END_RE "(255|254|252|248|240|224|192|128|0)"
/* POSIX extended regular expression for an Ethernet address. */
#define XX_RE "[0-9a-fA-F][0-9a-fA-F]"
#define MAC_RE XX_RE":"XX_RE":"XX_RE":"XX_RE":"XX_RE":"XX_RE
/* POSIX extended regular expression for a TCP or UDP port number. */
#define PORT_RE \
"([0-9]|" \
"[1-9][0-9]|" \
"[1-9][0-9][0-9]|" \
"[1-9][0-9][0-9][0-9]|" \
"[1-5][0-9][0-9][0-9][0-9]|" \
"6[1-4][0-9][0-9][0-9]|" \
"65[1-4][0-9][0-9]|" \
"655[1-2][0-9]|" \
"6553[1-5])"
enum {
VSCF_READONLY = 1 << 0,
VSCF_HIDDEN = 1 << 1
};
struct vsctl_column {
struct ovsdb_idl_column *idl;
int flags;
const char *constraint;
};
static const struct vsctl_column bridge_columns[] = {
{&ovsrec_bridge_col_controller, VSCF_READONLY, NULL},
{&ovsrec_bridge_col_datapath_id, VSCF_READONLY, NULL},
{&ovsrec_bridge_col_datapath_type, VSCF_READONLY, NULL},
{&ovsrec_bridge_col_external_ids, 0, NULL},
{&ovsrec_bridge_col_flood_vlans, 0, "[1,4095]"},
{&ovsrec_bridge_col_mirrors, VSCF_READONLY, NULL},
{&ovsrec_bridge_col_name, VSCF_READONLY, NULL},
{&ovsrec_bridge_col_netflow, VSCF_READONLY, NULL},
{&ovsrec_bridge_col_other_config, 0, NULL},
{&ovsrec_bridge_col_ports, VSCF_READONLY, NULL},
{NULL, 0, NULL},
};
static const struct vsctl_column controller_columns[] = {
{&ovsrec_controller_col_connection_mode, 0, "in-band|out-of-band"},
{&ovsrec_controller_col_controller_burst_limit, 0, "[25,]"},
{&ovsrec_controller_col_controller_rate_limit, 0, "[100,]"},
{&ovsrec_controller_col_discover_accept_regex, 0, NULL},
{&ovsrec_controller_col_discover_update_resolv_conf, 0, NULL},
{&ovsrec_controller_col_fail_mode, 0, "standalone|secure"},
{&ovsrec_controller_col_inactivity_probe, 0, "[5000,]"},
{&ovsrec_controller_col_local_gateway, 0, IP_RE},
{&ovsrec_controller_col_local_ip, 0, IP_RE},
{&ovsrec_controller_col_local_netmask, 0, NETMASK_RE},
{&ovsrec_controller_col_max_backoff, 0, "[1000,]"},
{&ovsrec_controller_col_target, 0, NULL},
{NULL, 0, NULL},
};
static const struct vsctl_column interface_columns[] = {
{&ovsrec_interface_col_external_ids, 0, NULL},
{&ovsrec_interface_col_ingress_policing_burst, 0, "[10,]"},
{&ovsrec_interface_col_ingress_policing_rate, 0, "[100,]"},
{&ovsrec_interface_col_mac, 0, MAC_RE},
{&ovsrec_interface_col_name, VSCF_READONLY, NULL},
{&ovsrec_interface_col_ofport, VSCF_READONLY, NULL},
{&ovsrec_interface_col_options, 0, NULL},
{&ovsrec_interface_col_type, VSCF_READONLY, NULL},
{NULL, 0, NULL},
};
static const struct vsctl_column mirror_columns[] = {
{&ovsrec_mirror_col_name, VSCF_READONLY, NULL},
{&ovsrec_mirror_col_output_port, 0, "Port"},
{&ovsrec_mirror_col_output_vlan, 0, "[1,4095]"},
{&ovsrec_mirror_col_select_dst_port, 0, "Port"},
{&ovsrec_mirror_col_select_src_port, 0, "Port"},
{&ovsrec_mirror_col_select_vlan, 0, "[1,4095]"},
{NULL, 0, NULL},
};
static const struct vsctl_column netflow_columns[] = {
{&ovsrec_netflow_col_active_timeout, 0, "[-1,]"},
{&ovsrec_netflow_col_add_id_to_interface, 0, NULL},
{&ovsrec_netflow_col_engine_id, 0, "[0,255]"},
{&ovsrec_netflow_col_engine_type, 0, "[0,255]"},
{&ovsrec_netflow_col_targets, 0, IP_RE":"PORT_RE},
{NULL, 0, NULL},
};
static const struct vsctl_column open_vswitch_columns[] = {
{&ovsrec_open_vswitch_col_bridges, VSCF_READONLY, NULL},
{&ovsrec_open_vswitch_col_controller, VSCF_READONLY, NULL},
{&ovsrec_open_vswitch_col_cur_cfg, VSCF_HIDDEN, NULL},
{&ovsrec_open_vswitch_col_managers, 0, "p?(ssl|tcp|unix):.*"},
{&ovsrec_open_vswitch_col_next_cfg, VSCF_HIDDEN, NULL},
{&ovsrec_open_vswitch_col_ssl, VSCF_READONLY, NULL},
{NULL, 0, NULL},
};
static const struct vsctl_column port_columns[] = {
{&ovsrec_port_col_bond_downdelay, 0, "[0,]"},
{&ovsrec_port_col_bond_fake_iface, VSCF_READONLY, NULL},
{&ovsrec_port_col_bond_updelay, 0, "[0,]"},
{&ovsrec_port_col_external_ids, 0, NULL},
{&ovsrec_port_col_fake_bridge, VSCF_READONLY, NULL},
{&ovsrec_port_col_interfaces, VSCF_READONLY, NULL},
{&ovsrec_port_col_mac, 0, MAC_RE},
{&ovsrec_port_col_name, VSCF_READONLY, NULL},
{&ovsrec_port_col_other_config, 0, NULL},
{&ovsrec_port_col_tag, 0, "[0,4095]"},
{&ovsrec_port_col_trunks, 0, "[0,4095]"},
{NULL, 0, NULL},
};
static const struct vsctl_column ssl_columns[] = {
{&ovsrec_ssl_col_bootstrap_ca_cert, 0, NULL},
{&ovsrec_ssl_col_ca_cert, 0, NULL},
{&ovsrec_ssl_col_certificate, 0, NULL},
{&ovsrec_ssl_col_private_key, 0, NULL},
{NULL, 0, NULL},
};
struct vsctl_row_id {
const struct ovsdb_idl_table_class *table;
const struct ovsdb_idl_column *name_column;
const struct ovsdb_idl_column *uuid_column;
};
struct vsctl_table_class {
struct ovsdb_idl_table_class *class;
const struct vsctl_column *columns;
struct vsctl_row_id row_ids[2];
};
static const struct vsctl_table_class tables[] = {
{&ovsrec_table_bridge, bridge_columns,
{{&ovsrec_table_bridge, &ovsrec_bridge_col_name, NULL},
{NULL, NULL, NULL}}},
{&ovsrec_table_controller, controller_columns,
{{&ovsrec_table_bridge,
&ovsrec_bridge_col_name,
&ovsrec_bridge_col_controller},
{&ovsrec_table_open_vswitch,
NULL,
&ovsrec_open_vswitch_col_controller}}},
{&ovsrec_table_interface, interface_columns,
{{&ovsrec_table_interface, &ovsrec_interface_col_name, NULL},
{NULL, NULL, NULL}}},
{&ovsrec_table_mirror, mirror_columns,
{{&ovsrec_table_mirror, &ovsrec_mirror_col_name, NULL},
{NULL, NULL, NULL}}},
{&ovsrec_table_netflow, netflow_columns,
{{&ovsrec_table_bridge,
&ovsrec_bridge_col_name,
&ovsrec_bridge_col_netflow},
{NULL, NULL, NULL}}},
{&ovsrec_table_open_vswitch, open_vswitch_columns,
{{&ovsrec_table_open_vswitch, NULL, NULL},
{NULL, NULL, NULL}}},
{&ovsrec_table_port, port_columns,
{{&ovsrec_table_port, &ovsrec_port_col_name, NULL},
{NULL, NULL, NULL}}},
{&ovsrec_table_ssl, ssl_columns,
{{&ovsrec_table_open_vswitch, NULL, &ovsrec_open_vswitch_col_ssl}}},
{NULL, NULL, {{NULL, NULL, NULL}, {NULL, NULL, NULL}}}
};
static void
die_if_error(char *error)
{
if (error) {
vsctl_fatal("%s", error);
}
}
static int
to_lower_and_underscores(unsigned c)
{
return c == '-' ? '_' : tolower(c);
}
static unsigned int
score_partial_match(const char *name, const char *s)
{
int score;
if (!strcmp(name, s)) {
return UINT_MAX;
}
for (score = 0; ; score++, name++, s++) {
if (to_lower_and_underscores(*name) != to_lower_and_underscores(*s)) {
break;
} else if (*name == '\0') {
return UINT_MAX - 1;
}
}
return *s == '\0' ? score : 0;
}
static const struct vsctl_table_class *
get_table(const char *table_name)
{
const struct vsctl_table_class *table;
const struct vsctl_table_class *best_match = NULL;
unsigned int best_score = 0;
for (table = tables; table->class; table++) {
unsigned int score = score_partial_match(table->class->name,
table_name);
if (score > best_score) {
best_match = table;
best_score = score;
} else if (score == best_score) {
best_match = NULL;
}
}
if (best_match) {
return best_match;
} else if (best_score) {
vsctl_fatal("multiple table names match \"%s\"", table_name);
} else {
vsctl_fatal("unknown table \"%s\"", table_name);
}
}
static const struct ovsdb_idl_row *
get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
const struct vsctl_row_id *id, const char *record_id)
{
const struct ovsdb_idl_row *referrer, *final;
if (!id->table) {
return NULL;
}
if (!id->name_column) {
if (strcmp(record_id, ".")) {
return NULL;
}
referrer = ovsdb_idl_first_row(ctx->idl, id->table);
if (!referrer || ovsdb_idl_next_row(referrer)) {
return NULL;
}
} else {
const struct ovsdb_idl_row *row;
unsigned int best_score = 0;
/* It might make sense to relax this assertion. */
assert(id->name_column->type.key_type == OVSDB_TYPE_STRING);
referrer = NULL;
for (row = ovsdb_idl_first_row(ctx->idl, id->table);
row != NULL && best_score != UINT_MAX;
row = ovsdb_idl_next_row(row))
{
struct ovsdb_datum name;
ovsdb_idl_txn_read(row, id->name_column, &name);
if (name.n == 1) {
unsigned int score = score_partial_match(name.keys[0].string,
record_id);
if (score > best_score) {
referrer = row;
best_score = score;
} else if (score == best_score) {
referrer = NULL;
}
}
ovsdb_datum_destroy(&name, &id->name_column->type);
}
if (best_score && !referrer) {
vsctl_fatal("multiple rows in %s match \"%s\"",
table->class->name, record_id);
}
}
if (!referrer) {
return NULL;
}
final = NULL;
if (id->uuid_column) {
struct ovsdb_datum uuid;
assert(id->uuid_column->type.key_type == OVSDB_TYPE_UUID);
assert(id->uuid_column->type.value_type == OVSDB_TYPE_VOID);
ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
if (uuid.n == 1) {
final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
&uuid.keys[0].uuid);
}
ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
} else {
final = referrer;
}
return final;
}
static const struct ovsdb_idl_row *
get_row(struct vsctl_context *ctx,
const struct vsctl_table_class *table, const char *record_id)
{
const struct ovsdb_idl_row *row;
struct uuid uuid;
if (uuid_from_string(&uuid, record_id)) {
row = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class, &uuid);
} else {
int i;
for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
if (row) {
break;
}
}
}
return row;
}
static const struct ovsdb_idl_row *
must_get_row(struct vsctl_context *ctx,
const struct vsctl_table_class *table, const char *record_id)
{
const struct ovsdb_idl_row *row = get_row(ctx, table, record_id);
if (!row) {
vsctl_fatal("no row \"%s\" in table %s",
record_id, table->class->name);
}
return row;
}
static char *
get_column(const struct vsctl_table_class *table, const char *column_name,
const struct vsctl_column **columnp)
{
const struct vsctl_column *column;
const struct vsctl_column *best_match = NULL;
unsigned int best_score = 0;
for (column = table->columns; column->idl; column++) {
if (!(column->flags & VSCF_HIDDEN)) {
unsigned int score = score_partial_match(column->idl->name,
column_name);
if (score > best_score) {
best_match = column;
best_score = score;
} else if (score == best_score) {
best_match = NULL;
}
}
}
*columnp = best_match;
if (best_match) {
return NULL;
} else if (best_score) {
return xasprintf("%s contains more than one column whose name "
"matches \"%s\"", table->class->name, column_name);
} else {
return xasprintf("%s does not contain a column whose name matches "
"\"%s\"", table->class->name, column_name);
}
}
static char * WARN_UNUSED_RESULT
parse_column_key_value(const char *arg, const struct vsctl_table_class *table,
const struct vsctl_column **columnp,
char **keyp, char **valuep)
{
const char *p = arg;
char *error;
assert(columnp || keyp);
if (keyp) {
*keyp = NULL;
}
if (valuep) {
*valuep = NULL;
}
/* Parse column name. */
if (columnp) {
char *column_name;
error = ovsdb_token_parse(&p, &column_name);
if (error) {
goto error;
}
if (column_name[0] == '\0') {
free(column_name);
error = xasprintf("%s: missing column name", arg);
goto error;
}
error = get_column(table, column_name, columnp);
if (error) {
goto error;
}
free(column_name);
}
/* Parse key string. */
if (*p == ':' || !columnp) {
if (columnp) {
p++;
} else if (!keyp) {
error = xasprintf("%s: key not accepted here", arg);
goto error;
}
error = ovsdb_token_parse(&p, keyp);
if (error) {
goto error;
}
} else if (keyp) {
*keyp = NULL;
}
/* Parse value string. */
if (*p == '=') {
if (!valuep) {
error = xasprintf("%s: value not accepted here", arg);
goto error;
}
*valuep = xstrdup(p + 1);
} else {
if (valuep) {
*valuep = NULL;
}
if (*p != '\0') {
error = xasprintf("%s: trailing garbage \"%s\" in argument",
arg, p);
goto error;
}
}
return NULL;
error:
if (columnp) {
*columnp = NULL;
}
if (keyp) {
free(*keyp);
*keyp = NULL;
}
if (valuep) {
free(*valuep);
*valuep = NULL;
}
return error;
}
static void
cmd_get(struct vsctl_context *ctx)
{
bool if_exists = shash_find(&ctx->options, "--if-exists");
const char *table_name = ctx->argv[1];
const char *record_id = ctx->argv[2];
const struct vsctl_table_class *table;
const struct ovsdb_idl_row *row;
struct ds *out = &ctx->output;
int i;
table = get_table(table_name);
row = must_get_row(ctx, table, record_id);
for (i = 3; i < ctx->argc; i++) {
const struct vsctl_column *column;
struct ovsdb_datum datum;
char *key_string;
die_if_error(parse_column_key_value(ctx->argv[i], table,
&column, &key_string, NULL));
ovsdb_idl_txn_read(row, column->idl, &datum);
if (key_string) {
union ovsdb_atom key;
unsigned int idx;
if (column->idl->type.value_type == OVSDB_TYPE_VOID) {
vsctl_fatal("cannot specify key to get for non-map column %s",
column->idl->name);
}
die_if_error(ovsdb_atom_from_string(&key,
column->idl->type.key_type,
key_string));
idx = ovsdb_datum_find_key(&datum, &key,
column->idl->type.key_type);
if (idx == UINT_MAX) {
if (!if_exists) {
vsctl_fatal("no key \"%s\" in %s record \"%s\" column %s",
key_string, table->class->name, record_id,
column->idl->name);
}
} else {
ovsdb_atom_to_string(&datum.values[idx],
column->idl->type.value_type, out);
}
ovsdb_atom_destroy(&key, column->idl->type.key_type);
} else {
ovsdb_datum_to_string(&datum, &column->idl->type, out);
}
ds_put_char(out, '\n');
ovsdb_datum_destroy(&datum, &column->idl->type);
free(key_string);
}
}
static void
list_record(const struct vsctl_table_class *table,
const struct ovsdb_idl_row *row, struct ds *out)
{
const struct vsctl_column *column;
ds_put_format(out, "%-20s (RO): "UUID_FMT"\n", "_uuid",
UUID_ARGS(&row->uuid));
for (column = table->columns; column->idl; column++) {
struct ovsdb_datum datum;
if (column->flags & VSCF_HIDDEN) {
continue;
}
ovsdb_idl_txn_read(row, column->idl, &datum);
ds_put_format(out, "%-20s (%s): ", column->idl->name,
column->flags & VSCF_READONLY ? "RO" : "RW");
ovsdb_datum_to_string(&datum, &column->idl->type, out);
ds_put_char(out, '\n');
ovsdb_datum_destroy(&datum, &column->idl->type);
}
}
static void
cmd_list(struct vsctl_context *ctx)
{
const char *table_name = ctx->argv[1];
const struct vsctl_table_class *table;
struct ds *out = &ctx->output;
int i;
table = get_table(table_name);
if (ctx->argc > 2) {
for (i = 2; i < ctx->argc; i++) {
if (i > 2) {
ds_put_char(out, '\n');
}
list_record(table, must_get_row(ctx, table, ctx->argv[i]), out);
}
} else {
const struct ovsdb_idl_row *row;
bool first;
for (row = ovsdb_idl_first_row(ctx->idl, table->class), first = true;
row != NULL;
row = ovsdb_idl_next_row(row), first = false) {
if (!first) {
ds_put_char(out, '\n');
}
list_record(table, row, out);
}
}
}
static void
check_string_constraint(const struct ovsdb_datum *datum,
const char *constraint)
{
unsigned int i;
char *regex;
regex_t re;
int retval;
regex = xasprintf("^%s$", constraint);
retval = regcomp(&re, regex, REG_NOSUB | REG_EXTENDED);
if (retval) {
size_t length = regerror(retval, &re, NULL, 0);
char *buffer = xmalloc(length);
regerror(retval, &re, buffer, length);
vsctl_fatal("internal error compiling regular expression %s: %s",
regex, buffer);
}
for (i = 0; i < datum->n; i++) {
const char *key = datum->keys[i].string;
if (regexec(&re, key, 0, NULL, 0)) {
vsctl_fatal("%s is not valid (it does not match %s)", key, regex);
}
}
free(regex);
regfree(&re);
}
static void
check_integer_constraint(const struct ovsdb_datum *datum,
const char *constraint)
{
int64_t min, max;
unsigned int i;
int n = -1;
sscanf(constraint, "[%"SCNd64",%"SCNd64"]%n", &min, &max, &n);
if (n == -1) {
sscanf(constraint, "[%"SCNd64",]%n", &min, &n);
if (n == -1) {
sscanf(constraint, "[,%"SCNd64"]%n", &max, &n);
if (n == -1) {
VLOG_DBG("internal error: bad integer contraint \"%s\"",
constraint);
return;
} else {
min = INT64_MIN;
}
} else {
max = INT64_MAX;
}
}
for (i = 0; i < datum->n; i++) {
int64_t value = datum->keys[i].integer;
if (value < min || value > max) {
if (max == INT64_MAX) {
vsctl_fatal("%"PRId64" is less than the minimum "
"allowed value %"PRId64, value, min);
} else if (min == INT64_MIN) {
vsctl_fatal("%"PRId64" is greater than the maximum "
"allowed value %"PRId64, value, max);
} else {
vsctl_fatal("%"PRId64" is outside the valid range %"PRId64" "
"to %"PRId64" (inclusive)", value, min, max);
}
}
}
}
static void
check_constraint(const struct ovsdb_datum *datum,
const struct ovsdb_type *type, const char *constraint)
{
if (constraint && datum->n) {
if (type->key_type == OVSDB_TYPE_STRING) {
check_string_constraint(datum, constraint);
} else if (type->key_type == OVSDB_TYPE_INTEGER) {
check_integer_constraint(datum, constraint);
}
}
}
static void
set_column(const struct vsctl_table_class *table,
const struct ovsdb_idl_row *row,
const char *arg, bool force)
{
const struct vsctl_column *column;
char *key_string, *value_string;
char *error;
error = parse_column_key_value(arg, table, &column, &key_string,
&value_string);
die_if_error(error);
if (column->flags & VSCF_READONLY && !force) {
vsctl_fatal("%s: cannot modify read-only column %s in table %s",
arg, column->idl->name, table->class->name);
}
if (!value_string) {
vsctl_fatal("%s: missing value", arg);
}
if (key_string) {
union ovsdb_atom key, value;
struct ovsdb_datum old, new;
if (column->idl->type.value_type == OVSDB_TYPE_VOID) {
vsctl_fatal("cannot specify key to set for non-map column %s",
column->idl->name);
}
die_if_error(ovsdb_atom_from_string(&key,
column->idl->type.key_type,
key_string));
die_if_error(ovsdb_atom_from_string(&value,
column->idl->type.value_type,
value_string));
ovsdb_datum_init_empty(&new);
ovsdb_datum_add_unsafe(&new, &key, &value, &column->idl->type);
ovsdb_idl_txn_read(row, column->idl, &old);
ovsdb_datum_union(&old, &new, &column->idl->type, true);
ovsdb_idl_txn_write(row, column->idl, &old);
ovsdb_datum_destroy(&new, &column->idl->type);
} else {
struct ovsdb_datum datum;
die_if_error(ovsdb_datum_from_string(&datum, &column->idl->type,
value_string));
if (!force) {
check_constraint(&datum, &column->idl->type,
column->constraint);
}
ovsdb_idl_txn_write(row, column->idl, &datum);
}
free(key_string);
}
static void
cmd_set(struct vsctl_context *ctx)
{
bool force = shash_find(&ctx->options, "--force");
const char *table_name = ctx->argv[1];
const char *record_id = ctx->argv[2];
const struct vsctl_table_class *table;
const struct ovsdb_idl_row *row;
int i;
table = get_table(table_name);
row = must_get_row(ctx, table, record_id);
for (i = 3; i < ctx->argc; i++) {
set_column(table, row, ctx->argv[i], force);
}
}
static void
cmd_add(struct vsctl_context *ctx)
{
bool force = shash_find(&ctx->options, "--force");
const char *table_name = ctx->argv[1];
const char *record_id = ctx->argv[2];
const char *column_name = ctx->argv[3];
const struct vsctl_table_class *table;
const struct vsctl_column *column;
const struct ovsdb_idl_row *row;
const struct ovsdb_type *type;
struct ovsdb_datum old;
int i;
table = get_table(table_name);
row = must_get_row(ctx, table, record_id);
die_if_error(get_column(table, column_name, &column));
if (column->flags & VSCF_READONLY && !force) {
vsctl_fatal("cannot modify read-only column %s in table %s",
column->idl->name, table->class->name);
}
type = &column->idl->type;
ovsdb_idl_txn_read(row, column->idl, &old);
for (i = 4; i < ctx->argc; i++) {
struct ovsdb_type add_type;
struct ovsdb_datum add;
add_type = *type;
add_type.n_min = 1;
add_type.n_max = UINT_MAX;
die_if_error(ovsdb_datum_from_string(&add, &add_type, ctx->argv[i]));
ovsdb_datum_union(&old, &add, type, false);
ovsdb_datum_destroy(&add, type);
}
if (old.n > type->n_max) {
vsctl_fatal("\"add\" operation would put %u %s in column %s of "
"table %s but the maximum number is %u",
old.n,
type->value_type == OVSDB_TYPE_VOID ? "values" : "pairs",
column->idl->name, table->class->name, type->n_max);
}
ovsdb_idl_txn_write(row, column->idl, &old);
}
static void
cmd_remove(struct vsctl_context *ctx)
{
bool force = shash_find(&ctx->options, "--force");
const char *table_name = ctx->argv[1];
const char *record_id = ctx->argv[2];
const char *column_name = ctx->argv[3];
const struct vsctl_table_class *table;
const struct vsctl_column *column;
const struct ovsdb_idl_row *row;
const struct ovsdb_type *type;
struct ovsdb_datum old;
int i;
table = get_table(table_name);
row = must_get_row(ctx, table, record_id);
die_if_error(get_column(table, column_name, &column));
if (column->flags & VSCF_READONLY && !force) {
vsctl_fatal("cannot modify read-only column %s in table %s",
column->idl->name, table->class->name);
}
type = &column->idl->type;
ovsdb_idl_txn_read(row, column->idl, &old);
for (i = 4; i < ctx->argc; i++) {
struct ovsdb_type rm_type;
struct ovsdb_datum rm;
char *error;
rm_type = *type;
rm_type.n_min = 1;
rm_type.n_max = UINT_MAX;
error = ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]);
if (error && ovsdb_type_is_map(&rm_type)) {
free(error);
rm_type.value_type = OVSDB_TYPE_VOID;
die_if_error(ovsdb_datum_from_string(&rm, &rm_type, ctx->argv[i]));
}
ovsdb_datum_subtract(&old, type, &rm, &rm_type);
ovsdb_datum_destroy(&rm, &rm_type);
}
if (old.n < type->n_min) {
vsctl_fatal("\"remove\" operation would put %u %s in column %s of "
"table %s but the minimun number is %u",
old.n,
type->value_type == OVSDB_TYPE_VOID ? "values" : "pairs",
column->idl->name, table->class->name, type->n_min);
}
ovsdb_idl_txn_write(row, column->idl, &old);
}
static void
cmd_clear(struct vsctl_context *ctx)
{
bool force = shash_find(&ctx->options, "--force");
const char *table_name = ctx->argv[1];
const char *record_id = ctx->argv[2];
const struct vsctl_table_class *table;
const struct ovsdb_idl_row *row;
int i;
table = get_table(table_name);
row = must_get_row(ctx, table, record_id);
for (i = 3; i < ctx->argc; i++) {
const struct vsctl_column *column;
const struct ovsdb_type *type;
struct ovsdb_datum datum;
die_if_error(get_column(table, ctx->argv[i], &column));
type = &column->idl->type;
if (column->flags & VSCF_READONLY && !force) {
vsctl_fatal("cannot modify read-only column %s in table %s",
column->idl->name, table->class->name);
} else if (type->n_min > 0) {
vsctl_fatal("\"clear\" operation cannot be applied to column %s "
"of table %s, which is not allowed to be empty",
column->idl->name, table->class->name);
}
ovsdb_datum_init_empty(&datum);
ovsdb_idl_txn_write(row, column->idl, &datum);
}
}
static void
cmd_create(struct vsctl_context *ctx)
{
bool force = shash_find(&ctx->options, "--force");
const char *table_name = ctx->argv[1];
const struct vsctl_table_class *table;
const struct ovsdb_idl_row *row;
int i;
if (!force) {
vsctl_fatal("\"create\" requires --force");
}
table = get_table(table_name);
row = ovsdb_idl_txn_insert(ctx->txn, table->class);
for (i = 2; i < ctx->argc; i++) {
set_column(table, row, ctx->argv[i], force);
}
ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
}
/* This function may be used as the 'postprocess' function for commands that
* insert new rows into the database. It expects that the command's 'run'
* function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
* sole output. It replaces that output by the row's permanent UUID assigned
* by the database server and appends a new-line.
*
* Currently we use this only for "create", because the higher-level commands
* are supposed to be independent of the actual structure of the vswitch
* configuration. */
static void
post_create(struct vsctl_context *ctx)
{
const struct uuid *real;
struct uuid dummy;
uuid_from_string(&dummy, ds_cstr(&ctx->output));
real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
if (real) {
ds_clear(&ctx->output);
ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
}
ds_put_char(&ctx->output, '\n');
}
static void
cmd_destroy(struct vsctl_context *ctx)
{
bool force = shash_find(&ctx->options, "--force");
bool must_exist = !shash_find(&ctx->options, "--if-exists");
const char *table_name = ctx->argv[1];
const struct vsctl_table_class *table;
int i;
if (!force) {
vsctl_fatal("\"destroy\" requires --force");
}
table = get_table(table_name);
for (i = 2; i < ctx->argc; i++) {
const struct ovsdb_idl_row *row;
row = (must_exist ? must_get_row : get_row)(ctx, table, ctx->argv[i]);
if (row) {
ovsdb_idl_txn_delete(row);
}
}
}
static struct json *
where_uuid_equals(const struct uuid *uuid)
{
return
json_array_create_1(
json_array_create_3(
json_string_create("_uuid"),
json_string_create("=="),
json_array_create_2(
json_string_create("uuid"),
json_string_create_nocopy(
xasprintf(UUID_FMT, UUID_ARGS(uuid))))));
}
static void
vsctl_context_init(struct vsctl_context *ctx, struct vsctl_command *command,
struct ovsdb_idl *idl, struct ovsdb_idl_txn *txn,
const struct ovsrec_open_vswitch *ovs)
{
ctx->argc = command->argc;
ctx->argv = command->argv;
ctx->options = command->options;
ds_swap(&ctx->output, &command->output);
ctx->idl = idl;
ctx->txn = txn;
ctx->ovs = ovs;
}
static void
vsctl_context_done(struct vsctl_context *ctx, struct vsctl_command *command)
{
ds_swap(&ctx->output, &command->output);
}
static void
do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
struct ovsdb_idl *idl)
{
struct ovsdb_idl_txn *txn;
const struct ovsrec_open_vswitch *ovs;
enum ovsdb_idl_txn_status status;
struct vsctl_command *c;
int64_t next_cfg = 0;
char *comment;
txn = ovsdb_idl_txn_create(idl);
if (dry_run) {
ovsdb_idl_txn_set_dry_run(txn);
}
comment = xasprintf("ovs-vsctl: %s", args);
ovsdb_idl_txn_add_comment(txn, comment);
free(comment);
ovs = ovsrec_open_vswitch_first(idl);
if (!ovs) {
/* XXX add verification that table is empty */
ovs = ovsrec_open_vswitch_insert(txn);
}
if (wait_for_reload) {
struct json *where = where_uuid_equals(&ovs->header_.uuid);
ovsdb_idl_txn_increment(txn, "Open_vSwitch", "next_cfg", where);
json_destroy(where);
}
for (c = commands; c < &commands[n_commands]; c++) {
struct vsctl_context ctx;
ds_init(&c->output);
vsctl_context_init(&ctx, c, idl, txn, ovs);
(c->syntax->run)(&ctx);
vsctl_context_done(&ctx, c);
}
while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
ovsdb_idl_run(idl);
ovsdb_idl_wait(idl);
ovsdb_idl_txn_wait(txn);
poll_block();
}
if (wait_for_reload && status == TXN_SUCCESS) {
next_cfg = ovsdb_idl_txn_get_increment_new_value(txn);
}
for (c = commands; c < &commands[n_commands]; c++) {
if (c->syntax->postprocess) {
struct vsctl_context ctx;
vsctl_context_init(&ctx, c, idl, txn, ovs);
(c->syntax->postprocess)(&ctx);
vsctl_context_done(&ctx, c);
}
}
ovsdb_idl_txn_destroy(txn);
switch (status) {
case TXN_INCOMPLETE:
NOT_REACHED();
case TXN_ABORTED:
/* Should not happen--we never call ovsdb_idl_txn_abort(). */
vsctl_fatal("transaction aborted");
case TXN_UNCHANGED:
case TXN_SUCCESS:
break;
case TXN_TRY_AGAIN:
for (c = commands; c < &commands[n_commands]; c++) {
ds_destroy(&c->output);
}
return;
case TXN_ERROR:
vsctl_fatal("transaction error");
default:
NOT_REACHED();
}
for (c = commands; c < &commands[n_commands]; c++) {
struct ds *ds = &c->output;
if (oneline) {
size_t j;
ds_chomp(ds, '\n');
for (j = 0; j < ds->length; j++) {
int c = ds->string[j];
switch (c) {
case '\n':
fputs("\\n", stdout);
break;
case '\\':
fputs("\\\\", stdout);
break;
default:
putchar(c);
}
}
putchar('\n');
} else {
fputs(ds_cstr(ds), stdout);
}
}
if (wait_for_reload && status != TXN_UNCHANGED) {
for (;;) {
const struct ovsrec_open_vswitch *ovs;
ovsdb_idl_run(idl);
OVSREC_OPEN_VSWITCH_FOR_EACH (ovs, idl) {
if (ovs->cur_cfg >= next_cfg) {
goto done;
}
}
ovsdb_idl_wait(idl);
poll_block();
}
done: ;
}
exit(EXIT_SUCCESS);
}
static const struct vsctl_command_syntax all_commands[] = {
/* Open vSwitch commands. */
{"init", 0, 0, cmd_init, NULL, ""},
/* Bridge commands. */
{"add-br", 1, 3, cmd_add_br, NULL, ""},
{"del-br", 1, 1, cmd_del_br, NULL, "--if-exists"},
{"list-br", 0, 0, cmd_list_br, NULL, ""},
{"br-exists", 1, 1, cmd_br_exists, NULL, ""},
{"br-to-vlan", 1, 1, cmd_br_to_vlan, NULL, ""},
{"br-to-parent", 1, 1, cmd_br_to_parent, NULL, ""},
{"br-set-external-id", 2, 3, cmd_br_set_external_id, NULL, ""},
{"br-get-external-id", 1, 2, cmd_br_get_external_id, NULL, ""},
/* Port commands. */
{"list-ports", 1, 1, cmd_list_ports, NULL, ""},
{"add-port", 2, 2, cmd_add_port, NULL, ""},
{"add-bond", 4, INT_MAX, cmd_add_bond, NULL, "--fake-iface"},
{"del-port", 1, 2, cmd_del_port, NULL, "--if-exists"},
{"port-to-br", 1, 1, cmd_port_to_br, NULL, ""},
/* Interface commands. */
{"list-ifaces", 1, 1, cmd_list_ifaces, NULL, ""},
{"iface-to-br", 1, 1, cmd_iface_to_br, NULL, ""},
/* Controller commands. */
{"get-controller", 0, 1, cmd_get_controller, NULL, ""},
{"del-controller", 0, 1, cmd_del_controller, NULL, ""},
{"set-controller", 1, 2, cmd_set_controller, NULL, ""},
{"get-fail-mode", 0, 1, cmd_get_fail_mode, NULL, ""},
{"del-fail-mode", 0, 1, cmd_del_fail_mode, NULL, ""},
{"set-fail-mode", 1, 2, cmd_set_fail_mode, NULL, ""},
/* SSL commands. */
{"get-ssl", 0, 0, cmd_get_ssl, NULL, ""},
{"del-ssl", 0, 0, cmd_del_ssl, NULL, ""},
{"set-ssl", 3, 3, cmd_set_ssl, NULL, "--bootstrap"},
/* Parameter commands. */
{"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
{"list", 1, INT_MAX, cmd_list, NULL, ""},
{"set", 3, INT_MAX, cmd_set, NULL, "--force"},
{"add", 4, INT_MAX, cmd_add, NULL, "--force"},
{"remove", 4, INT_MAX, cmd_remove, NULL, "--force"},
{"clear", 3, INT_MAX, cmd_clear, NULL, "--force"},
{"create", 2, INT_MAX, cmd_create, post_create, "--force"},
{"destroy", 1, INT_MAX, cmd_destroy, NULL, "--force,--if-exists"},
{NULL, 0, 0, NULL, NULL, NULL},
};
|