datapath.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. /*
  2. * Copyright (c) 2007-2013 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/lockdep.h>
  46. #include <linux/openvswitch.h>
  47. #include <linux/rculist.h>
  48. #include <linux/dmi.h>
  49. #include <linux/workqueue.h>
  50. #include <net/genetlink.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. #include "datapath.h"
  54. #include "flow.h"
  55. #include "flow_netlink.h"
  56. #include "vport-internal_dev.h"
  57. #include "vport-netdev.h"
  58. int ovs_net_id __read_mostly;
  59. static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
  60. struct genl_multicast_group *grp)
  61. {
  62. genl_notify(skb, genl_info_net(info), info->snd_portid,
  63. grp->id, info->nlhdr, GFP_KERNEL);
  64. }
  65. /**
  66. * DOC: Locking:
  67. *
  68. * All writes e.g. Writes to device state (add/remove datapath, port, set
  69. * operations on vports, etc.), Writes to other state (flow table
  70. * modifications, set miscellaneous datapath parameters, etc.) are protected
  71. * by ovs_lock.
  72. *
  73. * Reads are protected by RCU.
  74. *
  75. * There are a few special cases (mostly stats) that have their own
  76. * synchronization but they nest under all of above and don't interact with
  77. * each other.
  78. *
  79. * The RTNL lock nests inside ovs_mutex.
  80. */
  81. static DEFINE_MUTEX(ovs_mutex);
  82. void ovs_lock(void)
  83. {
  84. mutex_lock(&ovs_mutex);
  85. }
  86. void ovs_unlock(void)
  87. {
  88. mutex_unlock(&ovs_mutex);
  89. }
  90. #ifdef CONFIG_LOCKDEP
  91. int lockdep_ovsl_is_held(void)
  92. {
  93. if (debug_locks)
  94. return lockdep_is_held(&ovs_mutex);
  95. else
  96. return 1;
  97. }
  98. #endif
  99. static struct vport *new_vport(const struct vport_parms *);
  100. static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
  101. const struct dp_upcall_info *);
  102. static int queue_userspace_packet(struct net *, int dp_ifindex,
  103. struct sk_buff *,
  104. const struct dp_upcall_info *);
  105. /* Must be called with rcu_read_lock or ovs_mutex. */
  106. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  107. {
  108. struct datapath *dp = NULL;
  109. struct net_device *dev;
  110. rcu_read_lock();
  111. dev = dev_get_by_index_rcu(net, dp_ifindex);
  112. if (dev) {
  113. struct vport *vport = ovs_internal_dev_get_vport(dev);
  114. if (vport)
  115. dp = vport->dp;
  116. }
  117. rcu_read_unlock();
  118. return dp;
  119. }
  120. /* Must be called with rcu_read_lock or ovs_mutex. */
  121. const char *ovs_dp_name(const struct datapath *dp)
  122. {
  123. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  124. return vport->ops->get_name(vport);
  125. }
  126. static int get_dpifindex(struct datapath *dp)
  127. {
  128. struct vport *local;
  129. int ifindex;
  130. rcu_read_lock();
  131. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  132. if (local)
  133. ifindex = netdev_vport_priv(local)->dev->ifindex;
  134. else
  135. ifindex = 0;
  136. rcu_read_unlock();
  137. return ifindex;
  138. }
  139. static void destroy_dp_rcu(struct rcu_head *rcu)
  140. {
  141. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  142. ovs_flow_tbl_destroy(&dp->table, false);
  143. free_percpu(dp->stats_percpu);
  144. release_net(ovs_dp_get_net(dp));
  145. kfree(dp->ports);
  146. kfree(dp);
  147. }
  148. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  149. u16 port_no)
  150. {
  151. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  152. }
  153. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  154. {
  155. struct vport *vport;
  156. struct hlist_head *head;
  157. head = vport_hash_bucket(dp, port_no);
  158. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  159. if (vport->port_no == port_no)
  160. return vport;
  161. }
  162. return NULL;
  163. }
  164. /* Called with ovs_mutex. */
  165. static struct vport *new_vport(const struct vport_parms *parms)
  166. {
  167. struct vport *vport;
  168. vport = ovs_vport_add(parms);
  169. if (!IS_ERR(vport)) {
  170. struct datapath *dp = parms->dp;
  171. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  172. hlist_add_head_rcu(&vport->dp_hash_node, head);
  173. }
  174. return vport;
  175. }
  176. void ovs_dp_detach_port(struct vport *p)
  177. {
  178. ASSERT_OVSL();
  179. /* First drop references to device. */
  180. hlist_del_rcu(&p->dp_hash_node);
  181. /* Then destroy it. */
  182. ovs_vport_del(p);
  183. }
  184. /* Must be called with rcu_read_lock. */
  185. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  186. {
  187. struct datapath *dp = p->dp;
  188. struct sw_flow *flow;
  189. struct dp_stats_percpu *stats;
  190. struct sw_flow_key key;
  191. u64 *stats_counter;
  192. int error;
  193. stats = this_cpu_ptr(dp->stats_percpu);
  194. /* Extract flow from 'skb' into 'key'. */
  195. error = ovs_flow_extract(skb, p->port_no, &key);
  196. if (unlikely(error)) {
  197. kfree_skb(skb);
  198. return;
  199. }
  200. /* Look up flow. */
  201. flow = ovs_flow_tbl_lookup(&dp->table, &key);
  202. if (unlikely(!flow)) {
  203. struct dp_upcall_info upcall;
  204. upcall.cmd = OVS_PACKET_CMD_MISS;
  205. upcall.key = &key;
  206. upcall.userdata = NULL;
  207. upcall.portid = p->upcall_portid;
  208. ovs_dp_upcall(dp, skb, &upcall);
  209. consume_skb(skb);
  210. stats_counter = &stats->n_missed;
  211. goto out;
  212. }
  213. OVS_CB(skb)->flow = flow;
  214. OVS_CB(skb)->pkt_key = &key;
  215. stats_counter = &stats->n_hit;
  216. ovs_flow_used(OVS_CB(skb)->flow, skb);
  217. ovs_execute_actions(dp, skb);
  218. out:
  219. /* Update datapath statistics. */
  220. u64_stats_update_begin(&stats->sync);
  221. (*stats_counter)++;
  222. u64_stats_update_end(&stats->sync);
  223. }
  224. static struct genl_family dp_packet_genl_family = {
  225. .id = GENL_ID_GENERATE,
  226. .hdrsize = sizeof(struct ovs_header),
  227. .name = OVS_PACKET_FAMILY,
  228. .version = OVS_PACKET_VERSION,
  229. .maxattr = OVS_PACKET_ATTR_MAX,
  230. .netnsok = true,
  231. .parallel_ops = true,
  232. };
  233. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  234. const struct dp_upcall_info *upcall_info)
  235. {
  236. struct dp_stats_percpu *stats;
  237. int dp_ifindex;
  238. int err;
  239. if (upcall_info->portid == 0) {
  240. err = -ENOTCONN;
  241. goto err;
  242. }
  243. dp_ifindex = get_dpifindex(dp);
  244. if (!dp_ifindex) {
  245. err = -ENODEV;
  246. goto err;
  247. }
  248. if (!skb_is_gso(skb))
  249. err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  250. else
  251. err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  252. if (err)
  253. goto err;
  254. return 0;
  255. err:
  256. stats = this_cpu_ptr(dp->stats_percpu);
  257. u64_stats_update_begin(&stats->sync);
  258. stats->n_lost++;
  259. u64_stats_update_end(&stats->sync);
  260. return err;
  261. }
  262. static int queue_gso_packets(struct net *net, int dp_ifindex,
  263. struct sk_buff *skb,
  264. const struct dp_upcall_info *upcall_info)
  265. {
  266. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  267. struct dp_upcall_info later_info;
  268. struct sw_flow_key later_key;
  269. struct sk_buff *segs, *nskb;
  270. int err;
  271. segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
  272. if (IS_ERR(segs))
  273. return PTR_ERR(segs);
  274. /* Queue all of the segments. */
  275. skb = segs;
  276. do {
  277. err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
  278. if (err)
  279. break;
  280. if (skb == segs && gso_type & SKB_GSO_UDP) {
  281. /* The initial flow key extracted by ovs_flow_extract()
  282. * in this case is for a first fragment, so we need to
  283. * properly mark later fragments.
  284. */
  285. later_key = *upcall_info->key;
  286. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  287. later_info = *upcall_info;
  288. later_info.key = &later_key;
  289. upcall_info = &later_info;
  290. }
  291. } while ((skb = skb->next));
  292. /* Free all of the segments. */
  293. skb = segs;
  294. do {
  295. nskb = skb->next;
  296. if (err)
  297. kfree_skb(skb);
  298. else
  299. consume_skb(skb);
  300. } while ((skb = nskb));
  301. return err;
  302. }
  303. static size_t key_attr_size(void)
  304. {
  305. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  306. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  307. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  308. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  309. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  310. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  311. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  312. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  313. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  314. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  315. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  316. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  317. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  318. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  319. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  320. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  321. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  322. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  323. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  324. }
  325. static size_t upcall_msg_size(const struct sk_buff *skb,
  326. const struct nlattr *userdata)
  327. {
  328. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  329. + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
  330. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  331. /* OVS_PACKET_ATTR_USERDATA */
  332. if (userdata)
  333. size += NLA_ALIGN(userdata->nla_len);
  334. return size;
  335. }
  336. static int queue_userspace_packet(struct net *net, int dp_ifindex,
  337. struct sk_buff *skb,
  338. const struct dp_upcall_info *upcall_info)
  339. {
  340. struct ovs_header *upcall;
  341. struct sk_buff *nskb = NULL;
  342. struct sk_buff *user_skb; /* to be queued to userspace */
  343. struct nlattr *nla;
  344. int err;
  345. if (vlan_tx_tag_present(skb)) {
  346. nskb = skb_clone(skb, GFP_ATOMIC);
  347. if (!nskb)
  348. return -ENOMEM;
  349. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  350. if (!nskb)
  351. return -ENOMEM;
  352. nskb->vlan_tci = 0;
  353. skb = nskb;
  354. }
  355. if (nla_attr_size(skb->len) > USHRT_MAX) {
  356. err = -EFBIG;
  357. goto out;
  358. }
  359. user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
  360. if (!user_skb) {
  361. err = -ENOMEM;
  362. goto out;
  363. }
  364. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  365. 0, upcall_info->cmd);
  366. upcall->dp_ifindex = dp_ifindex;
  367. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  368. ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
  369. nla_nest_end(user_skb, nla);
  370. if (upcall_info->userdata)
  371. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  372. nla_len(upcall_info->userdata),
  373. nla_data(upcall_info->userdata));
  374. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  375. skb_copy_and_csum_dev(skb, nla_data(nla));
  376. genlmsg_end(user_skb, upcall);
  377. err = genlmsg_unicast(net, user_skb, upcall_info->portid);
  378. out:
  379. kfree_skb(nskb);
  380. return err;
  381. }
  382. static void clear_stats(struct sw_flow *flow)
  383. {
  384. flow->used = 0;
  385. flow->tcp_flags = 0;
  386. flow->packet_count = 0;
  387. flow->byte_count = 0;
  388. }
  389. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  390. {
  391. struct ovs_header *ovs_header = info->userhdr;
  392. struct nlattr **a = info->attrs;
  393. struct sw_flow_actions *acts;
  394. struct sk_buff *packet;
  395. struct sw_flow *flow;
  396. struct datapath *dp;
  397. struct ethhdr *eth;
  398. int len;
  399. int err;
  400. err = -EINVAL;
  401. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  402. !a[OVS_PACKET_ATTR_ACTIONS])
  403. goto err;
  404. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  405. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  406. err = -ENOMEM;
  407. if (!packet)
  408. goto err;
  409. skb_reserve(packet, NET_IP_ALIGN);
  410. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  411. skb_reset_mac_header(packet);
  412. eth = eth_hdr(packet);
  413. /* Normally, setting the skb 'protocol' field would be handled by a
  414. * call to eth_type_trans(), but it assumes there's a sending
  415. * device, which we may not have. */
  416. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  417. packet->protocol = eth->h_proto;
  418. else
  419. packet->protocol = htons(ETH_P_802_2);
  420. /* Build an sw_flow for sending this packet. */
  421. flow = ovs_flow_alloc();
  422. err = PTR_ERR(flow);
  423. if (IS_ERR(flow))
  424. goto err_kfree_skb;
  425. err = ovs_flow_extract(packet, -1, &flow->key);
  426. if (err)
  427. goto err_flow_free;
  428. err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
  429. if (err)
  430. goto err_flow_free;
  431. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  432. err = PTR_ERR(acts);
  433. if (IS_ERR(acts))
  434. goto err_flow_free;
  435. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  436. &flow->key, 0, &acts);
  437. rcu_assign_pointer(flow->sf_acts, acts);
  438. if (err)
  439. goto err_flow_free;
  440. OVS_CB(packet)->flow = flow;
  441. OVS_CB(packet)->pkt_key = &flow->key;
  442. packet->priority = flow->key.phy.priority;
  443. packet->mark = flow->key.phy.skb_mark;
  444. rcu_read_lock();
  445. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  446. err = -ENODEV;
  447. if (!dp)
  448. goto err_unlock;
  449. local_bh_disable();
  450. err = ovs_execute_actions(dp, packet);
  451. local_bh_enable();
  452. rcu_read_unlock();
  453. ovs_flow_free(flow, false);
  454. return err;
  455. err_unlock:
  456. rcu_read_unlock();
  457. err_flow_free:
  458. ovs_flow_free(flow, false);
  459. err_kfree_skb:
  460. kfree_skb(packet);
  461. err:
  462. return err;
  463. }
  464. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  465. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  466. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  467. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  468. };
  469. static struct genl_ops dp_packet_genl_ops[] = {
  470. { .cmd = OVS_PACKET_CMD_EXECUTE,
  471. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  472. .policy = packet_policy,
  473. .doit = ovs_packet_cmd_execute
  474. }
  475. };
  476. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  477. {
  478. int i;
  479. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  480. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  481. for_each_possible_cpu(i) {
  482. const struct dp_stats_percpu *percpu_stats;
  483. struct dp_stats_percpu local_stats;
  484. unsigned int start;
  485. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  486. do {
  487. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  488. local_stats = *percpu_stats;
  489. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  490. stats->n_hit += local_stats.n_hit;
  491. stats->n_missed += local_stats.n_missed;
  492. stats->n_lost += local_stats.n_lost;
  493. }
  494. }
  495. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  496. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  497. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  498. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  499. };
  500. static struct genl_family dp_flow_genl_family = {
  501. .id = GENL_ID_GENERATE,
  502. .hdrsize = sizeof(struct ovs_header),
  503. .name = OVS_FLOW_FAMILY,
  504. .version = OVS_FLOW_VERSION,
  505. .maxattr = OVS_FLOW_ATTR_MAX,
  506. .netnsok = true,
  507. .parallel_ops = true,
  508. };
  509. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  510. .name = OVS_FLOW_MCGROUP
  511. };
  512. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  513. {
  514. return NLMSG_ALIGN(sizeof(struct ovs_header))
  515. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  516. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  517. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  518. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  519. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  520. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  521. }
  522. /* Called with ovs_mutex. */
  523. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  524. struct sk_buff *skb, u32 portid,
  525. u32 seq, u32 flags, u8 cmd)
  526. {
  527. const int skb_orig_len = skb->len;
  528. struct nlattr *start;
  529. struct ovs_flow_stats stats;
  530. struct ovs_header *ovs_header;
  531. struct nlattr *nla;
  532. unsigned long used;
  533. u8 tcp_flags;
  534. int err;
  535. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  536. if (!ovs_header)
  537. return -EMSGSIZE;
  538. ovs_header->dp_ifindex = get_dpifindex(dp);
  539. /* Fill flow key. */
  540. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  541. if (!nla)
  542. goto nla_put_failure;
  543. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  544. if (err)
  545. goto error;
  546. nla_nest_end(skb, nla);
  547. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  548. if (!nla)
  549. goto nla_put_failure;
  550. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  551. if (err)
  552. goto error;
  553. nla_nest_end(skb, nla);
  554. spin_lock_bh(&flow->lock);
  555. used = flow->used;
  556. stats.n_packets = flow->packet_count;
  557. stats.n_bytes = flow->byte_count;
  558. tcp_flags = flow->tcp_flags;
  559. spin_unlock_bh(&flow->lock);
  560. if (used &&
  561. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  562. goto nla_put_failure;
  563. if (stats.n_packets &&
  564. nla_put(skb, OVS_FLOW_ATTR_STATS,
  565. sizeof(struct ovs_flow_stats), &stats))
  566. goto nla_put_failure;
  567. if (tcp_flags &&
  568. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  569. goto nla_put_failure;
  570. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  571. * this is the first flow to be dumped into 'skb'. This is unusual for
  572. * Netlink but individual action lists can be longer than
  573. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  574. * The userspace caller can always fetch the actions separately if it
  575. * really wants them. (Most userspace callers in fact don't care.)
  576. *
  577. * This can only fail for dump operations because the skb is always
  578. * properly sized for single flows.
  579. */
  580. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  581. if (start) {
  582. const struct sw_flow_actions *sf_acts;
  583. sf_acts = rcu_dereference_check(flow->sf_acts,
  584. lockdep_ovsl_is_held());
  585. err = ovs_nla_put_actions(sf_acts->actions,
  586. sf_acts->actions_len, skb);
  587. if (!err)
  588. nla_nest_end(skb, start);
  589. else {
  590. if (skb_orig_len)
  591. goto error;
  592. nla_nest_cancel(skb, start);
  593. }
  594. } else if (skb_orig_len)
  595. goto nla_put_failure;
  596. return genlmsg_end(skb, ovs_header);
  597. nla_put_failure:
  598. err = -EMSGSIZE;
  599. error:
  600. genlmsg_cancel(skb, ovs_header);
  601. return err;
  602. }
  603. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  604. {
  605. const struct sw_flow_actions *sf_acts;
  606. sf_acts = ovsl_dereference(flow->sf_acts);
  607. return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
  608. }
  609. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  610. struct datapath *dp,
  611. u32 portid, u32 seq, u8 cmd)
  612. {
  613. struct sk_buff *skb;
  614. int retval;
  615. skb = ovs_flow_cmd_alloc_info(flow);
  616. if (!skb)
  617. return ERR_PTR(-ENOMEM);
  618. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  619. BUG_ON(retval < 0);
  620. return skb;
  621. }
  622. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  623. {
  624. struct nlattr **a = info->attrs;
  625. struct ovs_header *ovs_header = info->userhdr;
  626. struct sw_flow_key key, masked_key;
  627. struct sw_flow *flow = NULL;
  628. struct sw_flow_mask mask;
  629. struct sk_buff *reply;
  630. struct datapath *dp;
  631. struct sw_flow_actions *acts = NULL;
  632. struct sw_flow_match match;
  633. int error;
  634. /* Extract key. */
  635. error = -EINVAL;
  636. if (!a[OVS_FLOW_ATTR_KEY])
  637. goto error;
  638. ovs_match_init(&match, &key, &mask);
  639. error = ovs_nla_get_match(&match,
  640. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  641. if (error)
  642. goto error;
  643. /* Validate actions. */
  644. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  645. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  646. error = PTR_ERR(acts);
  647. if (IS_ERR(acts))
  648. goto error;
  649. ovs_flow_mask_key(&masked_key, &key, &mask);
  650. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
  651. &masked_key, 0, &acts);
  652. if (error) {
  653. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  654. goto err_kfree;
  655. }
  656. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  657. error = -EINVAL;
  658. goto error;
  659. }
  660. ovs_lock();
  661. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  662. error = -ENODEV;
  663. if (!dp)
  664. goto err_unlock_ovs;
  665. /* Check if this is a duplicate flow */
  666. flow = ovs_flow_tbl_lookup(&dp->table, &key);
  667. if (!flow) {
  668. struct sw_flow_mask *mask_p;
  669. /* Bail out if we're not allowed to create a new flow. */
  670. error = -ENOENT;
  671. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  672. goto err_unlock_ovs;
  673. /* Allocate flow. */
  674. flow = ovs_flow_alloc();
  675. if (IS_ERR(flow)) {
  676. error = PTR_ERR(flow);
  677. goto err_unlock_ovs;
  678. }
  679. clear_stats(flow);
  680. flow->key = masked_key;
  681. flow->unmasked_key = key;
  682. /* Make sure mask is unique in the system */
  683. mask_p = ovs_sw_flow_mask_find(&dp->table, &mask);
  684. if (!mask_p) {
  685. /* Allocate a new mask if none exsits. */
  686. mask_p = ovs_sw_flow_mask_alloc();
  687. if (!mask_p)
  688. goto err_flow_free;
  689. mask_p->key = mask.key;
  690. mask_p->range = mask.range;
  691. ovs_sw_flow_mask_insert(&dp->table, mask_p);
  692. }
  693. ovs_sw_flow_mask_add_ref(mask_p);
  694. flow->mask = mask_p;
  695. rcu_assign_pointer(flow->sf_acts, acts);
  696. /* Put flow in bucket. */
  697. ovs_flow_tbl_insert(&dp->table, flow);
  698. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  699. info->snd_seq, OVS_FLOW_CMD_NEW);
  700. } else {
  701. /* We found a matching flow. */
  702. struct sw_flow_actions *old_acts;
  703. /* Bail out if we're not allowed to modify an existing flow.
  704. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  705. * because Generic Netlink treats the latter as a dump
  706. * request. We also accept NLM_F_EXCL in case that bug ever
  707. * gets fixed.
  708. */
  709. error = -EEXIST;
  710. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  711. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  712. goto err_unlock_ovs;
  713. /* The unmasked key has to be the same for flow updates. */
  714. error = -EINVAL;
  715. if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
  716. OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
  717. goto err_unlock_ovs;
  718. }
  719. /* Update actions. */
  720. old_acts = ovsl_dereference(flow->sf_acts);
  721. rcu_assign_pointer(flow->sf_acts, acts);
  722. ovs_nla_free_flow_actions(old_acts);
  723. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  724. info->snd_seq, OVS_FLOW_CMD_NEW);
  725. /* Clear stats. */
  726. if (a[OVS_FLOW_ATTR_CLEAR]) {
  727. spin_lock_bh(&flow->lock);
  728. clear_stats(flow);
  729. spin_unlock_bh(&flow->lock);
  730. }
  731. }
  732. ovs_unlock();
  733. if (!IS_ERR(reply))
  734. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  735. else
  736. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  737. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  738. return 0;
  739. err_flow_free:
  740. ovs_flow_free(flow, false);
  741. err_unlock_ovs:
  742. ovs_unlock();
  743. err_kfree:
  744. kfree(acts);
  745. error:
  746. return error;
  747. }
  748. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  749. {
  750. struct nlattr **a = info->attrs;
  751. struct ovs_header *ovs_header = info->userhdr;
  752. struct sw_flow_key key;
  753. struct sk_buff *reply;
  754. struct sw_flow *flow;
  755. struct datapath *dp;
  756. struct sw_flow_match match;
  757. int err;
  758. if (!a[OVS_FLOW_ATTR_KEY]) {
  759. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  760. return -EINVAL;
  761. }
  762. ovs_match_init(&match, &key, NULL);
  763. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  764. if (err)
  765. return err;
  766. ovs_lock();
  767. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  768. if (!dp) {
  769. err = -ENODEV;
  770. goto unlock;
  771. }
  772. flow = ovs_flow_tbl_lookup(&dp->table, &key);
  773. if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
  774. err = -ENOENT;
  775. goto unlock;
  776. }
  777. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  778. info->snd_seq, OVS_FLOW_CMD_NEW);
  779. if (IS_ERR(reply)) {
  780. err = PTR_ERR(reply);
  781. goto unlock;
  782. }
  783. ovs_unlock();
  784. return genlmsg_reply(reply, info);
  785. unlock:
  786. ovs_unlock();
  787. return err;
  788. }
  789. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  790. {
  791. struct nlattr **a = info->attrs;
  792. struct ovs_header *ovs_header = info->userhdr;
  793. struct sw_flow_key key;
  794. struct sk_buff *reply;
  795. struct sw_flow *flow;
  796. struct datapath *dp;
  797. struct sw_flow_match match;
  798. int err;
  799. ovs_lock();
  800. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  801. if (!dp) {
  802. err = -ENODEV;
  803. goto unlock;
  804. }
  805. if (!a[OVS_FLOW_ATTR_KEY]) {
  806. err = ovs_flow_tbl_flush(&dp->table);
  807. goto unlock;
  808. }
  809. ovs_match_init(&match, &key, NULL);
  810. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  811. if (err)
  812. goto unlock;
  813. flow = ovs_flow_tbl_lookup(&dp->table, &key);
  814. if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
  815. err = -ENOENT;
  816. goto unlock;
  817. }
  818. reply = ovs_flow_cmd_alloc_info(flow);
  819. if (!reply) {
  820. err = -ENOMEM;
  821. goto unlock;
  822. }
  823. ovs_flow_tbl_remove(&dp->table, flow);
  824. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  825. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  826. BUG_ON(err < 0);
  827. ovs_flow_free(flow, true);
  828. ovs_unlock();
  829. ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
  830. return 0;
  831. unlock:
  832. ovs_unlock();
  833. return err;
  834. }
  835. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  836. {
  837. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  838. struct table_instance *ti;
  839. struct datapath *dp;
  840. rcu_read_lock();
  841. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  842. if (!dp) {
  843. rcu_read_unlock();
  844. return -ENODEV;
  845. }
  846. ti = rcu_dereference(dp->table.ti);
  847. for (;;) {
  848. struct sw_flow *flow;
  849. u32 bucket, obj;
  850. bucket = cb->args[0];
  851. obj = cb->args[1];
  852. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  853. if (!flow)
  854. break;
  855. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  856. NETLINK_CB(cb->skb).portid,
  857. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  858. OVS_FLOW_CMD_NEW) < 0)
  859. break;
  860. cb->args[0] = bucket;
  861. cb->args[1] = obj;
  862. }
  863. rcu_read_unlock();
  864. return skb->len;
  865. }
  866. static struct genl_ops dp_flow_genl_ops[] = {
  867. { .cmd = OVS_FLOW_CMD_NEW,
  868. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  869. .policy = flow_policy,
  870. .doit = ovs_flow_cmd_new_or_set
  871. },
  872. { .cmd = OVS_FLOW_CMD_DEL,
  873. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  874. .policy = flow_policy,
  875. .doit = ovs_flow_cmd_del
  876. },
  877. { .cmd = OVS_FLOW_CMD_GET,
  878. .flags = 0, /* OK for unprivileged users. */
  879. .policy = flow_policy,
  880. .doit = ovs_flow_cmd_get,
  881. .dumpit = ovs_flow_cmd_dump
  882. },
  883. { .cmd = OVS_FLOW_CMD_SET,
  884. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  885. .policy = flow_policy,
  886. .doit = ovs_flow_cmd_new_or_set,
  887. },
  888. };
  889. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  890. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  891. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  892. };
  893. static struct genl_family dp_datapath_genl_family = {
  894. .id = GENL_ID_GENERATE,
  895. .hdrsize = sizeof(struct ovs_header),
  896. .name = OVS_DATAPATH_FAMILY,
  897. .version = OVS_DATAPATH_VERSION,
  898. .maxattr = OVS_DP_ATTR_MAX,
  899. .netnsok = true,
  900. .parallel_ops = true,
  901. };
  902. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  903. .name = OVS_DATAPATH_MCGROUP
  904. };
  905. static size_t ovs_dp_cmd_msg_size(void)
  906. {
  907. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  908. msgsize += nla_total_size(IFNAMSIZ);
  909. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  910. return msgsize;
  911. }
  912. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  913. u32 portid, u32 seq, u32 flags, u8 cmd)
  914. {
  915. struct ovs_header *ovs_header;
  916. struct ovs_dp_stats dp_stats;
  917. int err;
  918. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  919. flags, cmd);
  920. if (!ovs_header)
  921. goto error;
  922. ovs_header->dp_ifindex = get_dpifindex(dp);
  923. rcu_read_lock();
  924. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  925. rcu_read_unlock();
  926. if (err)
  927. goto nla_put_failure;
  928. get_dp_stats(dp, &dp_stats);
  929. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  930. goto nla_put_failure;
  931. return genlmsg_end(skb, ovs_header);
  932. nla_put_failure:
  933. genlmsg_cancel(skb, ovs_header);
  934. error:
  935. return -EMSGSIZE;
  936. }
  937. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  938. u32 seq, u8 cmd)
  939. {
  940. struct sk_buff *skb;
  941. int retval;
  942. skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
  943. if (!skb)
  944. return ERR_PTR(-ENOMEM);
  945. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  946. if (retval < 0) {
  947. kfree_skb(skb);
  948. return ERR_PTR(retval);
  949. }
  950. return skb;
  951. }
  952. /* Called with ovs_mutex. */
  953. static struct datapath *lookup_datapath(struct net *net,
  954. struct ovs_header *ovs_header,
  955. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  956. {
  957. struct datapath *dp;
  958. if (!a[OVS_DP_ATTR_NAME])
  959. dp = get_dp(net, ovs_header->dp_ifindex);
  960. else {
  961. struct vport *vport;
  962. rcu_read_lock();
  963. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  964. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  965. rcu_read_unlock();
  966. }
  967. return dp ? dp : ERR_PTR(-ENODEV);
  968. }
  969. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  970. {
  971. struct nlattr **a = info->attrs;
  972. struct vport_parms parms;
  973. struct sk_buff *reply;
  974. struct datapath *dp;
  975. struct vport *vport;
  976. struct ovs_net *ovs_net;
  977. int err, i;
  978. err = -EINVAL;
  979. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  980. goto err;
  981. ovs_lock();
  982. err = -ENOMEM;
  983. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  984. if (dp == NULL)
  985. goto err_unlock_ovs;
  986. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  987. /* Allocate table. */
  988. err = ovs_flow_tbl_init(&dp->table);
  989. if (err)
  990. goto err_free_dp;
  991. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  992. if (!dp->stats_percpu) {
  993. err = -ENOMEM;
  994. goto err_destroy_table;
  995. }
  996. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  997. GFP_KERNEL);
  998. if (!dp->ports) {
  999. err = -ENOMEM;
  1000. goto err_destroy_percpu;
  1001. }
  1002. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1003. INIT_HLIST_HEAD(&dp->ports[i]);
  1004. /* Set up our datapath device. */
  1005. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1006. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1007. parms.options = NULL;
  1008. parms.dp = dp;
  1009. parms.port_no = OVSP_LOCAL;
  1010. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1011. vport = new_vport(&parms);
  1012. if (IS_ERR(vport)) {
  1013. err = PTR_ERR(vport);
  1014. if (err == -EBUSY)
  1015. err = -EEXIST;
  1016. goto err_destroy_ports_array;
  1017. }
  1018. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1019. info->snd_seq, OVS_DP_CMD_NEW);
  1020. err = PTR_ERR(reply);
  1021. if (IS_ERR(reply))
  1022. goto err_destroy_local_port;
  1023. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1024. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1025. ovs_unlock();
  1026. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1027. return 0;
  1028. err_destroy_local_port:
  1029. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1030. err_destroy_ports_array:
  1031. kfree(dp->ports);
  1032. err_destroy_percpu:
  1033. free_percpu(dp->stats_percpu);
  1034. err_destroy_table:
  1035. ovs_flow_tbl_destroy(&dp->table, false);
  1036. err_free_dp:
  1037. release_net(ovs_dp_get_net(dp));
  1038. kfree(dp);
  1039. err_unlock_ovs:
  1040. ovs_unlock();
  1041. err:
  1042. return err;
  1043. }
  1044. /* Called with ovs_mutex. */
  1045. static void __dp_destroy(struct datapath *dp)
  1046. {
  1047. int i;
  1048. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1049. struct vport *vport;
  1050. struct hlist_node *n;
  1051. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1052. if (vport->port_no != OVSP_LOCAL)
  1053. ovs_dp_detach_port(vport);
  1054. }
  1055. list_del_rcu(&dp->list_node);
  1056. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1057. * all port in datapath are destroyed first before freeing datapath.
  1058. */
  1059. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1060. call_rcu(&dp->rcu, destroy_dp_rcu);
  1061. }
  1062. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1063. {
  1064. struct sk_buff *reply;
  1065. struct datapath *dp;
  1066. int err;
  1067. ovs_lock();
  1068. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1069. err = PTR_ERR(dp);
  1070. if (IS_ERR(dp))
  1071. goto unlock;
  1072. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1073. info->snd_seq, OVS_DP_CMD_DEL);
  1074. err = PTR_ERR(reply);
  1075. if (IS_ERR(reply))
  1076. goto unlock;
  1077. __dp_destroy(dp);
  1078. ovs_unlock();
  1079. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1080. return 0;
  1081. unlock:
  1082. ovs_unlock();
  1083. return err;
  1084. }
  1085. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1086. {
  1087. struct sk_buff *reply;
  1088. struct datapath *dp;
  1089. int err;
  1090. ovs_lock();
  1091. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1092. err = PTR_ERR(dp);
  1093. if (IS_ERR(dp))
  1094. goto unlock;
  1095. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1096. info->snd_seq, OVS_DP_CMD_NEW);
  1097. if (IS_ERR(reply)) {
  1098. err = PTR_ERR(reply);
  1099. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1100. ovs_dp_datapath_multicast_group.id, err);
  1101. err = 0;
  1102. goto unlock;
  1103. }
  1104. ovs_unlock();
  1105. ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
  1106. return 0;
  1107. unlock:
  1108. ovs_unlock();
  1109. return err;
  1110. }
  1111. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1112. {
  1113. struct sk_buff *reply;
  1114. struct datapath *dp;
  1115. int err;
  1116. ovs_lock();
  1117. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1118. if (IS_ERR(dp)) {
  1119. err = PTR_ERR(dp);
  1120. goto unlock;
  1121. }
  1122. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1123. info->snd_seq, OVS_DP_CMD_NEW);
  1124. if (IS_ERR(reply)) {
  1125. err = PTR_ERR(reply);
  1126. goto unlock;
  1127. }
  1128. ovs_unlock();
  1129. return genlmsg_reply(reply, info);
  1130. unlock:
  1131. ovs_unlock();
  1132. return err;
  1133. }
  1134. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1135. {
  1136. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1137. struct datapath *dp;
  1138. int skip = cb->args[0];
  1139. int i = 0;
  1140. rcu_read_lock();
  1141. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1142. if (i >= skip &&
  1143. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1144. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1145. OVS_DP_CMD_NEW) < 0)
  1146. break;
  1147. i++;
  1148. }
  1149. rcu_read_unlock();
  1150. cb->args[0] = i;
  1151. return skb->len;
  1152. }
  1153. static struct genl_ops dp_datapath_genl_ops[] = {
  1154. { .cmd = OVS_DP_CMD_NEW,
  1155. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1156. .policy = datapath_policy,
  1157. .doit = ovs_dp_cmd_new
  1158. },
  1159. { .cmd = OVS_DP_CMD_DEL,
  1160. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1161. .policy = datapath_policy,
  1162. .doit = ovs_dp_cmd_del
  1163. },
  1164. { .cmd = OVS_DP_CMD_GET,
  1165. .flags = 0, /* OK for unprivileged users. */
  1166. .policy = datapath_policy,
  1167. .doit = ovs_dp_cmd_get,
  1168. .dumpit = ovs_dp_cmd_dump
  1169. },
  1170. { .cmd = OVS_DP_CMD_SET,
  1171. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1172. .policy = datapath_policy,
  1173. .doit = ovs_dp_cmd_set,
  1174. },
  1175. };
  1176. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1177. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1178. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1179. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1180. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1181. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1182. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1183. };
  1184. static struct genl_family dp_vport_genl_family = {
  1185. .id = GENL_ID_GENERATE,
  1186. .hdrsize = sizeof(struct ovs_header),
  1187. .name = OVS_VPORT_FAMILY,
  1188. .version = OVS_VPORT_VERSION,
  1189. .maxattr = OVS_VPORT_ATTR_MAX,
  1190. .netnsok = true,
  1191. .parallel_ops = true,
  1192. };
  1193. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1194. .name = OVS_VPORT_MCGROUP
  1195. };
  1196. /* Called with ovs_mutex or RCU read lock. */
  1197. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1198. u32 portid, u32 seq, u32 flags, u8 cmd)
  1199. {
  1200. struct ovs_header *ovs_header;
  1201. struct ovs_vport_stats vport_stats;
  1202. int err;
  1203. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1204. flags, cmd);
  1205. if (!ovs_header)
  1206. return -EMSGSIZE;
  1207. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1208. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1209. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1210. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1211. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1212. goto nla_put_failure;
  1213. ovs_vport_get_stats(vport, &vport_stats);
  1214. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1215. &vport_stats))
  1216. goto nla_put_failure;
  1217. err = ovs_vport_get_options(vport, skb);
  1218. if (err == -EMSGSIZE)
  1219. goto error;
  1220. return genlmsg_end(skb, ovs_header);
  1221. nla_put_failure:
  1222. err = -EMSGSIZE;
  1223. error:
  1224. genlmsg_cancel(skb, ovs_header);
  1225. return err;
  1226. }
  1227. /* Called with ovs_mutex or RCU read lock. */
  1228. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1229. u32 seq, u8 cmd)
  1230. {
  1231. struct sk_buff *skb;
  1232. int retval;
  1233. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1234. if (!skb)
  1235. return ERR_PTR(-ENOMEM);
  1236. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1237. BUG_ON(retval < 0);
  1238. return skb;
  1239. }
  1240. /* Called with ovs_mutex or RCU read lock. */
  1241. static struct vport *lookup_vport(struct net *net,
  1242. struct ovs_header *ovs_header,
  1243. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1244. {
  1245. struct datapath *dp;
  1246. struct vport *vport;
  1247. if (a[OVS_VPORT_ATTR_NAME]) {
  1248. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1249. if (!vport)
  1250. return ERR_PTR(-ENODEV);
  1251. if (ovs_header->dp_ifindex &&
  1252. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1253. return ERR_PTR(-ENODEV);
  1254. return vport;
  1255. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1256. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1257. if (port_no >= DP_MAX_PORTS)
  1258. return ERR_PTR(-EFBIG);
  1259. dp = get_dp(net, ovs_header->dp_ifindex);
  1260. if (!dp)
  1261. return ERR_PTR(-ENODEV);
  1262. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1263. if (!vport)
  1264. return ERR_PTR(-ENODEV);
  1265. return vport;
  1266. } else
  1267. return ERR_PTR(-EINVAL);
  1268. }
  1269. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1270. {
  1271. struct nlattr **a = info->attrs;
  1272. struct ovs_header *ovs_header = info->userhdr;
  1273. struct vport_parms parms;
  1274. struct sk_buff *reply;
  1275. struct vport *vport;
  1276. struct datapath *dp;
  1277. u32 port_no;
  1278. int err;
  1279. err = -EINVAL;
  1280. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1281. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1282. goto exit;
  1283. ovs_lock();
  1284. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1285. err = -ENODEV;
  1286. if (!dp)
  1287. goto exit_unlock;
  1288. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1289. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1290. err = -EFBIG;
  1291. if (port_no >= DP_MAX_PORTS)
  1292. goto exit_unlock;
  1293. vport = ovs_vport_ovsl(dp, port_no);
  1294. err = -EBUSY;
  1295. if (vport)
  1296. goto exit_unlock;
  1297. } else {
  1298. for (port_no = 1; ; port_no++) {
  1299. if (port_no >= DP_MAX_PORTS) {
  1300. err = -EFBIG;
  1301. goto exit_unlock;
  1302. }
  1303. vport = ovs_vport_ovsl(dp, port_no);
  1304. if (!vport)
  1305. break;
  1306. }
  1307. }
  1308. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1309. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1310. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1311. parms.dp = dp;
  1312. parms.port_no = port_no;
  1313. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1314. vport = new_vport(&parms);
  1315. err = PTR_ERR(vport);
  1316. if (IS_ERR(vport))
  1317. goto exit_unlock;
  1318. err = 0;
  1319. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1320. OVS_VPORT_CMD_NEW);
  1321. if (IS_ERR(reply)) {
  1322. err = PTR_ERR(reply);
  1323. ovs_dp_detach_port(vport);
  1324. goto exit_unlock;
  1325. }
  1326. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1327. exit_unlock:
  1328. ovs_unlock();
  1329. exit:
  1330. return err;
  1331. }
  1332. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1333. {
  1334. struct nlattr **a = info->attrs;
  1335. struct sk_buff *reply;
  1336. struct vport *vport;
  1337. int err;
  1338. ovs_lock();
  1339. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1340. err = PTR_ERR(vport);
  1341. if (IS_ERR(vport))
  1342. goto exit_unlock;
  1343. if (a[OVS_VPORT_ATTR_TYPE] &&
  1344. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1345. err = -EINVAL;
  1346. goto exit_unlock;
  1347. }
  1348. reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1349. if (!reply) {
  1350. err = -ENOMEM;
  1351. goto exit_unlock;
  1352. }
  1353. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1354. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1355. if (err)
  1356. goto exit_free;
  1357. }
  1358. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1359. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1360. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1361. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1362. BUG_ON(err < 0);
  1363. ovs_unlock();
  1364. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1365. return 0;
  1366. exit_free:
  1367. kfree_skb(reply);
  1368. exit_unlock:
  1369. ovs_unlock();
  1370. return err;
  1371. }
  1372. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1373. {
  1374. struct nlattr **a = info->attrs;
  1375. struct sk_buff *reply;
  1376. struct vport *vport;
  1377. int err;
  1378. ovs_lock();
  1379. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1380. err = PTR_ERR(vport);
  1381. if (IS_ERR(vport))
  1382. goto exit_unlock;
  1383. if (vport->port_no == OVSP_LOCAL) {
  1384. err = -EINVAL;
  1385. goto exit_unlock;
  1386. }
  1387. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1388. info->snd_seq, OVS_VPORT_CMD_DEL);
  1389. err = PTR_ERR(reply);
  1390. if (IS_ERR(reply))
  1391. goto exit_unlock;
  1392. err = 0;
  1393. ovs_dp_detach_port(vport);
  1394. ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
  1395. exit_unlock:
  1396. ovs_unlock();
  1397. return err;
  1398. }
  1399. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1400. {
  1401. struct nlattr **a = info->attrs;
  1402. struct ovs_header *ovs_header = info->userhdr;
  1403. struct sk_buff *reply;
  1404. struct vport *vport;
  1405. int err;
  1406. rcu_read_lock();
  1407. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1408. err = PTR_ERR(vport);
  1409. if (IS_ERR(vport))
  1410. goto exit_unlock;
  1411. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1412. info->snd_seq, OVS_VPORT_CMD_NEW);
  1413. err = PTR_ERR(reply);
  1414. if (IS_ERR(reply))
  1415. goto exit_unlock;
  1416. rcu_read_unlock();
  1417. return genlmsg_reply(reply, info);
  1418. exit_unlock:
  1419. rcu_read_unlock();
  1420. return err;
  1421. }
  1422. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1423. {
  1424. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1425. struct datapath *dp;
  1426. int bucket = cb->args[0], skip = cb->args[1];
  1427. int i, j = 0;
  1428. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1429. if (!dp)
  1430. return -ENODEV;
  1431. rcu_read_lock();
  1432. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1433. struct vport *vport;
  1434. j = 0;
  1435. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1436. if (j >= skip &&
  1437. ovs_vport_cmd_fill_info(vport, skb,
  1438. NETLINK_CB(cb->skb).portid,
  1439. cb->nlh->nlmsg_seq,
  1440. NLM_F_MULTI,
  1441. OVS_VPORT_CMD_NEW) < 0)
  1442. goto out;
  1443. j++;
  1444. }
  1445. skip = 0;
  1446. }
  1447. out:
  1448. rcu_read_unlock();
  1449. cb->args[0] = i;
  1450. cb->args[1] = j;
  1451. return skb->len;
  1452. }
  1453. static struct genl_ops dp_vport_genl_ops[] = {
  1454. { .cmd = OVS_VPORT_CMD_NEW,
  1455. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1456. .policy = vport_policy,
  1457. .doit = ovs_vport_cmd_new
  1458. },
  1459. { .cmd = OVS_VPORT_CMD_DEL,
  1460. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1461. .policy = vport_policy,
  1462. .doit = ovs_vport_cmd_del
  1463. },
  1464. { .cmd = OVS_VPORT_CMD_GET,
  1465. .flags = 0, /* OK for unprivileged users. */
  1466. .policy = vport_policy,
  1467. .doit = ovs_vport_cmd_get,
  1468. .dumpit = ovs_vport_cmd_dump
  1469. },
  1470. { .cmd = OVS_VPORT_CMD_SET,
  1471. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1472. .policy = vport_policy,
  1473. .doit = ovs_vport_cmd_set,
  1474. },
  1475. };
  1476. struct genl_family_and_ops {
  1477. struct genl_family *family;
  1478. struct genl_ops *ops;
  1479. int n_ops;
  1480. struct genl_multicast_group *group;
  1481. };
  1482. static const struct genl_family_and_ops dp_genl_families[] = {
  1483. { &dp_datapath_genl_family,
  1484. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1485. &ovs_dp_datapath_multicast_group },
  1486. { &dp_vport_genl_family,
  1487. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1488. &ovs_dp_vport_multicast_group },
  1489. { &dp_flow_genl_family,
  1490. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1491. &ovs_dp_flow_multicast_group },
  1492. { &dp_packet_genl_family,
  1493. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1494. NULL },
  1495. };
  1496. static void dp_unregister_genl(int n_families)
  1497. {
  1498. int i;
  1499. for (i = 0; i < n_families; i++)
  1500. genl_unregister_family(dp_genl_families[i].family);
  1501. }
  1502. static int dp_register_genl(void)
  1503. {
  1504. int n_registered;
  1505. int err;
  1506. int i;
  1507. n_registered = 0;
  1508. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1509. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1510. err = genl_register_family_with_ops(f->family, f->ops,
  1511. f->n_ops);
  1512. if (err)
  1513. goto error;
  1514. n_registered++;
  1515. if (f->group) {
  1516. err = genl_register_mc_group(f->family, f->group);
  1517. if (err)
  1518. goto error;
  1519. }
  1520. }
  1521. return 0;
  1522. error:
  1523. dp_unregister_genl(n_registered);
  1524. return err;
  1525. }
  1526. static int __net_init ovs_init_net(struct net *net)
  1527. {
  1528. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1529. INIT_LIST_HEAD(&ovs_net->dps);
  1530. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1531. return 0;
  1532. }
  1533. static void __net_exit ovs_exit_net(struct net *net)
  1534. {
  1535. struct datapath *dp, *dp_next;
  1536. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1537. ovs_lock();
  1538. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1539. __dp_destroy(dp);
  1540. ovs_unlock();
  1541. cancel_work_sync(&ovs_net->dp_notify_work);
  1542. }
  1543. static struct pernet_operations ovs_net_ops = {
  1544. .init = ovs_init_net,
  1545. .exit = ovs_exit_net,
  1546. .id = &ovs_net_id,
  1547. .size = sizeof(struct ovs_net),
  1548. };
  1549. static int __init dp_init(void)
  1550. {
  1551. int err;
  1552. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1553. pr_info("Open vSwitch switching datapath\n");
  1554. err = ovs_flow_init();
  1555. if (err)
  1556. goto error;
  1557. err = ovs_vport_init();
  1558. if (err)
  1559. goto error_flow_exit;
  1560. err = register_pernet_device(&ovs_net_ops);
  1561. if (err)
  1562. goto error_vport_exit;
  1563. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1564. if (err)
  1565. goto error_netns_exit;
  1566. err = dp_register_genl();
  1567. if (err < 0)
  1568. goto error_unreg_notifier;
  1569. return 0;
  1570. error_unreg_notifier:
  1571. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1572. error_netns_exit:
  1573. unregister_pernet_device(&ovs_net_ops);
  1574. error_vport_exit:
  1575. ovs_vport_exit();
  1576. error_flow_exit:
  1577. ovs_flow_exit();
  1578. error:
  1579. return err;
  1580. }
  1581. static void dp_cleanup(void)
  1582. {
  1583. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1584. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1585. unregister_pernet_device(&ovs_net_ops);
  1586. rcu_barrier();
  1587. ovs_vport_exit();
  1588. ovs_flow_exit();
  1589. }
  1590. module_init(dp_init);
  1591. module_exit(dp_cleanup);
  1592. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1593. MODULE_LICENSE("GPL");