Debian Codegraph @

utils/bond2team

raw
1 #!/bin/bash
2 #
3 # Translate tool from bonding configuration to team.
4 #
5 # Copyright (C) 2013 Flavio Leitner <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License, version 2,
9 # as published by the Free Software Foundation.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #
20
21 VERSION="0.97"
22
23 PR_QUIET=0
24 PR_ERR=1
25 PR_WARN=2
26 PR_INFO=3
27 PR_DBG=4
28
29 #defaults
30 ARGC=$#
31
32 FORMAT_IFCFG=0
33 FORMAT_JANSSON=1
34 OUTPUT_FORMAT=${FORMAT_IFCFG}
35
36 MODE_IFCFG=0
37 MODE_NOIFCFG=1
38 MODE=${MODE_IFCFG}
39
40 OUTPUT_FILE=
41 RENAME=
42 DEVICE=
43 BOND_MASTER=
44 STDOUT=1
45 BONDING_OPTS=
46 CONFIGDIR="/etc/sysconfig/network-scripts"
47 PR_LVL=3
48 OUTPUT_DIR=
49 OUTPUT_TMP_DIR=
50 TMP_FILES=()
51 RUNNER_OPTS=()
52 LWATCH_OPTS=()
53 # array: ( 'port ifname', 'opt1', 'opt2', 'optn', 'opt4', 'port ifname', ... )
54 PORT_LIST=
55 PORTS_OPTS=()
56 PRIMARY=
57 PRIMARY_RESELECT=
58
59 # building file scratch memory area
60 VFILE=
61
62 show_examples()
63 {
64 cat << EOF
65
66 The following commands will deliver the ifcfg files into a temporary
67 directory. You can review the files and copy to the right location.
68
69 Add the following argument to the commands below to print the output
70 to the screen instead of writing to files.
71 --stdout
72
73 Add the following arguments to the commands below to set the
74 destination directory for the output files.
75 --outputdir </path/to/dir>
76
77 Add the following argument to the commands below to output the
78 files in teamd format (JSON) instead of the default ifcfg format.
79 --json
80
81 To convert the current "bond0" ifcfg configuration to team ifcfg:
82 # $0 --master bond0
83
84 To convert the current "bond0" ifcfg configuration out of the
85 standard ifcfg-:
86 # $0 --master bond0 --configdir </path/to/ifcfg>
87
88 To convert the current "bond0" ifcfg configuration to team ifcfg
89 renaming the interface name to "team0". (carefull: firewall rules,
90 aliases interfaces, etc., will break after the renaming because the
91 tool will only change the ifcfg file, nothing else)
92 # $0 --master bond0 --rename team0
93
94 To convert given bonding parameters without any ifcfg:
95 # $0 --bonding_opts "mode=1 miimon=500"
96
97 To convert given bonding parameters without any ifcfg with ports:
98 # $0 --bonding_opts "mode=1 miimon=500 primary=eth1 primary_reselect-0" \\
99 --port eth1 --port eth2 --port eth3 --port eth4
100
101 EOF
102 }
103 usage()
104 {
105 cat << EOF
106 usage: $0 [options]
107
108 This tool translates bonding configuration to team.
109 See bond2team(1) for detailed information.
110
111 OPTIONS:
112
113 --master <interface> set the master interface name or ifcfg
114
115 --rename <iface> rename the master interface to <iface>
116
117 --ifcfg set the output format to ifcfg style
118
119 --json set the output format to teamd style
120
121 --bonding_opts pass the bonding options instead of reading
122 from the ifcfg- file
123
124 --port <iface> add the interface to the port's list
125
126 --configdir <dir> set where the ifcfg- files are
127 default: /etc/sysconfig/network-scripts
128
129 --outputdir <dir> set the output diretory
130 default: temporary diretory
131
132 --stdout print to stdout instead of
133 modify the system's files.
134
135 --debug increase debug level
136
137 --quiet no messages
138
139 --version show the tool version
140
141 --help this screen
142
143 --examples show command examples
144
145 EOF
146 }
147
148 # Output Functions
149 pr()
150 {
151 if [ $1 -le $PR_LVL ]; then
152 shift;
153 echo "$*" > /dev/stderr
154 fi
155 }
156
157 pr_error()
158 {
159 pr ${PR_ERR} "ERROR: " $*
160 }
161
162 pr_warn()
163 {
164 pr ${PR_WARN} "WARNING: " $*
165 }
166
167 pr_info()
168 {
169 pr ${PR_INFO} "INFO: " $*
170 }
171
172 pr_dbg()
173 {
174 pr ${PR_DBG} "DEBUG: " $*
175 }
176
177 to_stdout()
178 {
179 return ${STDOUT}
180 }
181
182 create_output_file()
183 {
184 local f=$1
185
186 if [ ! -d "${OUTPUT_TMP_DIR}" ]; then
187 OUTPUT_TMP_DIR=$(LANG=C mktemp -d /tmp/bond2team.XXXXXX)
188 fi
189
190 if [ ! -d "${OUTPUT_TMP_DIR}" ]; then
191 pr_error "${FUNCNAME} can't create dir ${OUTPUT_TMP_DIR}"
192 return 1
193 fi
194
195 local tmpfile=${OUTPUT_TMP_DIR}/${f}
196 touch ${tmpfile}
197 if [ ! -f ${tmpfile} ]; then
198 pr_error "${FUNCNAME} can't create file ${tmpfile}"
199 return 1
200 fi
201
202 local pos=${#TMP_FILES[*]}
203 TMP_FILES[${pos}]="${tmpfile}"
204 OUTPUT_FILE=${tmpfile}
205 }
206
207 show_output_files()
208 {
209 echo ""
210 echo "Resulted files:"
211 for tmpf in $(seq 0 $((${#TMP_FILES[@]} - 1)))
212 do
213 echo " ${TMP_FILES[$tmpf]}"
214 done
215 }
216
217 clean_up()
218 {
219 pr_dbg "${FUNCNAME} $*"
220 for tmpf in $(seq 0 $((${#TMP_FILES[@]} - 1)))
221 do
222 pr_dbg "rm -f ${TMP_FILES[$tmpf]}"
223 rm -f ${TMP_FILES[$tmpf]}
224 done
225
226 if [ -d "{OUTPUT_TMP_DIR}" ]; then
227 rmdir ${OUTPUT_TMP_DIR}
228 fi
229 }
230
231 ifcfg_get_device()
232 {
233 local ifcfg=$1
234 if [ ! -f ${ifcfg} ]; then
235 pr_error "file not found: ${ifcfg}"
236 return 1
237 fi
238
239 DEVICE=`LANG=C sed -n \
240 "s@^[[:space:]]*DEVICE=[\"]*\(.*\)\([[:space:]#]\|\"\|$\)@\1@p" \
241 $ifcfg`
242
243 if [ -z "${DEVICE}" ]; then
244 pr_error "ifcfg file not supported: ${ifcfg}"
245 return 1
246 fi
247 }
248
249 ifcfg_get_master_file()
250 {
251 local dev=${1}
252 MASTER="${dev}"
253
254 if [ "${MODE}" -eq "${MODE_NOIFCFG}" ]; then
255 return 0
256 fi
257
258 if [ ! -f ${MASTER} ]; then
259 MASTER="${CONFIGDIR}/ifcfg-${dev}"
260 if [ -f ${MASTER} ]; then
261 return 0
262 fi
263
264 if [ -n "${BONDING_OPTS}" ]; then
265 # options provided, set noifcfg
266 MODE=${MODE_NOIFCFG}
267 MASTER=${dev}
268 return 0
269 fi
270
271 pr_error "Can't find ifcfg file for ${dev}"
272 return 1
273 fi
274
275 return 0
276 }
277
278 ifcfg_overwrite_files()
279 {
280 pr_dbg "${FUNCNAME} $*"
281
282 /bin/cp -f ${OUTPUT_TMP_DIR}/ifcfg* ${OUTPUT_DIR}
283 }
284
285 ifcfg_get_bond_opts()
286 {
287 pr_dbg "${FUNCNAME} $*"
288 local ifcfg=$1
289
290 if [ -n "${BONDING_OPTS}" ]; then
291 pr_dbg "${FUNCNAME} bonding_opts=${BONDING_OPTS}"
292 return 0
293 fi
294
295 if [ ! -f ${ifcfg} ]; then
296 pr_error "File not found: ${ifcfg}"
297 return 1
298 fi
299
300 BONDING_OPTS=`LANG=C sed -n \
301 "s@^[[:space:]]*BONDING_OPTS=[\"]*\(.*\)\([[:space:]#]\|\"\|$\)@\1@p" \
302 $ifcfg`
303
304 if [ -z "${BONDING_OPTS}" ]; then
305 pr_error "ifcfg file not supported: ${MASTER}"
306 return 1
307 fi
308
309 pr_dbg "${FUNCNAME} bonding_opts=${BONDING_OPTS}"
310 return 0
311 }
312
313 vfile_reset()
314 {
315 VFILE=()
316 }
317
318 vfile_load_ifcfg()
319 {
320 pr_dbg "${FUNCNAME} $*"
321 local ifcfg=$1
322
323 vfile_reset
324
325 if [ ${MODE} -eq ${MODE_NOIFCFG} ]; then
326 return 0
327 fi
328
329 # filter out bonding and team options and
330 # don't break lines with spaces
331 oIFS="$IFS"
332 IFS=$'\n'
333 VFILE=( $(LANG=C \
334 grep -iv 'BONDING_OPTS\|SLAVE\|MASTER\|TYPE\|DEVICETYPE\|TEAM' \
335 $ifcfg ))
336 IFS="$oIFS"
337 }
338
339 vfile_write_to_file()
340 {
341 pr_dbg "${FUNCNAME} $*"
342 local output=$1
343 for ln in $(seq 0 $((${#VFILE[@]} - 1)))
344 do
345 echo "${VFILE[$ln]}" >> $output
346 done
347
348 return 0
349 }
350
351 ifcfg_dump_stdout()
352 {
353 local dev="${1}"
354 local ifcfg="ifcfg-${dev}"
355 if [ -z "${dev}" ]; then
356 ifcfg="ifcfg-<interface name>"
357 fi
358
359 for ln in $(seq 0 $((${#VFILE[@]} - 1)))
360 do
361 [ $ln -eq 0 ] && echo "---8<--- ${ifcfg} ---8<---"
362 echo "${VFILE[$ln]}"
363 done
364
365 echo "---8<--- ${ifcfg} ---8<---"
366 echo ""
367
368 return 0
369 }
370
371 vfile_get_device()
372 {
373 pr_dbg "${FUNCNAME} $*"
374 if [ ${MODE} -eq ${MODE_NOIFCFG} ]; then
375 pr_dbg "${FUNCNAME} using DEVICE=${MASTER}"
376 DEVICE=${MASTER}
377 return 0
378 fi
379
380 for ln in $(seq 0 $((${#VFILE[@]} - 1)))
381 do
382 local line=${VFILE[$ln]}
383 if [ "${line%%=*}" = "DEVICE" ]; then
384 local name_line="${line##*=}"
385 local name="${name_line%%[ # ]*}"
386 DEVICE=${name}
387 pr_dbg "${FUNCNAME} from file: DEVICE=${DEVICE}"
388 return 0
389 fi
390 done
391
392 pr_error "Failed to find the device's name"
393 return 1
394 }
395
396 vfile_get_ipaddr()
397 {
398 for ln in $(seq 0 $((${#VFILE[@]} - 1)))
399 do
400 local line=${VFILE[$ln]}
401 if [ "${line%%=*}" = "IPADDR" ]; then
402 local ipaddr_line="${line##*=}"
403 local ipaddr="${ipaddr_line%%[ # ]*}"
404 echo "${ipaddr}"
405 fi
406 done
407 }
408
409 vfile_add_line()
410 {
411 pr_dbg "${FUNCNAME} $*"
412 local pos=${#VFILE[*]}
413 VFILE[${pos}]="$1"
414 }
415
416 ifcfg_device_rename()
417 {
418 local device=$1
419 local rename=$2
420
421 # neither device nor rename was provided
422 if [ -z "${rename}" ]; then
423 return 0
424 fi
425
426 # renaming with no ifcfg?
427 if [ ${MODE} -eq ${MODE_NOIFCFG} ]; then
428 return 0
429 fi
430
431 for ln in $(seq 0 $((${#VFILE[@]} - 1)))
432 do
433 local line=${VFILE[$ln]}
434 if [ "${line%%=*}" = "DEVICE" ]; then
435 newdev="${line/${device}/${rename}}"
436 VFILE[$ln]="$newdev"
437 TEAM_MASTER=${rename}
438 return 0
439 fi
440 done
441 pr_error "Failed to rename $device to $rename"
442 return 1
443 }
444
445 team_port_set_devtype()
446 {
447 pr_dbg "${FUNCNAME} $*"
448 local master=$1
449 vfile_add_line "DEVICETYPE=\"TeamPort\""
450 vfile_add_line "TEAM_MASTER=\"$master\""
451 }
452
453 team_port_set_config()
454 {
455 pr_dbg "${FUNCNAME} $*"
456 local port=$1
457 local team_port_config=""
458
459 if [ "${PRIMARY}" == "$port" ]; then
460 team_port_config="'{ \"prio\" : -10"
461 else
462 team_port_config="'{ \"prio\" : -100"
463 fi
464
465 if [ -n "${PRIMARY_RESELECT}" ]; then
466 if [ "${PRIMARY}" == "$port" ]; then
467 if [ -z "${team_port_config}" ]; then
468 team_port_config="'{ \"sticky\" : true }'"
469 else
470 team_port_config="${team_port_config}, \"sticky\" : true }'"
471 fi
472 else
473 if [ -z "${team_port_config}" ]; then
474 team_port_config="{ \"sticky\" : false }'"
475 else
476 team_port_config="${team_port_config}, \"sticky\" : false }'"
477 fi
478 fi
479 else
480 if [ -n "${team_port_config}" ]; then
481 team_port_config="${team_port_config} }'"
482 fi
483
484 fi
485
486 if [ -n "$team_port_config" ]; then
487 vfile_add_line "TEAM_PORT_CONFIG=$team_port_config"
488 fi
489 }
490
491 team_port_ifcfg_create()
492 {
493 local dev=$1
494 vfile_load_ifcfg $dev
495
496 if ! vfile_get_device; then
497 return 1
498 fi
499
500 team_port_set_devtype ${TEAM_MASTER}
501 team_port_set_config ${DEVICE}
502 return 0
503 }
504
505 team_master_set_devtype()
506 {
507 pr_dbg "${FUNCNAME} $*"
508 vfile_add_line "DEVICETYPE=\"Team\""
509 }
510
511 team_master_set_config()
512 {
513 pr_dbg "${FUNCNAME} $*"
514 local team_config="'{ \"runner\" : { "
515 local nr_opt=0
516
517 nr_opt=${#RUNNER_OPTS[@]}
518 if [ $nr_opt -eq 0 ]; then
519 # default to miimon/ethtool
520 team_config="${team_config} \"name\" : \"roundrobin\" }"
521 else
522 # add runner options
523 for pos in $(seq 0 $((${#RUNNER_OPTS[@]} - 1)))
524 do
525 if [ $pos -ne 0 ]; then
526 team_config="${team_config}, "
527 fi
528
529 team_config="${team_config} ${RUNNER_OPTS[$pos]}"
530 done
531
532 team_config="${team_config} }"
533 fi
534
535 nr_opt=${#LWATCH_OPTS[@]}
536 if [ $nr_opt -eq 0 ]; then
537 # default to miimon/ethtool
538 team_config="${team_config}, \"link_watch\" : { \"name\" : \"ethtool\" }"
539 else
540 team_config="${team_config}, \"link_watch\" : { "
541 # add linkwatch options
542 for pos in $(seq 0 $(($nr_opt - 1)))
543 do
544 if [ $pos -ne 0 ]; then
545 team_config="${team_config}, "
546 fi
547
548 team_config="${team_config} ${LWATCH_OPTS[$pos]}"
549 done
550
551 team_config="${team_config} }"
552
553 fi
554
555 team_config="${team_config} }'"
556 pr_dbg "built team_config=${team_config}"
557 vfile_add_line "TEAM_CONFIG=${team_config}"
558 return 0
559 }
560
561 team_ifcfg_dump_stdout()
562 {
563 pr_dbg "${FUNCNAME} $*"
564 local dev=$1
565 if ! ifcfg_dump_stdout ${dev}; then
566 return 1
567 fi
568
569 return 0
570 }
571
572 team_ifcfg_write_file()
573 {
574 pr_dbg "${FUNCNAME} $*"
575 local dev=$1
576 OUTPUT_FILE=
577
578 local filenm="ifcfg-${dev}"
579 if [ -z "${dev}" ]; then
580 filenm="ifcfg"
581 fi
582
583 create_output_file ${filenm}
584 if [ ! -f "${OUTPUT_FILE}" ]; then
585 return 1
586 fi
587
588 if ! vfile_write_to_file ${OUTPUT_FILE}; then
589 return 1
590 fi
591
592 return 0
593 }
594
595 team_master_ifcfg_create()
596 {
597 pr_dbg "${FUNCNAME} $*"
598 if ! team_master_set_devtype; then
599 return 1
600 fi
601
602 if ! team_master_set_config; then
603 return 1
604 fi
605
606 return 0
607 }
608
609 team_ifcfg_write()
610 {
611 pr_dbg "${FUNCNAME} $*"
612 local dev=${1}
613
614 if to_stdout; then
615 team_ifcfg_dump_stdout ${dev} || return 1
616 else
617 team_ifcfg_write_file ${dev} || return 1
618 fi
619
620 return 0
621 }
622
623 team_ifcfg_deliver()
624 {
625 pr_dbg "${FUNCNAME} $*"
626 if to_stdout; then
627 return 0
628 fi
629
630 if [ -z "${OUTPUT_DIR}" ]; then
631 show_output_files
632 else
633 ifcfg_overwrite_files
634 clean_up
635 fi
636
637 return 0
638 }
639
640 teamd_config_create()
641 {
642 vfile_reset
643 vfile_add_line "{"
644 # add runner options
645 vfile_add_line " \"device\" : \"${DEVICE}\","
646 vfile_add_line " \"runner\" : {"
647 local runner_nr=${#RUNNER_OPTS[@]}
648 if [ ${runner_nr} -eq 0 ]; then
649 # default roundrobin
650 vfile_add_line " \"runner\" : \"roundrobin\" "
651 else
652 local last_pos=$((${runner_nr} - 1))
653 for pos in $(seq 0 ${last_pos})
654 do
655 if [ $pos -eq ${last_pos} ]; then
656 vfile_add_line " ${RUNNER_OPTS[$pos]}"
657 else
658 vfile_add_line " ${RUNNER_OPTS[$pos]},"
659 fi
660 done
661 fi
662
663 vfile_add_line " },"
664 vfile_add_line " \"link_watch\" : {"
665
666 local lwatch_nr=${#LWATCH_OPTS[@]}
667 if [ ${lwatch_nr} -eq 0 ]; then
668 # default to miimon
669 lwatch_add_opt "\"name\" : \"ethtool\""
670 else
671 for pos in $(seq 0 ${last_pos})
672 do
673 last_pos=$((${lwatch_nr} - 1))
674 if [ $pos -eq ${last_pos} ]; then
675 vfile_add_line " ${LWATCH_OPTS[$pos]}"
676 else
677 vfile_add_line " ${LWATCH_OPTS[$pos]},"
678 fi
679 done
680 fi
681
682 vfile_add_line " },"
683 return 0
684 }
685
686 teamd_config_close()
687 {
688 vfile_add_line "}"
689 return 0
690 }
691
692 teamd_dump_stdout()
693 {
694 for ln in $(seq 0 $((${#VFILE[@]} - 1)))
695 do
696 [ $ln -eq 0 ] && echo "---8<--- teamd.conf ---8<---"
697 echo "${VFILE[$ln]}"
698 done
699
700 echo "---8<--- teamd.conf ---8<---"
701 echo ""
702
703 return 0
704 }
705
706 teamd_write_file()
707 {
708 pr_dbg "${FUNCNAME} $*"
709 local dev=$1
710 OUTPUT_FILE=
711
712 create_output_file "teamd.conf"
713 if [ ! -f "${OUTPUT_FILE}" ]; then
714 return 1
715 fi
716
717 if ! vfile_write_to_file ${OUTPUT_FILE}; then
718 return 1
719 fi
720
721 return 0
722 }
723
724
725 teamd_config_write()
726 {
727 pr_dbg "${FUNCNAME} $*"
728
729 if to_stdout; then
730 teamd_dump_stdout || return 1
731 else
732 teamd_write_file ${dev} || return 1
733 show_output_files
734 fi
735
736 return 0
737 }
738
739 teamd_port_create()
740 {
741 vfile_add_line " \"ports\" : {"
742 return 0
743 }
744
745 teamd_port_close()
746 {
747 vfile_add_line " }"
748 return 0
749 }
750
751 teamd_port_add()
752 {
753 pr_dbg "${FUNCNAME} $*"
754 local dev=${1}
755 local lastone=${2}
756
757 if [ -n "${PORT_LIST}" ]; then
758 DEVICE=${dev}
759 else
760 if ! ifcfg_get_device ${dev}; then
761 return 1
762 fi
763 fi
764
765 vfile_add_line " \"${DEVICE}\" : {"
766 if [ "${PRIMARY}" == "${DEVICE}" ]; then
767 vfile_add_line " \"prio\" : -10,"
768 else
769 vfile_add_line " \"prio\" : -100,"
770 fi
771
772 if [ -n "${PRIMARY_RESELECT}" ]; then
773 if [ "${PRIMARY}" == "$port" ]; then
774 vfile_add_line " \"sticky\" : true "
775 else
776 vfile_add_line " \"sticky\" : false "
777 fi
778 else
779 vfile_add_line " \"sticky\" : false "
780 fi
781
782 if [ ${lastone} -eq 1 ]; then
783 vfile_add_line " }"
784 else
785 vfile_add_line " },"
786 fi
787
788 return 0
789 }
790
791 # Runner Functions
792 runner_add_opt()
793 {
794 pr_dbg "${FUNCNAME} $*"
795 local pos=${#RUNNER_OPTS[*]}
796 RUNNER_OPTS[${pos}]="$1"
797 }
798
799 runner_parse_adselect()
800 {
801 pr_dbg "${FUNCNAME} $*"
802 local value=$1
803
804 case $value in
805 "0"|"stable")
806 runner_add_opt "\"agg_select_policy\" : \"bandwidth\"" || return 1
807 ;;
808 "1"|"bandwidth")
809 runner_add_opt "\"agg_select_policy\" : \"bandwidth\"" || return 1
810 ;;
811 "2"|"count")
812 runner_add_opt "\"agg_select_policy\" : \"count\"" || return 1
813 ;;
814 *)
815 pr_error "parameter ad_select=$value is not supported"
816 return 1
817 esac
818 }
819
820 runner_parse_failovermac()
821 {
822 pr_dbg "${FUNCNAME} $*"
823 local value=$1
824
825 case $value in
826 "0")
827 runner_add_opt "\"hwaddr_policy\" : \"same_all\"" || return 1
828 ;;
829 "1"|"active")
830 runner_add_opt "\"hwaddr_policy\" : \"by_active\"" || return 1
831 ;;
832 "2"|"follow")
833 runner_add_opt "\"hwaddr_policy\" : \"only_active\"" || return 1
834 ;;
835 *)
836 pr_error "parameter fail_over_mac $value is not supported"
837 return 1
838 ;;
839 esac
840
841 }
842
843 runner_parse_lacprate()
844 {
845 pr_dbg "${FUNCNAME} $*"
846 local value=$1
847
848 case $value in
849 "slow"|"0")
850 runner_add_opt "\"fast_rate\" : 0" || return 1
851 ;;
852 "fast"|"1")
853 runner_add_opt "\"fast_rate\" : 1" || return 1
854 ;;
855 *)
856 pr_error "parameter lacp_rate=$value is not supported"
857 return 1
858 ;;
859 esac
860 }
861
862 runner_parse_xmit_policy()
863 {
864 pr_dbg "${FUNCNAME} $*"
865 local value=$1
866
867 case $value in
868 "layer2")
869 runner_add_opt "\"tx_hash\" : [ \"eth\" ]" || return 1
870 ;;
871 "layer2+3")
872 runner_add_opt "\"tx_hash\" : [ \"eth\", \"l3\" ]" || return 1
873 ;;
874 "layer3+4")
875 runner_add_opt "\"tx_hash\" : [ \"l3\", \"l4\" ]" || return 1
876 ;;
877 *)
878 pr_error "parameter xmit_hash_policy=$value is not supported"
879 return 1
880 esac
881 }
882
883 runner_parse_mode()
884 {
885 pr_dbg "${FUNCNAME} $*"
886 local value=$1
887
888 case $value in
889 "0"|"balance-rr")
890 runner_add_opt "\"name\" : \"roundrobin\"" || return 1
891 ;;
892 "1"|"active-backup")
893 runner_add_opt "\"name\" : \"activebackup\"" || return 1
894 ;;
895 "2"|"balance-xor")
896 # FIXME
897 runner_add_opt "\"name\" : \"loadbalance\"" || return 1
898 ;;
899 "3"|"broadcast")
900 runner_add_opt "\"name\" : \"broadcast\"" || return 1
901 ;;
902 "4"|"802.3ad")
903 runner_add_opt "\"name\" : \"lacp\"" || return 1
904 ;;
905 "5"|"balance-tlb")
906 runner_add_opt "\"name\" : \"loadbalance\"" || return 1
907 ;;
908 "6"|"balance-alb")
909 pr_error "parameter mode=$value is not supported"
910 return 1
911 ;;
912 *)
913 pr_error "parameter mode=$value is not supported"
914 return 1
915 ;;
916 esac
917 }
918
919 runner_parse_opt()
920 {
921 pr_dbg "${FUNCNAME} $*"
922 local param=$1
923 local value=$2
924
925 case $param in
926 "ad_select")
927 runner_parse_adselect $value || return 1
928 ;;
929 "fail_over_mac")
930 runner_parse_failovermac $value || return 1
931 ;;
932 "lacp_rate")
933 runner_parse_lacprate $value || return 1
934 ;;
935 "min_links")
936 runner_add_opt "\"min_ports\" : $value" || return 1
937 ;;
938 "mode")
939 runner_parse_mode $value || return 1
940 ;;
941 "xmit_hash_policy")
942 runner_parse_xmit_policy $value || return 1
943 ;;
944 esac
945 }
946
947 # Link Watch functions
948 lwatch_add_opt()
949 {
950 pr_dbg "${FUNCNAME} $*"
951 local pos=${#LWATCH_OPTS[*]}
952 LWATCH_OPTS[${pos}]="$1"
953 }
954
955 lwatch_parse_arp_validate()
956 {
957 pr_dbg "${FUNCNAME} $*"
958 local value=$1
959
960 case $value in
961 "0"|"none")
962 ;;
963 "1"|"active")
964 lwatch_add_opt "\"validate_active\" : 1" || return 1
965 ;;
966 "2"|"backup")
967 lwatch_add_opt "\"validate_inactive\" : 1" || return 1
968 ;;
969 "3"|"all")
970 lwatch_add_opt "\"validate_active\" : 1" || return 1
971 lwatch_add_opt "\"validate_inactive\" : 1" || return 1
972 ;;
973 *)
974 pr_error "parameter arp_validate=$value is not supported"
975 return 1
976 ;;
977 esac
978
979 }
980
981 lwatch_parse_arpiptarget()
982 {
983 #FIXME: supports only one arp_ip_target address.
984 # otherwise a new linkwatch section must be create
985 pr_dbg "${FUNCNAME} $*"
986 local ip_addrs=$1
987 local ip_list=${ip_addrs//,/ }
988 local ip_array=($ip_list)
989 if [ ${#ip_array[*]} -ne 1 ]; then
990 pr_error "parameter arp_ip_target= with multiple IP addresses is not supported"
991 return 1
992 fi
993
994 for addr in ${ip_list}
995 do
996 lwatch_add_opt "\"target_host\" : \"$addr\""
997 done
998 }
999
1000 lwatch_parse_opt()
1001 {
1002 pr_dbg "${FUNCNAME} $*"
1003 local param=$1
1004 local value=$2
1005
1006 case $param in
1007 "arp_interval")
1008 lwatch_add_opt "\"interval\" : $value"
1009 ;;
1010 "arp_ip_target")
1011 lwatch_add_opt "\"name\" : \"arp_ping\""
1012 if ! lwatch_parse_arpiptarget $value; then
1013 return 1
1014 fi
1015 ;;
1016 "arp_validate")
1017 lwatch_parse_arp_validate $value
1018 ;;
1019 "downdelay")
1020 lwatch_add_opt "\"delay_down\" : $value"
1021 ;;
1022 "miimon")
1023 lwatch_add_opt "\"name\" : \"ethtool\""
1024 ;;
1025 "updelay")
1026 lwatch_add_opt "\"delay_up\" : $value"
1027 ;;
1028 *)
1029 pr_error "parameter $param=$value is not supported"
1030 return 1
1031 ;;
1032 esac
1033 }
1034
1035 port_parse_opt()
1036 {
1037 pr_dbg "${FUNCNAME} $*"
1038 local param=$1
1039 local value=$2
1040
1041 case $param in
1042 "primary")
1043 PRIMARY="$value"
1044 ;;
1045 "primary_reselect")
1046 case $value in
1047 "0"|"always")
1048 PRIMARY_RESELECT=1
1049 ;;
1050 "1"|"better")
1051 ;;
1052 "2"|"failure")
1053 ;;
1054 *)
1055 pr_error "parameter $param=$value is not supported"
1056 return 1
1057 esac
1058 ;;
1059 *)
1060 pr_error "parameter $param=$value is not supported"
1061 return 1
1062 ;;
1063 esac
1064
1065 }
1066
1067 convert_bond_opts()
1068 {
1069 local bonding_opts=$1
1070 pr_dbg "${FUNCNAME} $*"
1071
1072 for arg in $bonding_opts
1073 do
1074 key=${arg%%=*};
1075 value=${arg##*=};
1076 pr_dbg "parsing $key=$value"
1077 case "$key" in
1078 "active_slave"|"max_bonds"|"use_carrier")
1079 pr_info "parameter $key not supported, ignoring"
1080 continue
1081 ;;
1082 "all_slaves_active"|"resend_igmp"|"num_grat_arp"|"num_unsol_na")
1083 pr_error "parameter $key not supported, aborting"
1084 return 1
1085 ;;
1086 "ad_select"|"fail_over_mac"|"lacp_rate"|"min_links"|"mode"|"xmit_hash_policy")
1087 runner_parse_opt $key $value || return 1
1088 ;;
1089 "arp_interval"|"arp_ip_target"|"arp_validate"|"downdelay"|"miimon"|"updelay")
1090 lwatch_parse_opt $key $value || return 1
1091 ;;
1092 "primary"|"primary_reselect")
1093 port_parse_opt $key $value || return 1
1094 ;;
1095 *)
1096 pr_error "unknown parameter $key=$value, aborting"
1097 return 1
1098 ;;
1099 esac
1100 done
1101 }
1102
1103
1104
1105 # Parse command line options
1106 while :;
1107 do
1108 case "$1" in
1109 "--master")
1110 MASTER="$2"
1111 shift 2
1112 ;;
1113 "--bonding_opts")
1114 BONDING_OPTS="$2"
1115 shift 2
1116 ;;
1117 "--ifcfg")
1118 OUTPUT_FORMAT=${FORMAT_IFCFG}
1119 shift
1120 ;;
1121 "--json")
1122 OUTPUT_FORMAT=${FORMAT_JANSSON}
1123 shift
1124 ;;
1125 "--quiet")
1126 PR_LVL=${PR_QUIET}
1127 shift
1128 ;;
1129 "--debug")
1130 PR_LVL=`expr ${PR_LVL} + 1`
1131 shift
1132 ;;
1133 "--outputdir")
1134 OUTPUT_DIR="$2"
1135 shift 2
1136 ;;
1137 "--configdir")
1138 CONFIGDIR="$2"
1139 shift 2
1140 ;;
1141 "--rename")
1142 [ -n "${RENAME}" ] && usage && exit 1
1143 RENAME="$2"
1144 shift 2
1145 ;;
1146 "--stdout")
1147 STDOUT=0
1148 shift
1149 ;;
1150 "--port")
1151 PORT_LIST="${PORT_LIST} $2"
1152 shift 2
1153 ;;
1154 "--version")
1155 echo "$VERSION"
1156 exit 0
1157 ;;
1158 "--help")
1159 usage
1160 exit 0
1161 ;;
1162 "--examples")
1163 show_examples
1164 exit 0
1165 ;;
1166 *)
1167 if [ -z "$1" ]; then
1168 break
1169 fi
1170 pr_error "unknown parameter: $1"
1171 usage
1172 exit 1
1173 ;;
1174 esac
1175 done
1176
1177 if [ -n "${OUTPUT_DIR}" -a ! -d "${OUTPUT_DIR}" ]; then
1178 pr_error "Invalid output diretory: ${OUTPUT_DIR}"
1179 usage
1180 exit 1
1181 fi
1182
1183 if [ -z "${MASTER}" -a -z "${BONDING_OPTS}" ]; then
1184 pr_error "No master interface or bonding options specified"
1185 usage
1186 exit 1
1187 fi
1188
1189 # no master means no ifcfg to read
1190 if [ -z "${MASTER}" ]; then
1191 MODE=${MODE_NOIFCFG}
1192 fi
1193
1194 if [ ${OUTPUT_FORMAT} -eq ${FORMAT_JANSSON} -a -z "${MASTER}" ]; then
1195 MASTER="team0"
1196 fi
1197
1198 if ! ifcfg_get_master_file ${MASTER}; then
1199 exit 1
1200 fi
1201
1202 # load the ifcfg file
1203 if ! vfile_load_ifcfg ${MASTER}; then
1204 exit 1
1205 fi
1206
1207 # get the bonding options
1208 if ! ifcfg_get_bond_opts ${MASTER}; then
1209 exit 1
1210 fi
1211
1212 if ! convert_bond_opts "${BONDING_OPTS}"; then
1213 exit 1
1214 fi
1215
1216 if ! vfile_get_device; then
1217 exit 1
1218 fi
1219
1220 TEAM_MASTER=${DEVICE}
1221 if ! ifcfg_device_rename ${DEVICE} ${RENAME}; then
1222 exit 1
1223 fi
1224
1225 BOND_MASTER=${DEVICE}
1226
1227 if [ ${OUTPUT_FORMAT} -eq ${FORMAT_IFCFG} ]; then
1228 if ! team_master_ifcfg_create; then
1229 exit 1
1230 fi
1231
1232 if ! team_ifcfg_write ${TEAM_MASTER}; then
1233 clean_up
1234 exit 1
1235 fi
1236
1237 # process all ports
1238 for portcfg in $(LANG=C grep -s -l "^[[:space:]]*MASTER=\"\?${BOND_MASTER}\"\?\([[:space:]#]\|$\)" ${CONFIGDIR}/ifcfg-*)
1239 do
1240 if ! team_port_ifcfg_create $portcfg; then
1241 clean_up
1242 exit 1
1243 fi
1244
1245 if ! team_ifcfg_write ${DEVICE}; then
1246 clean_up
1247 exit 1
1248 fi
1249
1250 done
1251
1252 team_ifcfg_deliver
1253 else
1254 if ! teamd_config_create; then
1255 exit 1
1256 fi
1257
1258 if ! teamd_port_create; then
1259 exit 1
1260 fi
1261
1262 if [ -n "${PORT_LIST}" ]; then
1263 portcfg_list=${PORT_LIST}
1264 else
1265 portcfg_list=$(LANG=C grep -s -l "^[[:space:]]*MASTER=\"\?${BOND_MASTER}\"\?\([[:space:]#]\|$\)" ${CONFIGDIR}/ifcfg-*)
1266 fi
1267 # count number of ports
1268 portcfg_total=0
1269 for portcfg in ${portcfg_list}
1270 do
1271 portcfg_total=$((${portcfg_total} + 1))
1272 done
1273 # process all ports
1274 portcfg_nr=0
1275 lastone=0
1276 for portcfg in ${portcfg_list}
1277 do
1278 portcfg_nr=$((${portcfg_nr} + 1))
1279 if [ ${portcfg_nr} -eq ${portcfg_total} ]; then
1280 lastone=1
1281 fi
1282
1283 if ! teamd_port_add ${portcfg} ${lastone}; then
1284 exit 1
1285 fi
1286 done
1287
1288 if ! teamd_port_close; then
1289 exit 1
1290 fi
1291
1292 if ! teamd_config_close; then
1293 exit 1
1294 fi
1295
1296 if ! teamd_config_write; then
1297 exit 1
1298 fi
1299
1300 fi