datapath.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181
  1. /*
  2. * Copyright (c) 2007-2014 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/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <net/genetlink.h>
  49. #include <net/net_namespace.h>
  50. #include <net/netns/generic.h>
  51. #include "datapath.h"
  52. #include "flow.h"
  53. #include "flow_table.h"
  54. #include "flow_netlink.h"
  55. #include "vport-internal_dev.h"
  56. #include "vport-netdev.h"
  57. int ovs_net_id __read_mostly;
  58. EXPORT_SYMBOL(ovs_net_id);
  59. static struct genl_family dp_packet_genl_family;
  60. static struct genl_family dp_flow_genl_family;
  61. static struct genl_family dp_datapath_genl_family;
  62. static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  63. .name = OVS_FLOW_MCGROUP,
  64. };
  65. static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  66. .name = OVS_DATAPATH_MCGROUP,
  67. };
  68. static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  69. .name = OVS_VPORT_MCGROUP,
  70. };
  71. /* Check if need to build a reply message.
  72. * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  73. static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  74. unsigned int group)
  75. {
  76. return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  77. genl_has_listeners(family, genl_info_net(info)->genl_sock,
  78. group);
  79. }
  80. static void ovs_notify(struct genl_family *family,
  81. struct sk_buff *skb, struct genl_info *info)
  82. {
  83. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  84. 0, info->nlhdr, GFP_KERNEL);
  85. }
  86. /**
  87. * DOC: Locking:
  88. *
  89. * All writes e.g. Writes to device state (add/remove datapath, port, set
  90. * operations on vports, etc.), Writes to other state (flow table
  91. * modifications, set miscellaneous datapath parameters, etc.) are protected
  92. * by ovs_lock.
  93. *
  94. * Reads are protected by RCU.
  95. *
  96. * There are a few special cases (mostly stats) that have their own
  97. * synchronization but they nest under all of above and don't interact with
  98. * each other.
  99. *
  100. * The RTNL lock nests inside ovs_mutex.
  101. */
  102. static DEFINE_MUTEX(ovs_mutex);
  103. void ovs_lock(void)
  104. {
  105. mutex_lock(&ovs_mutex);
  106. }
  107. void ovs_unlock(void)
  108. {
  109. mutex_unlock(&ovs_mutex);
  110. }
  111. #ifdef CONFIG_LOCKDEP
  112. int lockdep_ovsl_is_held(void)
  113. {
  114. if (debug_locks)
  115. return lockdep_is_held(&ovs_mutex);
  116. else
  117. return 1;
  118. }
  119. EXPORT_SYMBOL(lockdep_ovsl_is_held);
  120. #endif
  121. static struct vport *new_vport(const struct vport_parms *);
  122. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  123. const struct dp_upcall_info *);
  124. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  125. const struct dp_upcall_info *);
  126. /* Must be called with rcu_read_lock. */
  127. static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
  128. {
  129. struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
  130. if (dev) {
  131. struct vport *vport = ovs_internal_dev_get_vport(dev);
  132. if (vport)
  133. return vport->dp;
  134. }
  135. return NULL;
  136. }
  137. /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
  138. * returned dp pointer valid.
  139. */
  140. static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
  141. {
  142. struct datapath *dp;
  143. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  144. rcu_read_lock();
  145. dp = get_dp_rcu(net, dp_ifindex);
  146. rcu_read_unlock();
  147. return dp;
  148. }
  149. /* Must be called with rcu_read_lock or ovs_mutex. */
  150. const char *ovs_dp_name(const struct datapath *dp)
  151. {
  152. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  153. return vport->ops->get_name(vport);
  154. }
  155. static int get_dpifindex(struct datapath *dp)
  156. {
  157. struct vport *local;
  158. int ifindex;
  159. rcu_read_lock();
  160. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  161. if (local)
  162. ifindex = netdev_vport_priv(local)->dev->ifindex;
  163. else
  164. ifindex = 0;
  165. rcu_read_unlock();
  166. return ifindex;
  167. }
  168. static void destroy_dp_rcu(struct rcu_head *rcu)
  169. {
  170. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  171. ovs_flow_tbl_destroy(&dp->table);
  172. free_percpu(dp->stats_percpu);
  173. release_net(ovs_dp_get_net(dp));
  174. kfree(dp->ports);
  175. kfree(dp);
  176. }
  177. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  178. u16 port_no)
  179. {
  180. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  181. }
  182. /* Called with ovs_mutex or RCU read lock. */
  183. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  184. {
  185. struct vport *vport;
  186. struct hlist_head *head;
  187. head = vport_hash_bucket(dp, port_no);
  188. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  189. if (vport->port_no == port_no)
  190. return vport;
  191. }
  192. return NULL;
  193. }
  194. /* Called with ovs_mutex. */
  195. static struct vport *new_vport(const struct vport_parms *parms)
  196. {
  197. struct vport *vport;
  198. vport = ovs_vport_add(parms);
  199. if (!IS_ERR(vport)) {
  200. struct datapath *dp = parms->dp;
  201. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  202. hlist_add_head_rcu(&vport->dp_hash_node, head);
  203. }
  204. return vport;
  205. }
  206. void ovs_dp_detach_port(struct vport *p)
  207. {
  208. ASSERT_OVSL();
  209. /* First drop references to device. */
  210. hlist_del_rcu(&p->dp_hash_node);
  211. /* Then destroy it. */
  212. ovs_vport_del(p);
  213. }
  214. /* Must be called with rcu_read_lock. */
  215. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
  216. {
  217. const struct vport *p = OVS_CB(skb)->input_vport;
  218. struct datapath *dp = p->dp;
  219. struct sw_flow *flow;
  220. struct sw_flow_actions *sf_acts;
  221. struct dp_stats_percpu *stats;
  222. u64 *stats_counter;
  223. u32 n_mask_hit;
  224. stats = this_cpu_ptr(dp->stats_percpu);
  225. /* Look up flow. */
  226. flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
  227. if (unlikely(!flow)) {
  228. struct dp_upcall_info upcall;
  229. int error;
  230. upcall.cmd = OVS_PACKET_CMD_MISS;
  231. upcall.key = key;
  232. upcall.userdata = NULL;
  233. upcall.portid = ovs_vport_find_upcall_portid(p, skb);
  234. error = ovs_dp_upcall(dp, skb, &upcall);
  235. if (unlikely(error))
  236. kfree_skb(skb);
  237. else
  238. consume_skb(skb);
  239. stats_counter = &stats->n_missed;
  240. goto out;
  241. }
  242. ovs_flow_stats_update(flow, key->tp.flags, skb);
  243. sf_acts = rcu_dereference(flow->sf_acts);
  244. ovs_execute_actions(dp, skb, sf_acts, key);
  245. stats_counter = &stats->n_hit;
  246. out:
  247. /* Update datapath statistics. */
  248. u64_stats_update_begin(&stats->syncp);
  249. (*stats_counter)++;
  250. stats->n_mask_hit += n_mask_hit;
  251. u64_stats_update_end(&stats->syncp);
  252. }
  253. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  254. const struct dp_upcall_info *upcall_info)
  255. {
  256. struct dp_stats_percpu *stats;
  257. int err;
  258. if (upcall_info->portid == 0) {
  259. err = -ENOTCONN;
  260. goto err;
  261. }
  262. if (!skb_is_gso(skb))
  263. err = queue_userspace_packet(dp, skb, upcall_info);
  264. else
  265. err = queue_gso_packets(dp, skb, upcall_info);
  266. if (err)
  267. goto err;
  268. return 0;
  269. err:
  270. stats = this_cpu_ptr(dp->stats_percpu);
  271. u64_stats_update_begin(&stats->syncp);
  272. stats->n_lost++;
  273. u64_stats_update_end(&stats->syncp);
  274. return err;
  275. }
  276. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  277. const struct dp_upcall_info *upcall_info)
  278. {
  279. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  280. struct dp_upcall_info later_info;
  281. struct sw_flow_key later_key;
  282. struct sk_buff *segs, *nskb;
  283. int err;
  284. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  285. if (IS_ERR(segs))
  286. return PTR_ERR(segs);
  287. if (segs == NULL)
  288. return -EINVAL;
  289. /* Queue all of the segments. */
  290. skb = segs;
  291. do {
  292. err = queue_userspace_packet(dp, skb, upcall_info);
  293. if (err)
  294. break;
  295. if (skb == segs && gso_type & SKB_GSO_UDP) {
  296. /* The initial flow key extracted by ovs_flow_extract()
  297. * in this case is for a first fragment, so we need to
  298. * properly mark later fragments.
  299. */
  300. later_key = *upcall_info->key;
  301. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  302. later_info = *upcall_info;
  303. later_info.key = &later_key;
  304. upcall_info = &later_info;
  305. }
  306. } while ((skb = skb->next));
  307. /* Free all of the segments. */
  308. skb = segs;
  309. do {
  310. nskb = skb->next;
  311. if (err)
  312. kfree_skb(skb);
  313. else
  314. consume_skb(skb);
  315. } while ((skb = nskb));
  316. return err;
  317. }
  318. static size_t upcall_msg_size(const struct nlattr *userdata,
  319. unsigned int hdrlen)
  320. {
  321. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  322. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  323. + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  324. /* OVS_PACKET_ATTR_USERDATA */
  325. if (userdata)
  326. size += NLA_ALIGN(userdata->nla_len);
  327. return size;
  328. }
  329. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  330. const struct dp_upcall_info *upcall_info)
  331. {
  332. struct ovs_header *upcall;
  333. struct sk_buff *nskb = NULL;
  334. struct sk_buff *user_skb = NULL; /* to be queued to userspace */
  335. struct nlattr *nla;
  336. struct genl_info info = {
  337. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  338. .snd_portid = upcall_info->portid,
  339. };
  340. size_t len;
  341. unsigned int hlen;
  342. int err, dp_ifindex;
  343. dp_ifindex = get_dpifindex(dp);
  344. if (!dp_ifindex)
  345. return -ENODEV;
  346. if (vlan_tx_tag_present(skb)) {
  347. nskb = skb_clone(skb, GFP_ATOMIC);
  348. if (!nskb)
  349. return -ENOMEM;
  350. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  351. if (!nskb)
  352. return -ENOMEM;
  353. nskb->vlan_tci = 0;
  354. skb = nskb;
  355. }
  356. if (nla_attr_size(skb->len) > USHRT_MAX) {
  357. err = -EFBIG;
  358. goto out;
  359. }
  360. /* Complete checksum if needed */
  361. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  362. (err = skb_checksum_help(skb)))
  363. goto out;
  364. /* Older versions of OVS user space enforce alignment of the last
  365. * Netlink attribute to NLA_ALIGNTO which would require extensive
  366. * padding logic. Only perform zerocopy if padding is not required.
  367. */
  368. if (dp->user_features & OVS_DP_F_UNALIGNED)
  369. hlen = skb_zerocopy_headlen(skb);
  370. else
  371. hlen = skb->len;
  372. len = upcall_msg_size(upcall_info->userdata, hlen);
  373. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  374. if (!user_skb) {
  375. err = -ENOMEM;
  376. goto out;
  377. }
  378. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  379. 0, upcall_info->cmd);
  380. upcall->dp_ifindex = dp_ifindex;
  381. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  382. err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
  383. BUG_ON(err);
  384. nla_nest_end(user_skb, nla);
  385. if (upcall_info->userdata)
  386. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  387. nla_len(upcall_info->userdata),
  388. nla_data(upcall_info->userdata));
  389. /* Only reserve room for attribute header, packet data is added
  390. * in skb_zerocopy() */
  391. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  392. err = -ENOBUFS;
  393. goto out;
  394. }
  395. nla->nla_len = nla_attr_size(skb->len);
  396. err = skb_zerocopy(user_skb, skb, skb->len, hlen);
  397. if (err)
  398. goto out;
  399. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  400. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  401. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  402. if (plen > 0)
  403. memset(skb_put(user_skb, plen), 0, plen);
  404. }
  405. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  406. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  407. user_skb = NULL;
  408. out:
  409. if (err)
  410. skb_tx_error(skb);
  411. kfree_skb(user_skb);
  412. kfree_skb(nskb);
  413. return err;
  414. }
  415. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  416. {
  417. struct ovs_header *ovs_header = info->userhdr;
  418. struct nlattr **a = info->attrs;
  419. struct sw_flow_actions *acts;
  420. struct sk_buff *packet;
  421. struct sw_flow *flow;
  422. struct sw_flow_actions *sf_acts;
  423. struct datapath *dp;
  424. struct ethhdr *eth;
  425. struct vport *input_vport;
  426. int len;
  427. int err;
  428. err = -EINVAL;
  429. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  430. !a[OVS_PACKET_ATTR_ACTIONS])
  431. goto err;
  432. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  433. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  434. err = -ENOMEM;
  435. if (!packet)
  436. goto err;
  437. skb_reserve(packet, NET_IP_ALIGN);
  438. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  439. skb_reset_mac_header(packet);
  440. eth = eth_hdr(packet);
  441. /* Normally, setting the skb 'protocol' field would be handled by a
  442. * call to eth_type_trans(), but it assumes there's a sending
  443. * device, which we may not have. */
  444. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  445. packet->protocol = eth->h_proto;
  446. else
  447. packet->protocol = htons(ETH_P_802_2);
  448. /* Build an sw_flow for sending this packet. */
  449. flow = ovs_flow_alloc();
  450. err = PTR_ERR(flow);
  451. if (IS_ERR(flow))
  452. goto err_kfree_skb;
  453. err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
  454. &flow->key);
  455. if (err)
  456. goto err_flow_free;
  457. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  458. &flow->key, &acts);
  459. if (err)
  460. goto err_flow_free;
  461. rcu_assign_pointer(flow->sf_acts, acts);
  462. OVS_CB(packet)->egress_tun_info = NULL;
  463. packet->priority = flow->key.phy.priority;
  464. packet->mark = flow->key.phy.skb_mark;
  465. rcu_read_lock();
  466. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  467. err = -ENODEV;
  468. if (!dp)
  469. goto err_unlock;
  470. input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
  471. if (!input_vport)
  472. input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
  473. if (!input_vport)
  474. goto err_unlock;
  475. OVS_CB(packet)->input_vport = input_vport;
  476. sf_acts = rcu_dereference(flow->sf_acts);
  477. local_bh_disable();
  478. err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
  479. local_bh_enable();
  480. rcu_read_unlock();
  481. ovs_flow_free(flow, false);
  482. return err;
  483. err_unlock:
  484. rcu_read_unlock();
  485. err_flow_free:
  486. ovs_flow_free(flow, false);
  487. err_kfree_skb:
  488. kfree_skb(packet);
  489. err:
  490. return err;
  491. }
  492. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  493. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  494. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  495. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  496. };
  497. static const struct genl_ops dp_packet_genl_ops[] = {
  498. { .cmd = OVS_PACKET_CMD_EXECUTE,
  499. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  500. .policy = packet_policy,
  501. .doit = ovs_packet_cmd_execute
  502. }
  503. };
  504. static struct genl_family dp_packet_genl_family = {
  505. .id = GENL_ID_GENERATE,
  506. .hdrsize = sizeof(struct ovs_header),
  507. .name = OVS_PACKET_FAMILY,
  508. .version = OVS_PACKET_VERSION,
  509. .maxattr = OVS_PACKET_ATTR_MAX,
  510. .netnsok = true,
  511. .parallel_ops = true,
  512. .ops = dp_packet_genl_ops,
  513. .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
  514. };
  515. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
  516. struct ovs_dp_megaflow_stats *mega_stats)
  517. {
  518. int i;
  519. memset(mega_stats, 0, sizeof(*mega_stats));
  520. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  521. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  522. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  523. for_each_possible_cpu(i) {
  524. const struct dp_stats_percpu *percpu_stats;
  525. struct dp_stats_percpu local_stats;
  526. unsigned int start;
  527. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  528. do {
  529. start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
  530. local_stats = *percpu_stats;
  531. } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
  532. stats->n_hit += local_stats.n_hit;
  533. stats->n_missed += local_stats.n_missed;
  534. stats->n_lost += local_stats.n_lost;
  535. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  536. }
  537. }
  538. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  539. {
  540. return NLMSG_ALIGN(sizeof(struct ovs_header))
  541. + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  542. + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  543. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  544. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  545. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  546. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  547. }
  548. /* Called with ovs_mutex or RCU read lock. */
  549. static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
  550. struct sk_buff *skb)
  551. {
  552. struct nlattr *nla;
  553. int err;
  554. /* Fill flow key. */
  555. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  556. if (!nla)
  557. return -EMSGSIZE;
  558. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  559. if (err)
  560. return err;
  561. nla_nest_end(skb, nla);
  562. /* Fill flow mask. */
  563. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  564. if (!nla)
  565. return -EMSGSIZE;
  566. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  567. if (err)
  568. return err;
  569. nla_nest_end(skb, nla);
  570. return 0;
  571. }
  572. /* Called with ovs_mutex or RCU read lock. */
  573. static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
  574. struct sk_buff *skb)
  575. {
  576. struct ovs_flow_stats stats;
  577. __be16 tcp_flags;
  578. unsigned long used;
  579. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  580. if (used &&
  581. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  582. return -EMSGSIZE;
  583. if (stats.n_packets &&
  584. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  585. return -EMSGSIZE;
  586. if ((u8)ntohs(tcp_flags) &&
  587. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  588. return -EMSGSIZE;
  589. return 0;
  590. }
  591. /* Called with ovs_mutex or RCU read lock. */
  592. static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
  593. struct sk_buff *skb, int skb_orig_len)
  594. {
  595. struct nlattr *start;
  596. int err;
  597. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  598. * this is the first flow to be dumped into 'skb'. This is unusual for
  599. * Netlink but individual action lists can be longer than
  600. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  601. * The userspace caller can always fetch the actions separately if it
  602. * really wants them. (Most userspace callers in fact don't care.)
  603. *
  604. * This can only fail for dump operations because the skb is always
  605. * properly sized for single flows.
  606. */
  607. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  608. if (start) {
  609. const struct sw_flow_actions *sf_acts;
  610. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  611. err = ovs_nla_put_actions(sf_acts->actions,
  612. sf_acts->actions_len, skb);
  613. if (!err)
  614. nla_nest_end(skb, start);
  615. else {
  616. if (skb_orig_len)
  617. return err;
  618. nla_nest_cancel(skb, start);
  619. }
  620. } else if (skb_orig_len) {
  621. return -EMSGSIZE;
  622. }
  623. return 0;
  624. }
  625. /* Called with ovs_mutex or RCU read lock. */
  626. static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
  627. struct sk_buff *skb, u32 portid,
  628. u32 seq, u32 flags, u8 cmd)
  629. {
  630. const int skb_orig_len = skb->len;
  631. struct ovs_header *ovs_header;
  632. int err;
  633. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
  634. flags, cmd);
  635. if (!ovs_header)
  636. return -EMSGSIZE;
  637. ovs_header->dp_ifindex = dp_ifindex;
  638. err = ovs_flow_cmd_fill_match(flow, skb);
  639. if (err)
  640. goto error;
  641. err = ovs_flow_cmd_fill_stats(flow, skb);
  642. if (err)
  643. goto error;
  644. err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
  645. if (err)
  646. goto error;
  647. return genlmsg_end(skb, ovs_header);
  648. error:
  649. genlmsg_cancel(skb, ovs_header);
  650. return err;
  651. }
  652. /* May not be called with RCU read lock. */
  653. static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
  654. struct genl_info *info,
  655. bool always)
  656. {
  657. struct sk_buff *skb;
  658. if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
  659. return NULL;
  660. skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
  661. if (!skb)
  662. return ERR_PTR(-ENOMEM);
  663. return skb;
  664. }
  665. /* Called with ovs_mutex. */
  666. static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
  667. int dp_ifindex,
  668. struct genl_info *info, u8 cmd,
  669. bool always)
  670. {
  671. struct sk_buff *skb;
  672. int retval;
  673. skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
  674. always);
  675. if (IS_ERR_OR_NULL(skb))
  676. return skb;
  677. retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
  678. info->snd_portid, info->snd_seq, 0,
  679. cmd);
  680. BUG_ON(retval < 0);
  681. return skb;
  682. }
  683. static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
  684. {
  685. struct nlattr **a = info->attrs;
  686. struct ovs_header *ovs_header = info->userhdr;
  687. struct sw_flow *flow, *new_flow;
  688. struct sw_flow_mask mask;
  689. struct sk_buff *reply;
  690. struct datapath *dp;
  691. struct sw_flow_actions *acts;
  692. struct sw_flow_match match;
  693. int error;
  694. /* Must have key and actions. */
  695. error = -EINVAL;
  696. if (!a[OVS_FLOW_ATTR_KEY]) {
  697. OVS_NLERR("Flow key attribute not present in new flow.\n");
  698. goto error;
  699. }
  700. if (!a[OVS_FLOW_ATTR_ACTIONS]) {
  701. OVS_NLERR("Flow actions attribute not present in new flow.\n");
  702. goto error;
  703. }
  704. /* Most of the time we need to allocate a new flow, do it before
  705. * locking.
  706. */
  707. new_flow = ovs_flow_alloc();
  708. if (IS_ERR(new_flow)) {
  709. error = PTR_ERR(new_flow);
  710. goto error;
  711. }
  712. /* Extract key. */
  713. ovs_match_init(&match, &new_flow->unmasked_key, &mask);
  714. error = ovs_nla_get_match(&match,
  715. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  716. if (error)
  717. goto err_kfree_flow;
  718. ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
  719. /* Validate actions. */
  720. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
  721. &acts);
  722. if (error) {
  723. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  724. goto err_kfree_flow;
  725. }
  726. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  727. if (IS_ERR(reply)) {
  728. error = PTR_ERR(reply);
  729. goto err_kfree_acts;
  730. }
  731. ovs_lock();
  732. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  733. if (unlikely(!dp)) {
  734. error = -ENODEV;
  735. goto err_unlock_ovs;
  736. }
  737. /* Check if this is a duplicate flow */
  738. flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
  739. if (likely(!flow)) {
  740. rcu_assign_pointer(new_flow->sf_acts, acts);
  741. /* Put flow in bucket. */
  742. error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
  743. if (unlikely(error)) {
  744. acts = NULL;
  745. goto err_unlock_ovs;
  746. }
  747. if (unlikely(reply)) {
  748. error = ovs_flow_cmd_fill_info(new_flow,
  749. ovs_header->dp_ifindex,
  750. reply, info->snd_portid,
  751. info->snd_seq, 0,
  752. OVS_FLOW_CMD_NEW);
  753. BUG_ON(error < 0);
  754. }
  755. ovs_unlock();
  756. } else {
  757. struct sw_flow_actions *old_acts;
  758. /* Bail out if we're not allowed to modify an existing flow.
  759. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  760. * because Generic Netlink treats the latter as a dump
  761. * request. We also accept NLM_F_EXCL in case that bug ever
  762. * gets fixed.
  763. */
  764. if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
  765. | NLM_F_EXCL))) {
  766. error = -EEXIST;
  767. goto err_unlock_ovs;
  768. }
  769. /* The unmasked key has to be the same for flow updates. */
  770. if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
  771. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  772. if (!flow) {
  773. error = -ENOENT;
  774. goto err_unlock_ovs;
  775. }
  776. }
  777. /* Update actions. */
  778. old_acts = ovsl_dereference(flow->sf_acts);
  779. rcu_assign_pointer(flow->sf_acts, acts);
  780. if (unlikely(reply)) {
  781. error = ovs_flow_cmd_fill_info(flow,
  782. ovs_header->dp_ifindex,
  783. reply, info->snd_portid,
  784. info->snd_seq, 0,
  785. OVS_FLOW_CMD_NEW);
  786. BUG_ON(error < 0);
  787. }
  788. ovs_unlock();
  789. ovs_nla_free_flow_actions(old_acts);
  790. ovs_flow_free(new_flow, false);
  791. }
  792. if (reply)
  793. ovs_notify(&dp_flow_genl_family, reply, info);
  794. return 0;
  795. err_unlock_ovs:
  796. ovs_unlock();
  797. kfree_skb(reply);
  798. err_kfree_acts:
  799. kfree(acts);
  800. err_kfree_flow:
  801. ovs_flow_free(new_flow, false);
  802. error:
  803. return error;
  804. }
  805. /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
  806. static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
  807. const struct sw_flow_key *key,
  808. const struct sw_flow_mask *mask)
  809. {
  810. struct sw_flow_actions *acts;
  811. struct sw_flow_key masked_key;
  812. int error;
  813. ovs_flow_mask_key(&masked_key, key, mask);
  814. error = ovs_nla_copy_actions(a, &masked_key, &acts);
  815. if (error) {
  816. OVS_NLERR("Actions may not be safe on all matching packets.\n");
  817. return ERR_PTR(error);
  818. }
  819. return acts;
  820. }
  821. static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
  822. {
  823. struct nlattr **a = info->attrs;
  824. struct ovs_header *ovs_header = info->userhdr;
  825. struct sw_flow_key key;
  826. struct sw_flow *flow;
  827. struct sw_flow_mask mask;
  828. struct sk_buff *reply = NULL;
  829. struct datapath *dp;
  830. struct sw_flow_actions *old_acts = NULL, *acts = NULL;
  831. struct sw_flow_match match;
  832. int error;
  833. /* Extract key. */
  834. error = -EINVAL;
  835. if (!a[OVS_FLOW_ATTR_KEY]) {
  836. OVS_NLERR("Flow key attribute not present in set flow.\n");
  837. goto error;
  838. }
  839. ovs_match_init(&match, &key, &mask);
  840. error = ovs_nla_get_match(&match,
  841. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  842. if (error)
  843. goto error;
  844. /* Validate actions. */
  845. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  846. acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
  847. if (IS_ERR(acts)) {
  848. error = PTR_ERR(acts);
  849. goto error;
  850. }
  851. /* Can allocate before locking if have acts. */
  852. reply = ovs_flow_cmd_alloc_info(acts, info, false);
  853. if (IS_ERR(reply)) {
  854. error = PTR_ERR(reply);
  855. goto err_kfree_acts;
  856. }
  857. }
  858. ovs_lock();
  859. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  860. if (unlikely(!dp)) {
  861. error = -ENODEV;
  862. goto err_unlock_ovs;
  863. }
  864. /* Check that the flow exists. */
  865. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  866. if (unlikely(!flow)) {
  867. error = -ENOENT;
  868. goto err_unlock_ovs;
  869. }
  870. /* Update actions, if present. */
  871. if (likely(acts)) {
  872. old_acts = ovsl_dereference(flow->sf_acts);
  873. rcu_assign_pointer(flow->sf_acts, acts);
  874. if (unlikely(reply)) {
  875. error = ovs_flow_cmd_fill_info(flow,
  876. ovs_header->dp_ifindex,
  877. reply, info->snd_portid,
  878. info->snd_seq, 0,
  879. OVS_FLOW_CMD_NEW);
  880. BUG_ON(error < 0);
  881. }
  882. } else {
  883. /* Could not alloc without acts before locking. */
  884. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
  885. info, OVS_FLOW_CMD_NEW, false);
  886. if (unlikely(IS_ERR(reply))) {
  887. error = PTR_ERR(reply);
  888. goto err_unlock_ovs;
  889. }
  890. }
  891. /* Clear stats. */
  892. if (a[OVS_FLOW_ATTR_CLEAR])
  893. ovs_flow_stats_clear(flow);
  894. ovs_unlock();
  895. if (reply)
  896. ovs_notify(&dp_flow_genl_family, reply, info);
  897. if (old_acts)
  898. ovs_nla_free_flow_actions(old_acts);
  899. return 0;
  900. err_unlock_ovs:
  901. ovs_unlock();
  902. kfree_skb(reply);
  903. err_kfree_acts:
  904. kfree(acts);
  905. error:
  906. return error;
  907. }
  908. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  909. {
  910. struct nlattr **a = info->attrs;
  911. struct ovs_header *ovs_header = info->userhdr;
  912. struct sw_flow_key key;
  913. struct sk_buff *reply;
  914. struct sw_flow *flow;
  915. struct datapath *dp;
  916. struct sw_flow_match match;
  917. int err;
  918. if (!a[OVS_FLOW_ATTR_KEY]) {
  919. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  920. return -EINVAL;
  921. }
  922. ovs_match_init(&match, &key, NULL);
  923. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  924. if (err)
  925. return err;
  926. ovs_lock();
  927. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  928. if (!dp) {
  929. err = -ENODEV;
  930. goto unlock;
  931. }
  932. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  933. if (!flow) {
  934. err = -ENOENT;
  935. goto unlock;
  936. }
  937. reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
  938. OVS_FLOW_CMD_NEW, true);
  939. if (IS_ERR(reply)) {
  940. err = PTR_ERR(reply);
  941. goto unlock;
  942. }
  943. ovs_unlock();
  944. return genlmsg_reply(reply, info);
  945. unlock:
  946. ovs_unlock();
  947. return err;
  948. }
  949. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  950. {
  951. struct nlattr **a = info->attrs;
  952. struct ovs_header *ovs_header = info->userhdr;
  953. struct sw_flow_key key;
  954. struct sk_buff *reply;
  955. struct sw_flow *flow;
  956. struct datapath *dp;
  957. struct sw_flow_match match;
  958. int err;
  959. if (likely(a[OVS_FLOW_ATTR_KEY])) {
  960. ovs_match_init(&match, &key, NULL);
  961. err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
  962. if (unlikely(err))
  963. return err;
  964. }
  965. ovs_lock();
  966. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  967. if (unlikely(!dp)) {
  968. err = -ENODEV;
  969. goto unlock;
  970. }
  971. if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
  972. err = ovs_flow_tbl_flush(&dp->table);
  973. goto unlock;
  974. }
  975. flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
  976. if (unlikely(!flow)) {
  977. err = -ENOENT;
  978. goto unlock;
  979. }
  980. ovs_flow_tbl_remove(&dp->table, flow);
  981. ovs_unlock();
  982. reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
  983. info, false);
  984. if (likely(reply)) {
  985. if (likely(!IS_ERR(reply))) {
  986. rcu_read_lock(); /*To keep RCU checker happy. */
  987. err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
  988. reply, info->snd_portid,
  989. info->snd_seq, 0,
  990. OVS_FLOW_CMD_DEL);
  991. rcu_read_unlock();
  992. BUG_ON(err < 0);
  993. ovs_notify(&dp_flow_genl_family, reply, info);
  994. } else {
  995. netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
  996. }
  997. }
  998. ovs_flow_free(flow, true);
  999. return 0;
  1000. unlock:
  1001. ovs_unlock();
  1002. return err;
  1003. }
  1004. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1005. {
  1006. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1007. struct table_instance *ti;
  1008. struct datapath *dp;
  1009. rcu_read_lock();
  1010. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1011. if (!dp) {
  1012. rcu_read_unlock();
  1013. return -ENODEV;
  1014. }
  1015. ti = rcu_dereference(dp->table.ti);
  1016. for (;;) {
  1017. struct sw_flow *flow;
  1018. u32 bucket, obj;
  1019. bucket = cb->args[0];
  1020. obj = cb->args[1];
  1021. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  1022. if (!flow)
  1023. break;
  1024. if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
  1025. NETLINK_CB(cb->skb).portid,
  1026. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1027. OVS_FLOW_CMD_NEW) < 0)
  1028. break;
  1029. cb->args[0] = bucket;
  1030. cb->args[1] = obj;
  1031. }
  1032. rcu_read_unlock();
  1033. return skb->len;
  1034. }
  1035. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  1036. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  1037. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  1038. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  1039. };
  1040. static const struct genl_ops dp_flow_genl_ops[] = {
  1041. { .cmd = OVS_FLOW_CMD_NEW,
  1042. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1043. .policy = flow_policy,
  1044. .doit = ovs_flow_cmd_new
  1045. },
  1046. { .cmd = OVS_FLOW_CMD_DEL,
  1047. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1048. .policy = flow_policy,
  1049. .doit = ovs_flow_cmd_del
  1050. },
  1051. { .cmd = OVS_FLOW_CMD_GET,
  1052. .flags = 0, /* OK for unprivileged users. */
  1053. .policy = flow_policy,
  1054. .doit = ovs_flow_cmd_get,
  1055. .dumpit = ovs_flow_cmd_dump
  1056. },
  1057. { .cmd = OVS_FLOW_CMD_SET,
  1058. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1059. .policy = flow_policy,
  1060. .doit = ovs_flow_cmd_set,
  1061. },
  1062. };
  1063. static struct genl_family dp_flow_genl_family = {
  1064. .id = GENL_ID_GENERATE,
  1065. .hdrsize = sizeof(struct ovs_header),
  1066. .name = OVS_FLOW_FAMILY,
  1067. .version = OVS_FLOW_VERSION,
  1068. .maxattr = OVS_FLOW_ATTR_MAX,
  1069. .netnsok = true,
  1070. .parallel_ops = true,
  1071. .ops = dp_flow_genl_ops,
  1072. .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
  1073. .mcgrps = &ovs_dp_flow_multicast_group,
  1074. .n_mcgrps = 1,
  1075. };
  1076. static size_t ovs_dp_cmd_msg_size(void)
  1077. {
  1078. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  1079. msgsize += nla_total_size(IFNAMSIZ);
  1080. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  1081. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  1082. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  1083. return msgsize;
  1084. }
  1085. /* Called with ovs_mutex or RCU read lock. */
  1086. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  1087. u32 portid, u32 seq, u32 flags, u8 cmd)
  1088. {
  1089. struct ovs_header *ovs_header;
  1090. struct ovs_dp_stats dp_stats;
  1091. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  1092. int err;
  1093. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1094. flags, cmd);
  1095. if (!ovs_header)
  1096. goto error;
  1097. ovs_header->dp_ifindex = get_dpifindex(dp);
  1098. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1099. if (err)
  1100. goto nla_put_failure;
  1101. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  1102. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  1103. &dp_stats))
  1104. goto nla_put_failure;
  1105. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  1106. sizeof(struct ovs_dp_megaflow_stats),
  1107. &dp_megaflow_stats))
  1108. goto nla_put_failure;
  1109. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  1110. goto nla_put_failure;
  1111. return genlmsg_end(skb, ovs_header);
  1112. nla_put_failure:
  1113. genlmsg_cancel(skb, ovs_header);
  1114. error:
  1115. return -EMSGSIZE;
  1116. }
  1117. static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
  1118. {
  1119. return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  1120. }
  1121. /* Called with rcu_read_lock or ovs_mutex. */
  1122. static struct datapath *lookup_datapath(struct net *net,
  1123. struct ovs_header *ovs_header,
  1124. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1125. {
  1126. struct datapath *dp;
  1127. if (!a[OVS_DP_ATTR_NAME])
  1128. dp = get_dp(net, ovs_header->dp_ifindex);
  1129. else {
  1130. struct vport *vport;
  1131. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1132. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1133. }
  1134. return dp ? dp : ERR_PTR(-ENODEV);
  1135. }
  1136. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  1137. {
  1138. struct datapath *dp;
  1139. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1140. if (IS_ERR(dp))
  1141. return;
  1142. WARN(dp->user_features, "Dropping previously announced user features\n");
  1143. dp->user_features = 0;
  1144. }
  1145. static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
  1146. {
  1147. if (a[OVS_DP_ATTR_USER_FEATURES])
  1148. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  1149. }
  1150. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1151. {
  1152. struct nlattr **a = info->attrs;
  1153. struct vport_parms parms;
  1154. struct sk_buff *reply;
  1155. struct datapath *dp;
  1156. struct vport *vport;
  1157. struct ovs_net *ovs_net;
  1158. int err, i;
  1159. err = -EINVAL;
  1160. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1161. goto err;
  1162. reply = ovs_dp_cmd_alloc_info(info);
  1163. if (!reply)
  1164. return -ENOMEM;
  1165. err = -ENOMEM;
  1166. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1167. if (dp == NULL)
  1168. goto err_free_reply;
  1169. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1170. /* Allocate table. */
  1171. err = ovs_flow_tbl_init(&dp->table);
  1172. if (err)
  1173. goto err_free_dp;
  1174. dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
  1175. if (!dp->stats_percpu) {
  1176. err = -ENOMEM;
  1177. goto err_destroy_table;
  1178. }
  1179. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1180. GFP_KERNEL);
  1181. if (!dp->ports) {
  1182. err = -ENOMEM;
  1183. goto err_destroy_percpu;
  1184. }
  1185. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1186. INIT_HLIST_HEAD(&dp->ports[i]);
  1187. /* Set up our datapath device. */
  1188. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1189. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1190. parms.options = NULL;
  1191. parms.dp = dp;
  1192. parms.port_no = OVSP_LOCAL;
  1193. parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
  1194. ovs_dp_change(dp, a);
  1195. /* So far only local changes have been made, now need the lock. */
  1196. ovs_lock();
  1197. vport = new_vport(&parms);
  1198. if (IS_ERR(vport)) {
  1199. err = PTR_ERR(vport);
  1200. if (err == -EBUSY)
  1201. err = -EEXIST;
  1202. if (err == -EEXIST) {
  1203. /* An outdated user space instance that does not understand
  1204. * the concept of user_features has attempted to create a new
  1205. * datapath and is likely to reuse it. Drop all user features.
  1206. */
  1207. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1208. ovs_dp_reset_user_features(skb, info);
  1209. }
  1210. goto err_destroy_ports_array;
  1211. }
  1212. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1213. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1214. BUG_ON(err < 0);
  1215. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1216. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1217. ovs_unlock();
  1218. ovs_notify(&dp_datapath_genl_family, reply, info);
  1219. return 0;
  1220. err_destroy_ports_array:
  1221. ovs_unlock();
  1222. kfree(dp->ports);
  1223. err_destroy_percpu:
  1224. free_percpu(dp->stats_percpu);
  1225. err_destroy_table:
  1226. ovs_flow_tbl_destroy(&dp->table);
  1227. err_free_dp:
  1228. release_net(ovs_dp_get_net(dp));
  1229. kfree(dp);
  1230. err_free_reply:
  1231. kfree_skb(reply);
  1232. err:
  1233. return err;
  1234. }
  1235. /* Called with ovs_mutex. */
  1236. static void __dp_destroy(struct datapath *dp)
  1237. {
  1238. int i;
  1239. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1240. struct vport *vport;
  1241. struct hlist_node *n;
  1242. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1243. if (vport->port_no != OVSP_LOCAL)
  1244. ovs_dp_detach_port(vport);
  1245. }
  1246. list_del_rcu(&dp->list_node);
  1247. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1248. * all ports in datapath are destroyed first before freeing datapath.
  1249. */
  1250. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1251. /* RCU destroy the flow table */
  1252. call_rcu(&dp->rcu, destroy_dp_rcu);
  1253. }
  1254. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1255. {
  1256. struct sk_buff *reply;
  1257. struct datapath *dp;
  1258. int err;
  1259. reply = ovs_dp_cmd_alloc_info(info);
  1260. if (!reply)
  1261. return -ENOMEM;
  1262. ovs_lock();
  1263. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1264. err = PTR_ERR(dp);
  1265. if (IS_ERR(dp))
  1266. goto err_unlock_free;
  1267. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1268. info->snd_seq, 0, OVS_DP_CMD_DEL);
  1269. BUG_ON(err < 0);
  1270. __dp_destroy(dp);
  1271. ovs_unlock();
  1272. ovs_notify(&dp_datapath_genl_family, reply, info);
  1273. return 0;
  1274. err_unlock_free:
  1275. ovs_unlock();
  1276. kfree_skb(reply);
  1277. return err;
  1278. }
  1279. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1280. {
  1281. struct sk_buff *reply;
  1282. struct datapath *dp;
  1283. int err;
  1284. reply = ovs_dp_cmd_alloc_info(info);
  1285. if (!reply)
  1286. return -ENOMEM;
  1287. ovs_lock();
  1288. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1289. err = PTR_ERR(dp);
  1290. if (IS_ERR(dp))
  1291. goto err_unlock_free;
  1292. ovs_dp_change(dp, info->attrs);
  1293. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1294. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1295. BUG_ON(err < 0);
  1296. ovs_unlock();
  1297. ovs_notify(&dp_datapath_genl_family, reply, info);
  1298. return 0;
  1299. err_unlock_free:
  1300. ovs_unlock();
  1301. kfree_skb(reply);
  1302. return err;
  1303. }
  1304. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1305. {
  1306. struct sk_buff *reply;
  1307. struct datapath *dp;
  1308. int err;
  1309. reply = ovs_dp_cmd_alloc_info(info);
  1310. if (!reply)
  1311. return -ENOMEM;
  1312. rcu_read_lock();
  1313. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1314. if (IS_ERR(dp)) {
  1315. err = PTR_ERR(dp);
  1316. goto err_unlock_free;
  1317. }
  1318. err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
  1319. info->snd_seq, 0, OVS_DP_CMD_NEW);
  1320. BUG_ON(err < 0);
  1321. rcu_read_unlock();
  1322. return genlmsg_reply(reply, info);
  1323. err_unlock_free:
  1324. rcu_read_unlock();
  1325. kfree_skb(reply);
  1326. return err;
  1327. }
  1328. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1329. {
  1330. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1331. struct datapath *dp;
  1332. int skip = cb->args[0];
  1333. int i = 0;
  1334. rcu_read_lock();
  1335. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1336. if (i >= skip &&
  1337. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1338. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1339. OVS_DP_CMD_NEW) < 0)
  1340. break;
  1341. i++;
  1342. }
  1343. rcu_read_unlock();
  1344. cb->args[0] = i;
  1345. return skb->len;
  1346. }
  1347. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  1348. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1349. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1350. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  1351. };
  1352. static const struct genl_ops dp_datapath_genl_ops[] = {
  1353. { .cmd = OVS_DP_CMD_NEW,
  1354. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1355. .policy = datapath_policy,
  1356. .doit = ovs_dp_cmd_new
  1357. },
  1358. { .cmd = OVS_DP_CMD_DEL,
  1359. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1360. .policy = datapath_policy,
  1361. .doit = ovs_dp_cmd_del
  1362. },
  1363. { .cmd = OVS_DP_CMD_GET,
  1364. .flags = 0, /* OK for unprivileged users. */
  1365. .policy = datapath_policy,
  1366. .doit = ovs_dp_cmd_get,
  1367. .dumpit = ovs_dp_cmd_dump
  1368. },
  1369. { .cmd = OVS_DP_CMD_SET,
  1370. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1371. .policy = datapath_policy,
  1372. .doit = ovs_dp_cmd_set,
  1373. },
  1374. };
  1375. static struct genl_family dp_datapath_genl_family = {
  1376. .id = GENL_ID_GENERATE,
  1377. .hdrsize = sizeof(struct ovs_header),
  1378. .name = OVS_DATAPATH_FAMILY,
  1379. .version = OVS_DATAPATH_VERSION,
  1380. .maxattr = OVS_DP_ATTR_MAX,
  1381. .netnsok = true,
  1382. .parallel_ops = true,
  1383. .ops = dp_datapath_genl_ops,
  1384. .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
  1385. .mcgrps = &ovs_dp_datapath_multicast_group,
  1386. .n_mcgrps = 1,
  1387. };
  1388. /* Called with ovs_mutex or RCU read lock. */
  1389. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1390. u32 portid, u32 seq, u32 flags, u8 cmd)
  1391. {
  1392. struct ovs_header *ovs_header;
  1393. struct ovs_vport_stats vport_stats;
  1394. int err;
  1395. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1396. flags, cmd);
  1397. if (!ovs_header)
  1398. return -EMSGSIZE;
  1399. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1400. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1401. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1402. nla_put_string(skb, OVS_VPORT_ATTR_NAME,
  1403. vport->ops->get_name(vport)))
  1404. goto nla_put_failure;
  1405. ovs_vport_get_stats(vport, &vport_stats);
  1406. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1407. &vport_stats))
  1408. goto nla_put_failure;
  1409. if (ovs_vport_get_upcall_portids(vport, skb))
  1410. goto nla_put_failure;
  1411. err = ovs_vport_get_options(vport, skb);
  1412. if (err == -EMSGSIZE)
  1413. goto error;
  1414. return genlmsg_end(skb, ovs_header);
  1415. nla_put_failure:
  1416. err = -EMSGSIZE;
  1417. error:
  1418. genlmsg_cancel(skb, ovs_header);
  1419. return err;
  1420. }
  1421. static struct sk_buff *ovs_vport_cmd_alloc_info(void)
  1422. {
  1423. return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1424. }
  1425. /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
  1426. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1427. u32 seq, u8 cmd)
  1428. {
  1429. struct sk_buff *skb;
  1430. int retval;
  1431. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1432. if (!skb)
  1433. return ERR_PTR(-ENOMEM);
  1434. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1435. BUG_ON(retval < 0);
  1436. return skb;
  1437. }
  1438. /* Called with ovs_mutex or RCU read lock. */
  1439. static struct vport *lookup_vport(struct net *net,
  1440. struct ovs_header *ovs_header,
  1441. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1442. {
  1443. struct datapath *dp;
  1444. struct vport *vport;
  1445. if (a[OVS_VPORT_ATTR_NAME]) {
  1446. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1447. if (!vport)
  1448. return ERR_PTR(-ENODEV);
  1449. if (ovs_header->dp_ifindex &&
  1450. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1451. return ERR_PTR(-ENODEV);
  1452. return vport;
  1453. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1454. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1455. if (port_no >= DP_MAX_PORTS)
  1456. return ERR_PTR(-EFBIG);
  1457. dp = get_dp(net, ovs_header->dp_ifindex);
  1458. if (!dp)
  1459. return ERR_PTR(-ENODEV);
  1460. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1461. if (!vport)
  1462. return ERR_PTR(-ENODEV);
  1463. return vport;
  1464. } else
  1465. return ERR_PTR(-EINVAL);
  1466. }
  1467. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1468. {
  1469. struct nlattr **a = info->attrs;
  1470. struct ovs_header *ovs_header = info->userhdr;
  1471. struct vport_parms parms;
  1472. struct sk_buff *reply;
  1473. struct vport *vport;
  1474. struct datapath *dp;
  1475. u32 port_no;
  1476. int err;
  1477. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1478. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1479. return -EINVAL;
  1480. port_no = a[OVS_VPORT_ATTR_PORT_NO]
  1481. ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
  1482. if (port_no >= DP_MAX_PORTS)
  1483. return -EFBIG;
  1484. reply = ovs_vport_cmd_alloc_info();
  1485. if (!reply)
  1486. return -ENOMEM;
  1487. ovs_lock();
  1488. restart:
  1489. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1490. err = -ENODEV;
  1491. if (!dp)
  1492. goto exit_unlock_free;
  1493. if (port_no) {
  1494. vport = ovs_vport_ovsl(dp, port_no);
  1495. err = -EBUSY;
  1496. if (vport)
  1497. goto exit_unlock_free;
  1498. } else {
  1499. for (port_no = 1; ; port_no++) {
  1500. if (port_no >= DP_MAX_PORTS) {
  1501. err = -EFBIG;
  1502. goto exit_unlock_free;
  1503. }
  1504. vport = ovs_vport_ovsl(dp, port_no);
  1505. if (!vport)
  1506. break;
  1507. }
  1508. }
  1509. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1510. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1511. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1512. parms.dp = dp;
  1513. parms.port_no = port_no;
  1514. parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1515. vport = new_vport(&parms);
  1516. err = PTR_ERR(vport);
  1517. if (IS_ERR(vport)) {
  1518. if (err == -EAGAIN)
  1519. goto restart;
  1520. goto exit_unlock_free;
  1521. }
  1522. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1523. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1524. BUG_ON(err < 0);
  1525. ovs_unlock();
  1526. ovs_notify(&dp_vport_genl_family, reply, info);
  1527. return 0;
  1528. exit_unlock_free:
  1529. ovs_unlock();
  1530. kfree_skb(reply);
  1531. return err;
  1532. }
  1533. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1534. {
  1535. struct nlattr **a = info->attrs;
  1536. struct sk_buff *reply;
  1537. struct vport *vport;
  1538. int err;
  1539. reply = ovs_vport_cmd_alloc_info();
  1540. if (!reply)
  1541. return -ENOMEM;
  1542. ovs_lock();
  1543. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1544. err = PTR_ERR(vport);
  1545. if (IS_ERR(vport))
  1546. goto exit_unlock_free;
  1547. if (a[OVS_VPORT_ATTR_TYPE] &&
  1548. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1549. err = -EINVAL;
  1550. goto exit_unlock_free;
  1551. }
  1552. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1553. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1554. if (err)
  1555. goto exit_unlock_free;
  1556. }
  1557. if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
  1558. struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
  1559. err = ovs_vport_set_upcall_portids(vport, ids);
  1560. if (err)
  1561. goto exit_unlock_free;
  1562. }
  1563. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1564. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1565. BUG_ON(err < 0);
  1566. ovs_unlock();
  1567. ovs_notify(&dp_vport_genl_family, reply, info);
  1568. return 0;
  1569. exit_unlock_free:
  1570. ovs_unlock();
  1571. kfree_skb(reply);
  1572. return err;
  1573. }
  1574. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1575. {
  1576. struct nlattr **a = info->attrs;
  1577. struct sk_buff *reply;
  1578. struct vport *vport;
  1579. int err;
  1580. reply = ovs_vport_cmd_alloc_info();
  1581. if (!reply)
  1582. return -ENOMEM;
  1583. ovs_lock();
  1584. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1585. err = PTR_ERR(vport);
  1586. if (IS_ERR(vport))
  1587. goto exit_unlock_free;
  1588. if (vport->port_no == OVSP_LOCAL) {
  1589. err = -EINVAL;
  1590. goto exit_unlock_free;
  1591. }
  1592. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1593. info->snd_seq, 0, OVS_VPORT_CMD_DEL);
  1594. BUG_ON(err < 0);
  1595. ovs_dp_detach_port(vport);
  1596. ovs_unlock();
  1597. ovs_notify(&dp_vport_genl_family, reply, info);
  1598. return 0;
  1599. exit_unlock_free:
  1600. ovs_unlock();
  1601. kfree_skb(reply);
  1602. return err;
  1603. }
  1604. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1605. {
  1606. struct nlattr **a = info->attrs;
  1607. struct ovs_header *ovs_header = info->userhdr;
  1608. struct sk_buff *reply;
  1609. struct vport *vport;
  1610. int err;
  1611. reply = ovs_vport_cmd_alloc_info();
  1612. if (!reply)
  1613. return -ENOMEM;
  1614. rcu_read_lock();
  1615. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1616. err = PTR_ERR(vport);
  1617. if (IS_ERR(vport))
  1618. goto exit_unlock_free;
  1619. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1620. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1621. BUG_ON(err < 0);
  1622. rcu_read_unlock();
  1623. return genlmsg_reply(reply, info);
  1624. exit_unlock_free:
  1625. rcu_read_unlock();
  1626. kfree_skb(reply);
  1627. return err;
  1628. }
  1629. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1630. {
  1631. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1632. struct datapath *dp;
  1633. int bucket = cb->args[0], skip = cb->args[1];
  1634. int i, j = 0;
  1635. rcu_read_lock();
  1636. dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
  1637. if (!dp) {
  1638. rcu_read_unlock();
  1639. return -ENODEV;
  1640. }
  1641. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1642. struct vport *vport;
  1643. j = 0;
  1644. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1645. if (j >= skip &&
  1646. ovs_vport_cmd_fill_info(vport, skb,
  1647. NETLINK_CB(cb->skb).portid,
  1648. cb->nlh->nlmsg_seq,
  1649. NLM_F_MULTI,
  1650. OVS_VPORT_CMD_NEW) < 0)
  1651. goto out;
  1652. j++;
  1653. }
  1654. skip = 0;
  1655. }
  1656. out:
  1657. rcu_read_unlock();
  1658. cb->args[0] = i;
  1659. cb->args[1] = j;
  1660. return skb->len;
  1661. }
  1662. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1663. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1664. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1665. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1666. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1667. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1668. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1669. };
  1670. static const struct genl_ops dp_vport_genl_ops[] = {
  1671. { .cmd = OVS_VPORT_CMD_NEW,
  1672. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1673. .policy = vport_policy,
  1674. .doit = ovs_vport_cmd_new
  1675. },
  1676. { .cmd = OVS_VPORT_CMD_DEL,
  1677. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1678. .policy = vport_policy,
  1679. .doit = ovs_vport_cmd_del
  1680. },
  1681. { .cmd = OVS_VPORT_CMD_GET,
  1682. .flags = 0, /* OK for unprivileged users. */
  1683. .policy = vport_policy,
  1684. .doit = ovs_vport_cmd_get,
  1685. .dumpit = ovs_vport_cmd_dump
  1686. },
  1687. { .cmd = OVS_VPORT_CMD_SET,
  1688. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1689. .policy = vport_policy,
  1690. .doit = ovs_vport_cmd_set,
  1691. },
  1692. };
  1693. struct genl_family dp_vport_genl_family = {
  1694. .id = GENL_ID_GENERATE,
  1695. .hdrsize = sizeof(struct ovs_header),
  1696. .name = OVS_VPORT_FAMILY,
  1697. .version = OVS_VPORT_VERSION,
  1698. .maxattr = OVS_VPORT_ATTR_MAX,
  1699. .netnsok = true,
  1700. .parallel_ops = true,
  1701. .ops = dp_vport_genl_ops,
  1702. .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
  1703. .mcgrps = &ovs_dp_vport_multicast_group,
  1704. .n_mcgrps = 1,
  1705. };
  1706. static struct genl_family * const dp_genl_families[] = {
  1707. &dp_datapath_genl_family,
  1708. &dp_vport_genl_family,
  1709. &dp_flow_genl_family,
  1710. &dp_packet_genl_family,
  1711. };
  1712. static void dp_unregister_genl(int n_families)
  1713. {
  1714. int i;
  1715. for (i = 0; i < n_families; i++)
  1716. genl_unregister_family(dp_genl_families[i]);
  1717. }
  1718. static int dp_register_genl(void)
  1719. {
  1720. int err;
  1721. int i;
  1722. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1723. err = genl_register_family(dp_genl_families[i]);
  1724. if (err)
  1725. goto error;
  1726. }
  1727. return 0;
  1728. error:
  1729. dp_unregister_genl(i);
  1730. return err;
  1731. }
  1732. static int __net_init ovs_init_net(struct net *net)
  1733. {
  1734. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1735. INIT_LIST_HEAD(&ovs_net->dps);
  1736. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1737. return 0;
  1738. }
  1739. static void __net_exit ovs_exit_net(struct net *net)
  1740. {
  1741. struct datapath *dp, *dp_next;
  1742. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1743. ovs_lock();
  1744. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1745. __dp_destroy(dp);
  1746. ovs_unlock();
  1747. cancel_work_sync(&ovs_net->dp_notify_work);
  1748. }
  1749. static struct pernet_operations ovs_net_ops = {
  1750. .init = ovs_init_net,
  1751. .exit = ovs_exit_net,
  1752. .id = &ovs_net_id,
  1753. .size = sizeof(struct ovs_net),
  1754. };
  1755. static int __init dp_init(void)
  1756. {
  1757. int err;
  1758. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1759. pr_info("Open vSwitch switching datapath\n");
  1760. err = action_fifos_init();
  1761. if (err)
  1762. goto error;
  1763. err = ovs_internal_dev_rtnl_link_register();
  1764. if (err)
  1765. goto error_action_fifos_exit;
  1766. err = ovs_flow_init();
  1767. if (err)
  1768. goto error_unreg_rtnl_link;
  1769. err = ovs_vport_init();
  1770. if (err)
  1771. goto error_flow_exit;
  1772. err = register_pernet_device(&ovs_net_ops);
  1773. if (err)
  1774. goto error_vport_exit;
  1775. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1776. if (err)
  1777. goto error_netns_exit;
  1778. err = ovs_netdev_init();
  1779. if (err)
  1780. goto error_unreg_notifier;
  1781. err = dp_register_genl();
  1782. if (err < 0)
  1783. goto error_unreg_netdev;
  1784. return 0;
  1785. error_unreg_netdev:
  1786. ovs_netdev_exit();
  1787. error_unreg_notifier:
  1788. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1789. error_netns_exit:
  1790. unregister_pernet_device(&ovs_net_ops);
  1791. error_vport_exit:
  1792. ovs_vport_exit();
  1793. error_flow_exit:
  1794. ovs_flow_exit();
  1795. error_unreg_rtnl_link:
  1796. ovs_internal_dev_rtnl_link_unregister();
  1797. error_action_fifos_exit:
  1798. action_fifos_exit();
  1799. error:
  1800. return err;
  1801. }
  1802. static void dp_cleanup(void)
  1803. {
  1804. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1805. ovs_netdev_exit();
  1806. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1807. unregister_pernet_device(&ovs_net_ops);
  1808. rcu_barrier();
  1809. ovs_vport_exit();
  1810. ovs_flow_exit();
  1811. ovs_internal_dev_rtnl_link_unregister();
  1812. action_fifos_exit();
  1813. }
  1814. module_init(dp_init);
  1815. module_exit(dp_cleanup);
  1816. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1817. MODULE_LICENSE("GPL");