summaryrefslogtreecommitdiff
path: root/src/devices/openvswitch/nm-ovsdb.c
blob: e146848bedcba14289dedd81a5fd0ba71c371c05 (plain)
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
/* NetworkManager -- Network link manager
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2017 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-ovsdb.h"

#include <string.h>
#include <jansson.h>
#include <gmodule.h>
#include <gio/gunixsocketaddress.h>

#include "devices/nm-device.h"
#include "nm-device-openvswitch.h"
#include "platform/nm-platform.h"
#include "nm-core-internal.h"

/*****************************************************************************/

typedef struct {
	char *name;
	char *connection_uuid;
	GPtrArray *interfaces;          /* interface uuids */
} OpenvswitchPort;

typedef struct {
	char *name;
	char *connection_uuid;
	GPtrArray *ports;               /* port uuids */
} OpenvswitchBridge;

typedef struct {
	char *name;
	char *connection_uuid;
} OpenvswitchInterface;

typedef struct {
	GSocketClient *client;
	GSocketConnection *conn;
	GCancellable *cancellable;
	char buf[4096];                 /* Input buffer */
	size_t bufp;                    /* Last decoded byte in the input buffer. */
	GString *input;                 /* JSON stream waiting for decoding. */
	GString *output;                /* JSON stream to be sent. */
	gint64 seq;
	GArray *calls;                  /* Method calls waiting for a response. */
	GHashTable *interfaces;         /* interface uuid => OpenvswitchInterface */
	GHashTable *ports;              /* port uuid => OpenvswitchPort */
	GHashTable *bridges;            /* bridge uuid => OpenvswitchBridge */
	const char *db_uuid;
} NMOvsdbPrivate;

struct _NMOvsdb {
	GObject parent;
	NMOvsdbPrivate _priv;
};

struct _NMOvsdbClass {
	GObjectClass parent;
};

G_DEFINE_TYPE (NMOvsdb, nm_ovsdb, G_TYPE_OBJECT)

#define NM_OVSDB_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMOvsdb, NM_IS_OVSDB)

#define _NMLOG_DOMAIN      LOGD_DEVICE
#define _NMLOG(level, ...) __NMLOG_DEFAULT (level, _NMLOG_DOMAIN, "ovsdb", __VA_ARGS__)

NM_DEFINE_SINGLETON_GETTER (NMOvsdb, nm_ovsdb_get, NM_TYPE_OVSDB);

/*****************************************************************************/

static void ovsdb_try_connect (NMOvsdb *self);
static void ovsdb_disconnect (NMOvsdb *self);
static void ovsdb_read (NMOvsdb *self);
static void ovsdb_write (NMOvsdb *self);
static void ovsdb_next_command (NMOvsdb *self);

/*****************************************************************************/

/* ovsdb command abstraction. */

typedef void (*OvsdbMethodCallback) (NMOvsdb *self, json_t *response,
                                     GError *error, gpointer user_data);

typedef struct {
	gint64 id;
#define COMMAND_PENDING -1                      /* id not yet assigned */
	OvsdbMethodCallback callback;
	gpointer user_data;
	NMOvsdbCommand command;
	const char *bridge;
	NMConnection *port;
	NMConnection *interface;
} OvsdbMethodCall;

static void
_call_trace (const char *comment, OvsdbMethodCall *call, json_t *msg)
{
#ifdef NM_MORE_LOGGING
	const char *op = NULL;
	char *str = NULL;

	switch (call->command) {
	case NM_OVSDB_MONITOR:
		op = "monitor";
		break;
	case NM_OVSDB_ADD_BR:
		op = "add-br";
		break;
	case NM_OVSDB_DEL_BR:
		op = "del-br";
		break;
	case NM_OVSDB_ADD_IFACE:
		op = "add-interface";
		break;
	case NM_OVSDB_DEL_IFACE:
		op = "del-interface";
		break;
	}

	if (msg)
		str = json_dumps (msg, 0);

	_LOGT ("%s: %s%s%s%s%s%s%s%s%s", comment, op,
	       call->bridge ? " bridge=" : "",
	       call->bridge ? call->bridge : "",
	       call->port ? " port=" : "",
	       call->port ? nm_connection_get_interface_name (call->port) : "",
	       call->interface ? " interface=" : "",
	       call->interface ? nm_connection_get_interface_name (call->interface) : "",
	       msg ? ": " : "",
	       msg ? str : "");

	if (msg)
		g_free (str);

	g_return_if_fail (op);
#endif
}

/**
 * ovsdb_call_method:
 *
 * Queues the ovsdb command. Eventually fires the command right away if
 * there's no command pending completion.
 */
static void
ovsdb_call_method (NMOvsdb *self, NMOvsdbCommand command,
                   const char *bridge, NMConnection *port, NMConnection *interface,
                   OvsdbMethodCallback callback, gpointer user_data)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	OvsdbMethodCall *call;

	/* Ensure we're not unsynchronized before we queue the method call. */
	ovsdb_try_connect (self);

	g_array_set_size (priv->calls, priv->calls->len + 1);
	call = &g_array_index (priv->calls, OvsdbMethodCall, priv->calls->len - 1);
	call->id = COMMAND_PENDING;
	call->command = command;
	if (bridge)
		call->bridge = g_strdup (bridge);
	if (port)
		call->port = nm_simple_connection_new_clone (port);
	if (interface)
		call->interface = nm_simple_connection_new_clone (interface);
	call->callback = callback;
	call->user_data = user_data;

	_call_trace ("enqueue", call, NULL);

	ovsdb_next_command (self);
}

/*****************************************************************************/

/* Create and process the JSON-RPC messages from ovsdb. */

/**
 * _fill_bridges:
 *
 * Put set of all bridges into @items and all but @exclude_bridge into
 * @new_items. The array with the ommited element is useful for replacement
 * or deletion while the full array is good for ensuring the database is
 * in the state we expect it to be prior to the transaction.
 */
static void
_fill_bridges (NMOvsdb *self, const char *exclude_bridge,
               json_t **items, json_t **new_items)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	GHashTableIter iter;
	char *bridge_uuid;
	OpenvswitchBridge *ovs_bridge;

	*items = json_array ();
	*new_items = json_array ();

	g_hash_table_iter_init (&iter, priv->bridges);
	while (g_hash_table_iter_next (&iter, (gpointer) &bridge_uuid, (gpointer) &ovs_bridge)) {
		json_array_append_new (*items, json_pack ("[s,s]", "uuid", bridge_uuid));
		if (   g_strcmp0 (exclude_bridge, ovs_bridge->name) == 0
		    && ovs_bridge->connection_uuid != NULL)
			continue;
		json_array_append_new (*new_items, json_pack ("[s,s]", "uuid", bridge_uuid));
	}
}

/**
 * _expect_bridges:
 *
 * Return a command that will fail the transaction if the actual set of
 * bridges doesn't match @bridges. This is a way of detecting race conditions
 * with other ovsdb clients that might be adding or removing bridges
 * at the same time.
 */
static json_t *
_expect_bridges (json_t *bridges, const char *db_uuid)
{
	return json_pack ("{s:s, s:s, s:i, s:[s], s:s, s:[{s:[s, o]}], s:[[s, s, [s, s]]]}",
	                  "op", "wait", "table", "Open_vSwitch",
	                  "timeout", 0, "columns", "bridges",
	                  "until", "==", "rows", "bridges", "set", bridges,
	                  "where", "_uuid", "==", "uuid", db_uuid);
}

/**
 * _set_bridges:
 *
 * Return a command that will update the list of bridges in @db_uuid
 * database to @bridges.
 */
static json_t *
_set_bridges (const json_t *bridges, const char *db_uuid)
{
	return json_pack ("{s:s, s:s, s:{s:[s, o]}, s:[[s, s, [s, s]]]}",
	                  "op", "update", "table", "Open_vSwitch",
	                  "row", "bridges", "set", bridges,
	                  "where", "_uuid", "==", "uuid", db_uuid);
}

/*
 * _fill_ports:
 *
 * Put set of all ports of @bridge into @items and all but @exclude_port into
 * @new_items.
 *
 * Returns: %TRUE if the specified port was actually seen, helping us to decide
 *          whether we need to put an itnerface into a new one or update the
 *          existing one.
 */
static gboolean
_fill_ports (NMOvsdb *self,
             const char *bridge, NMConnection *exclude_port,
             json_t **items, json_t **new_items)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	GHashTableIter iter;
	char *bridge_uuid;
	char *port_uuid;
	OpenvswitchBridge *ovs_bridge;
	OpenvswitchPort *ovs_port;
	gboolean found = FALSE;
	int i;

	*items = json_array ();
	*new_items = json_array ();

	g_hash_table_iter_init (&iter, priv->bridges);
	while (g_hash_table_iter_next (&iter, (gpointer) &bridge_uuid, (gpointer) &ovs_bridge)) {
		if (g_strcmp0 (ovs_bridge->name, bridge) != 0)
			continue;
		for (i = 0; i < ovs_bridge->ports->len; i++) {
			port_uuid = g_ptr_array_index (ovs_bridge->ports, i);
			json_array_append_new (*items, json_pack ("[s,s]", "uuid", port_uuid));

			ovs_port = g_hash_table_lookup (priv->ports, port_uuid);
			if (!ovs_port)
				continue;
			if (   g_strcmp0 (nm_connection_get_interface_name (exclude_port), ovs_port->name) == 0
			    && g_strcmp0 (nm_connection_get_uuid (exclude_port), ovs_port->connection_uuid) == 0) {
				found = TRUE;
				continue;
			}
			json_array_append_new (*new_items, json_pack ("[s,s]", "uuid", port_uuid));
		}
	}

	return found;
}

/**
 * _expect_ports:
 *
 * Return a command that will fail the transaction if the actual set of
 * ports in @bridge doesn't match @ports. This is a way of detecting
 * race conditions with other ovsdb clients that might be adding or removing
 * bridge ports at the same time.
 */
static json_t *
_expect_ports (const char *bridge, const json_t *ports)
{
	return json_pack ("{s:s, s:s, s:i, s:[s], s:s, s:[{s:[s, o]}], s:[[s, s, s]]}",
	                  "op", "wait", "table", "Bridge",
	                  "timeout", 0, "columns", "ports",
	                  "until", "==", "rows", "ports", "set", ports,
	                  "where", "name", "==", bridge);
}

/**
 * _set_ports:
 *
 * Return a command that will update the list of ports of @bridge
 * to @ports.
 */
static json_t *
_set_ports (const char *bridge, const json_t *ports)
{
	return json_pack ("{s:s, s:s, s:{s:[s, o]}, s:[[s, s, s]]}",
	                  "op", "update", "table", "Bridge",
	                  "row", "ports", "set", ports,
	                  "where", "name", "==", bridge);
}

/*
 * _fill_interfaces:
 *
 * Put set of all interfaces of @port into @items and all but
 * @exclude_interface into @new_items.
 */
static void
_fill_interfaces (NMOvsdb *self,
                  NMConnection *port, NMConnection *exclude_interface,
                  json_t **items, json_t **new_items)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	GHashTableIter iter;
	char *port_uuid;
	char *interface_uuid;
	OpenvswitchPort *ovs_port;
	OpenvswitchInterface *ovs_interface;
	int i;

	*items = json_array ();
	*new_items = json_array ();

	g_hash_table_iter_init (&iter, priv->ports);
	while (g_hash_table_iter_next (&iter, (gpointer) &port_uuid, (gpointer) &ovs_port)) {
		if (g_strcmp0 (ovs_port->name, nm_connection_get_interface_name (port)) != 0)
			continue;
		for (i = 0; i < ovs_port->interfaces->len; i++) {
			interface_uuid = g_ptr_array_index (ovs_port->interfaces, i);
			json_array_append_new (*items, json_pack ("[s,s]", "uuid", interface_uuid));

			ovs_interface = g_hash_table_lookup (priv->interfaces, interface_uuid);
			if (!ovs_interface)
				continue;
			if (   g_strcmp0 (nm_connection_get_interface_name (exclude_interface), ovs_interface->name) == 0
			    && g_strcmp0 (nm_connection_get_uuid (exclude_interface), ovs_interface->connection_uuid) == 0)
				continue;
			json_array_append_new (*new_items, json_pack ("[s,s]", "uuid", interface_uuid));
		}
	}
}

/**
 * _expect_interfaces:
 *
 * Return a command that will fail the transaction if the actual set of
 * interfaces in @port doesn't match @interfaces. This is a way of detecting
 * race conditions with other ovsdb clients that might be adding or removing
 * port interfaces at the same time.
 */
static json_t *
_expect_interfaces (NMConnection *port, const json_t *interfaces)
{
	return json_pack ("{s:s, s:s, s:i, s:[s], s:s, s:[{s:[s, o]}], s:[[s, s, s]]}",
	                  "op", "wait", "table", "Port",
	                  "timeout", 0, "columns", "interfaces",
	                  "until", "==", "rows", "interfaces", "set", interfaces,
	                  "where", "name", "==", nm_connection_get_interface_name (port));
}

/**
 * _set_interfaces:
 *
 * Return a command that will update the list of interfaces of @port
 * to @interfaces.
 */
static json_t *
_set_interfaces (NMConnection *port, const json_t *interfaces)
{
	return json_pack ("{s:s, s:s, s:{s:[s, o]}, s:[[s, s, s]]}",
	                  "op", "update", "table", "Port",
	                  "row", "interfaces", "set", interfaces,
	                  "where", "name", "==", nm_connection_get_interface_name (port));
}

/**
 * _inc_next_cfg:
 *
 * Returns an mutate command that bumps next_cfg upon successful completion
 * of the transaction it is in.
 */
static json_t *
_inc_next_cfg (const char *db_uuid)
{
	return json_pack ("{s:s, s:s, s:[[s, s, i]], s:[[s, s, [s, s]]]}",
                          "op", "mutate", "table", "Open_vSwitch",
	                  "mutations", "next_cfg", "+=", 1,
	                  "where", "_uuid", "==", "uuid", db_uuid);
}

/**
 * _new_interface:
 *
 * Returns an commands that adds new interface from a given connection.
 * If the connection is of a bridge then an internal interface for the
 * bridge is added, otherwise it's a regular one.
 */
static json_t *
_new_interface (NMConnection *interface)
{
	const char *type;

	if (nm_connection_get_setting_ovs_bridge (interface))
		type = "internal";
	else
		type = "";

	return json_pack ("{s:s, s:s, s:{s:s, s:s, s:[s, [[s, s]]]}, s:s}",
	                  "op", "insert", "table", "Interface", "row",
	                  "name", nm_connection_get_interface_name (interface),
	                  "type", type,
	                  "external_ids", "map", "NM.connection.uuid", nm_connection_get_uuid (interface),
	                  "uuid-name", "rowIntf");
}

/**
 * _new_port:
 *
 * Returns an commands that adds new port from a given connection.
 */
static json_t *
_new_port (NMConnection *port)
{
	NMSettingOvsPort *s_ovs_port;
	const char *vlan_mode = NULL;
	guint tag = 0;
	const char *lacp = NULL;
	const char *bond_mode = NULL;
	guint bond_updelay = 0;
	guint bond_downdelay = 0;
	json_t *row;

	s_ovs_port = nm_connection_get_setting_ovs_port (port);

	row = json_object ();

	if (s_ovs_port) {
		vlan_mode = nm_setting_ovs_port_get_vlan_mode (s_ovs_port);
		tag = nm_setting_ovs_port_get_tag (s_ovs_port);
		lacp = nm_setting_ovs_port_get_lacp (s_ovs_port);
		bond_mode = nm_setting_ovs_port_get_bond_mode (s_ovs_port);
		bond_updelay = nm_setting_ovs_port_get_bond_updelay (s_ovs_port);
		bond_downdelay = nm_setting_ovs_port_get_bond_downdelay (s_ovs_port);
	}

	if (vlan_mode)
		json_object_set_new (row, "vlan_mode", json_string (vlan_mode));
	if (tag)
		json_object_set_new (row, "tag", json_integer (tag));
	if (lacp)
		json_object_set_new (row, "lacp", json_string (lacp));
	if (bond_mode)
		json_object_set_new (row, "bond_mode", json_string (bond_mode));
	if (bond_updelay)
		json_object_set_new (row, "bond_updelay", json_integer (bond_updelay));
	if (bond_downdelay)
		json_object_set_new (row, "bond_downdelay", json_integer (bond_downdelay));

	json_object_set_new (row, "name", json_string (nm_connection_get_interface_name (port)));
	json_object_set_new (row, "interfaces", json_pack ("[s, s]", "named-uuid", "rowIntf"));
	json_object_set_new (row, "external_ids",
		json_pack ("[s, [[s, s]]]", "map",
		           "NM.connection.uuid", nm_connection_get_uuid (port)));

	return json_pack ("{s:s, s:s, s:o, s:s}", "op", "insert", "table", "Port",
	                  "row", row, "uuid-name", "rowPort");
}

/**
 * _new_bridge:
 *
 * Returns an commands that adds new bridge from a given connection.
 * The connection actually refers to the bridge's internal port so that we can get an UUID.
 */
static json_t *
_new_bridge (NMConnection *port)
{
	NMSettingOvsBridge *s_ovs_bridge;
	const char *fail_mode = NULL;
	gboolean mcast_snooping_enable = FALSE;
	gboolean rstp_enable = FALSE;
	gboolean stp_enable = FALSE;
	json_t *row;

	s_ovs_bridge = nm_connection_get_setting_ovs_bridge (port);

	row = json_object ();

	if (s_ovs_bridge) {
		fail_mode = nm_setting_ovs_bridge_get_fail_mode (s_ovs_bridge);
		mcast_snooping_enable = nm_setting_ovs_bridge_get_mcast_snooping_enable (s_ovs_bridge);
		rstp_enable = nm_setting_ovs_bridge_get_rstp_enable (s_ovs_bridge);
		stp_enable = nm_setting_ovs_bridge_get_stp_enable (s_ovs_bridge);
	}

	if (fail_mode)
		json_object_set_new (row, "fail_mode", json_string (fail_mode));
	if (mcast_snooping_enable)
		json_object_set_new (row, "mcast_snooping_enable", json_boolean (mcast_snooping_enable));
	if (rstp_enable)
		json_object_set_new (row, "rstp_enable", json_boolean (rstp_enable));
	if (stp_enable)
		json_object_set_new (row, "stp_enable", json_boolean (stp_enable));

	json_object_set_new (row, "name", json_string (nm_connection_get_interface_name (port)));
	json_object_set_new (row, "ports", json_pack ("[s, s]", "named-uuid", "rowPort"));
	json_object_set_new (row, "external_ids",
		json_pack ("[s, [[s, s]]]", "map",
		           "NM.connection.uuid", nm_connection_get_uuid (port)));

	return json_pack ("{s:s, s:s, s:o, s:s}", "op", "insert", "table", "Bridge",
	                  "row", row, "uuid-name", "rowBridge");
}

/**
 * ovsdb_next_command:
 *
 * Translates a higher level operation (add/remove bridge/port) to a RFC 7047
 * command serialized into JSON ands sends it over to the database.

 * Only called when no command is waiting for a response, since the serialized
 * command might depend on result of a previous one (add and remove need to
 * include an up to date bridge list in their transactions to rule out races).
 */
static void
ovsdb_next_command (NMOvsdb *self)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	OvsdbMethodCall *call = NULL;
	char *cmd;
	json_t *msg = NULL;
	json_t *items, *new_items;
	json_t *params;

	if (!priv->conn)
		return;
	if (!priv->calls->len)
		return;
	call = &g_array_index (priv->calls, OvsdbMethodCall, 0);
	if (call->id != COMMAND_PENDING)
		return;
	call->id = priv->seq++;

	switch (call->command) {
	case NM_OVSDB_MONITOR:
		msg = json_pack ("{s:i, s:s, s:[s, n, {"
		                 "  s:[{s:[s, s, s]}],"
		                 "  s:[{s:[s, s, s]}],"
		                 "  s:[{s:[s, s]}],"
		                 "  s:[{s:[]}]"
		                 "}]}",
		                 "id", call->id,
		                 "method", "monitor", "params", "Open_vSwitch",
		                 "Bridge", "columns", "name", "ports", "external_ids",
		                 "Port", "columns", "name", "interfaces", "external_ids",
		                 "Interface", "columns", "name", "external_ids",
		                 "Open_vSwitch", "columns");
		break;
	case NM_OVSDB_ADD_BR:
		_fill_bridges (self, call->bridge, &items, &new_items);
		json_array_append_new (new_items, json_pack ("[s,s]", "named-uuid", "rowBridge"));

		msg = json_pack ("{s:i, s:s, s:[s, o, o, o, o, o, o]}",
		                 "id", call->id,
		                 "method", "transact", "params", "Open_vSwitch",
		                 _expect_bridges (items, priv->db_uuid),
		                 _set_bridges (new_items, priv->db_uuid),
		                 _inc_next_cfg (priv->db_uuid),
		                 _new_interface (call->port),
		                 _new_port (call->port),
		                 _new_bridge (call->port));
		break;
	case NM_OVSDB_DEL_BR:
		_fill_bridges (self, call->bridge, &items, &new_items);

		msg = json_pack ("{s:i, s:s, s:[s,o,o,o]}",
		                 "id", call->id,
		                 "method", "transact", "params", "Open_vSwitch",
		                 _expect_bridges (items, priv->db_uuid),
		                 _set_bridges (new_items, priv->db_uuid),
		                 _inc_next_cfg (priv->db_uuid));
		break;
	case NM_OVSDB_ADD_IFACE:
		params = json_array ();
		json_array_append_new (params, json_string ("Open_vSwitch"));

		/* Insert the new interface. */
		json_array_append_new (params, _new_interface (call->interface));

		if (_fill_ports (self, call->bridge, call->port, &items, &new_items)) {
			/* The port exists, update it with the new interface. */
			json_decref (items);
			json_decref (new_items);
			_fill_interfaces (self, call->port, call->interface, &items, &new_items);
			json_array_append_new (new_items, json_pack ("[s,s]", "named-uuid", "rowIntf"));
			json_array_append_new (params, _expect_interfaces (call->port, items));
			json_array_append_new (params, _set_interfaces (call->port, new_items));
		} else {
		        /* Create a new port along with the interface. */
			json_array_append_new (params, _new_port (call->port));
			json_array_append_new (new_items, json_pack ("[s,s]", "named-uuid", "rowPort"));
			json_array_append_new (params, _expect_ports (call->bridge, items));
			json_array_append_new (params, _set_ports (call->bridge, new_items));
		}

		msg = json_pack ("{s:i, s:s, s:o}",
		                 "id", call->id,
		                 "method", "transact", "params", params);

		break;
	case NM_OVSDB_DEL_IFACE:
		params = json_array ();
		json_array_append_new (params, json_string ("Open_vSwitch"));

		_fill_interfaces (self, call->port, call->interface, &items, &new_items);
		if (json_array_size (new_items) == 0) {
			/* A port can't exist without interfaces, drop it altogether. */
			json_decref (items);
			json_decref (new_items);
			_fill_ports (self, call->bridge, call->port, &items, &new_items);
			json_array_append_new (params, _expect_ports (call->bridge, items));
			json_array_append_new (params, _set_ports (call->bridge, new_items));
		} else {
			/* Drop just the interface from the port. */
			json_array_append_new (params, _expect_interfaces (call->port, items));
			json_array_append_new (params, _set_interfaces (call->port, new_items));
		}

		msg = json_pack ("{s:i, s:s, s:o}",
		                 "id", call->id,
		                 "method", "transact", "params", params);
		break;
	}

	g_return_if_fail (msg);
	_call_trace ("send", call, msg);
	cmd = json_dumps (msg, 0);

	g_string_append (priv->output, cmd);
	json_decref (msg);
	free (cmd);

	ovsdb_write (self);
}

/**
 * _uuids_to_array:
 *
 * This tidies up the somewhat non-straightforward way ovsdb represents an array
 * of UUID elements. The single element is a tuple (called <atom> in RFC7047),
 *
 *   [ "uuid", "aa095ffb-e1f1-0fc4-8038-82c1ea7e4797" ]
 *
 * while the list of multiple UUIDs are turned into a set of such tuples ("atoms"):
 *
 *   [ "set", [ [ "uuid", "aa095ffb-e1f1-0fc4-8038-82c1ea7e4797" ],
 *              [ "uuid", "185c93f6-0b39-424e-8587-77d074aa7ce0" ], ... ] ]
 */
static void
_uuids_to_array (GPtrArray *array, const json_t *items)
{
	const char *key;
	json_t *value;
	size_t index = 0;
	json_t *set_value;
	size_t set_index;

	while (index < json_array_size (items)) {
		key = json_string_value (json_array_get (items, index));
		index++;
		value = json_array_get (items, index);
		index++;

		if (!value)
			return;

		if (g_strcmp0 (key, "uuid") == 0 && json_is_string (value)) {
			g_ptr_array_add (array, g_strdup (json_string_value (value)));
		} else if (g_strcmp0 (key, "set") == 0 && json_is_array (value)) {
			json_array_foreach (value, set_index, set_value) {
				_uuids_to_array (array, set_value);
			}
		}
	}
}

static char *
_connection_uuid_from_external_ids (json_t *external_ids)
{
	json_t *value;
	size_t index;

	if (g_strcmp0 ("map", json_string_value (json_array_get (external_ids, 0))) != 0)
		return NULL;

	json_array_foreach (json_array_get (external_ids, 1), index, value) {
		if (g_strcmp0 ("NM.connection.uuid", json_string_value (json_array_get (value, 0))) == 0)
			return g_strdup (json_string_value (json_array_get (value, 1)));
	}

	return NULL;
}

/**
 * ovsdb_got_update:
 *
 * Called when we've got an "update" method call (we asked for it with the monitor
 * command). We use it to maintain a consistent view of bridge list regardless of
 * whether the changes are done by us or externally.
 */
static void
ovsdb_got_update (NMOvsdb *self, json_t *msg)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	json_t *ovs = NULL;
	json_t *bridge = NULL;
	json_t *port = NULL;
	json_t *interface = NULL;
	json_t *items;
	json_t *external_ids;
	json_error_t json_error = { 0, };
	void *iter;
	const char *name;
	const char *key;
	json_t *value;
	OpenvswitchBridge *ovs_bridge;
	OpenvswitchPort *ovs_port;
	OpenvswitchInterface *ovs_interface;

	if (json_unpack_ex (msg, &json_error, 0, "{s?:o, s?:o, s?:o, s?:o}",
	                    "Open_vSwitch", &ovs,
	                    "Bridge", &bridge,
	                    "Port", &port,
	                    "Interface", &interface) == -1) {
		/* This doesn't really have to be an error; the key might
		 * be missing if there really are no bridges present. */
		_LOGD ("Bad update: %s", json_error.text);
	}

	if (ovs) {
		iter = json_object_iter (ovs);
		priv->db_uuid = g_strdup (iter ? json_object_iter_key (iter) : NULL);
	}

	json_object_foreach (bridge, key, value) {
		if (json_unpack (value, "{s:{}}", "old") == 0) {
			ovs_bridge = g_hash_table_lookup (priv->bridges, key);
			_LOGT ("removed a bridge: %s%s%s", ovs_bridge->name,
			       ovs_bridge->connection_uuid ? ", " : "",
			       ovs_bridge->connection_uuid ? ovs_bridge->connection_uuid : "");
			g_hash_table_remove (priv->bridges, key);
		}
		if (json_unpack (value, "{s:{s?:s, s?:o, s?:o}}", "new", "name", &name, "external_ids", &external_ids, "ports", &items) == 0) {
			ovs_bridge = g_slice_new (OpenvswitchBridge);
			ovs_bridge->name = g_strdup (name);
			ovs_bridge->connection_uuid = _connection_uuid_from_external_ids (external_ids);
			ovs_bridge->ports = g_ptr_array_new_with_free_func (g_free);
			_uuids_to_array (ovs_bridge->ports, items);
			g_hash_table_insert (priv->bridges, g_strdup (key), ovs_bridge);
			_LOGT ("added a bridge: %s%s%s", ovs_bridge->name,
			       ovs_bridge->connection_uuid ? ", " : "",
			       ovs_bridge->connection_uuid ? ovs_bridge->connection_uuid : "");
		}
	}

	json_object_foreach (port, key, value) {
		if (json_unpack (value, "{s:{}}", "old") == 0) {
			ovs_port = g_hash_table_lookup (priv->ports, key);
			_LOGT ("removed a port: %s%s%s", ovs_port->name,
			       ovs_port->connection_uuid ? ", " : "",
			       ovs_port->connection_uuid ? ovs_port->connection_uuid : "");
			g_hash_table_remove (priv->ports, key);
		}
		if (json_unpack (value, "{s:{s?:s, s?:o, s?:o}}", "new", "name", &name, "external_ids", &external_ids, "interfaces", &items) == 0) {
			ovs_port = g_slice_new (OpenvswitchPort);
			ovs_port->name = g_strdup (name);
			ovs_port->connection_uuid = _connection_uuid_from_external_ids (external_ids);
			ovs_port->interfaces = g_ptr_array_new_with_free_func (g_free);
			_uuids_to_array (ovs_port->interfaces, items);
			g_hash_table_insert (priv->ports, g_strdup (key), ovs_port);
			_LOGT ("added a port: %s%s%s", ovs_port->name,
			       ovs_port->connection_uuid ? ", " : "",
			       ovs_port->connection_uuid ? ovs_port->connection_uuid : "");
		}
	}

	json_object_foreach (interface, key, value) {
		if (json_unpack (value, "{s:{}}", "old") == 0) {
			ovs_interface = g_hash_table_lookup (priv->interfaces, key);
			_LOGT ("removed an interface: %s%s%s", ovs_interface->name,
			       ovs_interface->connection_uuid ? ", " : "",
			       ovs_interface->connection_uuid ? ovs_interface->connection_uuid : "");
			g_hash_table_remove (priv->interfaces, key);
		}
		if (json_unpack (value, "{s:{s?:s, s?:o}}", "new", "name", &name, "external_ids", &external_ids) == 0) {
			ovs_interface = g_slice_new (OpenvswitchInterface);
			ovs_interface->connection_uuid = _connection_uuid_from_external_ids (external_ids);
			ovs_interface->name = g_strdup (name);
			g_hash_table_insert (priv->interfaces, g_strdup (key), ovs_interface);
			_LOGT ("added an interface: %s%s%s", ovs_interface->name,
			       ovs_interface->connection_uuid ? ", " : "",
			       ovs_interface->connection_uuid ? ovs_interface->connection_uuid : "");
		}
	}
}

/**
 * ovsdb_got_echo:
 *
 * Only implemented because the specification mandates it. Actual ovsdb hasn't been
 * seen doing this.
 */
static void
ovsdb_got_echo (NMOvsdb *self, json_int_t id, json_t *data)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	json_t *msg;
	char *reply;
	gboolean output_was_empty;

	output_was_empty = priv->output->len == 0;

	msg = json_pack ("{s:I, s:O}", "id", id, "result", data);
	reply = json_dumps (msg, 0);
	g_string_append (priv->output, reply);
	json_decref (msg);
	free (reply);

	if (output_was_empty)
		ovsdb_write (self);
}

/**
 * ovsdb_got_msg::
 *
 * Called when when a complete JSON object was seen and unmarshalled.
 * Either finishes a method call or processes a method call.
 */
static void
ovsdb_got_msg (NMOvsdb *self, json_t *msg)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	json_error_t json_error = { 0, };
	json_t *json_id = NULL;
	gint64 id = -1;
	const char *method = NULL;
	json_t *params = NULL;
	json_t *result = NULL;
	json_t *error = NULL;
	OvsdbMethodCall *call = NULL;
	OvsdbMethodCallback callback;
	gpointer user_data;
	GError *local = NULL;

	if (json_unpack_ex (msg, &json_error, 0, "{s?:o, s?:s, s?:o, s?:o, s?:o}",
	                    "id", &json_id,
	                    "method", &method,
	                    "params", &params,
	                    "result", &result,
	                    "error", &error) == -1) {
		_LOGW ("couldn't grok the message: %s", json_error.text);
		ovsdb_disconnect (self);
		return;
	}

	if (json_is_number (json_id))
		id = json_integer_value (json_id);

	if (method) {
		/* It's a method call! */
		if (!params) {
			_LOGW ("a method call with no params: '%s'", method);
			ovsdb_disconnect (self);
			return;
		}

		if (g_strcmp0 (method, "update") == 0) {
			/* This is a update method call. */
			ovsdb_got_update (self, json_array_get (params, 1));
		} else if (g_strcmp0 (method, "echo") == 0) {
			/* This is an echo request. */
			ovsdb_got_echo (self, id, params);
		} else {
			_LOGW ("got an unknown method call: '%s'", method);
		}
		return;
	}

	if (id > -1) {
		/* This is a response to a method call. */
		if (!priv->calls->len) {
			_LOGE ("there are no queued calls expecting response %ld", id);
			ovsdb_disconnect (self);
			return;
		}
		call = &g_array_index (priv->calls, OvsdbMethodCall, 0);
		if (call->id != id) {
			_LOGE ("expected a response to call %ld, not %ld", call->id, id);
			ovsdb_disconnect (self);
			return;
		}
		/* Cool, we found a corresponsing call. Finish it. */

		_call_trace ("response", call, msg);

		if (!json_is_null (error)) {
			/* The response contains an error. */
			g_set_error (&local, G_IO_ERROR, G_IO_ERROR_FAILED,
			             "Error call to OVSDB returned an error: %s",
			              json_string_value (error));
		}

		callback = call->callback;
		user_data = call->user_data;
		g_array_remove_index (priv->calls, 0);
		callback (self, result, local, user_data);

		/* Don't progress further commands in case the callback hit an error
		 * and disconnected us. */
		if (!priv->conn)
			return;

		/* Now we're free to serialize and send the next command, if any. */
		ovsdb_next_command (self);

		return;
	}


	/* This is a message we are not interested in. */
	_LOGW ("got an unknown message, ignoring");
}

/*****************************************************************************/

/* Lower level marshalling and demarshalling of the JSON-RPC traffic on the
 * ovsdb socket. */

static size_t
_json_callback (void *buffer, size_t buflen, void *user_data)
{
	NMOvsdb *self = NM_OVSDB (user_data);
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);

	if (priv->bufp == priv->input->len) {
		/* No more bytes buffered for decoding. */
		return 0;
	}

	/* Pass one more byte to the JSON decoder. */
	*(char *)buffer = priv->input->str[priv->bufp];
	priv->bufp++;

	return (size_t)1;
}

/**
 * ovsdb_read_cb:
 *
 * Read out the data available from the ovsdb socket and try to deserialize
 * the JSON. If we see a complete object, pass it upwards to ovsdb_got_msg().
 */
static void
ovsdb_read_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
	NMOvsdb *self = NM_OVSDB (user_data);
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	GInputStream *stream = G_INPUT_STREAM (source_object);
	GError *error = NULL;
	gssize size;
	json_t *msg;
	json_error_t json_error = { 0, };

	size = g_input_stream_read_finish (stream, res, &error);
	if (size == -1) {
		_LOGW ("short read from ovsdb: %s", error->message);
		g_clear_error (&error);
		ovsdb_disconnect (self);
		return;
	}

	g_string_append_len (priv->input, priv->buf, size);
	do {
		priv->bufp = 0;
		/* The callback always eats up only up to a single byte. This makes
		 * it possible for us to identify complete JSON objects in spite of
		 * us not knowing the length in advance. */
		msg = json_load_callback (_json_callback, self, JSON_DISABLE_EOF_CHECK, &json_error);
		if (msg) {
			ovsdb_got_msg (self, msg);
			g_string_erase (priv->input, 0, priv->bufp);
		}
		json_decref (msg);
	} while (msg);

	if (!priv->conn)
		return;

	if (size)
		ovsdb_read (self);
}

static void
ovsdb_read (NMOvsdb *self)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);

	g_input_stream_read_async (g_io_stream_get_input_stream (G_IO_STREAM (priv->conn)),
		priv->buf, sizeof(priv->buf),
		G_PRIORITY_DEFAULT, NULL, ovsdb_read_cb, self);
}

static void
ovsdb_write_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
	GOutputStream *stream = G_OUTPUT_STREAM (source_object);
	NMOvsdb *self = NM_OVSDB (user_data);
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	GError *error = NULL;
	gssize size;

	size = g_output_stream_write_finish (stream, res, &error);
	if (size == -1) {
		_LOGW ("short write to ovsdb: %s", error->message);
		g_clear_error (&error);
		ovsdb_disconnect (self);
		return;
	}

	if (!priv->conn)
		return;

	g_string_erase (priv->output, 0, size);

	ovsdb_write (self);
}

static void
ovsdb_write (NMOvsdb *self)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	GOutputStream *stream;

	if (!priv->output->len)
		return;

	stream = g_io_stream_get_output_stream (G_IO_STREAM (priv->conn));
	if (g_output_stream_has_pending (stream))
		return;

	g_output_stream_write_async (stream,
	                             priv->output->str, priv->output->len,
	                             G_PRIORITY_DEFAULT, NULL, ovsdb_write_cb, self);
}
/*****************************************************************************/

/* Routines to maintain the ovsdb connection. */

/**
 * ovsdb_disconnect:
 *
 * Clean up the internal state to the point equivalent to before connecting.
 * Apart from clean shutdown this is a good response to unexpected trouble,
 * since the next method call attempt a will trigger reconnect which hopefully
 * puts us back in sync.
 */
static void
ovsdb_disconnect (NMOvsdb *self)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	OvsdbMethodCall *call;
	OvsdbMethodCallback callback;
	gpointer user_data;
	GError *error;

	_LOGD ("disconnecting from ovsdb");

	while (priv->calls->len) {
		error = NULL;
		call = &g_array_index (priv->calls, OvsdbMethodCall, priv->calls->len - 1);
		g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");

		callback = call->callback;
		user_data = call->user_data;
		g_array_remove_index (priv->calls, priv->calls->len - 1);
		callback (self, NULL, error, user_data);
	}

	priv->bufp = 0;
	g_string_truncate (priv->input, 0);
	g_string_truncate (priv->output, 0);
	g_clear_object (&priv->client);
	g_clear_object (&priv->conn);
	g_clear_pointer (&priv->db_uuid, g_free);
}

static void
_monitor_bridges_cb (NMOvsdb *self, json_t *result, GError *error, gpointer user_data)
{
	if (error) {
		if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
			_LOGI ("%s", error->message);
			ovsdb_disconnect (self);
		}

		g_clear_error (&error);
		return;
	}

	/* Treat the first response the same as the subsequent "update"
	 * messages we eventually get. */
	ovsdb_got_update (self, result);
}

static void
_client_connect_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
	GSocketClient *client = G_SOCKET_CLIENT (source_object);
	NMOvsdb *self = NM_OVSDB (user_data);
	NMOvsdbPrivate *priv;
	GError *error = NULL;
	GSocketConnection *conn;

	conn = g_socket_client_connect_finish (client, res, &error);
	if (conn == NULL) {
		if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
			_LOGI ("%s", error->message);

		ovsdb_disconnect (self);
		g_clear_error (&error);
		return;
	}

	priv = NM_OVSDB_GET_PRIVATE (self);
	priv->conn = conn;
	g_clear_object (&priv->cancellable);

	ovsdb_read (self);
	ovsdb_next_command (self);
}

/**
 * ovsdb_try_connect:
 *
 * Establish a connection to ovsdb unless it's already established or being
 * established. Queues a monitor command as a very first one so that we're in
 * sync when other commands are issued.
 */
static void
ovsdb_try_connect (NMOvsdb *self)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);
	GSocketAddress *addr;

	if (priv->client)
		return;

	/* XXX: This should probably be made configurable via NetworkManager.conf */
	addr = g_unix_socket_address_new (RUNSTATEDIR "/openvswitch/db.sock");

	priv->client = g_socket_client_new ();
	priv->cancellable = g_cancellable_new ();
	g_socket_client_connect_async (priv->client, G_SOCKET_CONNECTABLE (addr),
	                               priv->cancellable, _client_connect_cb, self);
	g_object_unref (addr);

	/* Queue a monitor call before any other command, ensuring that we have an up
	 * to date view of existing bridged that we need for add and remove ops. */
	ovsdb_call_method (self, NM_OVSDB_MONITOR, NULL, NULL, NULL, _monitor_bridges_cb, NULL);
}

/*****************************************************************************/

/* Public functions useful for NMDeviceOpenvswitch to maintain the life cycle of
 * their ovsdb entries without having to deal with ovsdb complexities themselves. */

typedef struct {
	NMOvsdbCallback callback;
	gpointer user_data;
} OvsdbCall;

static void
_transact_cb (NMOvsdb *self, json_t *result, GError *error, gpointer user_data)
{
	OvsdbCall *call = user_data;
	const char *err;
	const char *err_details;
	size_t index;
	json_t *value;

	if (error)
		goto out;

	json_array_foreach (result, index, value) {
		if (json_unpack (value, "{s:s, s:s}", "error", &err, "details", &err_details) == 0) {
			g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
			             "Error running the transaction: %s: %s", err, err_details);
			goto out;
		}
	}

out:
	call->callback (error, call->user_data);
	g_slice_free (OvsdbCall, call);
}

void
nm_ovsdb_transact (NMOvsdb *self, NMOvsdbCommand command,
                   const char *bridge, NMConnection *port, NMConnection *interface,
                   NMOvsdbCallback callback, gpointer user_data)
{
	OvsdbCall *call;

	call = g_slice_new (OvsdbCall);
	call->callback = callback;
	call->user_data = user_data;

	ovsdb_call_method (self, command, bridge, port, interface, _transact_cb, call);
}

/*****************************************************************************/

static void
_clear_call (gpointer data)
{
	OvsdbMethodCall *call = data;

	g_clear_pointer (&call->bridge, g_free);
	g_clear_object (&call->port);
	g_clear_object (&call->interface);
}

static void
_free_bridge (gpointer data)
{
	OpenvswitchBridge *ovs_bridge = data;

	g_free (ovs_bridge->name);
	g_free (ovs_bridge->connection_uuid);
	g_ptr_array_free (ovs_bridge->ports, TRUE);
	g_slice_free (OpenvswitchBridge, ovs_bridge);
}

static void
_free_port (gpointer data)
{
	OpenvswitchPort *ovs_port = data;

	g_free (ovs_port->name);
	g_free (ovs_port->connection_uuid);
	g_ptr_array_free (ovs_port->interfaces, TRUE);
	g_slice_free (OpenvswitchPort, ovs_port);
}

static void
_free_interface (gpointer data)
{
	OpenvswitchInterface *ovs_interface = data;

	g_free (ovs_interface->name);
	g_free (ovs_interface->connection_uuid);
	g_slice_free (OpenvswitchInterface, ovs_interface);
}

static void
nm_ovsdb_init (NMOvsdb *self)
{
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);

	priv->calls = g_array_new (FALSE, TRUE, sizeof (OvsdbMethodCall));
	g_array_set_clear_func (priv->calls, _clear_call);
	priv->input = g_string_new (NULL);
	priv->output = g_string_new (NULL);
	priv->bridges = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _free_bridge);
	priv->ports = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _free_port);
	priv->interfaces = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _free_interface);

	ovsdb_try_connect (self);
}

static void
dispose (GObject *object)
{
	NMOvsdb *self = NM_OVSDB (object);
	NMOvsdbPrivate *priv = NM_OVSDB_GET_PRIVATE (self);

	ovsdb_disconnect (self);

	g_string_free (priv->input, TRUE);
	priv->input = NULL;
	g_string_free (priv->output, TRUE);
	priv->output = NULL;

	if (priv->calls) {
		g_array_free (priv->calls, TRUE);
		priv->calls = NULL;
	}

	g_clear_pointer (&priv->bridges, g_hash_table_destroy);
	g_clear_pointer (&priv->ports, g_hash_table_destroy);
	g_clear_pointer (&priv->interfaces, g_hash_table_destroy);

	g_cancellable_cancel (priv->cancellable);
	g_clear_object (&priv->cancellable);

	G_OBJECT_CLASS (nm_ovsdb_parent_class)->dispose (object);
}

static void
nm_ovsdb_class_init (NMOvsdbClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = dispose;
}