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
|
/* $OpenBSD: ofp13.c,v 1.29 2016/11/17 16:24:00 rzalamena Exp $ */
/*
* Copyright (c) 2013-2016 Reyk Floeter <reyk@openbsd.org>
* Copyright (c) 2016 Rafael Zalamena <rzalamena@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/ofp.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/tcp.h>
#include <netmpls/mpls.h>
#include <endian.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <imsg.h>
#include <event.h>
#include "switchd.h"
#include "ofp_map.h"
int ofp13_echo_request(struct switchd *, struct switch_connection *,
struct ofp_header *, struct ibuf *);
int ofp13_validate_features_reply(struct switchd *,
struct sockaddr_storage *, struct sockaddr_storage *,
struct ofp_header *, struct ibuf *);
int ofp13_features_reply(struct switchd *, struct switch_connection *,
struct ofp_header *, struct ibuf *);
int ofp13_validate_error(struct switchd *,
struct sockaddr_storage *, struct sockaddr_storage *,
struct ofp_header *, struct ibuf *);
int ofp13_validate_action(struct switchd *, struct ofp_header *,
struct ibuf *, off_t *, struct ofp_action_header *);
int ofp13_validate_instruction(struct switchd *, struct ofp_header *,
struct ibuf *, off_t *, struct ofp_instruction *);
int ofp13_validate_flow_mod(struct switchd *, struct sockaddr_storage *,
struct sockaddr_storage *, struct ofp_header *, struct ibuf *);
int ofp13_validate_oxm_basic(struct ibuf *, off_t, int, uint8_t);
int ofp13_validate_oxm(struct switchd *, struct ofp_ox_match *,
struct ofp_header *, struct ibuf *, off_t);
int ofp13_validate_packet_in(struct switchd *,
struct sockaddr_storage *, struct sockaddr_storage *,
struct ofp_header *, struct ibuf *);
int ofp13_packet_match(struct ibuf *, struct packet *, struct ofp_match *);
int ofp13_packet_in(struct switchd *, struct switch_connection *,
struct ofp_header *, struct ibuf *);
int ofp13_flow_removed(struct switchd *, struct switch_connection *,
struct ofp_header *, struct ibuf *);
int ofp13_parse_instruction(struct ibuf *, struct ofp_instruction *);
int ofp13_parse_action(struct ibuf *, struct ofp_action_header *);
int ofp13_parse_oxm(struct ibuf *, struct ofp_ox_match *);
int ofp13_parse_tableproperties(struct ibuf *, struct ofp_table_features *);
int ofp13_multipart_reply(struct switchd *, struct switch_connection *,
struct ofp_header *, struct ibuf *);
int ofp13_multipart_reply_validate(struct switchd *,
struct sockaddr_storage *, struct sockaddr_storage *,
struct ofp_header *, struct ibuf *);
int ofp13_validate_packet_out(struct switchd *,
struct sockaddr_storage *, struct sockaddr_storage *,
struct ofp_header *, struct ibuf *);
struct ofp_multipart *
ofp13_multipart_request(struct switch_connection *, struct ibuf *,
uint16_t, uint16_t);
int ofp13_multipart_request_validate(struct switchd *,
struct sockaddr_storage *, struct sockaddr_storage *,
struct ofp_header *, struct ibuf *);
int ofp13_error(struct switchd *, struct switch_connection *,
struct ofp_header *, struct ibuf *, uint16_t, uint16_t);
struct ofp_group_mod *
ofp13_group(struct switch_connection *, struct ibuf *,
uint32_t, uint16_t, uint8_t);
struct ofp_bucket *
ofp13_bucket(struct ibuf *, uint16_t, uint32_t, uint32_t);
int ofp13_setconfig_validate(struct switchd *,
struct sockaddr_storage *, struct sockaddr_storage *,
struct ofp_header *, struct ibuf *);
int ofp13_setconfig(struct switchd *, struct switch_connection *,
uint16_t, uint16_t);
int ofp13_tablemiss_sendctrl(struct switchd *, struct switch_connection *,
uint8_t);
struct ofp_callback ofp13_callbacks[] = {
{ OFP_T_HELLO, ofp13_hello, NULL },
{ OFP_T_ERROR, NULL, ofp13_validate_error },
{ OFP_T_ECHO_REQUEST, ofp13_echo_request, NULL },
{ OFP_T_ECHO_REPLY, NULL, NULL },
{ OFP_T_EXPERIMENTER, NULL, NULL },
{ OFP_T_FEATURES_REQUEST, NULL, NULL },
{ OFP_T_FEATURES_REPLY, ofp13_features_reply,
ofp13_validate_features_reply },
{ OFP_T_GET_CONFIG_REQUEST, NULL, NULL },
{ OFP_T_GET_CONFIG_REPLY, NULL, NULL },
{ OFP_T_SET_CONFIG, NULL, ofp13_setconfig_validate },
{ OFP_T_PACKET_IN, ofp13_packet_in,
ofp13_validate_packet_in },
{ OFP_T_FLOW_REMOVED, ofp13_flow_removed, NULL },
{ OFP_T_PORT_STATUS, NULL, NULL },
{ OFP_T_PACKET_OUT, NULL, ofp13_validate_packet_out },
{ OFP_T_FLOW_MOD, NULL, ofp13_validate_flow_mod },
{ OFP_T_GROUP_MOD, NULL, NULL },
{ OFP_T_PORT_MOD, NULL, NULL },
{ OFP_T_TABLE_MOD, NULL, NULL },
{ OFP_T_MULTIPART_REQUEST, NULL,
ofp13_multipart_request_validate },
{ OFP_T_MULTIPART_REPLY, ofp13_multipart_reply,
ofp13_multipart_reply_validate },
{ OFP_T_BARRIER_REQUEST, NULL, NULL },
{ OFP_T_BARRIER_REPLY, NULL, NULL },
{ OFP_T_QUEUE_GET_CONFIG_REQUEST, NULL, NULL },
{ OFP_T_QUEUE_GET_CONFIG_REPLY, NULL, NULL },
{ OFP_T_ROLE_REQUEST, NULL, NULL },
{ OFP_T_ROLE_REPLY, NULL, NULL },
{ OFP_T_GET_ASYNC_REQUEST, NULL, NULL },
{ OFP_T_GET_ASYNC_REPLY, NULL, NULL },
{ OFP_T_SET_ASYNC, NULL, NULL },
{ OFP_T_METER_MOD, NULL, NULL },
};
int
ofp13_validate(struct switchd *sc,
struct sockaddr_storage *src, struct sockaddr_storage *dst,
struct ofp_header *oh, struct ibuf *ibuf)
{
uint8_t type;
if (ofp_validate_header(sc, src, dst, oh, OFP_V_1_3) != 0) {
log_debug("\tinvalid header");
return (-1);
}
if (ibuf == NULL) {
/* The response packet buffer is optional */
return (0);
}
type = oh->oh_type;
if (ofp13_callbacks[type].validate != NULL &&
ofp13_callbacks[type].validate(sc, src, dst, oh, ibuf) != 0) {
log_debug("\tinvalid packet");
return (-1);
}
return (0);
}
int
ofp13_validate_oxm_basic(struct ibuf *ibuf, off_t off, int hasmask,
uint8_t type)
{
uint8_t *ui8;
uint16_t *ui16;
uint32_t *ui32;
uint64_t *ui64;
int i, len;
char hex[8], buf[64], maskbuf[64];
switch (type) {
case OFP_XM_T_IN_PORT:
case OFP_XM_T_IN_PHY_PORT:
case OFP_XM_T_MPLS_LABEL:
if ((ui32 = ibuf_seek(ibuf, off, sizeof(*ui32))) == NULL)
return (-1);
log_debug("\t\t%u", ntohl(*ui32));
break;
case OFP_XM_T_META:
case OFP_XM_T_TUNNEL_ID:
len = sizeof(*ui64);
if (hasmask)
len *= 2;
if ((ui64 = ibuf_seek(ibuf, off, len)) == NULL)
return (-1);
if (hasmask)
log_debug("\t\t%llu mask %#16llx",
be64toh(*ui64), be64toh(*(ui64 + 1)));
else
log_debug("\t\t%llu", be64toh(*ui64));
break;
case OFP_XM_T_ETH_DST:
case OFP_XM_T_ETH_SRC:
case OFP_XM_T_ARP_SHA:
case OFP_XM_T_ARP_THA:
case OFP_XM_T_IPV6_ND_SLL:
case OFP_XM_T_IPV6_ND_TLL:
len = ETHER_ADDR_LEN;
if (hasmask)
len *= 2;
if ((ui8 = ibuf_seek(ibuf, off, len)) == NULL)
return (-1);
buf[0] = 0;
for (i = 0; i < ETHER_ADDR_LEN; i++) {
snprintf(hex, sizeof(hex), "%02x", *(ui8 + i));
strlcat(buf, hex, sizeof(buf));
}
if (hasmask) {
maskbuf[0] = 0;
for (i = 0; i < ETHER_ADDR_LEN; i++) {
snprintf(hex, sizeof(hex), "%02x", *(ui8 +
(i + ETHER_ADDR_LEN)));
strlcat(maskbuf, hex, sizeof(maskbuf));
}
log_debug("\t\t%s mask %s", buf, maskbuf);
} else
log_debug("\t\t%s", buf);
break;
case OFP_XM_T_ETH_TYPE:
if (hasmask)
return (-1);
len = sizeof(*ui16);
if ((ui16 = ibuf_seek(ibuf, off, len)) == NULL)
return (-1);
log_debug("\t\t0x%04x", ntohs(*ui16));
break;
case OFP_XM_T_ARP_OP:
case OFP_XM_T_VLAN_VID:
case OFP_XM_T_IP_PROTO:
case OFP_XM_T_TCP_SRC:
case OFP_XM_T_TCP_DST:
case OFP_XM_T_UDP_SRC:
case OFP_XM_T_UDP_DST:
case OFP_XM_T_SCTP_SRC:
case OFP_XM_T_SCTP_DST:
case OFP_XM_T_IPV6_EXTHDR:
len = sizeof(*ui16);
if (hasmask)
len *= 2;
if ((ui16 = ibuf_seek(ibuf, off, len)) == NULL)
return (-1);
if (type == OFP_XM_T_VLAN_VID) {
/* Remove the VID present bit to display. */
if (hasmask)
log_debug("\t\t%d mask %#04x",
ntohs(*ui16) & ~OFP_XM_VID_PRESENT,
ntohs(*(ui16 + 1)));
else
log_debug("\t\t%d",
ntohs(*ui16) & ~OFP_XM_VID_PRESENT);
break;
}
if (hasmask)
log_debug("\t\t%d mask %#04x",
ntohs(*ui16), ntohs(*(ui16 + 1)));
else
log_debug("\t\t%d", ntohs(*ui16));
break;
case OFP_XM_T_IP_DSCP:
case OFP_XM_T_IP_ECN:
case OFP_XM_T_ICMPV4_TYPE:
case OFP_XM_T_ICMPV4_CODE:
case OFP_XM_T_ICMPV6_TYPE:
case OFP_XM_T_ICMPV6_CODE:
case OFP_XM_T_MPLS_TC:
case OFP_XM_T_MPLS_BOS:
if ((ui8 = ibuf_seek(ibuf, off, sizeof(*ui8))) == NULL)
return (-1);
log_debug("\t\t%#02x", *ui8);
break;
case OFP_XM_T_IPV4_SRC:
case OFP_XM_T_IPV4_DST:
case OFP_XM_T_ARP_SPA:
case OFP_XM_T_ARP_TPA:
case OFP_XM_T_IPV6_FLABEL:
len = sizeof(*ui32);
if (hasmask)
len *= 2;
if ((ui32 = ibuf_seek(ibuf, off, len)) == NULL)
return (-1);
if (hasmask)
log_debug("\t\t%#08x mask %#08x",
ntohl(*ui32), ntohl(*(ui32 + 1)));
else
log_debug("\t\t%#08x", ntohl(*ui32));
break;
case OFP_XM_T_IPV6_SRC:
case OFP_XM_T_IPV6_DST:
case OFP_XM_T_IPV6_ND_TARGET:
len = sizeof(struct in6_addr);
if (hasmask)
len *= 2;
if ((ui8 = ibuf_seek(ibuf, off, len)) == NULL)
return (-1);
buf[0] = 0;
for (i = 0; i < (int)sizeof(struct in6_addr); i++) {
snprintf(hex, sizeof(hex), "%02x", *(ui8 + i));
strlcat(buf, hex, sizeof(buf));
}
if (hasmask) {
maskbuf[0] = 0;
for (i = 0; i < (int)sizeof(struct in6_addr); i++) {
snprintf(hex, sizeof(hex), "%02x", *(ui8 +
(i + sizeof(struct in6_addr))));
strlcat(maskbuf, hex, sizeof(maskbuf));
}
log_debug("\t\t%s mask %s", buf, maskbuf);
} else
log_debug("\t\t%s", buf);
break;
case OFP_XM_T_PBB_ISID:
/* TODO teach me how to read 24 bits and convert to be. */
break;
default:
log_debug("\t\tUnknown type");
return (-1);
}
return (0);
}
int
ofp13_validate_oxm(struct switchd *sc, struct ofp_ox_match *oxm,
struct ofp_header *oh, struct ibuf *ibuf, off_t off)
{
uint16_t class;
uint8_t type;
int hasmask;
/* match element is always followed by data */
if (oxm->oxm_length == 0)
return (0);
type = OFP_OXM_GET_FIELD(oxm);
hasmask = OFP_OXM_GET_HASMASK(oxm);
class = ntohs(oxm->oxm_class);
off += sizeof(*oxm);
log_debug("\tox match class %s type %s hasmask %s length %u",
print_map(class, ofp_oxm_c_map),
print_map(type, ofp_xm_t_map),
hasmask ? "yes" : "no",
oxm->oxm_length);
switch (class) {
case OFP_OXM_C_NXM_0:
case OFP_OXM_C_NXM_1:
/* TODO teach me how to read NXM_*. */
break;
case OFP_OXM_C_OPENFLOW_BASIC:
return (ofp13_validate_oxm_basic(ibuf, off, hasmask, type));
case OFP_OXM_C_OPENFLOW_EXPERIMENTER:
/* Implementation dependent: there is nothing to do here. */
break;
default:
return (-1);
}
return (0);
}
int
ofp13_validate_packet_in(struct switchd *sc,
struct sockaddr_storage *src, struct sockaddr_storage *dst,
struct ofp_header *oh, struct ibuf *ibuf)
{
struct ofp_packet_in *pin;
struct ofp_match *om;
struct ofp_ox_match *oxm;
uint8_t *p;
ssize_t len, mlen, plen;
off_t moff, off;
off = 0;
if ((pin = ibuf_seek(ibuf, off, sizeof(*pin))) == NULL)
return (-1);
log_debug("\tbuffer %d length %u reason %s table %u cookie 0x%016llx",
ntohl(pin->pin_buffer_id),
ntohs(pin->pin_total_len),
print_map(ntohs(pin->pin_reason), ofp_pktin_map),
pin->pin_table_id,
be64toh(pin->pin_cookie));
off += offsetof(struct ofp_packet_in, pin_match);
om = &pin->pin_match;
mlen = ntohs(om->om_length);
log_debug("\tmatch type %s length %zu (padded to %zu)",
print_map(ntohs(om->om_type), ofp_match_map),
mlen, OFP_ALIGN(mlen) + ETHER_ALIGN);
mlen -= sizeof(*om);
/* current match offset, aligned offset after all matches */
moff = off + sizeof(*om);
off += OFP_ALIGN(mlen) + ETHER_ALIGN;
switch (htons(om->om_type)) {
case OFP_MATCH_OXM:
do {
if ((oxm = ibuf_seek(ibuf, moff, sizeof(*oxm))) == NULL)
return (-1);
if (ofp13_validate_oxm(sc, oxm, oh, ibuf, moff) == -1)
return (-1);
moff += sizeof(*oxm) + oxm->oxm_length;
mlen -= sizeof(*oxm) + oxm->oxm_length;
} while (mlen > 0 && oxm->oxm_length);
break;
case OFP_MATCH_STANDARD:
/* deprecated */
break;
}
len = ntohs(pin->pin_total_len);
plen = ibuf_length(ibuf) - off;
if (plen < len) {
log_debug("\ttruncated packet %zu < %zu", plen, len);
/* Buffered packets can be truncated */
if (pin->pin_buffer_id != OFP_PKTOUT_NO_BUFFER)
len = plen;
else
return (-1);
}
if ((p = ibuf_seek(ibuf, off, len)) == NULL)
return (-1);
if (sc->sc_tap != -1)
(void)write(sc->sc_tap, p, len);
return (0);
}
int
ofp13_validate_packet_out(struct switchd *sc,
struct sockaddr_storage *src, struct sockaddr_storage *dst,
struct ofp_header *oh, struct ibuf *ibuf)
{
struct ofp_packet_out *pout;
size_t len;
off_t off;
struct ofp_action_header *ah;
struct ofp_action_output *ao;
off = 0;
if ((pout = ibuf_seek(ibuf, off, sizeof(*pout))) == NULL) {
log_debug("%s: seek failed: length %zd",
__func__, ibuf_length(ibuf));
return (-1);
}
log_debug("\tbuffer %d port %s "
"actions length %u",
ntohl(pout->pout_buffer_id),
print_map(ntohl(pout->pout_in_port), ofp_port_map),
ntohs(pout->pout_actions_len));
len = ntohl(pout->pout_actions_len);
off += sizeof(*pout);
while ((ah = ibuf_seek(ibuf, off, len)) != NULL &&
ntohs(ah->ah_len) >= (uint16_t)sizeof(*ah)) {
switch (ntohs(ah->ah_type)) {
case OFP_ACTION_OUTPUT:
ao = (struct ofp_action_output *)ah;
log_debug("\t\taction type %s length %d "
"port %s max length %d",
print_map(ntohs(ao->ao_type), ofp_action_map),
ntohs(ao->ao_len),
print_map(ntohs(ao->ao_port), ofp_port_map),
ntohs(ao->ao_max_len));
break;
default:
log_debug("\t\taction type %s length %d",
print_map(ntohs(ah->ah_type), ofp_action_map),
ntohs(ah->ah_len));
break;
}
if (pout->pout_buffer_id == (uint32_t)-1)
break;
off += ntohs(ah->ah_len);
}
return (0);
}
int
ofp13_validate_error(struct switchd *sc,
struct sockaddr_storage *src, struct sockaddr_storage *dst,
struct ofp_header *oh, struct ibuf *ibuf)
{
struct ofp_error *err;
off_t off;
const char *code;
off = 0;
if ((err = ibuf_seek(ibuf, off, sizeof(*err))) == NULL) {
log_debug("%s: seek failed: length %zd",
__func__, ibuf_length(ibuf));
return (-1);
}
switch (ntohs(err->err_type)) {
case OFP_ERRTYPE_FLOW_MOD_FAILED:
code = print_map(ntohs(err->err_code), ofp_errflowmod_map);
break;
case OFP_ERRTYPE_BAD_MATCH:
code = print_map(ntohs(err->err_code), ofp_errmatch_map);
break;
case OFP_ERRTYPE_BAD_INSTRUCTION:
code = print_map(ntohs(err->err_code), ofp_errinst_map);
break;
case OFP_ERRTYPE_BAD_REQUEST:
code = print_map(ntohs(err->err_code), ofp_errreq_map);
break;
default:
code = NULL;
break;
}
log_debug("\terror type %s code %u%s%s",
print_map(ntohs(err->err_type), ofp_errtype_map),
ntohs(err->err_code),
code == NULL ? "" : ": ",
code == NULL ? "" : code);
return (0);
}
int
ofp13_input(struct switchd *sc, struct switch_connection *con,
struct ofp_header *oh, struct ibuf *ibuf)
{
if (ofp13_validate(sc, &con->con_peer, &con->con_local, oh, ibuf) != 0)
return (-1);
if (ofp13_callbacks[oh->oh_type].cb == NULL) {
log_debug("%s: message not supported: %s", __func__,
print_map(oh->oh_type, ofp_t_map));
return (-1);
}
if (ofp13_callbacks[oh->oh_type].cb(sc, con, oh, ibuf) != 0) {
log_debug("%s: message parsing failed: %s", __func__,
print_map(oh->oh_type, ofp_t_map));
return (-1);
}
return (0);
}
int
ofp13_hello(struct switchd *sc, struct switch_connection *con,
struct ofp_header *oh, struct ibuf *ibuf)
{
if (switch_add(con) == NULL) {
log_debug("%s: failed to add switch", __func__);
return (-1);
}
/* Echo back the received Hello packet */
oh->oh_version = OFP_V_1_3;
oh->oh_length = htons(sizeof(*oh));
oh->oh_xid = htonl(con->con_xidnxt++);
if (ofp13_validate(sc, &con->con_local, &con->con_peer, oh, NULL) != 0)
return (-1);
ofp_output(con, oh, NULL);
/* Ask for switch features so we can get more information. */
if (ofp13_featuresrequest(sc, con) == -1)
return (-1);
return (0);
}
int
ofp13_echo_request(struct switchd *sc, struct switch_connection *con,
struct ofp_header *oh, struct ibuf *ibuf)
{
/* Echo reply */
oh->oh_type = OFP_T_ECHO_REPLY;
if (ofp13_validate(sc, &con->con_local, &con->con_peer, oh, NULL) != 0)
return (-1);
ofp_output(con, oh, NULL);
return (0);
}
int
ofp13_validate_features_reply(struct switchd *sc,
struct sockaddr_storage *src, struct sockaddr_storage *dst,
struct ofp_header *oh, struct ibuf *ibuf)
{
struct ofp_switch_features *swf;
if ((swf = ibuf_seek(ibuf, 0, sizeof(*swf))) == NULL)
return (-1);
log_debug("\tdatapath_id %#llx nbuffers %u ntables %d aux_id %d "
"capabilities %#08x actions %#08x",
be64toh(swf->swf_datapath_id), ntohl(swf->swf_nbuffers),
swf->swf_ntables, swf->swf_aux_id, ntohl(swf->swf_capabilities),
ntohl(swf->swf_actions));
return (0);
}
int
ofp13_features_reply(struct switchd *sc, struct switch_connection *con,
struct ofp_header *oh, struct ibuf *ibuf)
{
#if 0
/* Let's not ask this while we don't use it. */
ofp13_flow_stats(sc, con, OFP_PORT_ANY, OFP_GROUP_ANY,
OFP_TABLE_ID_ALL);
ofp13_table_features(sc, con, 0);
ofp13_desc(sc, con);
#endif
ofp13_setconfig(sc, con, OFP_CONFIG_FRAG_NORMAL,
OFP_CONTROLLER_MAXLEN_NO_BUFFER);
/* Use table '0' for switch(4) and '100' for HP 3800. */
ofp13_tablemiss_sendctrl(sc, con, 0);
return (0);
}
int
ofp13_validate_action(struct switchd *sc, struct ofp_header *oh,
struct ibuf *ibuf, off_t *off, struct ofp_action_header *ah)
{
struct ofp_action_output *ao;
struct ofp_action_mpls_ttl *amt;
struct ofp_action_push *ap;
struct ofp_action_pop_mpls *apm;
struct ofp_action_group *ag;
struct ofp_action_nw_ttl *ant;
struct ofp_action_set_field *asf;
struct ofp_action_set_queue *asq;
struct ofp_ox_match *oxm;
size_t len;
int type;
off_t moff;
type = ntohs(ah->ah_type);
len = ntohs(ah->ah_len);
switch (type) {
case OFP_ACTION_OUTPUT:
if (len != sizeof(*ao))
return (-1);
if ((ao = ibuf_seek(ibuf, *off, sizeof(*ao))) == NULL)
return (-1);
*off += len;
log_debug("\t\taction %s len %lu port %s max_len %d",
print_map(type, ofp_action_map), len,
print_map(ntohl(ao->ao_port), ofp_port_map),
ntohs(ao->ao_max_len));
break;
case OFP_ACTION_SET_MPLS_TTL:
if (len != sizeof(*amt))
return (-1);
if ((amt = ibuf_seek(ibuf, *off, sizeof(*amt))) == NULL)
return (-1);
*off += len;
log_debug("\t\taction %s len %lu ttl %d",
print_map(type, ofp_action_map), len, amt->amt_ttl);
break;
case OFP_ACTION_PUSH_VLAN:
case OFP_ACTION_PUSH_MPLS:
case OFP_ACTION_PUSH_PBB:
if (len != sizeof(*ap))
return (-1);
if ((ap = ibuf_seek(ibuf, *off, sizeof(*ap))) == NULL)
return (-1);
*off += len;
log_debug("\t\taction %s len %lu ethertype %#04x",
print_map(type, ofp_action_map), len,
ntohs(ap->ap_ethertype));
break;
case OFP_ACTION_POP_MPLS:
if (len != sizeof(*apm))
return (-1);
if ((apm = ibuf_seek(ibuf, *off, sizeof(*apm))) == NULL)
return (-1);
*off += len;
log_debug("\t\taction %s len %lu ethertype %#04x",
print_map(type, ofp_action_map), len,
ntohs(apm->apm_ethertype));
break;
case OFP_ACTION_SET_QUEUE:
if (len != sizeof(*asq))
return (-1);
if ((asq = ibuf_seek(ibuf, *off, sizeof(*asq))) == NULL)
return (-1);
*off += len;
log_debug("\t\taction %s len %lu queue_id %u",
print_map(type, ofp_action_map), len,
ntohl(asq->asq_queue_id));
break;
case OFP_ACTION_GROUP:
if (len != sizeof(*ag))
return (-1);
if ((ag = ibuf_seek(ibuf, *off, sizeof(*ag))) == NULL)
return (-1);
*off += len;
log_debug("\t\taction %s len %lu group_id %u",
print_map(type, ofp_action_map), len,
ntohl(ag->ag_group_id));
break;
case OFP_ACTION_SET_NW_TTL:
if (len != sizeof(*ant))
return (-1);
if ((ant = ibuf_seek(ibuf, *off, sizeof(*ant))) == NULL)
return (-1);
*off += len;
log_debug("\t\taction %s len %lu ttl %d",
print_map(type, ofp_action_map), len, ant->ant_ttl);
break;
case OFP_ACTION_SET_FIELD:
if (len < sizeof(*asf))
return (-1);
if ((asf = ibuf_seek(ibuf, *off, sizeof(*asf))) == NULL)
return (-1);
moff = *off + sizeof(*asf) - sizeof(asf->asf_field);
*off += len;
log_debug("\t\taction %s len %lu",
print_map(type, ofp_action_map), len);
len -= sizeof(*asf) - sizeof(asf->asf_field);
while (len) {
if ((oxm = ibuf_seek(ibuf, moff, sizeof(*oxm)))
== NULL)
return (-1);
if (ofp13_validate_oxm(sc, oxm, oh, ibuf, moff) == -1)
return (-1);
len -= sizeof(*oxm) - oxm->oxm_length;
moff += sizeof(*oxm) - oxm->oxm_length;
}
break;
default:
if (len < sizeof(*ah))
return (-1);
/* Generic header without information. */
*off += len;
log_debug("\t\taction %s len %lu",
print_map(type, ofp_action_map), len);
break;
}
return (0);
}
int
ofp13_validate_instruction(struct switchd *sc, struct ofp_header *oh,
struct ibuf *ibuf, off_t *off, struct ofp_instruction *i)
{
struct ofp_instruction_actions *ia;
struct ofp_instruction_goto_table *igt;
struct ofp_instruction_write_metadata *iwm;
struct ofp_instruction_meter *im;
struct ofp_action_header *ah;
int type;
size_t len;
off_t oldoff, diff;
type = ntohs(i->i_type);
len = ntohs(i->i_len);
switch (type) {
case OFP_INSTRUCTION_T_GOTO_TABLE:
if (len != sizeof(*igt))
return (-1);
if ((igt = ibuf_seek(ibuf, *off, sizeof(*igt))) == NULL)
return (-1);
*off += len;
log_debug("\tinstruction %s length %lu table_id %d",
print_map(type, ofp_instruction_t_map), len,
igt->igt_table_id);
break;
case OFP_INSTRUCTION_T_WRITE_META:
if (len != sizeof(*iwm))
return (-1);
if ((iwm = ibuf_seek(ibuf, *off, sizeof(*iwm))) == NULL)
return (-1);
*off += len;
log_debug("\tinstruction %s length %lu "
"metadata %llu mask %llu",
print_map(type, ofp_instruction_t_map), len,
be64toh(iwm->iwm_metadata),
be64toh(iwm->iwm_metadata_mask));
break;
case OFP_INSTRUCTION_T_METER:
if (len != sizeof(*im))
return (-1);
if ((im = ibuf_seek(ibuf, *off, sizeof(*im))) == NULL)
return (-1);
*off += len;
log_debug("\tinstruction %s length %lu meter_id %d",
print_map(type, ofp_instruction_t_map), len,
im->im_meter_id);
break;
case OFP_INSTRUCTION_T_WRITE_ACTIONS:
case OFP_INSTRUCTION_T_CLEAR_ACTIONS:
case OFP_INSTRUCTION_T_APPLY_ACTIONS:
if (len < sizeof(*ia))
return (-1);
if ((ia = ibuf_seek(ibuf, *off, sizeof(*ia))) == NULL)
return (-1);
log_debug("\tinstruction %s length %lu",
print_map(type, ofp_instruction_t_map), len);
*off += sizeof(*ia);
len -= sizeof(*ia);
while (len) {
oldoff = *off;
if ((ah = ibuf_seek(ibuf, *off, sizeof(*ah))) == NULL ||
ofp13_validate_action(sc, oh, ibuf, off, ah) == -1)
return (-1);
diff = *off - oldoff;
/* Loop prevention. */
if (*off < oldoff || diff == 0)
break;
len -= diff;
}
break;
default:
if (len < sizeof(*i))
return (-1);
log_debug("\tinstruction %s length %lu",
print_map(type, ofp_instruction_t_map), len);
*off += len;
break;
}
return (0);
}
int
ofp13_validate_flow_mod(struct switchd *sc,
struct sockaddr_storage *src, struct sockaddr_storage *dst,
struct ofp_header *oh, struct ibuf *ibuf)
{
struct ofp_flow_mod *fm;
struct ofp_match *om;
struct ofp_instruction *i;
struct ofp_ox_match *oxm;
off_t off, moff, offdiff;
int matchlen, matchtype, left;
off = 0;
if ((fm = ibuf_seek(ibuf, off, sizeof(*fm))) == NULL)
return (-1);
log_debug("\tcommand %s table %d timeout (idle %d hard %d) "
"priority %d buffer_id %u out_port %u out_group %u "
"flags %#04x cookie %llu mask %llu",
print_map(fm->fm_command, ofp_flowcmd_map),
fm->fm_table_id, ntohs(fm->fm_idle_timeout),
ntohs(fm->fm_hard_timeout), ntohs(fm->fm_priority),
ntohl(fm->fm_buffer_id), ntohl(fm->fm_out_port),
ntohl(fm->fm_out_group), ntohs(fm->fm_flags),
be64toh(fm->fm_cookie), be64toh(fm->fm_cookie_mask));
off += offsetof(struct ofp_flow_mod, fm_match);
om = &fm->fm_match;
matchtype = ntohs(om->om_type);
matchlen = ntohs(om->om_length);
moff = off + sizeof(*om);
off += OFP_ALIGN(matchlen);
matchlen -= sizeof(*om);
while (matchlen) {
if ((oxm = ibuf_seek(ibuf, moff, sizeof(*oxm))) == NULL ||
ofp13_validate_oxm(sc, oxm, oh, ibuf, moff) == -1)
return (-1);
moff += sizeof(*oxm) + oxm->oxm_length;
matchlen -= sizeof(*oxm) + oxm->oxm_length;
}
left = ntohs(oh->oh_length) - off;
moff = off;
while (left) {
if ((i = ibuf_seek(ibuf, moff, sizeof(*i))) == NULL ||
ofp13_validate_instruction(sc, oh, ibuf, &moff, i) == -1)
return (-1);
offdiff = moff - off;
/* Loop prevention. */
if (moff < off || offdiff == 0)
break;
left -= offdiff;
off = moff;
}
return (0);
}
int
ofp13_packet_match(struct ibuf *ibuf, struct packet *pkt, struct ofp_match *om)
{
struct ether_header *eh = pkt->pkt_eh;
size_t padsize, startpos, endpos, omlen;
if (eh == NULL)
return (-1);
startpos = ibuf->wpos;
if (oxm_etheraddr(ibuf, 1, eh->ether_shost, NULL) == -1)
return (-1);
if (oxm_etheraddr(ibuf, 0, eh->ether_dhost, NULL) == -1)
return (-1);
endpos = ibuf->wpos;
omlen = sizeof(*om) + (endpos - startpos);
padsize = OFP_ALIGN(omlen) - omlen;
om->om_type = htons(OFP_MATCH_OXM);
om->om_length = htons(omlen);
if (padsize && ibuf_advance(ibuf, padsize) == NULL)
return (-1);
return (0);
}
int
ofp13_packet_in(struct switchd *sc, struct switch_connection *con,
struct ofp_header *ih, struct ibuf *ibuf)
{
struct ofp_packet_in *pin;
struct ofp_packet_out *pout;
struct ofp_flow_mod *fm;
struct ofp_header *oh;
struct ofp_match *om;
struct ofp_ox_match *oxm;
struct packet pkt;
struct ibuf *obuf = NULL;
int ret = -1;
ssize_t len, mlen;
uint32_t srcport = 0, dstport;
int addflow = 0, sendbuffer = 0;
off_t off, moff;
void *ptr;
struct ofp_instruction_actions *ia;
if ((pin = ibuf_getdata(ibuf, sizeof(*pin))) == NULL)
return (-1);
/* We only handle no matches right now. */
if (pin->pin_reason != OFP_PKTIN_REASON_NO_MATCH)
return (-1);
bzero(&pkt, sizeof(pkt));
len = ntohs(pin->pin_total_len);
/* very basic way of getting the source port */
om = &pin->pin_match;
mlen = ntohs(om->om_length);
off = (OFP_ALIGN(mlen) + ETHER_ALIGN) - sizeof(pin->pin_match);
moff = ibuf_dataoffset(ibuf);
do {
if ((oxm = ibuf_seek(ibuf, moff, sizeof(*oxm))) == NULL)
return (-1);
/* Find IN_PORT */
switch (ntohs(oxm->oxm_class)) {
case OFP_OXM_C_OPENFLOW_BASIC:
switch (OFP_OXM_GET_FIELD(oxm)) {
case OFP_XM_T_IN_PORT:
moff += sizeof(*oxm);
if ((ptr = ibuf_seek(ibuf, moff,
sizeof(srcport))) == NULL)
return (-1);
srcport = htonl(*(uint32_t *)ptr);
mlen = 0; /* break loop */
break;
default:
/* ignore unsupported match types */
break;
}
default:
/* ignore unsupported match classes */
break;
}
moff += sizeof(*oxm) + oxm->oxm_length;
mlen -= sizeof(*oxm) + oxm->oxm_length;
} while (mlen > 0 && oxm->oxm_length);
/* Skip all matches and seek to the packet */
if (ibuf_getdata(ibuf, off) == NULL)
return (-1);
if (packet_input(sc, con->con_switch,
srcport, &dstport, ibuf, len, &pkt) == -1 ||
dstport > OFP_PORT_MAX) {
/* fallback to flooding */
dstport = OFP_PORT_FLOOD;
} else if (srcport == dstport) {
/*
* silently drop looping packet
* (don't use OFP_PORT_INPUT here)
*/
dstport = OFP_PORT_ANY;
}
if (dstport <= OFP_PORT_MAX)
addflow = 1;
if ((obuf = ibuf_static()) == NULL)
goto done;
again:
if (addflow) {
if ((fm = ibuf_advance(obuf, sizeof(*fm))) == NULL)
goto done;
oh = &fm->fm_oh;
fm->fm_cookie = 0; /* XXX should we set a cookie? */
fm->fm_command = htons(OFP_FLOWCMD_ADD);
fm->fm_idle_timeout = htons(sc->sc_cache_timeout);
fm->fm_hard_timeout = 0; /* permanent */
fm->fm_priority = 0;
fm->fm_buffer_id = pin->pin_buffer_id;
fm->fm_flags = htons(OFP_FLOWFLAG_SEND_FLOW_REMOVED);
if (pin->pin_buffer_id == OFP_PKTOUT_NO_BUFFER)
sendbuffer = 1;
/* Write flow matches to create an entry. */
if (ofp13_packet_match(obuf, &pkt, &fm->fm_match) == -1)
goto done;
/*
* Write the instruction action header and add the output
* action.
*/
if ((ia = ibuf_advance(obuf, sizeof(*ia))) == NULL ||
action_output(obuf, dstport,
OFP_CONTROLLER_MAXLEN_NO_BUFFER) == -1)
goto done;
ia->ia_type = htons(OFP_INSTRUCTION_T_APPLY_ACTIONS);
ia->ia_len = htons(sizeof(*ia) +
sizeof(struct ofp_action_output));
} else {
if ((pout = ibuf_advance(obuf, sizeof(*pout))) == NULL)
goto done;
oh = &pout->pout_oh;
pout->pout_buffer_id = pin->pin_buffer_id;
pout->pout_in_port = htonl(srcport);
pout->pout_actions_len =
htons(sizeof(struct ofp_action_output));
if (action_output(obuf, dstport,
OFP_CONTROLLER_MAXLEN_NO_BUFFER) == -1)
goto done;
/* Add optional packet payload */
if (pin->pin_buffer_id == OFP_PKTOUT_NO_BUFFER &&
imsg_add(obuf, pkt.pkt_buf, pkt.pkt_len) == -1)
goto done;
}
/* Set output header */
oh->oh_version = OFP_V_1_3;
oh->oh_length = htons(ibuf_length(obuf));
oh->oh_type = addflow ? OFP_T_FLOW_MOD : OFP_T_PACKET_OUT;
oh->oh_xid = htonl(con->con_xidnxt++);
if (ofp13_validate(sc, &con->con_local, &con->con_peer, oh, obuf) != 0)
return (-1);
ofp_output(con, NULL, obuf);
if (sendbuffer) {
ibuf_release(obuf);
/* loop to output the packet again */
addflow = sendbuffer = 0;
if ((obuf = ibuf_static()) == NULL)
goto done;
goto again;
}
ret = 0;
done:
ibuf_release(obuf);
return (ret);
}
int
ofp13_flow_removed(struct switchd *sc, struct switch_connection *con,
struct ofp_header *ih, struct ibuf *ibuf)
{
struct ofp_flow_removed *fr;
if ((fr = ibuf_getdata(ibuf, sizeof(*fr))) == NULL)
return (-1);
log_debug("%s: cookie:%llu priority:%d reason:%d tableid:%d "
"duration(%u sec, %u nsec) idleto:%d hard:%d packet:%llu byte:%llu",
__func__, be64toh(fr->fr_cookie), ntohs(fr->fr_priority),
fr->fr_reason, fr->fr_table_id, ntohl(fr->fr_duration_sec),
ntohl(fr->fr_duration_nsec), ntohs(fr->fr_idle_timeout),
ntohs(fr->fr_hard_timeout),
be64toh(fr->fr_packet_count), be64toh(fr->fr_byte_count));
switch (fr->fr_reason) {
case OFP_FLOWREM_REASON_IDLE_TIMEOUT:
log_debug("\tReason: IDLE TIMEOUT");
break;
case OFP_FLOWREM_REASON_HARD_TIMEOUT:
log_debug("\tReason: HARD TIMEOUT");
break;
case OFP_FLOWREM_REASON_DELETE:
log_debug("\tReason: DELETE");
break;
case OFP_FLOWREM_REASON_GROUP_DELETE:
log_debug("\tReason: GROUP DELETE");
break;
default:
log_debug("\tReason: UNKNOWN");
break;
}
return (0);
}
int
ofp13_parse_instruction(struct ibuf *ibuf, struct ofp_instruction *i)
{
int type;
int len;
type = ntohs(i->i_type);
len = ntohs(i->i_len);
log_debug("\t\t%s", print_map(type, ofp_instruction_t_map));
return (len);
}
int
ofp13_parse_action(struct ibuf *ibuf, struct ofp_action_header *ah)
{
int len, type;
len = htons(ah->ah_len);
type = htons(ah->ah_type);
log_debug("\t\t%s", print_map(type, ofp_action_map));
return (len);
}
int
ofp13_parse_oxm(struct ibuf *ibuf, struct ofp_ox_match *oxm)
{
int length, type, class, hasmask;
class = ntohs(oxm->oxm_class);
type = OFP_OXM_GET_FIELD(oxm);
hasmask = OFP_OXM_GET_HASMASK(oxm);
/*
* XXX the OpenFlow 1.3.5 specification says this field is only
* 4 bytes long, however the experimental type is 8 bytes.
*/
length = sizeof(*oxm);
log_debug("\t\t%s hasmask %s type %s",
print_map(class, ofp_oxm_c_map), hasmask ? "yes" : "no",
print_map(type, ofp_xm_t_map));
if (class == OFP_OXM_C_OPENFLOW_EXPERIMENTER) {
/* Get the last bytes. */
if (ibuf_getdata(ibuf, 4) == NULL)
return (-1);
return (8);
}
return (length);
}
int
ofp13_parse_tableproperties(struct ibuf *ibuf, struct ofp_table_features *tf)
{
struct ofp_table_feature_property *tp;
struct ofp_instruction *i;
struct ofp_action_header *ah;
struct ofp_ox_match *oxm;
uint8_t *next_table;
int remaining, type, length;
int totallen, padsize, rv;
log_debug("Table %s (%d): max_entries %u config %u",
tf->tf_name, tf->tf_tableid, ntohl(tf->tf_max_entries),
ntohl(tf->tf_config));
totallen = htons(tf->tf_length);
remaining = totallen - sizeof(*tf);
next_table_property:
if ((tp = ibuf_getdata(ibuf, sizeof(*tp))) == NULL)
return (-1);
type = ntohs(tp->tp_type);
length = ntohs(tp->tp_length);
/* Calculate the padding. */
padsize = OFP_ALIGN(length) - length;
remaining -= OFP_ALIGN(length);
length -= sizeof(*tp);
log_debug("\t%s:", print_map(type, ofp_table_featprop_map));
if (length == 0)
log_debug("\t\tNONE");
switch (type) {
case OFP_TABLE_FEATPROP_INSTRUCTION:
case OFP_TABLE_FEATPROP_INSTRUCTION_MISS:
while (length) {
if ((i = ibuf_getdata(ibuf, sizeof(*i))) == NULL)
return (-1);
if ((rv = ofp13_parse_instruction(ibuf, i)) == -1)
return (-1);
length -= rv;
}
break;
case OFP_TABLE_FEATPROP_NEXT_TABLES:
case OFP_TABLE_FEATPROP_NEXT_TABLES_MISS:
while (length) {
if ((next_table = ibuf_getdata(ibuf,
sizeof(*next_table))) == NULL)
return (-1);
log_debug("\t\t%d", *next_table);
length -= sizeof(*next_table);
}
break;
case OFP_TABLE_FEATPROP_WRITE_ACTIONS:
case OFP_TABLE_FEATPROP_WRITE_ACTIONS_MISS:
case OFP_TABLE_FEATPROP_APPLY_ACTIONS:
case OFP_TABLE_FEATPROP_APPLY_ACTIONS_MISS:
while (length) {
/*
* XXX the OpenFlow 1.3.5 specs says that we only
* get 4 bytes here instead of the full OXM.
*/
if ((ah = ibuf_getdata(ibuf, 4)) == NULL)
return (-1);
if ((rv = ofp13_parse_action(ibuf, ah)) == -1)
return (-1);
length -= rv;
}
break;
case OFP_TABLE_FEATPROP_MATCH:
case OFP_TABLE_FEATPROP_WILDCARDS:
case OFP_TABLE_FEATPROP_WRITE_SETFIELD:
case OFP_TABLE_FEATPROP_WRITE_SETFIELD_MISS:
case OFP_TABLE_FEATPROP_APPLY_SETFIELD:
case OFP_TABLE_FEATPROP_APPLY_SETFIELD_MISS:
while (length) {
if ((oxm = ibuf_getdata(ibuf, sizeof(*oxm))) == NULL)
return (-1);
if ((rv = ofp13_parse_oxm(ibuf, oxm)) == -1)
return (-1);
length -= rv;
}
break;
default:
log_debug("%s: unsupported property type: %d", __func__, type);
/* Skip this field and try to continue otherwise fail. */
if (ibuf_getdata(ibuf, length) == NULL)
return (-1);
break;
}
/* Skip the padding and read the next property if any. */
if (padsize && ibuf_getdata(ibuf, padsize) == NULL)
return (-1);
if (remaining)
goto next_table_property;
return (totallen);
}
int
ofp13_multipart_reply(struct switchd *sc, struct switch_connection *con,
struct ofp_header *oh, struct ibuf *ibuf)
{
struct ofp_multipart *mp;
struct ofp_table_features *tf;
int readlen, type, flags;
int remaining;
if ((mp = ibuf_getdata(ibuf, sizeof(*mp))) == NULL)
return (-1);
type = ntohs(mp->mp_type);
flags = ntohs(mp->mp_flags);
remaining = ntohs(oh->oh_length) - sizeof(*mp);
if (flags & OFP_MP_FLAG_REPLY_MORE) {
if (ofp_multipart_add(con, oh->oh_xid, type) == -1) {
ofp13_error(sc, con, oh, ibuf,
OFP_ERRTYPE_BAD_REQUEST,
OFP_ERRREQ_MULTIPART_OVERFLOW);
ofp_multipart_del(con, oh->oh_xid);
return (0);
}
/*
* We don't need to concatenate with other messages,
* because the specification says that switches don't
* break objects. We should only need this if our parser
* requires the whole data before hand, but then we have
* better ways to achieve the same thing.
*/
} else
ofp_multipart_del(con, oh->oh_xid);
switch (type) {
case OFP_MP_T_TABLE_FEATURES:
read_next_table:
if ((tf = ibuf_getdata(ibuf, sizeof(*tf))) == NULL)
return (-1);
if ((readlen = ofp13_parse_tableproperties(ibuf, tf)) == -1)
return (-1);
remaining -= readlen;
if (remaining)
goto read_next_table;
break;
}
return (0);
}
int
ofp13_multipart_reply_validate(struct switchd *sc,
struct sockaddr_storage *src, struct sockaddr_storage *dst,
struct ofp_header *oh, struct ibuf *ibuf)
{
struct ofp_multipart *mp;
struct ofp_table_features *tf;
struct ofp_flow_stats *fs;
struct ofp_desc *d;
struct ofp_match *om;
struct ofp_ox_match *oxm;
int mptype, mpflags, hlen;
int remaining, matchlen, matchtype;
off_t off, moff;
remaining = ntohs(oh->oh_length);
off = 0;
if ((mp = ibuf_seek(ibuf, off, sizeof(*mp))) == NULL)
return (-1);
mptype = ntohs(mp->mp_type);
mpflags = ntohs(mp->mp_flags);
log_debug("\ttype %s flags %#04x",
print_map(mptype, ofp_mp_t_map), mpflags);
off += sizeof(*mp);
remaining -= sizeof(*mp);
if (remaining == 0) {
log_debug("\tempty reply");
return (0);
}
switch (mptype) {
case OFP_MP_T_DESC:
if ((d = ibuf_seek(ibuf, off, sizeof(*d))) == NULL)
return (-1);
off += sizeof(*d);
remaining -= sizeof(*d);
log_debug("\tmfr_desc \"%s\" hw_desc \"%s\" sw_desc \"%s\" "
"serial_num \"%s\" dp_desc \"%s\"",
d->d_mfr_desc, d->d_hw_desc, d->d_sw_desc,
d->d_serial_num, d->d_dp_desc);
break;
case OFP_MP_T_FLOW:
read_next_flow:
if ((fs = ibuf_seek(ibuf, off, sizeof(*fs))) == NULL)
return (-1);
hlen = ntohs(fs->fs_length);
remaining -= hlen;
moff = off + sizeof(*fs);
off += hlen;
log_debug("\tflow length %d table_id %d duration sec %u "
"nsec %u prio %d timeout idle %d hard %d flags %#04x "
"cookie %llu packet count %llu byte count %llu",
hlen, fs->fs_table_id, ntohl(fs->fs_duration_sec),
ntohl(fs->fs_duration_nsec), ntohs(fs->fs_priority),
ntohs(fs->fs_idle_timeout), ntohs(fs->fs_hard_timeout),
ntohs(fs->fs_flags), be64toh(fs->fs_cookie),
be64toh(fs->fs_packet_count), be64toh(fs->fs_byte_count));
om = &fs->fs_match;
matchtype = ntohs(om->om_type);
matchlen = ntohs(om->om_length) - sizeof(*om);
/* We don't know how to parse anything else yet. */
if (matchtype != OFP_MATCH_OXM)
break;
while (matchlen) {
if ((oxm = ibuf_seek(ibuf, moff, sizeof(*oxm))) == NULL)
return (-1);
if (ofp13_validate_oxm(sc, oxm, oh, ibuf, moff) == -1)
return (-1);
moff += sizeof(*oxm) + oxm->oxm_length;
matchlen -= sizeof(*oxm) + oxm->oxm_length;
}
if (remaining)
goto read_next_flow;
break;
case OFP_MP_T_AGGREGATE:
case OFP_MP_T_TABLE:
case OFP_MP_T_PORT_STATS:
case OFP_MP_T_QUEUE:
case OFP_MP_T_GROUP:
case OFP_MP_T_GROUP_DESC:
case OFP_MP_T_GROUP_FEATURES:
case OFP_MP_T_METER:
case OFP_MP_T_METER_CONFIG:
case OFP_MP_T_METER_FEATURES:
break;
case OFP_MP_T_TABLE_FEATURES:
read_next_table:
if ((tf = ibuf_seek(ibuf, off, sizeof(*tf))) == NULL)
return (-1);
hlen = ntohs(tf->tf_length);
log_debug("\ttable features length %d tableid %d name \"%s\" "
"metadata match %llu write %llu config %u max_entries %u",
hlen, tf->tf_tableid, tf->tf_name,
be64toh(tf->tf_metadata_match),
be64toh(tf->tf_metadata_write), ntohl(tf->tf_config),
ntohl(tf->tf_max_entries));
remaining -= hlen;
off += hlen;
if (remaining)
goto read_next_table;
break;
case OFP_MP_T_PORT_DESC:
case OFP_MP_T_EXPERIMENTER:
break;
default:
return (-1);
}
return (0);
}
/* Don't forget to update mp->mp_oh.oh_length */
struct ofp_multipart *
ofp13_multipart_request(struct switch_connection *con, struct ibuf *ibuf,
uint16_t type, uint16_t flags)
{
struct ofp_multipart *mp;
struct ofp_header *oh;
if ((mp = ibuf_advance(ibuf, sizeof(*mp))) == NULL)
return (NULL);
oh = &mp->mp_oh;
oh->oh_version = OFP_V_1_3;
oh->oh_type = OFP_T_MULTIPART_REQUEST;
oh->oh_xid = htonl(con->con_xidnxt++);
mp->mp_type = htons(type);
mp->mp_flags = htons(flags);
return (mp);
}
int
ofp13_multipart_request_validate(struct switchd *sc,
struct sockaddr_storage *src, struct sockaddr_storage *dst,
struct ofp_header *oh, struct ibuf *ibuf)
{
struct ofp_multipart *mp;
struct ofp_match *om;
struct ofp_flow_stats_request *fsr;
struct ofp_ox_match *oxm;
off_t off, moff;
int type, flags, totallen;
int matchlen, matchtype;
off = 0;
if ((mp = ibuf_seek(ibuf, off, sizeof(*mp))) == NULL)
return (-1);
type = ntohs(mp->mp_type);
flags = ntohs(mp->mp_flags);
log_debug("\ttype %s flags %#04x",
print_map(type, ofp_mp_t_map), flags);
totallen = ntohs(oh->oh_length);
off += sizeof(*mp);
switch (type) {
case OFP_MP_T_DESC:
/* This type doesn't use a payload. */
if (totallen != sizeof(*mp))
return (-1);
break;
case OFP_MP_T_FLOW:
if ((fsr = ibuf_seek(ibuf, off, sizeof(*fsr))) == NULL)
return (-1);
om = &fsr->fsr_match;
matchtype = ntohs(om->om_type);
matchlen = ntohs(om->om_length);
log_debug("\ttable_id %d out_port %u out_group %u "
"cookie %llu mask %llu match type %s length %d "
"(padded to %d)",
fsr->fsr_table_id, ntohl(fsr->fsr_out_port),
ntohl(fsr->fsr_out_group), be64toh(fsr->fsr_cookie),
be64toh(fsr->fsr_cookie_mask),
print_map(matchtype, ofp_match_map), matchlen,
OFP_ALIGN(matchlen));
/* Get the first OXM byte offset. */
moff = off + sizeof(*fsr);
/* Don't count the header bytes. */
matchlen -= sizeof(*om);
/* We don't know how to parse anything else yet. */
if (matchtype != OFP_MATCH_OXM)
break;
while (matchlen) {
if ((oxm = ibuf_seek(ibuf, moff, sizeof(*oxm))) == NULL)
return (-1);
if (ofp13_validate_oxm(sc, oxm, oh, ibuf, moff) == -1)
return (-1);
moff += sizeof(*oxm) + oxm->oxm_length;
matchlen -= sizeof(*oxm) + oxm->oxm_length;
}
break;
case OFP_MP_T_AGGREGATE:
case OFP_MP_T_TABLE:
case OFP_MP_T_PORT_STATS:
case OFP_MP_T_QUEUE:
case OFP_MP_T_GROUP:
case OFP_MP_T_GROUP_DESC:
case OFP_MP_T_GROUP_FEATURES:
case OFP_MP_T_METER:
case OFP_MP_T_METER_CONFIG:
case OFP_MP_T_METER_FEATURES:
break;
case OFP_MP_T_TABLE_FEATURES:
if (totallen == sizeof(*mp)) {
log_debug("\tempty table properties request");
break;
}
break;
case OFP_MP_T_PORT_DESC:
case OFP_MP_T_EXPERIMENTER:
break;
default:
return (-1);
}
return (0);
}
int
ofp13_desc(struct switchd *sc, struct switch_connection *con)
{
struct ofp_header *oh;
struct ofp_multipart *mp;
struct ibuf *ibuf;
if ((ibuf = ibuf_static()) == NULL)
return (-1);
if ((mp = ofp13_multipart_request(con, ibuf, OFP_MP_T_DESC, 0)) == NULL)
return (-1);
oh = &mp->mp_oh;
oh->oh_length = htons(sizeof(*mp));
if (ofp13_validate(sc, &con->con_local, &con->con_peer, oh, ibuf) != 0)
return (-1);
ofp_output(con, NULL, ibuf);
ibuf_release(ibuf);
return (0);
}
int
ofp13_flow_stats(struct switchd *sc, struct switch_connection *con,
uint32_t out_port, uint32_t out_group, uint8_t table_id)
{
struct ofp_header *oh;
struct ofp_multipart *mp;
struct ofp_flow_stats_request *fsr;
struct ofp_match *om;
struct ibuf *ibuf;
int padsize;
if ((ibuf = ibuf_static()) == NULL)
return (-1);
if ((mp = ofp13_multipart_request(con, ibuf, OFP_MP_T_FLOW, 0)) == NULL)
return (-1);
if ((fsr = ibuf_advance(ibuf, sizeof(*fsr))) == NULL)
return (-1);
oh = &mp->mp_oh;
fsr->fsr_table_id = table_id;
fsr->fsr_out_port = htonl(out_port);
fsr->fsr_out_group = htonl(out_group);
om = &fsr->fsr_match;
om->om_type = htons(OFP_MATCH_OXM);
om->om_length = htons(sizeof(*om));
padsize = OFP_ALIGN(sizeof(*om)) - sizeof(*om);
if (padsize && ibuf_advance(ibuf, padsize) == NULL)
return (-1);
oh->oh_length = htons(ibuf_length(ibuf));
if (ofp13_validate(sc, &con->con_local, &con->con_peer, oh, ibuf) != 0)
return (-1);
ofp_output(con, NULL, ibuf);
ibuf_release(ibuf);
return (0);
}
int
ofp13_table_features(struct switchd *sc, struct switch_connection *con,
uint8_t tableid)
{
struct ofp_header *oh;
struct ofp_multipart *mp;
struct ibuf *ibuf;
if ((ibuf = ibuf_static()) == NULL)
return (-1);
if ((mp = ofp13_multipart_request(con, ibuf,
OFP_MP_T_TABLE_FEATURES, 0)) == NULL)
return (-1);
oh = &mp->mp_oh;
oh->oh_length = htons(sizeof(*mp));
if (ofp13_validate(sc, &con->con_local, &con->con_peer, oh, ibuf) != 0)
return (-1);
ofp_output(con, NULL, ibuf);
ibuf_release(ibuf);
return (0);
}
int
ofp13_error(struct switchd *sc, struct switch_connection *con,
struct ofp_header *oh, struct ibuf *ibuf, uint16_t type, uint16_t code)
{
struct ibuf *obuf;
struct ofp_error *err;
struct ofp_header *header;
int rv, dlen;
if ((obuf = ibuf_static()) == NULL ||
(err = ibuf_advance(obuf, sizeof(*err))) == NULL)
return (-1);
header = &err->err_oh;
err->err_type = htons(type);
err->err_code = htons(code);
/* Copy the first message bytes for the error payload. */
dlen = ibuf_size(ibuf);
if (dlen > OFP_ERRDATA_MAX)
dlen = OFP_ERRDATA_MAX;
if (ibuf_add(obuf, ibuf_seek(ibuf, 0, dlen), dlen) == -1)
return (-1);
header->oh_version = OFP_V_1_3;
header->oh_type = OFP_T_ERROR;
header->oh_length = htons(ibuf_length(obuf));
header->oh_xid = oh->oh_xid;
if (ofp13_validate(sc, &con->con_peer, &con->con_local, header,
obuf) == -1) {
ibuf_release(obuf);
return (-1);
}
rv = ofp_output(con, NULL, obuf);
ibuf_release(obuf);
return (rv);
}
/*
* The valid commands for groups are:
* OFP_GROUPCMD_{ADD,MODIFY,DELETE}
*
* The valid type for groups are:
* OFP_GROUP_T_{ALL,SELECT,INDIRECT,FAST_FAILOVER}
*
* You have to update the gm->gm_oh.oh_length = htons(ibuf_length(ibuf));
*/
struct ofp_group_mod *
ofp13_group(struct switch_connection *con, struct ibuf *ibuf,
uint32_t gid, uint16_t cmd, uint8_t type)
{
struct ofp_group_mod *gm;
struct ofp_header *oh;
if ((gm = ibuf_advance(ibuf, sizeof(*gm))) == NULL)
return (NULL);
oh = &gm->gm_oh;
oh->oh_version = OFP_V_1_3;
oh->oh_type = OFP_T_GROUP_MOD;
oh->oh_xid = htonl(con->con_xidnxt++);
gm->gm_command = htons(cmd);
gm->gm_type = type;
gm->gm_group_id = htonl(gid);
return (gm);
}
/* Remember to update b->b_len. */
struct ofp_bucket *
ofp13_bucket(struct ibuf *ibuf, uint16_t weight, uint32_t watchport,
uint32_t watchgroup)
{
struct ofp_bucket *b;
if ((b = ibuf_advance(ibuf, sizeof(*b))) == NULL)
return (NULL);
b->b_weight = htons(weight);
b->b_watch_port = htonl(watchport);
b->b_watch_group = htonl(watchgroup);
return (b);
}
int
ofp13_setconfig_validate(struct switchd *sc,
struct sockaddr_storage *src, struct sockaddr_storage *dst,
struct ofp_header *oh, struct ibuf *ibuf)
{
struct ofp_switch_config *cfg;
if ((cfg = ibuf_seek(ibuf, 0, sizeof(*cfg))) == NULL)
return (-1);
log_debug("\tflags %#04x miss_send_len %d",
ntohs(cfg->cfg_flags), ntohs(cfg->cfg_miss_send_len));
return (0);
}
int
ofp13_setconfig(struct switchd *sc, struct switch_connection *con,
uint16_t flags, uint16_t misslen)
{
struct ibuf *ibuf;
struct ofp_switch_config *cfg;
struct ofp_header *oh;
int rv;
if ((ibuf = ibuf_static()) == NULL ||
(cfg = ibuf_advance(ibuf, sizeof(*cfg))) == NULL)
return (-1);
cfg->cfg_flags = htons(flags);
cfg->cfg_miss_send_len = htons(misslen);
oh = &cfg->cfg_oh;
oh->oh_version = OFP_V_1_3;
oh->oh_type = OFP_T_SET_CONFIG;
oh->oh_length = htons(ibuf_length(ibuf));
oh->oh_xid = htonl(con->con_xidnxt++);
if (ofp13_validate(sc, &con->con_local, &con->con_peer, oh, ibuf) != 0)
return (-1);
rv = ofp_output(con, NULL, ibuf);
ibuf_free(ibuf);
return (rv);
}
int
ofp13_featuresrequest(struct switchd *sc, struct switch_connection *con)
{
struct ibuf *ibuf;
struct ofp_header *oh;
int rv;
if ((ibuf = ibuf_static()) == NULL ||
(oh = ibuf_advance(ibuf, sizeof(*oh))) == NULL)
return (-1);
oh->oh_version = OFP_V_1_3;
oh->oh_type = OFP_T_FEATURES_REQUEST;
oh->oh_length = htons(ibuf_length(ibuf));
oh->oh_xid = htonl(con->con_xidnxt++);
if (ofp13_validate(sc, &con->con_local, &con->con_peer, oh, ibuf) != 0)
return (-1);
rv = ofp_output(con, NULL, ibuf);
ibuf_free(ibuf);
return (rv);
}
/*
* Flow modification message.
*
* After the flow-mod header we have N OXM filters to match packets, when
* you finish adding them you must update match header:
* fm_match.om_length = sizeof(fm_match) + OXM length.
*
* Then you must add flow instructions and update the OFP header length:
* fm_oh.oh_length =
* sizeof(*fm) + (fm_match.om_len - sizeof(fm_match)) + instructionslen.
* or
* fm_oh.oh_length = ibuf_length(ibuf).
*
* Note on match payload:
* After adding all matches and before starting to insert instructions you
* must add the mandatory padding to fm_match. You can calculate the padding
* size with this formula:
* padsize = OFP_ALIGN(fm_match.om_length) - fm_match.om_length;
*
* Note on Table-miss:
* To make a table miss you need to set priority 0 and don't add any
* matches, just instructions.
*/
struct ofp_flow_mod *
ofp13_flowmod(struct switch_connection *con, struct ibuf *ibuf,
uint8_t cmd, uint8_t table, uint16_t idleto, uint16_t hardto,
uint16_t prio)
{
struct ofp_flow_mod *fm;
if ((fm = ibuf_advance(ibuf, sizeof(*fm))) == NULL)
return (NULL);
fm->fm_oh.oh_version = OFP_V_1_3;
fm->fm_oh.oh_type = OFP_T_FLOW_MOD;
fm->fm_oh.oh_length = htons(sizeof(*fm));
fm->fm_oh.oh_xid = htonl(con->con_xidnxt++);
fm->fm_match.om_type = htons(OFP_MATCH_OXM);
fm->fm_match.om_length = htons(sizeof(fm->fm_match));
return (fm);
}
int
ofp13_tablemiss_sendctrl(struct switchd *sc, struct switch_connection *con,
uint8_t table)
{
struct oflowmod_ctx ctx;
struct ibuf *ibuf;
int ret;
if ((ibuf = oflowmod_open(&ctx, con, NULL, OFP_V_1_3)) == NULL)
goto err;
if (oflowmod_iopen(&ctx) == -1)
goto err;
/* Update header */
ctx.ctx_fm->fm_table_id = table;
if (oflowmod_instruction(&ctx,
OFP_INSTRUCTION_T_APPLY_ACTIONS) == -1)
goto err;
if (action_output(ibuf, OFP_PORT_CONTROLLER,
OFP_CONTROLLER_MAXLEN_MAX) == -1)
goto err;
if (oflowmod_iclose(&ctx) == -1)
goto err;
if (oflowmod_close(&ctx) == -1)
goto err;
if (ofp13_validate(sc, &con->con_local, &con->con_peer,
&ctx.ctx_fm->fm_oh, ibuf) != 0)
goto err;
ret = ofp_output(con, NULL, ibuf);
ibuf_release(ibuf);
return (ret);
err:
(void)oflowmod_err(&ctx, __func__, __LINE__);
return (-1);
}
|